More clearly define 'named menu' in the XML parser
[glib.git] / gio / gfile.c
blob00439e3519c5e1145a65aec6eb9d38fc5126659e
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
3 /* GIO - GLib Input, Output and Streaming Library
4 *
5 * Copyright (C) 2006-2007 Red Hat, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 * Boston, MA 02111-1307, USA.
22 * Author: Alexander Larsson <alexl@redhat.com>
25 #include "config.h"
26 #ifdef HAVE_SPLICE
27 #include <sys/stat.h>
28 #include <unistd.h>
29 #include <fcntl.h>
30 #include <errno.h>
31 #endif
32 #include <string.h>
33 #include <sys/types.h>
34 #ifdef HAVE_PWD_H
35 #include <pwd.h>
36 #endif
37 #include "gfile.h"
38 #include "gvfs.h"
39 #include "gioscheduler.h"
40 #include "gsimpleasyncresult.h"
41 #include "gfileattribute-priv.h"
42 #include "gfiledescriptorbased.h"
43 #include "gpollfilemonitor.h"
44 #include "gappinfo.h"
45 #include "gfileinputstream.h"
46 #include "gfileoutputstream.h"
47 #include "glocalfileoutputstream.h"
48 #include "glocalfileiostream.h"
49 #include "gcancellable.h"
50 #include "gasyncresult.h"
51 #include "gioerror.h"
52 #include "glibintl.h"
55 /**
56 * SECTION:gfile
57 * @short_description: File and Directory Handling
58 * @include: gio/gio.h
59 * @see_also: #GFileInfo, #GFileEnumerator
61 * #GFile is a high level abstraction for manipulating files on a
62 * virtual file system. #GFile<!-- -->s are lightweight, immutable
63 * objects that do no I/O upon creation. It is necessary to understand that
64 * #GFile objects do not represent files, merely an identifier for a file. All
65 * file content I/O is implemented as streaming operations (see #GInputStream and
66 * #GOutputStream).
68 * To construct a #GFile, you can use:
69 * g_file_new_for_path() if you have a path.
70 * g_file_new_for_uri() if you have a URI.
71 * g_file_new_for_commandline_arg() for a command line argument.
72 * g_file_new_tmp() to create a temporary file from a template.
73 * g_file_parse_name() from a utf8 string gotten from g_file_get_parse_name().
75 * One way to think of a #GFile is as an abstraction of a pathname. For normal
76 * files the system pathname is what is stored internally, but as #GFile<!-- -->s
77 * are extensible it could also be something else that corresponds to a pathname
78 * in a userspace implementation of a filesystem.
80 * #GFile<!-- -->s make up hierarchies of directories and files that correspond to the
81 * files on a filesystem. You can move through the file system with #GFile using
82 * g_file_get_parent() to get an identifier for the parent directory, g_file_get_child()
83 * to get a child within a directory, g_file_resolve_relative_path() to resolve a relative
84 * path between two #GFile<!-- -->s. There can be multiple hierarchies, so you may not
85 * end up at the same root if you repeatedly call g_file_get_parent() on two different
86 * files.
88 * All #GFile<!-- -->s have a basename (get with g_file_get_basename()). These names
89 * are byte strings that are used to identify the file on the filesystem (relative to
90 * its parent directory) and there is no guarantees that they have any particular charset
91 * encoding or even make any sense at all. If you want to use filenames in a user
92 * interface you should use the display name that you can get by requesting the
93 * %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME attribute with g_file_query_info().
94 * This is guaranteed to be in utf8 and can be used in a user interface. But always
95 * store the real basename or the #GFile to use to actually access the file, because
96 * there is no way to go from a display name to the actual name.
98 * Using #GFile as an identifier has the same weaknesses as using a path in that
99 * there may be multiple aliases for the same file. For instance, hard or
100 * soft links may cause two different #GFile<!-- -->s to refer to the same file.
101 * Other possible causes for aliases are: case insensitive filesystems, short
102 * and long names on Fat/NTFS, or bind mounts in Linux. If you want to check if
103 * two #GFile<!-- -->s point to the same file you can query for the
104 * %G_FILE_ATTRIBUTE_ID_FILE attribute. Note that #GFile does some trivial
105 * canonicalization of pathnames passed in, so that trivial differences in the
106 * path string used at creation (duplicated slashes, slash at end of path, "."
107 * or ".." path segments, etc) does not create different #GFile<!-- -->s.
109 * Many #GFile operations have both synchronous and asynchronous versions
110 * to suit your application. Asynchronous versions of synchronous functions
111 * simply have _async() appended to their function names. The asynchronous
112 * I/O functions call a #GAsyncReadyCallback which is then used to finalize
113 * the operation, producing a GAsyncResult which is then passed to the
114 * function's matching _finish() operation.
116 * Some #GFile operations do not have synchronous analogs, as they may
117 * take a very long time to finish, and blocking may leave an application
118 * unusable. Notable cases include:
119 * g_file_mount_mountable() to mount a mountable file.
120 * g_file_unmount_mountable_with_operation() to unmount a mountable file.
121 * g_file_eject_mountable_with_operation() to eject a mountable file.
123 * <para id="gfile-etag"><indexterm><primary>entity tag</primary></indexterm>
124 * One notable feature of #GFile<!-- -->s are entity tags, or "etags" for
125 * short. Entity tags are somewhat like a more abstract version of the
126 * traditional mtime, and can be used to quickly determine if the file has
127 * been modified from the version on the file system. See the HTTP 1.1
128 * <ulink url="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html">specification</ulink>
129 * for HTTP Etag headers, which are a very similar concept.
130 * </para>
133 static void g_file_real_query_info_async (GFile *file,
134 const char *attributes,
135 GFileQueryInfoFlags flags,
136 int io_priority,
137 GCancellable *cancellable,
138 GAsyncReadyCallback callback,
139 gpointer user_data);
140 static GFileInfo * g_file_real_query_info_finish (GFile *file,
141 GAsyncResult *res,
142 GError **error);
143 static void g_file_real_query_filesystem_info_async (GFile *file,
144 const char *attributes,
145 int io_priority,
146 GCancellable *cancellable,
147 GAsyncReadyCallback callback,
148 gpointer user_data);
149 static GFileInfo * g_file_real_query_filesystem_info_finish (GFile *file,
150 GAsyncResult *res,
151 GError **error);
152 static void g_file_real_enumerate_children_async (GFile *file,
153 const char *attributes,
154 GFileQueryInfoFlags flags,
155 int io_priority,
156 GCancellable *cancellable,
157 GAsyncReadyCallback callback,
158 gpointer user_data);
159 static GFileEnumerator * g_file_real_enumerate_children_finish (GFile *file,
160 GAsyncResult *res,
161 GError **error);
162 static void g_file_real_read_async (GFile *file,
163 int io_priority,
164 GCancellable *cancellable,
165 GAsyncReadyCallback callback,
166 gpointer user_data);
167 static GFileInputStream * g_file_real_read_finish (GFile *file,
168 GAsyncResult *res,
169 GError **error);
170 static void g_file_real_append_to_async (GFile *file,
171 GFileCreateFlags flags,
172 int io_priority,
173 GCancellable *cancellable,
174 GAsyncReadyCallback callback,
175 gpointer user_data);
176 static GFileOutputStream *g_file_real_append_to_finish (GFile *file,
177 GAsyncResult *res,
178 GError **error);
179 static void g_file_real_create_async (GFile *file,
180 GFileCreateFlags flags,
181 int io_priority,
182 GCancellable *cancellable,
183 GAsyncReadyCallback callback,
184 gpointer user_data);
185 static GFileOutputStream *g_file_real_create_finish (GFile *file,
186 GAsyncResult *res,
187 GError **error);
188 static void g_file_real_replace_async (GFile *file,
189 const char *etag,
190 gboolean make_backup,
191 GFileCreateFlags flags,
192 int io_priority,
193 GCancellable *cancellable,
194 GAsyncReadyCallback callback,
195 gpointer user_data);
196 static GFileOutputStream *g_file_real_replace_finish (GFile *file,
197 GAsyncResult *res,
198 GError **error);
199 static void g_file_real_open_readwrite_async (GFile *file,
200 int io_priority,
201 GCancellable *cancellable,
202 GAsyncReadyCallback callback,
203 gpointer user_data);
204 static GFileIOStream * g_file_real_open_readwrite_finish (GFile *file,
205 GAsyncResult *res,
206 GError **error);
207 static void g_file_real_create_readwrite_async (GFile *file,
208 GFileCreateFlags flags,
209 int io_priority,
210 GCancellable *cancellable,
211 GAsyncReadyCallback callback,
212 gpointer user_data);
213 static GFileIOStream * g_file_real_create_readwrite_finish (GFile *file,
214 GAsyncResult *res,
215 GError **error);
216 static void g_file_real_replace_readwrite_async (GFile *file,
217 const char *etag,
218 gboolean make_backup,
219 GFileCreateFlags flags,
220 int io_priority,
221 GCancellable *cancellable,
222 GAsyncReadyCallback callback,
223 gpointer user_data);
224 static GFileIOStream * g_file_real_replace_readwrite_finish (GFile *file,
225 GAsyncResult *res,
226 GError **error);
227 static gboolean g_file_real_set_attributes_from_info (GFile *file,
228 GFileInfo *info,
229 GFileQueryInfoFlags flags,
230 GCancellable *cancellable,
231 GError **error);
232 static void g_file_real_set_display_name_async (GFile *file,
233 const char *display_name,
234 int io_priority,
235 GCancellable *cancellable,
236 GAsyncReadyCallback callback,
237 gpointer user_data);
238 static GFile * g_file_real_set_display_name_finish (GFile *file,
239 GAsyncResult *res,
240 GError **error);
241 static void g_file_real_set_attributes_async (GFile *file,
242 GFileInfo *info,
243 GFileQueryInfoFlags flags,
244 int io_priority,
245 GCancellable *cancellable,
246 GAsyncReadyCallback callback,
247 gpointer user_data);
248 static gboolean g_file_real_set_attributes_finish (GFile *file,
249 GAsyncResult *res,
250 GFileInfo **info,
251 GError **error);
252 static void g_file_real_find_enclosing_mount_async (GFile *file,
253 int io_priority,
254 GCancellable *cancellable,
255 GAsyncReadyCallback callback,
256 gpointer user_data);
257 static GMount * g_file_real_find_enclosing_mount_finish (GFile *file,
258 GAsyncResult *res,
259 GError **error);
260 static void g_file_real_copy_async (GFile *source,
261 GFile *destination,
262 GFileCopyFlags flags,
263 int io_priority,
264 GCancellable *cancellable,
265 GFileProgressCallback progress_callback,
266 gpointer progress_callback_data,
267 GAsyncReadyCallback callback,
268 gpointer user_data);
269 static gboolean g_file_real_copy_finish (GFile *file,
270 GAsyncResult *res,
271 GError **error);
273 typedef GFileIface GFileInterface;
274 G_DEFINE_INTERFACE (GFile, g_file, G_TYPE_OBJECT)
276 static void
277 g_file_default_init (GFileIface *iface)
279 iface->enumerate_children_async = g_file_real_enumerate_children_async;
280 iface->enumerate_children_finish = g_file_real_enumerate_children_finish;
281 iface->set_display_name_async = g_file_real_set_display_name_async;
282 iface->set_display_name_finish = g_file_real_set_display_name_finish;
283 iface->query_info_async = g_file_real_query_info_async;
284 iface->query_info_finish = g_file_real_query_info_finish;
285 iface->query_filesystem_info_async = g_file_real_query_filesystem_info_async;
286 iface->query_filesystem_info_finish = g_file_real_query_filesystem_info_finish;
287 iface->set_attributes_async = g_file_real_set_attributes_async;
288 iface->set_attributes_finish = g_file_real_set_attributes_finish;
289 iface->read_async = g_file_real_read_async;
290 iface->read_finish = g_file_real_read_finish;
291 iface->append_to_async = g_file_real_append_to_async;
292 iface->append_to_finish = g_file_real_append_to_finish;
293 iface->create_async = g_file_real_create_async;
294 iface->create_finish = g_file_real_create_finish;
295 iface->replace_async = g_file_real_replace_async;
296 iface->replace_finish = g_file_real_replace_finish;
297 iface->open_readwrite_async = g_file_real_open_readwrite_async;
298 iface->open_readwrite_finish = g_file_real_open_readwrite_finish;
299 iface->create_readwrite_async = g_file_real_create_readwrite_async;
300 iface->create_readwrite_finish = g_file_real_create_readwrite_finish;
301 iface->replace_readwrite_async = g_file_real_replace_readwrite_async;
302 iface->replace_readwrite_finish = g_file_real_replace_readwrite_finish;
303 iface->find_enclosing_mount_async = g_file_real_find_enclosing_mount_async;
304 iface->find_enclosing_mount_finish = g_file_real_find_enclosing_mount_finish;
305 iface->set_attributes_from_info = g_file_real_set_attributes_from_info;
306 iface->copy_async = g_file_real_copy_async;
307 iface->copy_finish = g_file_real_copy_finish;
312 * g_file_is_native:
313 * @file: input #GFile.
315 * Checks to see if a file is native to the platform.
317 * A native file s one expressed in the platform-native filename format,
318 * e.g. "C:\Windows" or "/usr/bin/". This does not mean the file is local,
319 * as it might be on a locally mounted remote filesystem.
321 * On some systems non-native files may be available using
322 * the native filesystem via a userspace filesystem (FUSE), in
323 * these cases this call will return %FALSE, but g_file_get_path()
324 * will still return a native path.
326 * This call does no blocking i/o.
328 * Returns: %TRUE if file is native.
330 gboolean
331 g_file_is_native (GFile *file)
333 GFileIface *iface;
335 g_return_val_if_fail (G_IS_FILE (file), FALSE);
337 iface = G_FILE_GET_IFACE (file);
339 return (* iface->is_native) (file);
344 * g_file_has_uri_scheme:
345 * @file: input #GFile.
346 * @uri_scheme: a string containing a URI scheme.
348 * Checks to see if a #GFile has a given URI scheme.
350 * This call does no blocking i/o.
352 * Returns: %TRUE if #GFile's backend supports the
353 * given URI scheme, %FALSE if URI scheme is %NULL,
354 * not supported, or #GFile is invalid.
356 gboolean
357 g_file_has_uri_scheme (GFile *file,
358 const char *uri_scheme)
360 GFileIface *iface;
362 g_return_val_if_fail (G_IS_FILE (file), FALSE);
363 g_return_val_if_fail (uri_scheme != NULL, FALSE);
365 iface = G_FILE_GET_IFACE (file);
367 return (* iface->has_uri_scheme) (file, uri_scheme);
372 * g_file_get_uri_scheme:
373 * @file: input #GFile.
375 * Gets the URI scheme for a #GFile.
376 * RFC 3986 decodes the scheme as:
377 * <programlisting>
378 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
379 * </programlisting>
380 * Common schemes include "file", "http", "ftp", etc.
382 * This call does no blocking i/o.
384 * Returns: a string containing the URI scheme for the given
385 * #GFile. The returned string should be freed with g_free()
386 * when no longer needed.
388 char *
389 g_file_get_uri_scheme (GFile *file)
391 GFileIface *iface;
393 g_return_val_if_fail (G_IS_FILE (file), NULL);
395 iface = G_FILE_GET_IFACE (file);
397 return (* iface->get_uri_scheme) (file);
402 * g_file_get_basename:
403 * @file: input #GFile.
405 * Gets the base name (the last component of the path) for a given #GFile.
407 * If called for the top level of a system (such as the filesystem root
408 * or a uri like sftp://host/) it will return a single directory separator
409 * (and on Windows, possibly a drive letter).
411 * The base name is a byte string (*not* UTF-8). It has no defined encoding
412 * or rules other than it may not contain zero bytes. If you want to use
413 * filenames in a user interface you should use the display name that you
414 * can get by requesting the %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME
415 * attribute with g_file_query_info().
417 * This call does no blocking i/o.
419 * Returns: string containing the #GFile's base name, or %NULL
420 * if given #GFile is invalid. The returned string should be
421 * freed with g_free() when no longer needed.
423 char *
424 g_file_get_basename (GFile *file)
426 GFileIface *iface;
428 g_return_val_if_fail (G_IS_FILE (file), NULL);
430 iface = G_FILE_GET_IFACE (file);
432 return (* iface->get_basename) (file);
436 * g_file_get_path:
437 * @file: input #GFile.
439 * Gets the local pathname for #GFile, if one exists.
441 * This call does no blocking i/o.
443 * Returns: string containing the #GFile's path, or %NULL if
444 * no such path exists. The returned string should be
445 * freed with g_free() when no longer needed.
447 char *
448 g_file_get_path (GFile *file)
450 GFileIface *iface;
452 g_return_val_if_fail (G_IS_FILE (file), NULL);
454 iface = G_FILE_GET_IFACE (file);
456 return (* iface->get_path) (file);
460 * g_file_get_uri:
461 * @file: input #GFile.
463 * Gets the URI for the @file.
465 * This call does no blocking i/o.
467 * Returns: a string containing the #GFile's URI.
468 * The returned string should be freed with g_free() when no longer needed.
470 char *
471 g_file_get_uri (GFile *file)
473 GFileIface *iface;
475 g_return_val_if_fail (G_IS_FILE (file), NULL);
477 iface = G_FILE_GET_IFACE (file);
479 return (* iface->get_uri) (file);
483 * g_file_get_parse_name:
484 * @file: input #GFile.
486 * Gets the parse name of the @file.
487 * A parse name is a UTF-8 string that describes the
488 * file such that one can get the #GFile back using
489 * g_file_parse_name().
491 * This is generally used to show the #GFile as a nice
492 * full-pathname kind of string in a user interface,
493 * like in a location entry.
495 * For local files with names that can safely be converted
496 * to UTF8 the pathname is used, otherwise the IRI is used
497 * (a form of URI that allows UTF8 characters unescaped).
499 * This call does no blocking i/o.
501 * Returns: a string containing the #GFile's parse name. The returned
502 * string should be freed with g_free() when no longer needed.
504 char *
505 g_file_get_parse_name (GFile *file)
507 GFileIface *iface;
509 g_return_val_if_fail (G_IS_FILE (file), NULL);
511 iface = G_FILE_GET_IFACE (file);
513 return (* iface->get_parse_name) (file);
517 * g_file_dup:
518 * @file: input #GFile.
520 * Duplicates a #GFile handle. This operation does not duplicate
521 * the actual file or directory represented by the #GFile; see
522 * g_file_copy() if attempting to copy a file.
524 * This call does no blocking i/o.
526 * Returns: (transfer full): a new #GFile that is a duplicate of the given #GFile.
528 GFile *
529 g_file_dup (GFile *file)
531 GFileIface *iface;
533 g_return_val_if_fail (G_IS_FILE (file), NULL);
535 iface = G_FILE_GET_IFACE (file);
537 return (* iface->dup) (file);
541 * g_file_hash:
542 * @file: (type GFile): #gconstpointer to a #GFile.
544 * Creates a hash value for a #GFile.
546 * This call does no blocking i/o.
548 * Virtual: hash
549 * Returns: 0 if @file is not a valid #GFile, otherwise an
550 * integer that can be used as hash value for the #GFile.
551 * This function is intended for easily hashing a #GFile to
552 * add to a #GHashTable or similar data structure.
554 guint
555 g_file_hash (gconstpointer file)
557 GFileIface *iface;
559 g_return_val_if_fail (G_IS_FILE (file), 0);
561 iface = G_FILE_GET_IFACE (file);
563 return (* iface->hash) ((GFile *)file);
567 * g_file_equal:
568 * @file1: the first #GFile.
569 * @file2: the second #GFile.
571 * Checks equality of two given #GFile<!-- -->s. Note that two
572 * #GFile<!-- -->s that differ can still refer to the same
573 * file on the filesystem due to various forms of filename
574 * aliasing.
576 * This call does no blocking i/o.
578 * Returns: %TRUE if @file1 and @file2 are equal.
579 * %FALSE if either is not a #GFile.
581 gboolean
582 g_file_equal (GFile *file1,
583 GFile *file2)
585 GFileIface *iface;
587 g_return_val_if_fail (G_IS_FILE (file1), FALSE);
588 g_return_val_if_fail (G_IS_FILE (file2), FALSE);
590 if (G_TYPE_FROM_INSTANCE (file1) != G_TYPE_FROM_INSTANCE (file2))
591 return FALSE;
593 iface = G_FILE_GET_IFACE (file1);
595 return (* iface->equal) (file1, file2);
600 * g_file_get_parent:
601 * @file: input #GFile.
603 * Gets the parent directory for the @file.
604 * If the @file represents the root directory of the
605 * file system, then %NULL will be returned.
607 * This call does no blocking i/o.
609 * Returns: (transfer full): a #GFile structure to the parent of the given
610 * #GFile or %NULL if there is no parent.
611 * Free the returned object with g_object_unref().
613 GFile *
614 g_file_get_parent (GFile *file)
616 GFileIface *iface;
618 g_return_val_if_fail (G_IS_FILE (file), NULL);
620 iface = G_FILE_GET_IFACE (file);
622 return (* iface->get_parent) (file);
626 * g_file_has_parent:
627 * @file: input #GFile
628 * @parent: the parent to check for, or %NULL
630 * Checks if @file has a parent, and optionally, if it is @parent.
632 * If @parent is %NULL then this function returns %TRUE if @file has any
633 * parent at all. If @parent is non-%NULL then %TRUE is only returned
634 * if @file is a child of @parent.
636 * Returns: %TRUE if @file is a child of @parent (or any parent in the
637 * case that @parent is %NULL).
639 * Since: 2.24
641 gboolean
642 g_file_has_parent (GFile *file,
643 GFile *parent)
645 GFile *actual_parent;
646 gboolean result;
648 g_return_val_if_fail (G_IS_FILE (file), FALSE);
649 g_return_val_if_fail (parent == NULL || G_IS_FILE (parent), FALSE);
651 actual_parent = g_file_get_parent (file);
653 if (actual_parent != NULL)
655 if (parent != NULL)
656 result = g_file_equal (parent, actual_parent);
657 else
658 result = TRUE;
660 g_object_unref (actual_parent);
662 else
663 result = FALSE;
665 return result;
669 * g_file_get_child:
670 * @file: input #GFile.
671 * @name: string containing the child's basename.
673 * Gets a child of @file with basename equal to @name.
675 * Note that the file with that specific name might not exist, but
676 * you can still have a #GFile that points to it. You can use this
677 * for instance to create that file.
679 * This call does no blocking i/o.
681 * Returns: (transfer full): a #GFile to a child specified by @name.
682 * Free the returned object with g_object_unref().
684 GFile *
685 g_file_get_child (GFile *file,
686 const char *name)
688 g_return_val_if_fail (G_IS_FILE (file), NULL);
689 g_return_val_if_fail (name != NULL, NULL);
691 return g_file_resolve_relative_path (file, name);
695 * g_file_get_child_for_display_name:
696 * @file: input #GFile.
697 * @display_name: string to a possible child.
698 * @error: #GError.
700 * Gets the child of @file for a given @display_name (i.e. a UTF8
701 * version of the name). If this function fails, it returns %NULL and @error will be
702 * set. This is very useful when constructing a GFile for a new file
703 * and the user entered the filename in the user interface, for instance
704 * when you select a directory and type a filename in the file selector.
706 * This call does no blocking i/o.
708 * Returns: (transfer full): a #GFile to the specified child, or
709 * %NULL if the display name couldn't be converted.
710 * Free the returned object with g_object_unref().
712 GFile *
713 g_file_get_child_for_display_name (GFile *file,
714 const char *display_name,
715 GError **error)
717 GFileIface *iface;
719 g_return_val_if_fail (G_IS_FILE (file), NULL);
720 g_return_val_if_fail (display_name != NULL, NULL);
722 iface = G_FILE_GET_IFACE (file);
724 return (* iface->get_child_for_display_name) (file, display_name, error);
728 * g_file_has_prefix:
729 * @file: input #GFile.
730 * @prefix: input #GFile.
732 * Checks whether @file has the prefix specified by @prefix. In other word,
733 * if the names of initial elements of @file<!-- -->s pathname match @prefix.
734 * Only full pathname elements are matched, so a path like /foo is not
735 * considered a prefix of /foobar, only of /foo/bar.
737 * This call does no i/o, as it works purely on names. As such it can
738 * sometimes return %FALSE even if @file is inside a @prefix (from a
739 * filesystem point of view), because the prefix of @file is an alias
740 * of @prefix.
742 * Virtual: prefix_matches
743 * Returns: %TRUE if the @files's parent, grandparent, etc is @prefix.
744 * %FALSE otherwise.
746 gboolean
747 g_file_has_prefix (GFile *file,
748 GFile *prefix)
750 GFileIface *iface;
752 g_return_val_if_fail (G_IS_FILE (file), FALSE);
753 g_return_val_if_fail (G_IS_FILE (prefix), FALSE);
755 if (G_TYPE_FROM_INSTANCE (file) != G_TYPE_FROM_INSTANCE (prefix))
756 return FALSE;
758 iface = G_FILE_GET_IFACE (file);
760 /* The vtable function differs in arg order since we're
761 using the old contains_file call */
762 return (* iface->prefix_matches) (prefix, file);
766 * g_file_get_relative_path:
767 * @parent: input #GFile.
768 * @descendant: input #GFile.
770 * Gets the path for @descendant relative to @parent.
772 * This call does no blocking i/o.
774 * Returns: string with the relative path from @descendant
775 * to @parent, or %NULL if @descendant doesn't have @parent as prefix.
776 * The returned string should be freed with g_free() when no longer needed.
778 char *
779 g_file_get_relative_path (GFile *parent,
780 GFile *descendant)
782 GFileIface *iface;
784 g_return_val_if_fail (G_IS_FILE (parent), NULL);
785 g_return_val_if_fail (G_IS_FILE (descendant), NULL);
787 if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
788 return NULL;
790 iface = G_FILE_GET_IFACE (parent);
792 return (* iface->get_relative_path) (parent, descendant);
796 * g_file_resolve_relative_path:
797 * @file: input #GFile.
798 * @relative_path: a given relative path string.
800 * Resolves a relative path for @file to an absolute path.
802 * This call does no blocking i/o.
804 * Returns: (transfer full): #GFile to the resolved path. %NULL if @relative_path
805 * is %NULL or if @file is invalid.
806 * Free the returned object with g_object_unref().
808 GFile *
809 g_file_resolve_relative_path (GFile *file,
810 const char *relative_path)
812 GFileIface *iface;
814 g_return_val_if_fail (G_IS_FILE (file), NULL);
815 g_return_val_if_fail (relative_path != NULL, NULL);
817 iface = G_FILE_GET_IFACE (file);
819 return (* iface->resolve_relative_path) (file, relative_path);
823 * g_file_enumerate_children:
824 * @file: input #GFile.
825 * @attributes: an attribute query string.
826 * @flags: a set of #GFileQueryInfoFlags.
827 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
828 * @error: #GError for error reporting.
830 * Gets the requested information about the files in a directory. The result
831 * is a #GFileEnumerator object that will give out #GFileInfo objects for
832 * all the files in the directory.
834 * The @attributes value is a string that specifies the file attributes that
835 * should be gathered. It is not an error if it's not possible to read a particular
836 * requested attribute from a file - it just won't be set. @attributes should
837 * be a comma-separated list of attributes or attribute wildcards. The wildcard "*"
838 * means all attributes, and a wildcard like "standard::*" means all attributes in the standard
839 * namespace. An example attribute query be "standard::*,owner::user".
840 * The standard attributes are available as defines, like #G_FILE_ATTRIBUTE_STANDARD_NAME.
842 * If @cancellable is not %NULL, then the operation can be cancelled by
843 * triggering the cancellable object from another thread. If the operation
844 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
846 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
847 * If the file is not a directory, the G_FILE_ERROR_NOTDIR error will be returned.
848 * Other errors are possible too.
850 * Returns: (transfer full): A #GFileEnumerator if successful, %NULL on error.
851 * Free the returned object with g_object_unref().
853 GFileEnumerator *
854 g_file_enumerate_children (GFile *file,
855 const char *attributes,
856 GFileQueryInfoFlags flags,
857 GCancellable *cancellable,
858 GError **error)
861 GFileIface *iface;
863 g_return_val_if_fail (G_IS_FILE (file), NULL);
865 if (g_cancellable_set_error_if_cancelled (cancellable, error))
866 return NULL;
868 iface = G_FILE_GET_IFACE (file);
870 if (iface->enumerate_children == NULL)
872 g_set_error_literal (error, G_IO_ERROR,
873 G_IO_ERROR_NOT_SUPPORTED,
874 _("Operation not supported"));
875 return NULL;
878 return (* iface->enumerate_children) (file, attributes, flags,
879 cancellable, error);
883 * g_file_enumerate_children_async:
884 * @file: input #GFile.
885 * @attributes: an attribute query string.
886 * @flags: a set of #GFileQueryInfoFlags.
887 * @io_priority: the <link linkend="io-priority">I/O priority</link>
888 * of the request.
889 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
890 * @callback: (scope async): a #GAsyncReadyCallback to call when the
891 * request is satisfied
892 * @user_data: (closure): the data to pass to callback function
894 * Asynchronously gets the requested information about the files in a directory. The result
895 * is a #GFileEnumerator object that will give out #GFileInfo objects for
896 * all the files in the directory.
898 * For more details, see g_file_enumerate_children() which is
899 * the synchronous version of this call.
901 * When the operation is finished, @callback will be called. You can then call
902 * g_file_enumerate_children_finish() to get the result of the operation.
904 void
905 g_file_enumerate_children_async (GFile *file,
906 const char *attributes,
907 GFileQueryInfoFlags flags,
908 int io_priority,
909 GCancellable *cancellable,
910 GAsyncReadyCallback callback,
911 gpointer user_data)
913 GFileIface *iface;
915 g_return_if_fail (G_IS_FILE (file));
917 iface = G_FILE_GET_IFACE (file);
918 (* iface->enumerate_children_async) (file,
919 attributes,
920 flags,
921 io_priority,
922 cancellable,
923 callback,
924 user_data);
928 * g_file_enumerate_children_finish:
929 * @file: input #GFile.
930 * @res: a #GAsyncResult.
931 * @error: a #GError.
933 * Finishes an async enumerate children operation.
934 * See g_file_enumerate_children_async().
936 * Returns: (transfer full): a #GFileEnumerator or %NULL if an error occurred.
937 * Free the returned object with g_object_unref().
939 GFileEnumerator *
940 g_file_enumerate_children_finish (GFile *file,
941 GAsyncResult *res,
942 GError **error)
944 GFileIface *iface;
946 g_return_val_if_fail (G_IS_FILE (file), NULL);
947 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
949 if (G_IS_SIMPLE_ASYNC_RESULT (res))
951 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
952 if (g_simple_async_result_propagate_error (simple, error))
953 return NULL;
956 iface = G_FILE_GET_IFACE (file);
957 return (* iface->enumerate_children_finish) (file, res, error);
961 * g_file_query_exists:
962 * @file: input #GFile.
963 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
965 * Utility function to check if a particular file exists. This is
966 * implemented using g_file_query_info() and as such does blocking I/O.
968 * Note that in many cases it is racy to first check for file existence
969 * and then execute something based on the outcome of that, because the
970 * file might have been created or removed in between the operations. The
971 * general approach to handling that is to not check, but just do the
972 * operation and handle the errors as they come.
974 * As an example of race-free checking, take the case of reading a file, and
975 * if it doesn't exist, creating it. There are two racy versions: read it, and
976 * on error create it; and: check if it exists, if not create it. These
977 * can both result in two processes creating the file (with perhaps a partially
978 * written file as the result). The correct approach is to always try to create
979 * the file with g_file_create() which will either atomically create the file
980 * or fail with a G_IO_ERROR_EXISTS error.
982 * However, in many cases an existence check is useful in a user
983 * interface, for instance to make a menu item sensitive/insensitive, so that
984 * you don't have to fool users that something is possible and then just show
985 * and error dialog. If you do this, you should make sure to also handle the
986 * errors that can happen due to races when you execute the operation.
988 * Returns: %TRUE if the file exists (and can be detected without error), %FALSE otherwise (or if cancelled).
990 gboolean
991 g_file_query_exists (GFile *file,
992 GCancellable *cancellable)
994 GFileInfo *info;
996 g_return_val_if_fail (G_IS_FILE(file), FALSE);
998 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE,
999 G_FILE_QUERY_INFO_NONE, cancellable, NULL);
1000 if (info != NULL)
1002 g_object_unref (info);
1003 return TRUE;
1006 return FALSE;
1010 * g_file_query_file_type:
1011 * @file: input #GFile.
1012 * @flags: a set of #GFileQueryInfoFlags passed to g_file_query_info().
1013 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
1015 * Utility function to inspect the #GFileType of a file. This is
1016 * implemented using g_file_query_info() and as such does blocking I/O.
1018 * The primary use case of this method is to check if a file is a regular file,
1019 * directory, or symlink.
1021 * Returns: The #GFileType of the file and #G_FILE_TYPE_UNKNOWN if the file
1022 * does not exist
1024 * Since: 2.18
1026 GFileType
1027 g_file_query_file_type (GFile *file,
1028 GFileQueryInfoFlags flags,
1029 GCancellable *cancellable)
1031 GFileInfo *info;
1032 GFileType file_type;
1034 g_return_val_if_fail (G_IS_FILE(file), G_FILE_TYPE_UNKNOWN);
1035 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE, flags,
1036 cancellable, NULL);
1037 if (info != NULL)
1039 file_type = g_file_info_get_file_type (info);
1040 g_object_unref (info);
1042 else
1043 file_type = G_FILE_TYPE_UNKNOWN;
1045 return file_type;
1049 * g_file_query_info:
1050 * @file: input #GFile.
1051 * @attributes: an attribute query string.
1052 * @flags: a set of #GFileQueryInfoFlags.
1053 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
1054 * @error: a #GError.
1056 * Gets the requested information about specified @file. The result
1057 * is a #GFileInfo object that contains key-value attributes (such as
1058 * the type or size of the file).
1060 * The @attributes value is a string that specifies the file attributes that
1061 * should be gathered. It is not an error if it's not possible to read a particular
1062 * requested attribute from a file - it just won't be set. @attributes should
1063 * be a comma-separated list of attributes or attribute wildcards. The wildcard "*"
1064 * means all attributes, and a wildcard like "standard::*" means all attributes in the standard
1065 * namespace. An example attribute query be "standard::*,owner::user".
1066 * The standard attributes are available as defines, like #G_FILE_ATTRIBUTE_STANDARD_NAME.
1068 * If @cancellable is not %NULL, then the operation can be cancelled by
1069 * triggering the cancellable object from another thread. If the operation
1070 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1072 * For symlinks, normally the information about the target of the
1073 * symlink is returned, rather than information about the symlink itself.
1074 * However if you pass #G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS in @flags the
1075 * information about the symlink itself will be returned. Also, for symlinks
1076 * that point to non-existing files the information about the symlink itself
1077 * will be returned.
1079 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1080 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1082 * Returns: (transfer full): a #GFileInfo for the given @file, or %NULL on error.
1083 * Free the returned object with g_object_unref().
1085 GFileInfo *
1086 g_file_query_info (GFile *file,
1087 const char *attributes,
1088 GFileQueryInfoFlags flags,
1089 GCancellable *cancellable,
1090 GError **error)
1092 GFileIface *iface;
1094 g_return_val_if_fail (G_IS_FILE (file), NULL);
1096 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1097 return NULL;
1099 iface = G_FILE_GET_IFACE (file);
1101 if (iface->query_info == NULL)
1103 g_set_error_literal (error, G_IO_ERROR,
1104 G_IO_ERROR_NOT_SUPPORTED,
1105 _("Operation not supported"));
1106 return NULL;
1109 return (* iface->query_info) (file, attributes, flags, cancellable, error);
1113 * g_file_query_info_async:
1114 * @file: input #GFile.
1115 * @attributes: an attribute query string.
1116 * @flags: a set of #GFileQueryInfoFlags.
1117 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1118 * of the request.
1119 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
1120 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
1121 * @user_data: (closure): the data to pass to callback function
1123 * Asynchronously gets the requested information about specified @file. The result
1124 * is a #GFileInfo object that contains key-value attributes (such as type or size
1125 * for the file).
1127 * For more details, see g_file_query_info() which is
1128 * the synchronous version of this call.
1130 * When the operation is finished, @callback will be called. You can then call
1131 * g_file_query_info_finish() to get the result of the operation.
1133 void
1134 g_file_query_info_async (GFile *file,
1135 const char *attributes,
1136 GFileQueryInfoFlags flags,
1137 int io_priority,
1138 GCancellable *cancellable,
1139 GAsyncReadyCallback callback,
1140 gpointer user_data)
1142 GFileIface *iface;
1144 g_return_if_fail (G_IS_FILE (file));
1146 iface = G_FILE_GET_IFACE (file);
1147 (* iface->query_info_async) (file,
1148 attributes,
1149 flags,
1150 io_priority,
1151 cancellable,
1152 callback,
1153 user_data);
1157 * g_file_query_info_finish:
1158 * @file: input #GFile.
1159 * @res: a #GAsyncResult.
1160 * @error: a #GError.
1162 * Finishes an asynchronous file info query.
1163 * See g_file_query_info_async().
1165 * Returns: (transfer full): #GFileInfo for given @file or %NULL on error.
1166 * Free the returned object with g_object_unref().
1168 GFileInfo *
1169 g_file_query_info_finish (GFile *file,
1170 GAsyncResult *res,
1171 GError **error)
1173 GFileIface *iface;
1175 g_return_val_if_fail (G_IS_FILE (file), NULL);
1176 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1178 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1180 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1181 if (g_simple_async_result_propagate_error (simple, error))
1182 return NULL;
1185 iface = G_FILE_GET_IFACE (file);
1186 return (* iface->query_info_finish) (file, res, error);
1190 * g_file_query_filesystem_info:
1191 * @file: input #GFile.
1192 * @attributes: an attribute query string.
1193 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
1194 * @error: a #GError.
1196 * Similar to g_file_query_info(), but obtains information
1197 * about the filesystem the @file is on, rather than the file itself.
1198 * For instance the amount of space available and the type of
1199 * the filesystem.
1201 * The @attributes value is a string that specifies the file attributes that
1202 * should be gathered. It is not an error if it's not possible to read a particular
1203 * requested attribute from a file - it just won't be set. @attributes should
1204 * be a comma-separated list of attributes or attribute wildcards. The wildcard "*"
1205 * means all attributes, and a wildcard like "filesystem::*" means all attributes in the
1206 * filesystem namespace. The standard namespace for filesystem attributes is "filesystem".
1207 * Common attributes of interest are #G_FILE_ATTRIBUTE_FILESYSTEM_SIZE
1208 * (the total size of the filesystem in bytes), #G_FILE_ATTRIBUTE_FILESYSTEM_FREE (number of
1209 * bytes available), and #G_FILE_ATTRIBUTE_FILESYSTEM_TYPE (type of the filesystem).
1211 * If @cancellable is not %NULL, then the operation can be cancelled by
1212 * triggering the cancellable object from another thread. If the operation
1213 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1215 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1216 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1218 * Returns: (transfer full): a #GFileInfo or %NULL if there was an error.
1219 * Free the returned object with g_object_unref().
1221 GFileInfo *
1222 g_file_query_filesystem_info (GFile *file,
1223 const char *attributes,
1224 GCancellable *cancellable,
1225 GError **error)
1227 GFileIface *iface;
1229 g_return_val_if_fail (G_IS_FILE (file), NULL);
1231 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1232 return NULL;
1234 iface = G_FILE_GET_IFACE (file);
1236 if (iface->query_filesystem_info == NULL)
1238 g_set_error_literal (error, G_IO_ERROR,
1239 G_IO_ERROR_NOT_SUPPORTED,
1240 _("Operation not supported"));
1241 return NULL;
1244 return (* iface->query_filesystem_info) (file, attributes, cancellable, error);
1248 * g_file_query_filesystem_info_async:
1249 * @file: input #GFile.
1250 * @attributes: an attribute query string.
1251 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1252 * of the request.
1253 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
1254 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
1255 * @user_data: (closure): the data to pass to callback function
1257 * Asynchronously gets the requested information about the filesystem
1258 * that the specified @file is on. The result is a #GFileInfo object
1259 * that contains key-value attributes (such as type or size for the
1260 * file).
1262 * For more details, see g_file_query_filesystem_info() which is the
1263 * synchronous version of this call.
1265 * When the operation is finished, @callback will be called. You can
1266 * then call g_file_query_info_finish() to get the result of the
1267 * operation.
1269 void
1270 g_file_query_filesystem_info_async (GFile *file,
1271 const char *attributes,
1272 int io_priority,
1273 GCancellable *cancellable,
1274 GAsyncReadyCallback callback,
1275 gpointer user_data)
1277 GFileIface *iface;
1279 g_return_if_fail (G_IS_FILE (file));
1281 iface = G_FILE_GET_IFACE (file);
1282 (* iface->query_filesystem_info_async) (file,
1283 attributes,
1284 io_priority,
1285 cancellable,
1286 callback,
1287 user_data);
1291 * g_file_query_filesystem_info_finish:
1292 * @file: input #GFile.
1293 * @res: a #GAsyncResult.
1294 * @error: a #GError.
1296 * Finishes an asynchronous filesystem info query. See
1297 * g_file_query_filesystem_info_async().
1299 * Returns: (transfer full): #GFileInfo for given @file or %NULL on error.
1300 * Free the returned object with g_object_unref().
1302 GFileInfo *
1303 g_file_query_filesystem_info_finish (GFile *file,
1304 GAsyncResult *res,
1305 GError **error)
1307 GFileIface *iface;
1309 g_return_val_if_fail (G_IS_FILE (file), NULL);
1310 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1312 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1314 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1315 if (g_simple_async_result_propagate_error (simple, error))
1316 return NULL;
1319 iface = G_FILE_GET_IFACE (file);
1320 return (* iface->query_filesystem_info_finish) (file, res, error);
1324 * g_file_find_enclosing_mount:
1325 * @file: input #GFile.
1326 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
1327 * @error: a #GError.
1329 * Gets a #GMount for the #GFile.
1331 * If the #GFileIface for @file does not have a mount (e.g. possibly a
1332 * remote share), @error will be set to %G_IO_ERROR_NOT_FOUND and %NULL
1333 * will be returned.
1335 * If @cancellable is not %NULL, then the operation can be cancelled by
1336 * triggering the cancellable object from another thread. If the operation
1337 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1339 * Returns: (transfer full): a #GMount where the @file is located or %NULL on error.
1340 * Free the returned object with g_object_unref().
1342 GMount *
1343 g_file_find_enclosing_mount (GFile *file,
1344 GCancellable *cancellable,
1345 GError **error)
1347 GFileIface *iface;
1349 g_return_val_if_fail (G_IS_FILE (file), NULL);
1351 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1352 return NULL;
1354 iface = G_FILE_GET_IFACE (file);
1355 if (iface->find_enclosing_mount == NULL)
1358 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1359 /* Translators: This is an error message when trying to find the
1360 * enclosing (user visible) mount of a file, but none exists. */
1361 _("Containing mount does not exist"));
1362 return NULL;
1365 return (* iface->find_enclosing_mount) (file, cancellable, error);
1369 * g_file_find_enclosing_mount_async:
1370 * @file: a #GFile
1371 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1372 * of the request.
1373 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
1374 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
1375 * @user_data: (closure): the data to pass to callback function
1377 * Asynchronously gets the mount for the file.
1379 * For more details, see g_file_find_enclosing_mount() which is
1380 * the synchronous version of this call.
1382 * When the operation is finished, @callback will be called. You can then call
1383 * g_file_find_enclosing_mount_finish() to get the result of the operation.
1385 void
1386 g_file_find_enclosing_mount_async (GFile *file,
1387 int io_priority,
1388 GCancellable *cancellable,
1389 GAsyncReadyCallback callback,
1390 gpointer user_data)
1392 GFileIface *iface;
1394 g_return_if_fail (G_IS_FILE (file));
1396 iface = G_FILE_GET_IFACE (file);
1397 (* iface->find_enclosing_mount_async) (file,
1398 io_priority,
1399 cancellable,
1400 callback,
1401 user_data);
1405 * g_file_find_enclosing_mount_finish:
1406 * @file: a #GFile
1407 * @res: a #GAsyncResult
1408 * @error: a #GError
1410 * Finishes an asynchronous find mount request.
1411 * See g_file_find_enclosing_mount_async().
1413 * Returns: (transfer full): #GMount for given @file or %NULL on error.
1414 * Free the returned object with g_object_unref().
1416 GMount *
1417 g_file_find_enclosing_mount_finish (GFile *file,
1418 GAsyncResult *res,
1419 GError **error)
1421 GFileIface *iface;
1423 g_return_val_if_fail (G_IS_FILE (file), NULL);
1424 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1426 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1428 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1429 if (g_simple_async_result_propagate_error (simple, error))
1430 return NULL;
1433 iface = G_FILE_GET_IFACE (file);
1434 return (* iface->find_enclosing_mount_finish) (file, res, error);
1439 * g_file_read:
1440 * @file: #GFile to read.
1441 * @cancellable: (allow-none): a #GCancellable
1442 * @error: a #GError, or %NULL
1444 * Opens a file for reading. The result is a #GFileInputStream that
1445 * can be used to read the contents of the file.
1447 * If @cancellable is not %NULL, then the operation can be cancelled by
1448 * triggering the cancellable object from another thread. If the operation
1449 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1451 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1452 * If the file is a directory, the G_IO_ERROR_IS_DIRECTORY error will be returned.
1453 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1455 * Virtual: read_fn
1456 * Returns: (transfer full): #GFileInputStream or %NULL on error.
1457 * Free the returned object with g_object_unref().
1459 GFileInputStream *
1460 g_file_read (GFile *file,
1461 GCancellable *cancellable,
1462 GError **error)
1464 GFileIface *iface;
1466 g_return_val_if_fail (G_IS_FILE (file), NULL);
1468 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1469 return NULL;
1471 iface = G_FILE_GET_IFACE (file);
1473 if (iface->read_fn == NULL)
1475 g_set_error_literal (error, G_IO_ERROR,
1476 G_IO_ERROR_NOT_SUPPORTED,
1477 _("Operation not supported"));
1478 return NULL;
1481 return (* iface->read_fn) (file, cancellable, error);
1485 * g_file_append_to:
1486 * @file: input #GFile.
1487 * @flags: a set of #GFileCreateFlags.
1488 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
1489 * @error: a #GError, or %NULL
1491 * Gets an output stream for appending data to the file. If
1492 * the file doesn't already exist it is created.
1494 * By default files created are generally readable by everyone,
1495 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1496 * will be made readable only to the current user, to the level that
1497 * is supported on the target filesystem.
1499 * If @cancellable is not %NULL, then the operation can be cancelled by
1500 * triggering the cancellable object from another thread. If the operation
1501 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1503 * Some file systems don't allow all file names, and may
1504 * return an %G_IO_ERROR_INVALID_FILENAME error.
1505 * If the file is a directory the %G_IO_ERROR_IS_DIRECTORY error will be
1506 * returned. Other errors are possible too, and depend on what kind of
1507 * filesystem the file is on.
1509 * Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
1510 * Free the returned object with g_object_unref().
1512 GFileOutputStream *
1513 g_file_append_to (GFile *file,
1514 GFileCreateFlags flags,
1515 GCancellable *cancellable,
1516 GError **error)
1518 GFileIface *iface;
1520 g_return_val_if_fail (G_IS_FILE (file), NULL);
1522 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1523 return NULL;
1525 iface = G_FILE_GET_IFACE (file);
1527 if (iface->append_to == NULL)
1529 g_set_error_literal (error, G_IO_ERROR,
1530 G_IO_ERROR_NOT_SUPPORTED,
1531 _("Operation not supported"));
1532 return NULL;
1535 return (* iface->append_to) (file, flags, cancellable, error);
1539 * g_file_create:
1540 * @file: input #GFile.
1541 * @flags: a set of #GFileCreateFlags.
1542 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
1543 * @error: a #GError, or %NULL
1545 * Creates a new file and returns an output stream for writing to it.
1546 * The file must not already exist.
1548 * By default files created are generally readable by everyone,
1549 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1550 * will be made readable only to the current user, to the level that
1551 * is supported on the target filesystem.
1553 * If @cancellable is not %NULL, then the operation can be cancelled by
1554 * triggering the cancellable object from another thread. If the operation
1555 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1557 * If a file or directory with this name already exists the G_IO_ERROR_EXISTS
1558 * error will be returned.
1559 * Some file systems don't allow all file names, and may
1560 * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1561 * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1562 * Other errors are possible too, and depend on what kind of
1563 * filesystem the file is on.
1565 * Returns: (transfer full): a #GFileOutputStream for the newly created file, or
1566 * %NULL on error.
1567 * Free the returned object with g_object_unref().
1569 GFileOutputStream *
1570 g_file_create (GFile *file,
1571 GFileCreateFlags flags,
1572 GCancellable *cancellable,
1573 GError **error)
1575 GFileIface *iface;
1577 g_return_val_if_fail (G_IS_FILE (file), NULL);
1579 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1580 return NULL;
1582 iface = G_FILE_GET_IFACE (file);
1584 if (iface->create == NULL)
1586 g_set_error_literal (error, G_IO_ERROR,
1587 G_IO_ERROR_NOT_SUPPORTED,
1588 _("Operation not supported"));
1589 return NULL;
1592 return (* iface->create) (file, flags, cancellable, error);
1596 * g_file_replace:
1597 * @file: input #GFile.
1598 * @etag: (allow-none): an optional <link linkend="gfile-etag">entity tag</link> for the
1599 * current #GFile, or #NULL to ignore.
1600 * @make_backup: %TRUE if a backup should be created.
1601 * @flags: a set of #GFileCreateFlags.
1602 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
1603 * @error: a #GError, or %NULL
1605 * Returns an output stream for overwriting the file, possibly
1606 * creating a backup copy of the file first. If the file doesn't exist,
1607 * it will be created.
1609 * This will try to replace the file in the safest way possible so
1610 * that any errors during the writing will not affect an already
1611 * existing copy of the file. For instance, for local files it
1612 * may write to a temporary file and then atomically rename over
1613 * the destination when the stream is closed.
1615 * By default files created are generally readable by everyone,
1616 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1617 * will be made readable only to the current user, to the level that
1618 * is supported on the target filesystem.
1620 * If @cancellable is not %NULL, then the operation can be cancelled by
1621 * triggering the cancellable object from another thread. If the operation
1622 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1624 * If you pass in a non-#NULL @etag value, then this value is
1625 * compared to the current entity tag of the file, and if they differ
1626 * an G_IO_ERROR_WRONG_ETAG error is returned. This generally means
1627 * that the file has been changed since you last read it. You can get
1628 * the new etag from g_file_output_stream_get_etag() after you've
1629 * finished writing and closed the #GFileOutputStream. When you load
1630 * a new file you can use g_file_input_stream_query_info() to get
1631 * the etag of the file.
1633 * If @make_backup is %TRUE, this function will attempt to make a backup
1634 * of the current file before overwriting it. If this fails a G_IO_ERROR_CANT_CREATE_BACKUP
1635 * error will be returned. If you want to replace anyway, try again with
1636 * @make_backup set to %FALSE.
1638 * If the file is a directory the G_IO_ERROR_IS_DIRECTORY error will be returned,
1639 * and if the file is some other form of non-regular file then a
1640 * G_IO_ERROR_NOT_REGULAR_FILE error will be returned.
1641 * Some file systems don't allow all file names, and may
1642 * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1643 * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1644 * Other errors are possible too, and depend on what kind of
1645 * filesystem the file is on.
1647 * Returns: (transfer full): a #GFileOutputStream or %NULL on error.
1648 * Free the returned object with g_object_unref().
1650 GFileOutputStream *
1651 g_file_replace (GFile *file,
1652 const char *etag,
1653 gboolean make_backup,
1654 GFileCreateFlags flags,
1655 GCancellable *cancellable,
1656 GError **error)
1658 GFileIface *iface;
1660 g_return_val_if_fail (G_IS_FILE (file), NULL);
1662 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1663 return NULL;
1665 iface = G_FILE_GET_IFACE (file);
1667 if (iface->replace == NULL)
1669 g_set_error_literal (error, G_IO_ERROR,
1670 G_IO_ERROR_NOT_SUPPORTED,
1671 _("Operation not supported"));
1672 return NULL;
1676 /* Handle empty tag string as NULL in consistent way. */
1677 if (etag && *etag == 0)
1678 etag = NULL;
1680 return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1684 * g_file_open_readwrite:
1685 * @file: #GFile to open
1686 * @cancellable: (allow-none): a #GCancellable
1687 * @error: a #GError, or %NULL
1689 * Opens an existing file for reading and writing. The result is
1690 * a #GFileIOStream that can be used to read and write the contents of the file.
1692 * If @cancellable is not %NULL, then the operation can be cancelled by
1693 * triggering the cancellable object from another thread. If the operation
1694 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1696 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1697 * If the file is a directory, the G_IO_ERROR_IS_DIRECTORY error will be returned.
1698 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1699 * Note that in many non-local file cases read and write streams are not supported,
1700 * so make sure you really need to do read and write streaming, rather than
1701 * just opening for reading or writing.
1703 * Returns: (transfer full): #GFileIOStream or %NULL on error.
1704 * Free the returned object with g_object_unref().
1706 * Since: 2.22
1708 GFileIOStream *
1709 g_file_open_readwrite (GFile *file,
1710 GCancellable *cancellable,
1711 GError **error)
1713 GFileIface *iface;
1715 g_return_val_if_fail (G_IS_FILE (file), NULL);
1717 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1718 return NULL;
1720 iface = G_FILE_GET_IFACE (file);
1722 if (iface->open_readwrite == NULL)
1724 g_set_error_literal (error, G_IO_ERROR,
1725 G_IO_ERROR_NOT_SUPPORTED,
1726 _("Operation not supported"));
1727 return NULL;
1730 return (* iface->open_readwrite) (file, cancellable, error);
1734 * g_file_create_readwrite:
1735 * @file: a #GFile
1736 * @flags: a set of #GFileCreateFlags
1737 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
1738 * @error: return location for a #GError, or %NULL
1740 * Creates a new file and returns a stream for reading and writing to it.
1741 * The file must not already exist.
1743 * By default files created are generally readable by everyone,
1744 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1745 * will be made readable only to the current user, to the level that
1746 * is supported on the target filesystem.
1748 * If @cancellable is not %NULL, then the operation can be cancelled by
1749 * triggering the cancellable object from another thread. If the operation
1750 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1752 * If a file or directory with this name already exists the %G_IO_ERROR_EXISTS
1753 * error will be returned. Some file systems don't allow all file names,
1754 * and may return an %G_IO_ERROR_INVALID_FILENAME error, and if the name
1755 * is too long, %G_IO_ERROR_FILENAME_TOO_LONG will be returned. Other errors
1756 * are possible too, and depend on what kind of filesystem the file is on.
1758 * Note that in many non-local file cases read and write streams are not
1759 * supported, so make sure you really need to do read and write streaming,
1760 * rather than just opening for reading or writing.
1762 * Returns: (transfer full): a #GFileIOStream for the newly created file, or %NULL on error.
1763 * Free the returned object with g_object_unref().
1765 * Since: 2.22
1767 GFileIOStream *
1768 g_file_create_readwrite (GFile *file,
1769 GFileCreateFlags flags,
1770 GCancellable *cancellable,
1771 GError **error)
1773 GFileIface *iface;
1775 g_return_val_if_fail (G_IS_FILE (file), NULL);
1777 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1778 return NULL;
1780 iface = G_FILE_GET_IFACE (file);
1782 if (iface->create_readwrite == NULL)
1784 g_set_error_literal (error, G_IO_ERROR,
1785 G_IO_ERROR_NOT_SUPPORTED,
1786 _("Operation not supported"));
1787 return NULL;
1790 return (* iface->create_readwrite) (file, flags, cancellable, error);
1794 * g_file_replace_readwrite:
1795 * @file: a #GFile
1796 * @etag: (allow-none): an optional <link linkend="gfile-etag">entity tag</link> for the
1797 * current #GFile, or #NULL to ignore
1798 * @make_backup: %TRUE if a backup should be created
1799 * @flags: a set of #GFileCreateFlags
1800 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
1801 * @error: return location for a #GError, or %NULL
1803 * Returns an output stream for overwriting the file in readwrite mode,
1804 * possibly creating a backup copy of the file first. If the file doesn't
1805 * exist, it will be created.
1807 * For details about the behaviour, see g_file_replace() which does the same
1808 * thing but returns an output stream only.
1810 * Note that in many non-local file cases read and write streams are not
1811 * supported, so make sure you really need to do read and write streaming,
1812 * rather than just opening for reading or writing.
1814 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
1815 * Free the returned object with g_object_unref().
1817 * Since: 2.22
1819 GFileIOStream *
1820 g_file_replace_readwrite (GFile *file,
1821 const char *etag,
1822 gboolean make_backup,
1823 GFileCreateFlags flags,
1824 GCancellable *cancellable,
1825 GError **error)
1827 GFileIface *iface;
1829 g_return_val_if_fail (G_IS_FILE (file), NULL);
1831 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1832 return NULL;
1834 iface = G_FILE_GET_IFACE (file);
1836 if (iface->replace_readwrite == NULL)
1838 g_set_error_literal (error, G_IO_ERROR,
1839 G_IO_ERROR_NOT_SUPPORTED,
1840 _("Operation not supported"));
1841 return NULL;
1844 return (* iface->replace_readwrite) (file, etag, make_backup, flags, cancellable, error);
1848 * g_file_read_async:
1849 * @file: input #GFile
1850 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1851 * of the request.
1852 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
1853 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
1854 * @user_data: (closure): the data to pass to callback function
1856 * Asynchronously opens @file for reading.
1858 * For more details, see g_file_read() which is
1859 * the synchronous version of this call.
1861 * When the operation is finished, @callback will be called. You can then call
1862 * g_file_read_finish() to get the result of the operation.
1864 void
1865 g_file_read_async (GFile *file,
1866 int io_priority,
1867 GCancellable *cancellable,
1868 GAsyncReadyCallback callback,
1869 gpointer user_data)
1871 GFileIface *iface;
1873 g_return_if_fail (G_IS_FILE (file));
1875 iface = G_FILE_GET_IFACE (file);
1876 (* iface->read_async) (file,
1877 io_priority,
1878 cancellable,
1879 callback,
1880 user_data);
1884 * g_file_read_finish:
1885 * @file: input #GFile.
1886 * @res: a #GAsyncResult.
1887 * @error: a #GError, or %NULL
1889 * Finishes an asynchronous file read operation started with
1890 * g_file_read_async().
1892 * Returns: (transfer full): a #GFileInputStream or %NULL on error.
1893 * Free the returned object with g_object_unref().
1895 GFileInputStream *
1896 g_file_read_finish (GFile *file,
1897 GAsyncResult *res,
1898 GError **error)
1900 GFileIface *iface;
1902 g_return_val_if_fail (G_IS_FILE (file), NULL);
1903 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1905 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1907 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1908 if (g_simple_async_result_propagate_error (simple, error))
1909 return NULL;
1912 iface = G_FILE_GET_IFACE (file);
1913 return (* iface->read_finish) (file, res, error);
1917 * g_file_append_to_async:
1918 * @file: input #GFile.
1919 * @flags: a set of #GFileCreateFlags.
1920 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1921 * of the request.
1922 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
1923 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
1924 * @user_data: (closure): the data to pass to callback function
1926 * Asynchronously opens @file for appending.
1928 * For more details, see g_file_append_to() which is
1929 * the synchronous version of this call.
1931 * When the operation is finished, @callback will be called. You can then call
1932 * g_file_append_to_finish() to get the result of the operation.
1934 void
1935 g_file_append_to_async (GFile *file,
1936 GFileCreateFlags flags,
1937 int io_priority,
1938 GCancellable *cancellable,
1939 GAsyncReadyCallback callback,
1940 gpointer user_data)
1942 GFileIface *iface;
1944 g_return_if_fail (G_IS_FILE (file));
1946 iface = G_FILE_GET_IFACE (file);
1947 (* iface->append_to_async) (file,
1948 flags,
1949 io_priority,
1950 cancellable,
1951 callback,
1952 user_data);
1956 * g_file_append_to_finish:
1957 * @file: input #GFile.
1958 * @res: #GAsyncResult
1959 * @error: a #GError, or %NULL
1961 * Finishes an asynchronous file append operation started with
1962 * g_file_append_to_async().
1964 * Returns: (transfer full): a valid #GFileOutputStream or %NULL on error.
1965 * Free the returned object with g_object_unref().
1967 GFileOutputStream *
1968 g_file_append_to_finish (GFile *file,
1969 GAsyncResult *res,
1970 GError **error)
1972 GFileIface *iface;
1974 g_return_val_if_fail (G_IS_FILE (file), NULL);
1975 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1977 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1979 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1980 if (g_simple_async_result_propagate_error (simple, error))
1981 return NULL;
1984 iface = G_FILE_GET_IFACE (file);
1985 return (* iface->append_to_finish) (file, res, error);
1989 * g_file_create_async:
1990 * @file: input #GFile.
1991 * @flags: a set of #GFileCreateFlags.
1992 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1993 * of the request.
1994 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
1995 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
1996 * @user_data: (closure): the data to pass to callback function
1998 * Asynchronously creates a new file and returns an output stream for writing to it.
1999 * The file must not already exist.
2001 * For more details, see g_file_create() which is
2002 * the synchronous version of this call.
2004 * When the operation is finished, @callback will be called. You can then call
2005 * g_file_create_finish() to get the result of the operation.
2007 void
2008 g_file_create_async (GFile *file,
2009 GFileCreateFlags flags,
2010 int io_priority,
2011 GCancellable *cancellable,
2012 GAsyncReadyCallback callback,
2013 gpointer user_data)
2015 GFileIface *iface;
2017 g_return_if_fail (G_IS_FILE (file));
2019 iface = G_FILE_GET_IFACE (file);
2020 (* iface->create_async) (file,
2021 flags,
2022 io_priority,
2023 cancellable,
2024 callback,
2025 user_data);
2029 * g_file_create_finish:
2030 * @file: input #GFile.
2031 * @res: a #GAsyncResult.
2032 * @error: a #GError, or %NULL
2034 * Finishes an asynchronous file create operation started with
2035 * g_file_create_async().
2037 * Returns: (transfer full): a #GFileOutputStream or %NULL on error.
2038 * Free the returned object with g_object_unref().
2040 GFileOutputStream *
2041 g_file_create_finish (GFile *file,
2042 GAsyncResult *res,
2043 GError **error)
2045 GFileIface *iface;
2047 g_return_val_if_fail (G_IS_FILE (file), NULL);
2048 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2050 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2052 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2053 if (g_simple_async_result_propagate_error (simple, error))
2054 return NULL;
2057 iface = G_FILE_GET_IFACE (file);
2058 return (* iface->create_finish) (file, res, error);
2062 * g_file_replace_async:
2063 * @file: input #GFile.
2064 * @etag: (allow-none): an <link linkend="gfile-etag">entity tag</link> for the
2065 * current #GFile, or NULL to ignore.
2066 * @make_backup: %TRUE if a backup should be created.
2067 * @flags: a set of #GFileCreateFlags.
2068 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2069 * of the request.
2070 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
2071 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
2072 * @user_data: (closure): the data to pass to callback function
2074 * Asynchronously overwrites the file, replacing the contents, possibly
2075 * creating a backup copy of the file first.
2077 * For more details, see g_file_replace() which is
2078 * the synchronous version of this call.
2080 * When the operation is finished, @callback will be called. You can then call
2081 * g_file_replace_finish() to get the result of the operation.
2083 void
2084 g_file_replace_async (GFile *file,
2085 const char *etag,
2086 gboolean make_backup,
2087 GFileCreateFlags flags,
2088 int io_priority,
2089 GCancellable *cancellable,
2090 GAsyncReadyCallback callback,
2091 gpointer user_data)
2093 GFileIface *iface;
2095 g_return_if_fail (G_IS_FILE (file));
2097 iface = G_FILE_GET_IFACE (file);
2098 (* iface->replace_async) (file,
2099 etag,
2100 make_backup,
2101 flags,
2102 io_priority,
2103 cancellable,
2104 callback,
2105 user_data);
2109 * g_file_replace_finish:
2110 * @file: input #GFile.
2111 * @res: a #GAsyncResult.
2112 * @error: a #GError, or %NULL
2114 * Finishes an asynchronous file replace operation started with
2115 * g_file_replace_async().
2117 * Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
2118 * Free the returned object with g_object_unref().
2120 GFileOutputStream *
2121 g_file_replace_finish (GFile *file,
2122 GAsyncResult *res,
2123 GError **error)
2125 GFileIface *iface;
2127 g_return_val_if_fail (G_IS_FILE (file), NULL);
2128 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2130 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2132 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2133 if (g_simple_async_result_propagate_error (simple, error))
2134 return NULL;
2137 iface = G_FILE_GET_IFACE (file);
2138 return (* iface->replace_finish) (file, res, error);
2143 * g_file_open_readwrite_async:
2144 * @file: input #GFile.
2145 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2146 * of the request.
2147 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
2148 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
2149 * @user_data: (closure): the data to pass to callback function
2151 * Asynchronously opens @file for reading and writing.
2153 * For more details, see g_file_open_readwrite() which is
2154 * the synchronous version of this call.
2156 * When the operation is finished, @callback will be called. You can then call
2157 * g_file_open_readwrite_finish() to get the result of the operation.
2159 * Since: 2.22
2161 void
2162 g_file_open_readwrite_async (GFile *file,
2163 int io_priority,
2164 GCancellable *cancellable,
2165 GAsyncReadyCallback callback,
2166 gpointer user_data)
2168 GFileIface *iface;
2170 g_return_if_fail (G_IS_FILE (file));
2172 iface = G_FILE_GET_IFACE (file);
2173 (* iface->open_readwrite_async) (file,
2174 io_priority,
2175 cancellable,
2176 callback,
2177 user_data);
2181 * g_file_open_readwrite_finish:
2182 * @file: input #GFile.
2183 * @res: a #GAsyncResult.
2184 * @error: a #GError, or %NULL
2186 * Finishes an asynchronous file read operation started with
2187 * g_file_open_readwrite_async().
2189 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2190 * Free the returned object with g_object_unref().
2192 * Since: 2.22
2194 GFileIOStream *
2195 g_file_open_readwrite_finish (GFile *file,
2196 GAsyncResult *res,
2197 GError **error)
2199 GFileIface *iface;
2201 g_return_val_if_fail (G_IS_FILE (file), NULL);
2202 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2204 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2206 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2207 if (g_simple_async_result_propagate_error (simple, error))
2208 return NULL;
2211 iface = G_FILE_GET_IFACE (file);
2212 return (* iface->open_readwrite_finish) (file, res, error);
2217 * g_file_create_readwrite_async:
2218 * @file: input #GFile
2219 * @flags: a set of #GFileCreateFlags
2220 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2221 * of the request
2222 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
2223 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
2224 * @user_data: (closure): the data to pass to callback function
2226 * Asynchronously creates a new file and returns a stream for reading and
2227 * writing to it. The file must not already exist.
2229 * For more details, see g_file_create_readwrite() which is
2230 * the synchronous version of this call.
2232 * When the operation is finished, @callback will be called. You can then
2233 * call g_file_create_readwrite_finish() to get the result of the operation.
2235 * Since: 2.22
2237 void
2238 g_file_create_readwrite_async (GFile *file,
2239 GFileCreateFlags flags,
2240 int io_priority,
2241 GCancellable *cancellable,
2242 GAsyncReadyCallback callback,
2243 gpointer user_data)
2245 GFileIface *iface;
2247 g_return_if_fail (G_IS_FILE (file));
2249 iface = G_FILE_GET_IFACE (file);
2250 (* iface->create_readwrite_async) (file,
2251 flags,
2252 io_priority,
2253 cancellable,
2254 callback,
2255 user_data);
2259 * g_file_create_readwrite_finish:
2260 * @file: input #GFile
2261 * @res: a #GAsyncResult
2262 * @error: a #GError, or %NULL
2264 * Finishes an asynchronous file create operation started with
2265 * g_file_create_readwrite_async().
2267 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2268 * Free the returned object with g_object_unref().
2270 * Since: 2.22
2272 GFileIOStream *
2273 g_file_create_readwrite_finish (GFile *file,
2274 GAsyncResult *res,
2275 GError **error)
2277 GFileIface *iface;
2279 g_return_val_if_fail (G_IS_FILE (file), NULL);
2280 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2282 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2284 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2285 if (g_simple_async_result_propagate_error (simple, error))
2286 return NULL;
2289 iface = G_FILE_GET_IFACE (file);
2290 return (* iface->create_readwrite_finish) (file, res, error);
2294 * g_file_replace_readwrite_async:
2295 * @file: input #GFile.
2296 * @etag: (allow-none): an <link linkend="gfile-etag">entity tag</link> for the
2297 * current #GFile, or NULL to ignore.
2298 * @make_backup: %TRUE if a backup should be created.
2299 * @flags: a set of #GFileCreateFlags.
2300 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2301 * of the request.
2302 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
2303 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
2304 * @user_data: (closure): the data to pass to callback function
2306 * Asynchronously overwrites the file in read-write mode, replacing the
2307 * contents, possibly creating a backup copy of the file first.
2309 * For more details, see g_file_replace_readwrite() which is
2310 * the synchronous version of this call.
2312 * When the operation is finished, @callback will be called. You can then
2313 * call g_file_replace_readwrite_finish() to get the result of the operation.
2315 * Since: 2.22
2317 void
2318 g_file_replace_readwrite_async (GFile *file,
2319 const char *etag,
2320 gboolean make_backup,
2321 GFileCreateFlags flags,
2322 int io_priority,
2323 GCancellable *cancellable,
2324 GAsyncReadyCallback callback,
2325 gpointer user_data)
2327 GFileIface *iface;
2329 g_return_if_fail (G_IS_FILE (file));
2331 iface = G_FILE_GET_IFACE (file);
2332 (* iface->replace_readwrite_async) (file,
2333 etag,
2334 make_backup,
2335 flags,
2336 io_priority,
2337 cancellable,
2338 callback,
2339 user_data);
2343 * g_file_replace_readwrite_finish:
2344 * @file: input #GFile.
2345 * @res: a #GAsyncResult.
2346 * @error: a #GError, or %NULL
2348 * Finishes an asynchronous file replace operation started with
2349 * g_file_replace_readwrite_async().
2351 * Returns: (transfer full): a #GFileIOStream, or %NULL on error.
2352 * Free the returned object with g_object_unref().
2354 * Since: 2.22
2356 GFileIOStream *
2357 g_file_replace_readwrite_finish (GFile *file,
2358 GAsyncResult *res,
2359 GError **error)
2361 GFileIface *iface;
2363 g_return_val_if_fail (G_IS_FILE (file), NULL);
2364 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2366 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2368 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2369 if (g_simple_async_result_propagate_error (simple, error))
2370 return NULL;
2373 iface = G_FILE_GET_IFACE (file);
2374 return (* iface->replace_readwrite_finish) (file, res, error);
2377 static gboolean
2378 copy_symlink (GFile *destination,
2379 GFileCopyFlags flags,
2380 GCancellable *cancellable,
2381 const char *target,
2382 GError **error)
2384 GError *my_error;
2385 gboolean tried_delete;
2386 GFileInfo *info;
2387 GFileType file_type;
2389 tried_delete = FALSE;
2391 retry:
2392 my_error = NULL;
2393 if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
2395 /* Maybe it already existed, and we want to overwrite? */
2396 if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) &&
2397 my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
2399 g_error_free (my_error);
2402 /* Don't overwrite if the destination is a directory */
2403 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2404 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2405 cancellable, &my_error);
2406 if (info != NULL)
2408 file_type = g_file_info_get_file_type (info);
2409 g_object_unref (info);
2411 if (file_type == G_FILE_TYPE_DIRECTORY)
2413 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
2414 _("Can't copy over directory"));
2415 return FALSE;
2419 if (!g_file_delete (destination, cancellable, error))
2420 return FALSE;
2422 tried_delete = TRUE;
2423 goto retry;
2425 /* Nah, fail */
2426 g_propagate_error (error, my_error);
2427 return FALSE;
2430 return TRUE;
2433 static GInputStream *
2434 open_source_for_copy (GFile *source,
2435 GFile *destination,
2436 GFileCopyFlags flags,
2437 GCancellable *cancellable,
2438 GError **error)
2440 GError *my_error;
2441 GInputStream *in;
2442 GFileInfo *info;
2443 GFileType file_type;
2445 my_error = NULL;
2446 in = (GInputStream *)g_file_read (source, cancellable, &my_error);
2447 if (in != NULL)
2448 return in;
2450 /* There was an error opening the source, try to set a good error for it: */
2452 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
2454 /* The source is a directory, don't fail with WOULD_RECURSE immediately,
2455 * as that is less useful to the app. Better check for errors on the
2456 * target instead.
2458 g_error_free (my_error);
2459 my_error = NULL;
2461 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2462 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2463 cancellable, &my_error);
2464 if (info != NULL &&
2465 g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_TYPE))
2467 file_type = g_file_info_get_file_type (info);
2468 g_object_unref (info);
2470 if (flags & G_FILE_COPY_OVERWRITE)
2472 if (file_type == G_FILE_TYPE_DIRECTORY)
2474 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
2475 _("Can't copy directory over directory"));
2476 return NULL;
2478 /* continue to would_recurse error */
2480 else
2482 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
2483 _("Target file exists"));
2484 return NULL;
2487 else
2489 /* Error getting info from target, return that error
2490 * (except for NOT_FOUND, which is no error here)
2492 if (my_error != NULL && !g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
2494 g_propagate_error (error, my_error);
2495 return NULL;
2497 g_clear_error (&my_error);
2500 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
2501 _("Can't recursively copy directory"));
2502 return NULL;
2505 g_propagate_error (error, my_error);
2506 return NULL;
2509 static gboolean
2510 should_copy (GFileAttributeInfo *info,
2511 gboolean as_move,
2512 gboolean skip_perms)
2514 if (skip_perms && strcmp(info->name, "unix::mode") == 0)
2515 return FALSE;
2517 if (as_move)
2518 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
2519 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
2522 static char *
2523 build_attribute_list_for_copy (GFileAttributeInfoList *attributes,
2524 GFileAttributeInfoList *namespaces,
2525 gboolean as_move,
2526 gboolean skip_perms)
2528 GString *s;
2529 gboolean first;
2530 int i;
2532 first = TRUE;
2533 s = g_string_new ("");
2535 if (attributes)
2537 for (i = 0; i < attributes->n_infos; i++)
2539 if (should_copy (&attributes->infos[i], as_move, skip_perms))
2541 if (first)
2542 first = FALSE;
2543 else
2544 g_string_append_c (s, ',');
2546 g_string_append (s, attributes->infos[i].name);
2551 if (namespaces)
2553 for (i = 0; i < namespaces->n_infos; i++)
2555 if (should_copy (&namespaces->infos[i], as_move, FALSE))
2557 if (first)
2558 first = FALSE;
2559 else
2560 g_string_append_c (s, ',');
2562 g_string_append (s, namespaces->infos[i].name);
2563 g_string_append (s, "::*");
2568 return g_string_free (s, FALSE);
2572 * g_file_copy_attributes:
2573 * @source: a #GFile with attributes.
2574 * @destination: a #GFile to copy attributes to.
2575 * @flags: a set of #GFileCopyFlags.
2576 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
2577 * @error: a #GError, %NULL to ignore.
2579 * Copies the file attributes from @source to @destination.
2581 * Normally only a subset of the file attributes are copied,
2582 * those that are copies in a normal file copy operation
2583 * (which for instance does not include e.g. owner). However
2584 * if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
2585 * all the metadata that is possible to copy is copied. This
2586 * is useful when implementing move by copy + delete source.
2588 * Returns: %TRUE if the attributes were copied successfully, %FALSE otherwise.
2590 gboolean
2591 g_file_copy_attributes (GFile *source,
2592 GFile *destination,
2593 GFileCopyFlags flags,
2594 GCancellable *cancellable,
2595 GError **error)
2597 GFileAttributeInfoList *attributes, *namespaces;
2598 char *attrs_to_read;
2599 gboolean res;
2600 GFileInfo *info;
2601 gboolean as_move;
2602 gboolean source_nofollow_symlinks;
2603 gboolean skip_perms;
2605 as_move = flags & G_FILE_COPY_ALL_METADATA;
2606 source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
2607 skip_perms = (flags & G_FILE_COPY_TARGET_DEFAULT_PERMS) != 0;
2609 /* Ignore errors here, if the target supports no attributes there is nothing to copy */
2610 attributes = g_file_query_settable_attributes (destination, cancellable, NULL);
2611 namespaces = g_file_query_writable_namespaces (destination, cancellable, NULL);
2613 if (attributes == NULL && namespaces == NULL)
2614 return TRUE;
2616 attrs_to_read = build_attribute_list_for_copy (attributes, namespaces, as_move, skip_perms);
2618 /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
2619 * we just don't copy it.
2621 info = g_file_query_info (source, attrs_to_read,
2622 source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
2623 cancellable,
2624 NULL);
2626 g_free (attrs_to_read);
2628 res = TRUE;
2629 if (info)
2631 res = g_file_set_attributes_from_info (destination,
2632 info,
2633 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2634 cancellable,
2635 error);
2636 g_object_unref (info);
2639 g_file_attribute_info_list_unref (attributes);
2640 g_file_attribute_info_list_unref (namespaces);
2642 return res;
2645 static gboolean
2646 copy_stream_with_progress (GInputStream *in,
2647 GOutputStream *out,
2648 GFile *source,
2649 GCancellable *cancellable,
2650 GFileProgressCallback progress_callback,
2651 gpointer progress_callback_data,
2652 GError **error)
2654 gssize n_read, n_written;
2655 goffset current_size;
2656 char buffer[1024*64], *p;
2657 gboolean res;
2658 goffset total_size;
2659 GFileInfo *info;
2661 total_size = -1;
2662 /* avoid performance impact of querying total size when it's not needed */
2663 if (progress_callback)
2665 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
2666 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2667 cancellable, NULL);
2668 if (info)
2670 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2671 total_size = g_file_info_get_size (info);
2672 g_object_unref (info);
2675 if (total_size == -1)
2677 info = g_file_query_info (source,
2678 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2679 G_FILE_QUERY_INFO_NONE,
2680 cancellable, NULL);
2681 if (info)
2683 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2684 total_size = g_file_info_get_size (info);
2685 g_object_unref (info);
2690 if (total_size == -1)
2691 total_size = 0;
2693 current_size = 0;
2694 res = TRUE;
2695 while (TRUE)
2697 n_read = g_input_stream_read (in, buffer, sizeof (buffer), cancellable, error);
2698 if (n_read == -1)
2700 res = FALSE;
2701 break;
2704 if (n_read == 0)
2705 break;
2707 current_size += n_read;
2709 p = buffer;
2710 while (n_read > 0)
2712 n_written = g_output_stream_write (out, p, n_read, cancellable, error);
2713 if (n_written == -1)
2715 res = FALSE;
2716 break;
2719 p += n_written;
2720 n_read -= n_written;
2723 if (!res)
2724 break;
2726 if (progress_callback)
2727 progress_callback (current_size, total_size, progress_callback_data);
2730 /* Make sure we send full copied size */
2731 if (progress_callback)
2732 progress_callback (current_size, total_size, progress_callback_data);
2734 return res;
2737 #ifdef HAVE_SPLICE
2739 static gboolean
2740 do_splice (int fd_in,
2741 loff_t *off_in,
2742 int fd_out,
2743 loff_t *off_out,
2744 size_t len,
2745 long *bytes_transferd,
2746 GError **error)
2748 long result;
2750 retry:
2751 result = splice (fd_in, off_in, fd_out, off_out, len, SPLICE_F_MORE);
2753 if (result == -1)
2755 int errsv = errno;
2757 if (errsv == EINTR)
2758 goto retry;
2759 else if (errsv == ENOSYS || errsv == EINVAL)
2760 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2761 _("Splice not supported"));
2762 else
2763 g_set_error (error, G_IO_ERROR,
2764 g_io_error_from_errno (errsv),
2765 _("Error splicing file: %s"),
2766 g_strerror (errsv));
2768 return FALSE;
2771 *bytes_transferd = result;
2772 return TRUE;
2775 static gboolean
2776 splice_stream_with_progress (GInputStream *in,
2777 GOutputStream *out,
2778 GCancellable *cancellable,
2779 GFileProgressCallback progress_callback,
2780 gpointer progress_callback_data,
2781 GError **error)
2783 int buffer[2];
2784 gboolean res;
2785 goffset total_size;
2786 loff_t offset_in;
2787 loff_t offset_out;
2788 int fd_in, fd_out;
2790 fd_in = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in));
2791 fd_out = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out));
2793 if (pipe (buffer) != 0)
2795 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2796 "Pipe creation failed");
2797 return FALSE;
2800 total_size = -1;
2801 /* avoid performance impact of querying total size when it's not needed */
2802 if (progress_callback)
2804 struct stat sbuf;
2806 if (fstat (fd_in, &sbuf) == 0)
2807 total_size = sbuf.st_size;
2810 if (total_size == -1)
2811 total_size = 0;
2813 offset_in = offset_out = 0;
2814 res = FALSE;
2815 while (TRUE)
2817 long n_read;
2818 long n_written;
2820 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2821 break;
2823 if (!do_splice (fd_in, &offset_in, buffer[1], NULL, 1024*64, &n_read, error))
2824 break;
2826 if (n_read == 0)
2828 res = TRUE;
2829 break;
2832 while (n_read > 0)
2834 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2835 goto out;
2837 if (!do_splice (buffer[0], NULL, fd_out, &offset_out, n_read, &n_written, error))
2838 goto out;
2840 n_read -= n_written;
2843 if (progress_callback)
2844 progress_callback (offset_in, total_size, progress_callback_data);
2847 /* Make sure we send full copied size */
2848 if (progress_callback)
2849 progress_callback (offset_in, total_size, progress_callback_data);
2851 out:
2852 close (buffer[0]);
2853 close (buffer[1]);
2855 return res;
2857 #endif
2859 static gboolean
2860 file_copy_fallback (GFile *source,
2861 GFile *destination,
2862 GFileCopyFlags flags,
2863 GCancellable *cancellable,
2864 GFileProgressCallback progress_callback,
2865 gpointer progress_callback_data,
2866 GError **error)
2868 GInputStream *in;
2869 GOutputStream *out;
2870 GFileInfo *info;
2871 const char *target;
2872 gboolean result;
2873 #ifdef HAVE_SPLICE
2874 gboolean fallback = TRUE;
2875 #endif
2877 /* need to know the file type */
2878 info = g_file_query_info (source,
2879 G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
2880 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2881 cancellable,
2882 error);
2884 if (info == NULL)
2885 return FALSE;
2887 /* Maybe copy the symlink? */
2888 if ((flags & G_FILE_COPY_NOFOLLOW_SYMLINKS) &&
2889 g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK)
2891 target = g_file_info_get_symlink_target (info);
2892 if (target)
2894 if (!copy_symlink (destination, flags, cancellable, target, error))
2896 g_object_unref (info);
2897 return FALSE;
2900 g_object_unref (info);
2901 goto copied_file;
2903 /* ... else fall back on a regular file copy */
2904 g_object_unref (info);
2906 /* Handle "special" files (pipes, device nodes, ...)? */
2907 else if (g_file_info_get_file_type (info) == G_FILE_TYPE_SPECIAL)
2909 /* FIXME: could try to recreate device nodes and others? */
2911 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2912 _("Can't copy special file"));
2913 g_object_unref (info);
2914 return FALSE;
2916 /* Everything else should just fall back on a regular copy. */
2917 else
2918 g_object_unref (info);
2920 in = open_source_for_copy (source, destination, flags, cancellable, error);
2921 if (in == NULL)
2922 return FALSE;
2924 if (flags & G_FILE_COPY_OVERWRITE)
2926 out = (GOutputStream *)g_file_replace (destination,
2927 NULL,
2928 flags & G_FILE_COPY_BACKUP,
2929 G_FILE_CREATE_REPLACE_DESTINATION,
2930 cancellable, error);
2932 else
2934 out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
2937 if (out == NULL)
2939 g_object_unref (in);
2940 return FALSE;
2943 #ifdef HAVE_SPLICE
2944 if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
2946 GError *splice_err = NULL;
2948 result = splice_stream_with_progress (in, out, cancellable,
2949 progress_callback, progress_callback_data,
2950 &splice_err);
2952 if (result || !g_error_matches (splice_err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
2954 fallback = FALSE;
2955 if (!result)
2956 g_propagate_error (error, splice_err);
2958 else
2959 g_clear_error (&splice_err);
2962 if (fallback)
2963 #endif
2964 result = copy_stream_with_progress (in, out, source, cancellable,
2965 progress_callback, progress_callback_data,
2966 error);
2968 /* Don't care about errors in source here */
2969 g_input_stream_close (in, cancellable, NULL);
2971 /* But write errors on close are bad! */
2972 if (!g_output_stream_close (out, cancellable, result ? error : NULL))
2973 result = FALSE;
2975 g_object_unref (in);
2976 g_object_unref (out);
2978 if (result == FALSE)
2979 return FALSE;
2981 copied_file:
2982 /* Ignore errors here. Failure to copy metadata is not a hard error */
2983 g_file_copy_attributes (source, destination,
2984 flags, cancellable, NULL);
2986 return TRUE;
2990 * g_file_copy:
2991 * @source: input #GFile.
2992 * @destination: destination #GFile
2993 * @flags: set of #GFileCopyFlags
2994 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
2995 * @progress_callback: (allow-none) (scope call): function to callback with
2996 * progress information, or %NULL if progress information is not needed
2997 * @progress_callback_data: (closure): user data to pass to @progress_callback
2998 * @error: #GError to set on error, or %NULL
3000 * Copies the file @source to the location specified by @destination.
3001 * Can not handle recursive copies of directories.
3003 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3004 * existing @destination file is overwritten.
3006 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3007 * will be copied as symlinks, otherwise the target of the
3008 * @source symlink will be copied.
3010 * If @cancellable is not %NULL, then the operation can be cancelled by
3011 * triggering the cancellable object from another thread. If the operation
3012 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3014 * If @progress_callback is not %NULL, then the operation can be monitored by
3015 * setting this to a #GFileProgressCallback function. @progress_callback_data
3016 * will be passed to this function. It is guaranteed that this callback will
3017 * be called after all data has been transferred with the total number of bytes
3018 * copied during the operation.
3020 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
3021 * error is returned, independent on the status of the @destination.
3023 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
3024 * error G_IO_ERROR_EXISTS is returned.
3026 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
3027 * error is returned. If trying to overwrite a directory with a directory the
3028 * G_IO_ERROR_WOULD_MERGE error is returned.
3030 * If the source is a directory and the target does not exist, or
3031 * #G_FILE_COPY_OVERWRITE is specified and the target is a file, then the
3032 * G_IO_ERROR_WOULD_RECURSE error is returned.
3034 * If you are interested in copying the #GFile object itself (not the on-disk
3035 * file), see g_file_dup().
3037 * Returns: %TRUE on success, %FALSE otherwise.
3039 gboolean
3040 g_file_copy (GFile *source,
3041 GFile *destination,
3042 GFileCopyFlags flags,
3043 GCancellable *cancellable,
3044 GFileProgressCallback progress_callback,
3045 gpointer progress_callback_data,
3046 GError **error)
3048 GFileIface *iface;
3049 GError *my_error;
3050 gboolean res;
3052 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3053 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3055 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3056 return FALSE;
3058 iface = G_FILE_GET_IFACE (destination);
3059 if (iface->copy)
3061 my_error = NULL;
3062 res = (* iface->copy) (source, destination,
3063 flags, cancellable,
3064 progress_callback, progress_callback_data,
3065 &my_error);
3067 if (res)
3068 return TRUE;
3070 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3072 g_propagate_error (error, my_error);
3073 return FALSE;
3075 else
3076 g_clear_error (&my_error);
3079 /* If the types are different, and the destination method failed
3080 also try the source method */
3081 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3083 iface = G_FILE_GET_IFACE (source);
3085 if (iface->copy)
3087 my_error = NULL;
3088 res = (* iface->copy) (source, destination,
3089 flags, cancellable,
3090 progress_callback, progress_callback_data,
3091 &my_error);
3093 if (res)
3094 return TRUE;
3096 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3098 g_propagate_error (error, my_error);
3099 return FALSE;
3101 else
3102 g_clear_error (&my_error);
3106 return file_copy_fallback (source, destination, flags, cancellable,
3107 progress_callback, progress_callback_data,
3108 error);
3112 * g_file_copy_async: (skip)
3113 * @source: input #GFile.
3114 * @destination: destination #GFile
3115 * @flags: set of #GFileCopyFlags
3116 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3117 * of the request
3118 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
3119 * @progress_callback: (allow-none): function to callback with progress
3120 * information, or %NULL if progress information is not needed
3121 * @progress_callback_data: (closure): user data to pass to @progress_callback
3122 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
3123 * @user_data: the data to pass to callback function
3125 * Copies the file @source to the location specified by @destination
3126 * asynchronously. For details of the behaviour, see g_file_copy().
3128 * If @progress_callback is not %NULL, then that function that will be called
3129 * just like in g_file_copy(), however the callback will run in the main loop,
3130 * not in the thread that is doing the I/O operation.
3132 * When the operation is finished, @callback will be called. You can then call
3133 * g_file_copy_finish() to get the result of the operation.
3135 void
3136 g_file_copy_async (GFile *source,
3137 GFile *destination,
3138 GFileCopyFlags flags,
3139 int io_priority,
3140 GCancellable *cancellable,
3141 GFileProgressCallback progress_callback,
3142 gpointer progress_callback_data,
3143 GAsyncReadyCallback callback,
3144 gpointer user_data)
3146 GFileIface *iface;
3148 g_return_if_fail (G_IS_FILE (source));
3149 g_return_if_fail (G_IS_FILE (destination));
3151 iface = G_FILE_GET_IFACE (source);
3152 (* iface->copy_async) (source,
3153 destination,
3154 flags,
3155 io_priority,
3156 cancellable,
3157 progress_callback,
3158 progress_callback_data,
3159 callback,
3160 user_data);
3164 * g_file_copy_finish:
3165 * @file: input #GFile.
3166 * @res: a #GAsyncResult.
3167 * @error: a #GError, or %NULL
3169 * Finishes copying the file started with
3170 * g_file_copy_async().
3172 * Returns: a %TRUE on success, %FALSE on error.
3174 gboolean
3175 g_file_copy_finish (GFile *file,
3176 GAsyncResult *res,
3177 GError **error)
3179 GFileIface *iface;
3181 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3182 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
3184 if (G_IS_SIMPLE_ASYNC_RESULT (res))
3186 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3188 if (g_simple_async_result_propagate_error (simple, error))
3189 return FALSE;
3192 iface = G_FILE_GET_IFACE (file);
3193 return (* iface->copy_finish) (file, res, error);
3197 * g_file_move:
3198 * @source: #GFile pointing to the source location.
3199 * @destination: #GFile pointing to the destination location.
3200 * @flags: set of #GFileCopyFlags.
3201 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
3202 * @progress_callback: (scope call): #GFileProgressCallback function for updates.
3203 * @progress_callback_data: (closure): gpointer to user data for the callback function.
3204 * @error: #GError for returning error conditions, or %NULL
3207 * Tries to move the file or directory @source to the location specified by @destination.
3208 * If native move operations are supported then this is used, otherwise a copy + delete
3209 * fallback is used. The native implementation may support moving directories (for instance
3210 * on moves inside the same filesystem), but the fallback code does not.
3212 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3213 * existing @destination file is overwritten.
3215 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3216 * will be copied as symlinks, otherwise the target of the
3217 * @source symlink will be copied.
3219 * If @cancellable is not %NULL, then the operation can be cancelled by
3220 * triggering the cancellable object from another thread. If the operation
3221 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3223 * If @progress_callback is not %NULL, then the operation can be monitored by
3224 * setting this to a #GFileProgressCallback function. @progress_callback_data
3225 * will be passed to this function. It is guaranteed that this callback will
3226 * be called after all data has been transferred with the total number of bytes
3227 * copied during the operation.
3229 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
3230 * error is returned, independent on the status of the @destination.
3232 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
3233 * error G_IO_ERROR_EXISTS is returned.
3235 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
3236 * error is returned. If trying to overwrite a directory with a directory the
3237 * G_IO_ERROR_WOULD_MERGE error is returned.
3239 * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
3240 * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
3241 * may be returned (if the native move operation isn't available).
3243 * Returns: %TRUE on successful move, %FALSE otherwise.
3245 gboolean
3246 g_file_move (GFile *source,
3247 GFile *destination,
3248 GFileCopyFlags flags,
3249 GCancellable *cancellable,
3250 GFileProgressCallback progress_callback,
3251 gpointer progress_callback_data,
3252 GError **error)
3254 GFileIface *iface;
3255 GError *my_error;
3256 gboolean res;
3258 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3259 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3261 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3262 return FALSE;
3264 iface = G_FILE_GET_IFACE (destination);
3265 if (iface->move)
3267 my_error = NULL;
3268 res = (* iface->move) (source, destination,
3269 flags, cancellable,
3270 progress_callback, progress_callback_data,
3271 &my_error);
3273 if (res)
3274 return TRUE;
3276 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3278 g_propagate_error (error, my_error);
3279 return FALSE;
3283 /* If the types are different, and the destination method failed
3284 also try the source method */
3285 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3287 iface = G_FILE_GET_IFACE (source);
3289 if (iface->move)
3291 my_error = NULL;
3292 res = (* iface->move) (source, destination,
3293 flags, cancellable,
3294 progress_callback, progress_callback_data,
3295 &my_error);
3297 if (res)
3298 return TRUE;
3300 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3302 g_propagate_error (error, my_error);
3303 return FALSE;
3308 if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
3310 g_set_error_literal (error, G_IO_ERROR,
3311 G_IO_ERROR_NOT_SUPPORTED,
3312 _("Operation not supported"));
3313 return FALSE;
3316 flags |= G_FILE_COPY_ALL_METADATA;
3317 if (!g_file_copy (source, destination, flags, cancellable,
3318 progress_callback, progress_callback_data,
3319 error))
3320 return FALSE;
3322 return g_file_delete (source, cancellable, error);
3326 * g_file_make_directory
3327 * @file: input #GFile.
3328 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
3329 * @error: a #GError, or %NULL
3331 * Creates a directory. Note that this will only create a child directory of
3332 * the immediate parent directory of the path or URI given by the #GFile. To
3333 * recursively create directories, see g_file_make_directory_with_parents().
3334 * This function will fail if the parent directory does not exist, setting
3335 * @error to %G_IO_ERROR_NOT_FOUND. If the file system doesn't support creating
3336 * directories, this function will fail, setting @error to
3337 * %G_IO_ERROR_NOT_SUPPORTED.
3339 * For a local #GFile the newly created directory will have the default
3340 * (current) ownership and permissions of the current process.
3342 * If @cancellable is not %NULL, then the operation can be cancelled by
3343 * triggering the cancellable object from another thread. If the operation
3344 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3346 * Returns: %TRUE on successful creation, %FALSE otherwise.
3348 gboolean
3349 g_file_make_directory (GFile *file,
3350 GCancellable *cancellable,
3351 GError **error)
3353 GFileIface *iface;
3355 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3357 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3358 return FALSE;
3360 iface = G_FILE_GET_IFACE (file);
3362 if (iface->make_directory == NULL)
3364 g_set_error_literal (error, G_IO_ERROR,
3365 G_IO_ERROR_NOT_SUPPORTED,
3366 _("Operation not supported"));
3367 return FALSE;
3370 return (* iface->make_directory) (file, cancellable, error);
3374 * g_file_make_directory_with_parents:
3375 * @file: input #GFile.
3376 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
3377 * @error: a #GError, or %NULL
3379 * Creates a directory and any parent directories that may not exist similar to
3380 * 'mkdir -p'. If the file system does not support creating directories, this
3381 * function will fail, setting @error to %G_IO_ERROR_NOT_SUPPORTED. If the
3382 * directory itself already exists, this function will fail setting @error
3383 * to %G_IO_ERROR_EXISTS, unlike the similar g_mkdir_with_parents().
3385 * For a local #GFile the newly created directories will have the default
3386 * (current) ownership and permissions of the current process.
3388 * If @cancellable is not %NULL, then the operation can be cancelled by
3389 * triggering the cancellable object from another thread. If the operation
3390 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3392 * Returns: %TRUE if all directories have been successfully created, %FALSE
3393 * otherwise.
3395 * Since: 2.18
3397 gboolean
3398 g_file_make_directory_with_parents (GFile *file,
3399 GCancellable *cancellable,
3400 GError **error)
3402 gboolean result;
3403 GFile *parent_file, *work_file;
3404 GList *list = NULL, *l;
3405 GError *my_error = NULL;
3407 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3408 return FALSE;
3410 result = g_file_make_directory (file, cancellable, &my_error);
3411 if (result || my_error->code != G_IO_ERROR_NOT_FOUND)
3413 if (my_error)
3414 g_propagate_error (error, my_error);
3415 return result;
3418 work_file = file;
3420 while (!result && my_error->code == G_IO_ERROR_NOT_FOUND)
3422 g_clear_error (&my_error);
3424 parent_file = g_file_get_parent (work_file);
3425 if (parent_file == NULL)
3426 break;
3427 result = g_file_make_directory (parent_file, cancellable, &my_error);
3429 if (!result && my_error->code == G_IO_ERROR_NOT_FOUND)
3430 list = g_list_prepend (list, parent_file);
3432 work_file = parent_file;
3435 for (l = list; result && l; l = l->next)
3437 result = g_file_make_directory ((GFile *) l->data, cancellable, &my_error);
3440 /* Clean up */
3441 while (list != NULL)
3443 g_object_unref ((GFile *) list->data);
3444 list = g_list_remove (list, list->data);
3447 if (!result)
3449 g_propagate_error (error, my_error);
3450 return result;
3453 return g_file_make_directory (file, cancellable, error);
3457 * g_file_make_symbolic_link:
3458 * @file: a #GFile with the name of the symlink to create
3459 * @symlink_value: a string with the path for the target of the new symlink
3460 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
3461 * @error: a #GError.
3463 * Creates a symbolic link named @file which contains the string
3464 * @symlink_value.
3466 * If @cancellable is not %NULL, then the operation can be cancelled by
3467 * triggering the cancellable object from another thread. If the operation
3468 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3470 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
3472 gboolean
3473 g_file_make_symbolic_link (GFile *file,
3474 const char *symlink_value,
3475 GCancellable *cancellable,
3476 GError **error)
3478 GFileIface *iface;
3480 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3481 g_return_val_if_fail (symlink_value != NULL, FALSE);
3483 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3484 return FALSE;
3486 if (*symlink_value == '\0')
3488 g_set_error_literal (error, G_IO_ERROR,
3489 G_IO_ERROR_INVALID_ARGUMENT,
3490 _("Invalid symlink value given"));
3491 return FALSE;
3494 iface = G_FILE_GET_IFACE (file);
3496 if (iface->make_symbolic_link == NULL)
3498 g_set_error_literal (error, G_IO_ERROR,
3499 G_IO_ERROR_NOT_SUPPORTED,
3500 _("Operation not supported"));
3501 return FALSE;
3504 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
3508 * g_file_delete:
3509 * @file: input #GFile.
3510 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
3511 * @error: a #GError, or %NULL
3513 * Deletes a file. If the @file is a directory, it will only be deleted if it
3514 * is empty.
3516 * If @cancellable is not %NULL, then the operation can be cancelled by
3517 * triggering the cancellable object from another thread. If the operation
3518 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3520 * Virtual: delete_file
3521 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
3523 gboolean
3524 g_file_delete (GFile *file,
3525 GCancellable *cancellable,
3526 GError **error)
3528 GFileIface *iface;
3530 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3532 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3533 return FALSE;
3535 iface = G_FILE_GET_IFACE (file);
3537 if (iface->delete_file == NULL)
3539 g_set_error_literal (error, G_IO_ERROR,
3540 G_IO_ERROR_NOT_SUPPORTED,
3541 _("Operation not supported"));
3542 return FALSE;
3545 return (* iface->delete_file) (file, cancellable, error);
3549 * g_file_trash:
3550 * @file: #GFile to send to trash.
3551 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
3552 * @error: a #GError, or %NULL
3554 * Sends @file to the "Trashcan", if possible. This is similar to
3555 * deleting it, but the user can recover it before emptying the trashcan.
3556 * Not all file systems support trashing, so this call can return the
3557 * %G_IO_ERROR_NOT_SUPPORTED error.
3560 * If @cancellable is not %NULL, then the operation can be cancelled by
3561 * triggering the cancellable object from another thread. If the operation
3562 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3564 * Returns: %TRUE on successful trash, %FALSE otherwise.
3566 gboolean
3567 g_file_trash (GFile *file,
3568 GCancellable *cancellable,
3569 GError **error)
3571 GFileIface *iface;
3573 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3575 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3576 return FALSE;
3578 iface = G_FILE_GET_IFACE (file);
3580 if (iface->trash == NULL)
3582 g_set_error_literal (error,
3583 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
3584 _("Trash not supported"));
3585 return FALSE;
3588 return (* iface->trash) (file, cancellable, error);
3592 * g_file_set_display_name:
3593 * @file: input #GFile.
3594 * @display_name: a string.
3595 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
3596 * @error: a #GError, or %NULL
3598 * Renames @file to the specified display name.
3600 * The display name is converted from UTF8 to the correct encoding for the target
3601 * filesystem if possible and the @file is renamed to this.
3603 * If you want to implement a rename operation in the user interface the edit name
3604 * (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the initial value in the rename
3605 * widget, and then the result after editing should be passed to g_file_set_display_name().
3607 * On success the resulting converted filename is returned.
3609 * If @cancellable is not %NULL, then the operation can be cancelled by
3610 * triggering the cancellable object from another thread. If the operation
3611 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3613 * Returns: (transfer full): a #GFile specifying what @file was renamed to, or %NULL
3614 * if there was an error.
3615 * Free the returned object with g_object_unref().
3617 GFile *
3618 g_file_set_display_name (GFile *file,
3619 const char *display_name,
3620 GCancellable *cancellable,
3621 GError **error)
3623 GFileIface *iface;
3625 g_return_val_if_fail (G_IS_FILE (file), NULL);
3626 g_return_val_if_fail (display_name != NULL, NULL);
3628 if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
3630 g_set_error (error,
3631 G_IO_ERROR,
3632 G_IO_ERROR_INVALID_ARGUMENT,
3633 _("File names cannot contain '%c'"), G_DIR_SEPARATOR);
3634 return NULL;
3637 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3638 return NULL;
3640 iface = G_FILE_GET_IFACE (file);
3642 return (* iface->set_display_name) (file, display_name, cancellable, error);
3646 * g_file_set_display_name_async:
3647 * @file: input #GFile.
3648 * @display_name: a string.
3649 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3650 * of the request.
3651 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
3652 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
3653 * @user_data: (closure): the data to pass to callback function
3655 * Asynchronously sets the display name for a given #GFile.
3657 * For more details, see g_file_set_display_name() which is
3658 * the synchronous version of this call.
3660 * When the operation is finished, @callback will be called. You can then call
3661 * g_file_set_display_name_finish() to get the result of the operation.
3663 void
3664 g_file_set_display_name_async (GFile *file,
3665 const char *display_name,
3666 int io_priority,
3667 GCancellable *cancellable,
3668 GAsyncReadyCallback callback,
3669 gpointer user_data)
3671 GFileIface *iface;
3673 g_return_if_fail (G_IS_FILE (file));
3674 g_return_if_fail (display_name != NULL);
3676 iface = G_FILE_GET_IFACE (file);
3677 (* iface->set_display_name_async) (file,
3678 display_name,
3679 io_priority,
3680 cancellable,
3681 callback,
3682 user_data);
3686 * g_file_set_display_name_finish:
3687 * @file: input #GFile.
3688 * @res: a #GAsyncResult.
3689 * @error: a #GError, or %NULL
3691 * Finishes setting a display name started with
3692 * g_file_set_display_name_async().
3694 * Returns: (transfer full): a #GFile or %NULL on error.
3695 * Free the returned object with g_object_unref().
3697 GFile *
3698 g_file_set_display_name_finish (GFile *file,
3699 GAsyncResult *res,
3700 GError **error)
3702 GFileIface *iface;
3704 g_return_val_if_fail (G_IS_FILE (file), NULL);
3705 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
3707 if (G_IS_SIMPLE_ASYNC_RESULT (res))
3709 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3710 if (g_simple_async_result_propagate_error (simple, error))
3711 return NULL;
3714 iface = G_FILE_GET_IFACE (file);
3715 return (* iface->set_display_name_finish) (file, res, error);
3719 * g_file_query_settable_attributes:
3720 * @file: input #GFile.
3721 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
3722 * @error: a #GError, or %NULL
3724 * Obtain the list of settable attributes for the file.
3726 * Returns the type and full attribute name of all the attributes
3727 * that can be set on this file. This doesn't mean setting it will always
3728 * succeed though, you might get an access failure, or some specific
3729 * file may not support a specific attribute.
3731 * If @cancellable is not %NULL, then the operation can be cancelled by
3732 * triggering the cancellable object from another thread. If the operation
3733 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3735 * Returns: a #GFileAttributeInfoList describing the settable attributes.
3736 * When you are done with it, release it with g_file_attribute_info_list_unref()
3738 GFileAttributeInfoList *
3739 g_file_query_settable_attributes (GFile *file,
3740 GCancellable *cancellable,
3741 GError **error)
3743 GFileIface *iface;
3744 GError *my_error;
3745 GFileAttributeInfoList *list;
3747 g_return_val_if_fail (G_IS_FILE (file), NULL);
3749 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3750 return NULL;
3752 iface = G_FILE_GET_IFACE (file);
3754 if (iface->query_settable_attributes == NULL)
3755 return g_file_attribute_info_list_new ();
3757 my_error = NULL;
3758 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
3760 if (list == NULL)
3762 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
3764 list = g_file_attribute_info_list_new ();
3765 g_error_free (my_error);
3767 else
3768 g_propagate_error (error, my_error);
3771 return list;
3775 * g_file_query_writable_namespaces:
3776 * @file: input #GFile.
3777 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
3778 * @error: a #GError, or %NULL
3780 * Obtain the list of attribute namespaces where new attributes
3781 * can be created by a user. An example of this is extended
3782 * attributes (in the "xattr" namespace).
3784 * If @cancellable is not %NULL, then the operation can be cancelled by
3785 * triggering the cancellable object from another thread. If the operation
3786 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3788 * Returns: a #GFileAttributeInfoList describing the writable namespaces.
3789 * When you are done with it, release it with g_file_attribute_info_list_unref()
3791 GFileAttributeInfoList *
3792 g_file_query_writable_namespaces (GFile *file,
3793 GCancellable *cancellable,
3794 GError **error)
3796 GFileIface *iface;
3797 GError *my_error;
3798 GFileAttributeInfoList *list;
3800 g_return_val_if_fail (G_IS_FILE (file), NULL);
3802 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3803 return NULL;
3805 iface = G_FILE_GET_IFACE (file);
3807 if (iface->query_writable_namespaces == NULL)
3808 return g_file_attribute_info_list_new ();
3810 my_error = NULL;
3811 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
3813 if (list == NULL)
3815 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
3817 list = g_file_attribute_info_list_new ();
3818 g_error_free (my_error);
3820 else
3821 g_propagate_error (error, my_error);
3824 return list;
3828 * g_file_set_attribute:
3829 * @file: input #GFile.
3830 * @attribute: a string containing the attribute's name.
3831 * @type: The type of the attribute
3832 * @value_p: (allow-none): a pointer to the value (or the pointer itself if the type is a pointer type)
3833 * @flags: a set of #GFileQueryInfoFlags.
3834 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
3835 * @error: a #GError, or %NULL
3837 * Sets an attribute in the file with attribute name @attribute to @value.
3839 * Some attributes can be unset by setting @attribute to
3840 * %G_FILE_ATTRIBUTE_TYPE_INVALID and @value_p to %NULL.
3842 * If @cancellable is not %NULL, then the operation can be cancelled by
3843 * triggering the cancellable object from another thread. If the operation
3844 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3846 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
3848 gboolean
3849 g_file_set_attribute (GFile *file,
3850 const char *attribute,
3851 GFileAttributeType type,
3852 gpointer value_p,
3853 GFileQueryInfoFlags flags,
3854 GCancellable *cancellable,
3855 GError **error)
3857 GFileIface *iface;
3859 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3860 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
3862 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3863 return FALSE;
3865 iface = G_FILE_GET_IFACE (file);
3867 if (iface->set_attribute == NULL)
3869 g_set_error_literal (error, G_IO_ERROR,
3870 G_IO_ERROR_NOT_SUPPORTED,
3871 _("Operation not supported"));
3872 return FALSE;
3875 return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
3879 * g_file_set_attributes_from_info:
3880 * @file: input #GFile.
3881 * @info: a #GFileInfo.
3882 * @flags: #GFileQueryInfoFlags
3883 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
3884 * @error: a #GError, or %NULL
3886 * Tries to set all attributes in the #GFileInfo on the target values,
3887 * not stopping on the first error.
3889 * If there is any error during this operation then @error will be set to
3890 * the first error. Error on particular fields are flagged by setting
3891 * the "status" field in the attribute value to
3892 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can also detect
3893 * further errors.
3895 * If @cancellable is not %NULL, then the operation can be cancelled by
3896 * triggering the cancellable object from another thread. If the operation
3897 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3899 * Returns: %TRUE if there was any error, %FALSE otherwise.
3901 gboolean
3902 g_file_set_attributes_from_info (GFile *file,
3903 GFileInfo *info,
3904 GFileQueryInfoFlags flags,
3905 GCancellable *cancellable,
3906 GError **error)
3908 GFileIface *iface;
3910 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3911 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
3913 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3914 return FALSE;
3916 g_file_info_clear_status (info);
3918 iface = G_FILE_GET_IFACE (file);
3920 return (* iface->set_attributes_from_info) (file,
3921 info,
3922 flags,
3923 cancellable,
3924 error);
3928 static gboolean
3929 g_file_real_set_attributes_from_info (GFile *file,
3930 GFileInfo *info,
3931 GFileQueryInfoFlags flags,
3932 GCancellable *cancellable,
3933 GError **error)
3935 char **attributes;
3936 int i;
3937 gboolean res;
3938 GFileAttributeValue *value;
3940 res = TRUE;
3942 attributes = g_file_info_list_attributes (info, NULL);
3944 for (i = 0; attributes[i] != NULL; i++)
3946 value = _g_file_info_get_attribute_value (info, attributes[i]);
3948 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
3949 continue;
3951 if (!g_file_set_attribute (file, attributes[i],
3952 value->type, _g_file_attribute_value_peek_as_pointer (value),
3953 flags, cancellable, error))
3955 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
3956 res = FALSE;
3957 /* Don't set error multiple times */
3958 error = NULL;
3960 else
3961 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
3964 g_strfreev (attributes);
3966 return res;
3970 * g_file_set_attributes_async:
3971 * @file: input #GFile.
3972 * @info: a #GFileInfo.
3973 * @flags: a #GFileQueryInfoFlags.
3974 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3975 * of the request.
3976 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
3977 * @callback: (scope async): a #GAsyncReadyCallback.
3978 * @user_data: (closure): a #gpointer.
3980 * Asynchronously sets the attributes of @file with @info.
3982 * For more details, see g_file_set_attributes_from_info() which is
3983 * the synchronous version of this call.
3985 * When the operation is finished, @callback will be called. You can then call
3986 * g_file_set_attributes_finish() to get the result of the operation.
3988 void
3989 g_file_set_attributes_async (GFile *file,
3990 GFileInfo *info,
3991 GFileQueryInfoFlags flags,
3992 int io_priority,
3993 GCancellable *cancellable,
3994 GAsyncReadyCallback callback,
3995 gpointer user_data)
3997 GFileIface *iface;
3999 g_return_if_fail (G_IS_FILE (file));
4000 g_return_if_fail (G_IS_FILE_INFO (info));
4002 iface = G_FILE_GET_IFACE (file);
4003 (* iface->set_attributes_async) (file,
4004 info,
4005 flags,
4006 io_priority,
4007 cancellable,
4008 callback,
4009 user_data);
4013 * g_file_set_attributes_finish:
4014 * @file: input #GFile.
4015 * @result: a #GAsyncResult.
4016 * @info: (out) (transfer full): a #GFileInfo.
4017 * @error: a #GError, or %NULL
4019 * Finishes setting an attribute started in g_file_set_attributes_async().
4021 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
4023 gboolean
4024 g_file_set_attributes_finish (GFile *file,
4025 GAsyncResult *result,
4026 GFileInfo **info,
4027 GError **error)
4029 GFileIface *iface;
4031 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4032 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4034 /* No standard handling of errors here, as we must set info even
4035 * on errors
4037 iface = G_FILE_GET_IFACE (file);
4038 return (* iface->set_attributes_finish) (file, result, info, error);
4042 * g_file_set_attribute_string:
4043 * @file: input #GFile.
4044 * @attribute: a string containing the attribute's name.
4045 * @value: a string containing the attribute's value.
4046 * @flags: #GFileQueryInfoFlags.
4047 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
4048 * @error: a #GError, or %NULL
4050 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
4051 * If @attribute is of a different type, this operation will fail.
4053 * If @cancellable is not %NULL, then the operation can be cancelled by
4054 * triggering the cancellable object from another thread. If the operation
4055 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4057 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4059 gboolean
4060 g_file_set_attribute_string (GFile *file,
4061 const char *attribute,
4062 const char *value,
4063 GFileQueryInfoFlags flags,
4064 GCancellable *cancellable,
4065 GError **error)
4067 return g_file_set_attribute (file, attribute,
4068 G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
4069 flags, cancellable, error);
4073 * g_file_set_attribute_byte_string:
4074 * @file: input #GFile.
4075 * @attribute: a string containing the attribute's name.
4076 * @value: a string containing the attribute's new value.
4077 * @flags: a #GFileQueryInfoFlags.
4078 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
4079 * @error: a #GError, or %NULL
4081 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
4082 * If @attribute is of a different type, this operation will fail,
4083 * returning %FALSE.
4085 * If @cancellable is not %NULL, then the operation can be cancelled by
4086 * triggering the cancellable object from another thread. If the operation
4087 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4089 * Returns: %TRUE if the @attribute was successfully set to @value
4090 * in the @file, %FALSE otherwise.
4092 gboolean
4093 g_file_set_attribute_byte_string (GFile *file,
4094 const char *attribute,
4095 const char *value,
4096 GFileQueryInfoFlags flags,
4097 GCancellable *cancellable,
4098 GError **error)
4100 return g_file_set_attribute (file, attribute,
4101 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
4102 flags, cancellable, error);
4106 * g_file_set_attribute_uint32:
4107 * @file: input #GFile.
4108 * @attribute: a string containing the attribute's name.
4109 * @value: a #guint32 containing the attribute's new value.
4110 * @flags: a #GFileQueryInfoFlags.
4111 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
4112 * @error: a #GError, or %NULL
4114 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
4115 * If @attribute is of a different type, this operation will fail.
4117 * If @cancellable is not %NULL, then the operation can be cancelled by
4118 * triggering the cancellable object from another thread. If the operation
4119 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4121 * Returns: %TRUE if the @attribute was successfully set to @value
4122 * in the @file, %FALSE otherwise.
4124 gboolean
4125 g_file_set_attribute_uint32 (GFile *file,
4126 const char *attribute,
4127 guint32 value,
4128 GFileQueryInfoFlags flags,
4129 GCancellable *cancellable,
4130 GError **error)
4132 return g_file_set_attribute (file, attribute,
4133 G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
4134 flags, cancellable, error);
4138 * g_file_set_attribute_int32:
4139 * @file: input #GFile.
4140 * @attribute: a string containing the attribute's name.
4141 * @value: a #gint32 containing the attribute's new value.
4142 * @flags: a #GFileQueryInfoFlags.
4143 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
4144 * @error: a #GError, or %NULL
4146 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
4147 * If @attribute is of a different type, this operation will fail.
4149 * If @cancellable is not %NULL, then the operation can be cancelled by
4150 * triggering the cancellable object from another thread. If the operation
4151 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4153 * Returns: %TRUE if the @attribute was successfully set to @value
4154 * in the @file, %FALSE otherwise.
4156 gboolean
4157 g_file_set_attribute_int32 (GFile *file,
4158 const char *attribute,
4159 gint32 value,
4160 GFileQueryInfoFlags flags,
4161 GCancellable *cancellable,
4162 GError **error)
4164 return g_file_set_attribute (file, attribute,
4165 G_FILE_ATTRIBUTE_TYPE_INT32, &value,
4166 flags, cancellable, error);
4170 * g_file_set_attribute_uint64:
4171 * @file: input #GFile.
4172 * @attribute: a string containing the attribute's name.
4173 * @value: a #guint64 containing the attribute's new value.
4174 * @flags: a #GFileQueryInfoFlags.
4175 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
4176 * @error: a #GError, or %NULL
4178 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
4179 * If @attribute is of a different type, this operation will fail.
4181 * If @cancellable is not %NULL, then the operation can be cancelled by
4182 * triggering the cancellable object from another thread. If the operation
4183 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4185 * Returns: %TRUE if the @attribute was successfully set to @value
4186 * in the @file, %FALSE otherwise.
4188 gboolean
4189 g_file_set_attribute_uint64 (GFile *file,
4190 const char *attribute,
4191 guint64 value,
4192 GFileQueryInfoFlags flags,
4193 GCancellable *cancellable,
4194 GError **error)
4196 return g_file_set_attribute (file, attribute,
4197 G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
4198 flags, cancellable, error);
4202 * g_file_set_attribute_int64:
4203 * @file: input #GFile.
4204 * @attribute: a string containing the attribute's name.
4205 * @value: a #guint64 containing the attribute's new value.
4206 * @flags: a #GFileQueryInfoFlags.
4207 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
4208 * @error: a #GError, or %NULL
4210 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
4211 * If @attribute is of a different type, this operation will fail.
4213 * If @cancellable is not %NULL, then the operation can be cancelled by
4214 * triggering the cancellable object from another thread. If the operation
4215 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4217 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4219 gboolean
4220 g_file_set_attribute_int64 (GFile *file,
4221 const char *attribute,
4222 gint64 value,
4223 GFileQueryInfoFlags flags,
4224 GCancellable *cancellable,
4225 GError **error)
4227 return g_file_set_attribute (file, attribute,
4228 G_FILE_ATTRIBUTE_TYPE_INT64, &value,
4229 flags, cancellable, error);
4233 * g_file_mount_mountable:
4234 * @file: input #GFile.
4235 * @flags: flags affecting the operation
4236 * @mount_operation: a #GMountOperation, or %NULL to avoid user interaction.
4237 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
4238 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4239 * @user_data: (closure): the data to pass to callback function
4241 * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
4242 * Using @mount_operation, you can request callbacks when, for instance,
4243 * passwords are needed during authentication.
4245 * If @cancellable is not %NULL, then the operation can be cancelled by
4246 * triggering the cancellable object from another thread. If the operation
4247 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4249 * When the operation is finished, @callback will be called. You can then call
4250 * g_file_mount_mountable_finish() to get the result of the operation.
4252 void
4253 g_file_mount_mountable (GFile *file,
4254 GMountMountFlags flags,
4255 GMountOperation *mount_operation,
4256 GCancellable *cancellable,
4257 GAsyncReadyCallback callback,
4258 gpointer user_data)
4260 GFileIface *iface;
4262 g_return_if_fail (G_IS_FILE (file));
4264 iface = G_FILE_GET_IFACE (file);
4266 if (iface->mount_mountable == NULL)
4268 g_simple_async_report_error_in_idle (G_OBJECT (file),
4269 callback,
4270 user_data,
4271 G_IO_ERROR,
4272 G_IO_ERROR_NOT_SUPPORTED,
4273 _("Operation not supported"));
4274 return;
4277 (* iface->mount_mountable) (file,
4278 flags,
4279 mount_operation,
4280 cancellable,
4281 callback,
4282 user_data);
4286 * g_file_mount_mountable_finish:
4287 * @file: input #GFile.
4288 * @result: a #GAsyncResult.
4289 * @error: a #GError, or %NULL
4291 * Finishes a mount operation. See g_file_mount_mountable() for details.
4293 * Finish an asynchronous mount operation that was started
4294 * with g_file_mount_mountable().
4296 * Returns: (transfer full): a #GFile or %NULL on error.
4297 * Free the returned object with g_object_unref().
4299 GFile *
4300 g_file_mount_mountable_finish (GFile *file,
4301 GAsyncResult *result,
4302 GError **error)
4304 GFileIface *iface;
4306 g_return_val_if_fail (G_IS_FILE (file), NULL);
4307 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
4309 if (G_IS_SIMPLE_ASYNC_RESULT (result))
4311 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
4312 if (g_simple_async_result_propagate_error (simple, error))
4313 return NULL;
4316 iface = G_FILE_GET_IFACE (file);
4317 return (* iface->mount_mountable_finish) (file, result, error);
4321 * g_file_unmount_mountable:
4322 * @file: input #GFile.
4323 * @flags: flags affecting the operation
4324 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
4325 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4326 * @user_data: (closure): the data to pass to callback function
4328 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
4330 * If @cancellable is not %NULL, then the operation can be cancelled by
4331 * triggering the cancellable object from another thread. If the operation
4332 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4334 * When the operation is finished, @callback will be called. You can then call
4335 * g_file_unmount_mountable_finish() to get the result of the operation.
4337 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation() instead.
4339 void
4340 g_file_unmount_mountable (GFile *file,
4341 GMountUnmountFlags flags,
4342 GCancellable *cancellable,
4343 GAsyncReadyCallback callback,
4344 gpointer user_data)
4346 GFileIface *iface;
4348 g_return_if_fail (G_IS_FILE (file));
4350 iface = G_FILE_GET_IFACE (file);
4352 if (iface->unmount_mountable == NULL)
4354 g_simple_async_report_error_in_idle (G_OBJECT (file),
4355 callback,
4356 user_data,
4357 G_IO_ERROR,
4358 G_IO_ERROR_NOT_SUPPORTED,
4359 _("Operation not supported"));
4360 return;
4363 (* iface->unmount_mountable) (file,
4364 flags,
4365 cancellable,
4366 callback,
4367 user_data);
4371 * g_file_unmount_mountable_finish:
4372 * @file: input #GFile.
4373 * @result: a #GAsyncResult.
4374 * @error: a #GError, or %NULL
4376 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
4378 * Finish an asynchronous unmount operation that was started
4379 * with g_file_unmount_mountable().
4381 * Returns: %TRUE if the operation finished successfully. %FALSE
4382 * otherwise.
4384 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation_finish() instead.
4386 gboolean
4387 g_file_unmount_mountable_finish (GFile *file,
4388 GAsyncResult *result,
4389 GError **error)
4391 GFileIface *iface;
4393 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4394 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4396 if (G_IS_SIMPLE_ASYNC_RESULT (result))
4398 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
4399 if (g_simple_async_result_propagate_error (simple, error))
4400 return FALSE;
4403 iface = G_FILE_GET_IFACE (file);
4404 return (* iface->unmount_mountable_finish) (file, result, error);
4408 * g_file_unmount_mountable_with_operation:
4409 * @file: input #GFile.
4410 * @flags: flags affecting the operation
4411 * @mount_operation: a #GMountOperation, or %NULL to avoid user interaction.
4412 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
4413 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4414 * @user_data: (closure): the data to pass to callback function
4416 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
4418 * If @cancellable is not %NULL, then the operation can be cancelled by
4419 * triggering the cancellable object from another thread. If the operation
4420 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4422 * When the operation is finished, @callback will be called. You can then call
4423 * g_file_unmount_mountable_finish() to get the result of the operation.
4425 * Since: 2.22
4427 void
4428 g_file_unmount_mountable_with_operation (GFile *file,
4429 GMountUnmountFlags flags,
4430 GMountOperation *mount_operation,
4431 GCancellable *cancellable,
4432 GAsyncReadyCallback callback,
4433 gpointer user_data)
4435 GFileIface *iface;
4437 g_return_if_fail (G_IS_FILE (file));
4439 iface = G_FILE_GET_IFACE (file);
4441 if (iface->unmount_mountable == NULL && iface->unmount_mountable_with_operation == NULL)
4443 g_simple_async_report_error_in_idle (G_OBJECT (file),
4444 callback,
4445 user_data,
4446 G_IO_ERROR,
4447 G_IO_ERROR_NOT_SUPPORTED,
4448 _("Operation not supported"));
4449 return;
4452 if (iface->unmount_mountable_with_operation != NULL)
4453 (* iface->unmount_mountable_with_operation) (file,
4454 flags,
4455 mount_operation,
4456 cancellable,
4457 callback,
4458 user_data);
4459 else
4460 (* iface->unmount_mountable) (file,
4461 flags,
4462 cancellable,
4463 callback,
4464 user_data);
4468 * g_file_unmount_mountable_with_operation_finish:
4469 * @file: input #GFile.
4470 * @result: a #GAsyncResult.
4471 * @error: a #GError, or %NULL
4473 * Finishes an unmount operation, see g_file_unmount_mountable_with_operation() for details.
4475 * Finish an asynchronous unmount operation that was started
4476 * with g_file_unmount_mountable_with_operation().
4478 * Returns: %TRUE if the operation finished successfully. %FALSE
4479 * otherwise.
4481 * Since: 2.22
4483 gboolean
4484 g_file_unmount_mountable_with_operation_finish (GFile *file,
4485 GAsyncResult *result,
4486 GError **error)
4488 GFileIface *iface;
4490 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4491 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4493 if (G_IS_SIMPLE_ASYNC_RESULT (result))
4495 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
4496 if (g_simple_async_result_propagate_error (simple, error))
4497 return FALSE;
4500 iface = G_FILE_GET_IFACE (file);
4501 if (iface->unmount_mountable_with_operation_finish != NULL)
4502 return (* iface->unmount_mountable_with_operation_finish) (file, result, error);
4503 else
4504 return (* iface->unmount_mountable_finish) (file, result, error);
4508 * g_file_eject_mountable:
4509 * @file: input #GFile.
4510 * @flags: flags affecting the operation
4511 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
4512 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4513 * @user_data: (closure): the data to pass to callback function
4515 * Starts an asynchronous eject on a mountable.
4516 * When this operation has completed, @callback will be called with
4517 * @user_user data, and the operation can be finalized with
4518 * g_file_eject_mountable_finish().
4520 * If @cancellable is not %NULL, then the operation can be cancelled by
4521 * triggering the cancellable object from another thread. If the operation
4522 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4524 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation() instead.
4526 void
4527 g_file_eject_mountable (GFile *file,
4528 GMountUnmountFlags flags,
4529 GCancellable *cancellable,
4530 GAsyncReadyCallback callback,
4531 gpointer user_data)
4533 GFileIface *iface;
4535 g_return_if_fail (G_IS_FILE (file));
4537 iface = G_FILE_GET_IFACE (file);
4539 if (iface->eject_mountable == NULL)
4541 g_simple_async_report_error_in_idle (G_OBJECT (file),
4542 callback,
4543 user_data,
4544 G_IO_ERROR,
4545 G_IO_ERROR_NOT_SUPPORTED,
4546 _("Operation not supported"));
4547 return;
4550 (* iface->eject_mountable) (file,
4551 flags,
4552 cancellable,
4553 callback,
4554 user_data);
4558 * g_file_eject_mountable_finish:
4559 * @file: input #GFile.
4560 * @result: a #GAsyncResult.
4561 * @error: a #GError, or %NULL
4563 * Finishes an asynchronous eject operation started by
4564 * g_file_eject_mountable().
4566 * Returns: %TRUE if the @file was ejected successfully. %FALSE
4567 * otherwise.
4569 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation_finish() instead.
4571 gboolean
4572 g_file_eject_mountable_finish (GFile *file,
4573 GAsyncResult *result,
4574 GError **error)
4576 GFileIface *iface;
4578 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4579 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4581 if (G_IS_SIMPLE_ASYNC_RESULT (result))
4583 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
4584 if (g_simple_async_result_propagate_error (simple, error))
4585 return FALSE;
4588 iface = G_FILE_GET_IFACE (file);
4589 return (* iface->eject_mountable_finish) (file, result, error);
4593 * g_file_eject_mountable_with_operation:
4594 * @file: input #GFile.
4595 * @flags: flags affecting the operation
4596 * @mount_operation: a #GMountOperation, or %NULL to avoid user interaction.
4597 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
4598 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4599 * @user_data: (closure): the data to pass to callback function
4601 * Starts an asynchronous eject on a mountable.
4602 * When this operation has completed, @callback will be called with
4603 * @user_user data, and the operation can be finalized with
4604 * g_file_eject_mountable_with_operation_finish().
4606 * If @cancellable is not %NULL, then the operation can be cancelled by
4607 * triggering the cancellable object from another thread. If the operation
4608 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4610 * Since: 2.22
4612 void
4613 g_file_eject_mountable_with_operation (GFile *file,
4614 GMountUnmountFlags flags,
4615 GMountOperation *mount_operation,
4616 GCancellable *cancellable,
4617 GAsyncReadyCallback callback,
4618 gpointer user_data)
4620 GFileIface *iface;
4622 g_return_if_fail (G_IS_FILE (file));
4624 iface = G_FILE_GET_IFACE (file);
4626 if (iface->eject_mountable == NULL && iface->eject_mountable_with_operation == NULL)
4628 g_simple_async_report_error_in_idle (G_OBJECT (file),
4629 callback,
4630 user_data,
4631 G_IO_ERROR,
4632 G_IO_ERROR_NOT_SUPPORTED,
4633 _("Operation not supported"));
4634 return;
4637 if (iface->eject_mountable_with_operation != NULL)
4638 (* iface->eject_mountable_with_operation) (file,
4639 flags,
4640 mount_operation,
4641 cancellable,
4642 callback,
4643 user_data);
4644 else
4645 (* iface->eject_mountable) (file,
4646 flags,
4647 cancellable,
4648 callback,
4649 user_data);
4653 * g_file_eject_mountable_with_operation_finish:
4654 * @file: input #GFile.
4655 * @result: a #GAsyncResult.
4656 * @error: a #GError, or %NULL
4658 * Finishes an asynchronous eject operation started by
4659 * g_file_eject_mountable_with_operation().
4661 * Returns: %TRUE if the @file was ejected successfully. %FALSE
4662 * otherwise.
4664 * Since: 2.22
4666 gboolean
4667 g_file_eject_mountable_with_operation_finish (GFile *file,
4668 GAsyncResult *result,
4669 GError **error)
4671 GFileIface *iface;
4673 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4674 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4676 if (G_IS_SIMPLE_ASYNC_RESULT (result))
4678 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
4679 if (g_simple_async_result_propagate_error (simple, error))
4680 return FALSE;
4683 iface = G_FILE_GET_IFACE (file);
4684 if (iface->eject_mountable_with_operation_finish != NULL)
4685 return (* iface->eject_mountable_with_operation_finish) (file, result, error);
4686 else
4687 return (* iface->eject_mountable_finish) (file, result, error);
4691 * g_file_monitor_directory:
4692 * @file: input #GFile.
4693 * @flags: a set of #GFileMonitorFlags.
4694 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
4695 * @error: a #GError, or %NULL.
4697 * Obtains a directory monitor for the given file.
4698 * This may fail if directory monitoring is not supported.
4700 * If @cancellable is not %NULL, then the operation can be cancelled by
4701 * triggering the cancellable object from another thread. If the operation
4702 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4704 * Virtual: monitor_dir
4705 * Returns: (transfer full): a #GFileMonitor for the given @file, or %NULL on error.
4706 * Free the returned object with g_object_unref().
4708 GFileMonitor*
4709 g_file_monitor_directory (GFile *file,
4710 GFileMonitorFlags flags,
4711 GCancellable *cancellable,
4712 GError **error)
4714 GFileIface *iface;
4716 g_return_val_if_fail (G_IS_FILE (file), NULL);
4718 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4719 return NULL;
4721 iface = G_FILE_GET_IFACE (file);
4723 if (iface->monitor_dir == NULL)
4725 g_set_error_literal (error, G_IO_ERROR,
4726 G_IO_ERROR_NOT_SUPPORTED,
4727 _("Operation not supported"));
4728 return NULL;
4731 return (* iface->monitor_dir) (file, flags, cancellable, error);
4735 * g_file_monitor_file:
4736 * @file: input #GFile.
4737 * @flags: a set of #GFileMonitorFlags.
4738 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
4739 * @error: a #GError, or %NULL.
4741 * Obtains a file monitor for the given file. If no file notification
4742 * mechanism exists, then regular polling of the file is used.
4744 * If @cancellable is not %NULL, then the operation can be cancelled by
4745 * triggering the cancellable object from another thread. If the operation
4746 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4748 * Returns: (transfer full): a #GFileMonitor for the given @file, or %NULL on error.
4749 * Free the returned object with g_object_unref().
4751 GFileMonitor*
4752 g_file_monitor_file (GFile *file,
4753 GFileMonitorFlags flags,
4754 GCancellable *cancellable,
4755 GError **error)
4757 GFileIface *iface;
4758 GFileMonitor *monitor;
4760 g_return_val_if_fail (G_IS_FILE (file), NULL);
4762 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4763 return NULL;
4765 iface = G_FILE_GET_IFACE (file);
4767 monitor = NULL;
4769 if (iface->monitor_file)
4770 monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
4772 /* Fallback to polling */
4773 if (monitor == NULL)
4774 monitor = _g_poll_file_monitor_new (file);
4776 return monitor;
4780 * g_file_monitor:
4781 * @file: input #GFile
4782 * @flags: a set of #GFileMonitorFlags
4783 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
4784 * @error: a #GError, or %NULL
4786 * Obtains a file or directory monitor for the given file, depending
4787 * on the type of the file.
4789 * If @cancellable is not %NULL, then the operation can be cancelled by
4790 * triggering the cancellable object from another thread. If the operation
4791 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4793 * Returns: (transfer full): a #GFileMonitor for the given @file, or %NULL on error.
4794 * Free the returned object with g_object_unref().
4796 * Since: 2.18
4798 GFileMonitor*
4799 g_file_monitor (GFile *file,
4800 GFileMonitorFlags flags,
4801 GCancellable *cancellable,
4802 GError **error)
4804 if (g_file_query_file_type (file, 0, cancellable) == G_FILE_TYPE_DIRECTORY)
4805 return g_file_monitor_directory (file, flags, cancellable, error);
4806 else
4807 return g_file_monitor_file (file, flags, cancellable, error);
4810 /********************************************
4811 * Default implementation of async ops *
4812 ********************************************/
4814 typedef struct {
4815 char *attributes;
4816 GFileQueryInfoFlags flags;
4817 GFileInfo *info;
4818 } QueryInfoAsyncData;
4820 static void
4821 query_info_data_free (QueryInfoAsyncData *data)
4823 if (data->info)
4824 g_object_unref (data->info);
4825 g_free (data->attributes);
4826 g_free (data);
4829 static void
4830 query_info_async_thread (GSimpleAsyncResult *res,
4831 GObject *object,
4832 GCancellable *cancellable)
4834 GError *error = NULL;
4835 QueryInfoAsyncData *data;
4836 GFileInfo *info;
4838 data = g_simple_async_result_get_op_res_gpointer (res);
4840 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
4842 if (info == NULL)
4843 g_simple_async_result_take_error (res, error);
4844 else
4845 data->info = info;
4848 static void
4849 g_file_real_query_info_async (GFile *file,
4850 const char *attributes,
4851 GFileQueryInfoFlags flags,
4852 int io_priority,
4853 GCancellable *cancellable,
4854 GAsyncReadyCallback callback,
4855 gpointer user_data)
4857 GSimpleAsyncResult *res;
4858 QueryInfoAsyncData *data;
4860 data = g_new0 (QueryInfoAsyncData, 1);
4861 data->attributes = g_strdup (attributes);
4862 data->flags = flags;
4864 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_info_async);
4865 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
4867 g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
4868 g_object_unref (res);
4871 static GFileInfo *
4872 g_file_real_query_info_finish (GFile *file,
4873 GAsyncResult *res,
4874 GError **error)
4876 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4877 QueryInfoAsyncData *data;
4879 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_info_async);
4881 data = g_simple_async_result_get_op_res_gpointer (simple);
4882 if (data->info)
4883 return g_object_ref (data->info);
4885 return NULL;
4888 typedef struct {
4889 char *attributes;
4890 GFileInfo *info;
4891 } QueryFilesystemInfoAsyncData;
4893 static void
4894 query_filesystem_info_data_free (QueryFilesystemInfoAsyncData *data)
4896 if (data->info)
4897 g_object_unref (data->info);
4898 g_free (data->attributes);
4899 g_free (data);
4902 static void
4903 query_filesystem_info_async_thread (GSimpleAsyncResult *res,
4904 GObject *object,
4905 GCancellable *cancellable)
4907 GError *error = NULL;
4908 QueryFilesystemInfoAsyncData *data;
4909 GFileInfo *info;
4911 data = g_simple_async_result_get_op_res_gpointer (res);
4913 info = g_file_query_filesystem_info (G_FILE (object), data->attributes, cancellable, &error);
4915 if (info == NULL)
4916 g_simple_async_result_take_error (res, error);
4917 else
4918 data->info = info;
4921 static void
4922 g_file_real_query_filesystem_info_async (GFile *file,
4923 const char *attributes,
4924 int io_priority,
4925 GCancellable *cancellable,
4926 GAsyncReadyCallback callback,
4927 gpointer user_data)
4929 GSimpleAsyncResult *res;
4930 QueryFilesystemInfoAsyncData *data;
4932 data = g_new0 (QueryFilesystemInfoAsyncData, 1);
4933 data->attributes = g_strdup (attributes);
4935 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_filesystem_info_async);
4936 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_filesystem_info_data_free);
4938 g_simple_async_result_run_in_thread (res, query_filesystem_info_async_thread, io_priority, cancellable);
4939 g_object_unref (res);
4942 static GFileInfo *
4943 g_file_real_query_filesystem_info_finish (GFile *file,
4944 GAsyncResult *res,
4945 GError **error)
4947 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4948 QueryFilesystemInfoAsyncData *data;
4950 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_filesystem_info_async);
4952 data = g_simple_async_result_get_op_res_gpointer (simple);
4953 if (data->info)
4954 return g_object_ref (data->info);
4956 return NULL;
4959 typedef struct {
4960 char *attributes;
4961 GFileQueryInfoFlags flags;
4962 GFileEnumerator *enumerator;
4963 } EnumerateChildrenAsyncData;
4965 static void
4966 enumerate_children_data_free (EnumerateChildrenAsyncData *data)
4968 if (data->enumerator)
4969 g_object_unref (data->enumerator);
4970 g_free (data->attributes);
4971 g_free (data);
4974 static void
4975 enumerate_children_async_thread (GSimpleAsyncResult *res,
4976 GObject *object,
4977 GCancellable *cancellable)
4979 GError *error = NULL;
4980 EnumerateChildrenAsyncData *data;
4981 GFileEnumerator *enumerator;
4983 data = g_simple_async_result_get_op_res_gpointer (res);
4985 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
4987 if (enumerator == NULL)
4988 g_simple_async_result_take_error (res, error);
4989 else
4990 data->enumerator = enumerator;
4993 static void
4994 g_file_real_enumerate_children_async (GFile *file,
4995 const char *attributes,
4996 GFileQueryInfoFlags flags,
4997 int io_priority,
4998 GCancellable *cancellable,
4999 GAsyncReadyCallback callback,
5000 gpointer user_data)
5002 GSimpleAsyncResult *res;
5003 EnumerateChildrenAsyncData *data;
5005 data = g_new0 (EnumerateChildrenAsyncData, 1);
5006 data->attributes = g_strdup (attributes);
5007 data->flags = flags;
5009 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_enumerate_children_async);
5010 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)enumerate_children_data_free);
5012 g_simple_async_result_run_in_thread (res, enumerate_children_async_thread, io_priority, cancellable);
5013 g_object_unref (res);
5016 static GFileEnumerator *
5017 g_file_real_enumerate_children_finish (GFile *file,
5018 GAsyncResult *res,
5019 GError **error)
5021 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5022 EnumerateChildrenAsyncData *data;
5024 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_enumerate_children_async);
5026 data = g_simple_async_result_get_op_res_gpointer (simple);
5027 if (data->enumerator)
5028 return g_object_ref (data->enumerator);
5030 return NULL;
5033 static void
5034 open_read_async_thread (GSimpleAsyncResult *res,
5035 GObject *object,
5036 GCancellable *cancellable)
5038 GFileIface *iface;
5039 GFileInputStream *stream;
5040 GError *error = NULL;
5042 iface = G_FILE_GET_IFACE (object);
5044 if (iface->read_fn == NULL)
5046 g_set_error_literal (&error, G_IO_ERROR,
5047 G_IO_ERROR_NOT_SUPPORTED,
5048 _("Operation not supported"));
5050 g_simple_async_result_take_error (res, error);
5052 return;
5055 stream = iface->read_fn (G_FILE (object), cancellable, &error);
5057 if (stream == NULL)
5058 g_simple_async_result_take_error (res, error);
5059 else
5060 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
5063 static void
5064 g_file_real_read_async (GFile *file,
5065 int io_priority,
5066 GCancellable *cancellable,
5067 GAsyncReadyCallback callback,
5068 gpointer user_data)
5070 GSimpleAsyncResult *res;
5072 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_read_async);
5074 g_simple_async_result_run_in_thread (res, open_read_async_thread, io_priority, cancellable);
5075 g_object_unref (res);
5078 static GFileInputStream *
5079 g_file_real_read_finish (GFile *file,
5080 GAsyncResult *res,
5081 GError **error)
5083 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5084 gpointer op;
5086 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_read_async);
5088 op = g_simple_async_result_get_op_res_gpointer (simple);
5089 if (op)
5090 return g_object_ref (op);
5092 return NULL;
5095 static void
5096 append_to_async_thread (GSimpleAsyncResult *res,
5097 GObject *object,
5098 GCancellable *cancellable)
5100 GFileIface *iface;
5101 GFileCreateFlags *data;
5102 GFileOutputStream *stream;
5103 GError *error = NULL;
5105 iface = G_FILE_GET_IFACE (object);
5107 data = g_simple_async_result_get_op_res_gpointer (res);
5109 stream = iface->append_to (G_FILE (object), *data, cancellable, &error);
5111 if (stream == NULL)
5112 g_simple_async_result_take_error (res, error);
5113 else
5114 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
5117 static void
5118 g_file_real_append_to_async (GFile *file,
5119 GFileCreateFlags flags,
5120 int io_priority,
5121 GCancellable *cancellable,
5122 GAsyncReadyCallback callback,
5123 gpointer user_data)
5125 GFileCreateFlags *data;
5126 GSimpleAsyncResult *res;
5128 data = g_new0 (GFileCreateFlags, 1);
5129 *data = flags;
5131 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_append_to_async);
5132 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
5134 g_simple_async_result_run_in_thread (res, append_to_async_thread, io_priority, cancellable);
5135 g_object_unref (res);
5138 static GFileOutputStream *
5139 g_file_real_append_to_finish (GFile *file,
5140 GAsyncResult *res,
5141 GError **error)
5143 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5144 gpointer op;
5146 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_append_to_async);
5148 op = g_simple_async_result_get_op_res_gpointer (simple);
5149 if (op)
5150 return g_object_ref (op);
5152 return NULL;
5155 static void
5156 create_async_thread (GSimpleAsyncResult *res,
5157 GObject *object,
5158 GCancellable *cancellable)
5160 GFileIface *iface;
5161 GFileCreateFlags *data;
5162 GFileOutputStream *stream;
5163 GError *error = NULL;
5165 iface = G_FILE_GET_IFACE (object);
5167 data = g_simple_async_result_get_op_res_gpointer (res);
5169 stream = iface->create (G_FILE (object), *data, cancellable, &error);
5171 if (stream == NULL)
5172 g_simple_async_result_take_error (res, error);
5173 else
5174 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
5177 static void
5178 g_file_real_create_async (GFile *file,
5179 GFileCreateFlags flags,
5180 int io_priority,
5181 GCancellable *cancellable,
5182 GAsyncReadyCallback callback,
5183 gpointer user_data)
5185 GFileCreateFlags *data;
5186 GSimpleAsyncResult *res;
5188 data = g_new0 (GFileCreateFlags, 1);
5189 *data = flags;
5191 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_create_async);
5192 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
5194 g_simple_async_result_run_in_thread (res, create_async_thread, io_priority, cancellable);
5195 g_object_unref (res);
5198 static GFileOutputStream *
5199 g_file_real_create_finish (GFile *file,
5200 GAsyncResult *res,
5201 GError **error)
5203 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5204 gpointer op;
5206 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_create_async);
5208 op = g_simple_async_result_get_op_res_gpointer (simple);
5209 if (op)
5210 return g_object_ref (op);
5212 return NULL;
5215 typedef struct {
5216 GFileOutputStream *stream;
5217 char *etag;
5218 gboolean make_backup;
5219 GFileCreateFlags flags;
5220 } ReplaceAsyncData;
5222 static void
5223 replace_async_data_free (ReplaceAsyncData *data)
5225 if (data->stream)
5226 g_object_unref (data->stream);
5227 g_free (data->etag);
5228 g_free (data);
5231 static void
5232 replace_async_thread (GSimpleAsyncResult *res,
5233 GObject *object,
5234 GCancellable *cancellable)
5236 GFileIface *iface;
5237 GFileOutputStream *stream;
5238 GError *error = NULL;
5239 ReplaceAsyncData *data;
5241 iface = G_FILE_GET_IFACE (object);
5243 data = g_simple_async_result_get_op_res_gpointer (res);
5245 stream = iface->replace (G_FILE (object),
5246 data->etag,
5247 data->make_backup,
5248 data->flags,
5249 cancellable,
5250 &error);
5252 if (stream == NULL)
5253 g_simple_async_result_take_error (res, error);
5254 else
5255 data->stream = stream;
5258 static void
5259 g_file_real_replace_async (GFile *file,
5260 const char *etag,
5261 gboolean make_backup,
5262 GFileCreateFlags flags,
5263 int io_priority,
5264 GCancellable *cancellable,
5265 GAsyncReadyCallback callback,
5266 gpointer user_data)
5268 GSimpleAsyncResult *res;
5269 ReplaceAsyncData *data;
5271 data = g_new0 (ReplaceAsyncData, 1);
5272 data->etag = g_strdup (etag);
5273 data->make_backup = make_backup;
5274 data->flags = flags;
5276 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_replace_async);
5277 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_async_data_free);
5279 g_simple_async_result_run_in_thread (res, replace_async_thread, io_priority, cancellable);
5280 g_object_unref (res);
5283 static GFileOutputStream *
5284 g_file_real_replace_finish (GFile *file,
5285 GAsyncResult *res,
5286 GError **error)
5288 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5289 ReplaceAsyncData *data;
5291 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_replace_async);
5293 data = g_simple_async_result_get_op_res_gpointer (simple);
5294 if (data->stream)
5295 return g_object_ref (data->stream);
5297 return NULL;
5300 static void
5301 open_readwrite_async_thread (GSimpleAsyncResult *res,
5302 GObject *object,
5303 GCancellable *cancellable)
5305 GFileIface *iface;
5306 GFileIOStream *stream;
5307 GError *error = NULL;
5309 iface = G_FILE_GET_IFACE (object);
5311 if (iface->open_readwrite == NULL)
5313 g_set_error_literal (&error, G_IO_ERROR,
5314 G_IO_ERROR_NOT_SUPPORTED,
5315 _("Operation not supported"));
5317 g_simple_async_result_take_error (res, error);
5319 return;
5322 stream = iface->open_readwrite (G_FILE (object), cancellable, &error);
5324 if (stream == NULL)
5325 g_simple_async_result_take_error (res, error);
5326 else
5327 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
5330 static void
5331 g_file_real_open_readwrite_async (GFile *file,
5332 int io_priority,
5333 GCancellable *cancellable,
5334 GAsyncReadyCallback callback,
5335 gpointer user_data)
5337 GSimpleAsyncResult *res;
5339 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_open_readwrite_async);
5341 g_simple_async_result_run_in_thread (res, open_readwrite_async_thread, io_priority, cancellable);
5342 g_object_unref (res);
5345 static GFileIOStream *
5346 g_file_real_open_readwrite_finish (GFile *file,
5347 GAsyncResult *res,
5348 GError **error)
5350 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5351 gpointer op;
5353 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_open_readwrite_async);
5355 op = g_simple_async_result_get_op_res_gpointer (simple);
5356 if (op)
5357 return g_object_ref (op);
5359 return NULL;
5362 static void
5363 create_readwrite_async_thread (GSimpleAsyncResult *res,
5364 GObject *object,
5365 GCancellable *cancellable)
5367 GFileIface *iface;
5368 GFileCreateFlags *data;
5369 GFileIOStream *stream;
5370 GError *error = NULL;
5372 iface = G_FILE_GET_IFACE (object);
5374 data = g_simple_async_result_get_op_res_gpointer (res);
5376 if (iface->create_readwrite == NULL)
5378 g_set_error_literal (&error, G_IO_ERROR,
5379 G_IO_ERROR_NOT_SUPPORTED,
5380 _("Operation not supported"));
5382 g_simple_async_result_take_error (res, error);
5384 return;
5387 stream = iface->create_readwrite (G_FILE (object), *data, cancellable, &error);
5389 if (stream == NULL)
5390 g_simple_async_result_take_error (res, error);
5391 else
5392 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
5395 static void
5396 g_file_real_create_readwrite_async (GFile *file,
5397 GFileCreateFlags flags,
5398 int io_priority,
5399 GCancellable *cancellable,
5400 GAsyncReadyCallback callback,
5401 gpointer user_data)
5403 GFileCreateFlags *data;
5404 GSimpleAsyncResult *res;
5406 data = g_new0 (GFileCreateFlags, 1);
5407 *data = flags;
5409 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_create_readwrite_async);
5410 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
5412 g_simple_async_result_run_in_thread (res, create_readwrite_async_thread, io_priority, cancellable);
5413 g_object_unref (res);
5416 static GFileIOStream *
5417 g_file_real_create_readwrite_finish (GFile *file,
5418 GAsyncResult *res,
5419 GError **error)
5421 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5422 gpointer op;
5424 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_create_readwrite_async);
5426 op = g_simple_async_result_get_op_res_gpointer (simple);
5427 if (op)
5428 return g_object_ref (op);
5430 return NULL;
5433 typedef struct {
5434 GFileIOStream *stream;
5435 char *etag;
5436 gboolean make_backup;
5437 GFileCreateFlags flags;
5438 } ReplaceRWAsyncData;
5440 static void
5441 replace_rw_async_data_free (ReplaceRWAsyncData *data)
5443 if (data->stream)
5444 g_object_unref (data->stream);
5445 g_free (data->etag);
5446 g_free (data);
5449 static void
5450 replace_readwrite_async_thread (GSimpleAsyncResult *res,
5451 GObject *object,
5452 GCancellable *cancellable)
5454 GFileIface *iface;
5455 GFileIOStream *stream;
5456 GError *error = NULL;
5457 ReplaceRWAsyncData *data;
5459 iface = G_FILE_GET_IFACE (object);
5461 data = g_simple_async_result_get_op_res_gpointer (res);
5463 stream = iface->replace_readwrite (G_FILE (object),
5464 data->etag,
5465 data->make_backup,
5466 data->flags,
5467 cancellable,
5468 &error);
5470 if (stream == NULL)
5471 g_simple_async_result_take_error (res, error);
5472 else
5473 data->stream = stream;
5476 static void
5477 g_file_real_replace_readwrite_async (GFile *file,
5478 const char *etag,
5479 gboolean make_backup,
5480 GFileCreateFlags flags,
5481 int io_priority,
5482 GCancellable *cancellable,
5483 GAsyncReadyCallback callback,
5484 gpointer user_data)
5486 GSimpleAsyncResult *res;
5487 ReplaceRWAsyncData *data;
5489 data = g_new0 (ReplaceRWAsyncData, 1);
5490 data->etag = g_strdup (etag);
5491 data->make_backup = make_backup;
5492 data->flags = flags;
5494 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_replace_readwrite_async);
5495 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_rw_async_data_free);
5497 g_simple_async_result_run_in_thread (res, replace_readwrite_async_thread, io_priority, cancellable);
5498 g_object_unref (res);
5501 static GFileIOStream *
5502 g_file_real_replace_readwrite_finish (GFile *file,
5503 GAsyncResult *res,
5504 GError **error)
5506 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5507 ReplaceRWAsyncData *data;
5509 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_replace_readwrite_async);
5511 data = g_simple_async_result_get_op_res_gpointer (simple);
5512 if (data->stream)
5513 return g_object_ref (data->stream);
5515 return NULL;
5518 typedef struct {
5519 char *name;
5520 GFile *file;
5521 } SetDisplayNameAsyncData;
5523 static void
5524 set_display_name_data_free (SetDisplayNameAsyncData *data)
5526 g_free (data->name);
5527 if (data->file)
5528 g_object_unref (data->file);
5529 g_free (data);
5532 static void
5533 set_display_name_async_thread (GSimpleAsyncResult *res,
5534 GObject *object,
5535 GCancellable *cancellable)
5537 GError *error = NULL;
5538 SetDisplayNameAsyncData *data;
5539 GFile *file;
5541 data = g_simple_async_result_get_op_res_gpointer (res);
5543 file = g_file_set_display_name (G_FILE (object), data->name, cancellable, &error);
5545 if (file == NULL)
5546 g_simple_async_result_take_error (res, error);
5547 else
5548 data->file = file;
5551 static void
5552 g_file_real_set_display_name_async (GFile *file,
5553 const char *display_name,
5554 int io_priority,
5555 GCancellable *cancellable,
5556 GAsyncReadyCallback callback,
5557 gpointer user_data)
5559 GSimpleAsyncResult *res;
5560 SetDisplayNameAsyncData *data;
5562 data = g_new0 (SetDisplayNameAsyncData, 1);
5563 data->name = g_strdup (display_name);
5565 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_display_name_async);
5566 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_display_name_data_free);
5568 g_simple_async_result_run_in_thread (res, set_display_name_async_thread, io_priority, cancellable);
5569 g_object_unref (res);
5572 static GFile *
5573 g_file_real_set_display_name_finish (GFile *file,
5574 GAsyncResult *res,
5575 GError **error)
5577 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5578 SetDisplayNameAsyncData *data;
5580 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_display_name_async);
5582 data = g_simple_async_result_get_op_res_gpointer (simple);
5583 if (data->file)
5584 return g_object_ref (data->file);
5586 return NULL;
5589 typedef struct {
5590 GFileQueryInfoFlags flags;
5591 GFileInfo *info;
5592 gboolean res;
5593 GError *error;
5594 } SetInfoAsyncData;
5596 static void
5597 set_info_data_free (SetInfoAsyncData *data)
5599 if (data->info)
5600 g_object_unref (data->info);
5601 if (data->error)
5602 g_error_free (data->error);
5603 g_free (data);
5606 static void
5607 set_info_async_thread (GSimpleAsyncResult *res,
5608 GObject *object,
5609 GCancellable *cancellable)
5611 SetInfoAsyncData *data;
5613 data = g_simple_async_result_get_op_res_gpointer (res);
5615 data->error = NULL;
5616 data->res = g_file_set_attributes_from_info (G_FILE (object),
5617 data->info,
5618 data->flags,
5619 cancellable,
5620 &data->error);
5623 static void
5624 g_file_real_set_attributes_async (GFile *file,
5625 GFileInfo *info,
5626 GFileQueryInfoFlags flags,
5627 int io_priority,
5628 GCancellable *cancellable,
5629 GAsyncReadyCallback callback,
5630 gpointer user_data)
5632 GSimpleAsyncResult *res;
5633 SetInfoAsyncData *data;
5635 data = g_new0 (SetInfoAsyncData, 1);
5636 data->info = g_file_info_dup (info);
5637 data->flags = flags;
5639 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_attributes_async);
5640 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_info_data_free);
5642 g_simple_async_result_run_in_thread (res, set_info_async_thread, io_priority, cancellable);
5643 g_object_unref (res);
5646 static gboolean
5647 g_file_real_set_attributes_finish (GFile *file,
5648 GAsyncResult *res,
5649 GFileInfo **info,
5650 GError **error)
5652 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5653 SetInfoAsyncData *data;
5655 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_attributes_async);
5657 data = g_simple_async_result_get_op_res_gpointer (simple);
5659 if (info)
5660 *info = g_object_ref (data->info);
5662 if (error != NULL && data->error)
5663 *error = g_error_copy (data->error);
5665 return data->res;
5668 static void
5669 find_enclosing_mount_async_thread (GSimpleAsyncResult *res,
5670 GObject *object,
5671 GCancellable *cancellable)
5673 GError *error = NULL;
5674 GMount *mount;
5676 mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
5678 if (mount == NULL)
5679 g_simple_async_result_take_error (res, error);
5680 else
5681 g_simple_async_result_set_op_res_gpointer (res, mount, (GDestroyNotify)g_object_unref);
5684 static void
5685 g_file_real_find_enclosing_mount_async (GFile *file,
5686 int io_priority,
5687 GCancellable *cancellable,
5688 GAsyncReadyCallback callback,
5689 gpointer user_data)
5691 GSimpleAsyncResult *res;
5693 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_find_enclosing_mount_async);
5695 g_simple_async_result_run_in_thread (res, find_enclosing_mount_async_thread, io_priority, cancellable);
5696 g_object_unref (res);
5699 static GMount *
5700 g_file_real_find_enclosing_mount_finish (GFile *file,
5701 GAsyncResult *res,
5702 GError **error)
5704 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5705 GMount *mount;
5707 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_find_enclosing_mount_async);
5709 mount = g_simple_async_result_get_op_res_gpointer (simple);
5710 return g_object_ref (mount);
5714 typedef struct {
5715 GFile *source;
5716 GFile *destination;
5717 GFileCopyFlags flags;
5718 GFileProgressCallback progress_cb;
5719 gpointer progress_cb_data;
5720 GIOSchedulerJob *job;
5721 } CopyAsyncData;
5723 static void
5724 copy_async_data_free (CopyAsyncData *data)
5726 g_object_unref (data->source);
5727 g_object_unref (data->destination);
5728 g_free (data);
5731 typedef struct {
5732 CopyAsyncData *data;
5733 goffset current_num_bytes;
5734 goffset total_num_bytes;
5735 } ProgressData;
5737 static gboolean
5738 copy_async_progress_in_main (gpointer user_data)
5740 ProgressData *progress = user_data;
5741 CopyAsyncData *data = progress->data;
5743 data->progress_cb (progress->current_num_bytes,
5744 progress->total_num_bytes,
5745 data->progress_cb_data);
5747 return FALSE;
5750 static gboolean
5751 mainloop_barrier (gpointer user_data)
5753 /* Does nothing, but ensures all queued idles before
5754 this are run */
5755 return FALSE;
5759 static void
5760 copy_async_progress_callback (goffset current_num_bytes,
5761 goffset total_num_bytes,
5762 gpointer user_data)
5764 CopyAsyncData *data = user_data;
5765 ProgressData *progress;
5767 progress = g_new (ProgressData, 1);
5768 progress->data = data;
5769 progress->current_num_bytes = current_num_bytes;
5770 progress->total_num_bytes = total_num_bytes;
5772 g_io_scheduler_job_send_to_mainloop_async (data->job,
5773 copy_async_progress_in_main,
5774 progress,
5775 g_free);
5778 static gboolean
5779 copy_async_thread (GIOSchedulerJob *job,
5780 GCancellable *cancellable,
5781 gpointer user_data)
5783 GSimpleAsyncResult *res;
5784 CopyAsyncData *data;
5785 gboolean result;
5786 GError *error;
5788 res = user_data;
5789 data = g_simple_async_result_get_op_res_gpointer (res);
5791 error = NULL;
5792 data->job = job;
5793 result = g_file_copy (data->source,
5794 data->destination,
5795 data->flags,
5796 cancellable,
5797 (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
5798 data,
5799 &error);
5801 /* Ensure all progress callbacks are done running in main thread */
5802 if (data->progress_cb != NULL)
5803 g_io_scheduler_job_send_to_mainloop (job,
5804 mainloop_barrier,
5805 NULL, NULL);
5807 if (!result)
5808 g_simple_async_result_take_error (res, error);
5810 g_simple_async_result_complete_in_idle (res);
5812 return FALSE;
5815 static void
5816 g_file_real_copy_async (GFile *source,
5817 GFile *destination,
5818 GFileCopyFlags flags,
5819 int io_priority,
5820 GCancellable *cancellable,
5821 GFileProgressCallback progress_callback,
5822 gpointer progress_callback_data,
5823 GAsyncReadyCallback callback,
5824 gpointer user_data)
5826 GSimpleAsyncResult *res;
5827 CopyAsyncData *data;
5829 data = g_new0 (CopyAsyncData, 1);
5830 data->source = g_object_ref (source);
5831 data->destination = g_object_ref (destination);
5832 data->flags = flags;
5833 data->progress_cb = progress_callback;
5834 data->progress_cb_data = progress_callback_data;
5836 res = g_simple_async_result_new (G_OBJECT (source), callback, user_data, g_file_real_copy_async);
5837 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)copy_async_data_free);
5839 g_io_scheduler_push_job (copy_async_thread, res, g_object_unref, io_priority, cancellable);
5842 static gboolean
5843 g_file_real_copy_finish (GFile *file,
5844 GAsyncResult *res,
5845 GError **error)
5847 /* Error handled in g_file_copy_finish() */
5848 return TRUE;
5852 /********************************************
5853 * Default VFS operations *
5854 ********************************************/
5857 * g_file_new_for_path:
5858 * @path: a string containing a relative or absolute path. The string
5859 * must be encoded in the glib filename encoding.
5861 * Constructs a #GFile for a given path. This operation never
5862 * fails, but the returned object might not support any I/O
5863 * operation if @path is malformed.
5865 * Returns: (transfer full): a new #GFile for the given @path.
5866 * Free the returned object with g_object_unref().
5868 GFile *
5869 g_file_new_for_path (const char *path)
5871 g_return_val_if_fail (path != NULL, NULL);
5873 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
5877 * g_file_new_for_uri:
5878 * @uri: a UTF8 string containing a URI.
5880 * Constructs a #GFile for a given URI. This operation never
5881 * fails, but the returned object might not support any I/O
5882 * operation if @uri is malformed or if the uri type is
5883 * not supported.
5885 * Returns: (transfer full): a new #GFile for the given @uri.
5886 * Free the returned object with g_object_unref().
5887 **/
5888 GFile *
5889 g_file_new_for_uri (const char *uri)
5891 g_return_val_if_fail (uri != NULL, NULL);
5893 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
5897 * g_file_new_tmp:
5898 * @tmpl: (type filename) (allow-none): Template for the file
5899 * name, as in g_file_open_tmp(), or %NULL for a default template.
5900 * @iostream: (out): on return, a #GFileIOStream for the created file.
5901 * @error: a #GError, or %NULL
5903 * Opens a file in the preferred directory for temporary files (as
5904 * returned by g_get_tmp_dir()) and returns a #GFile and
5905 * #GFileIOStream pointing to it.
5907 * @template should be a string in the GLib file name encoding
5908 * containing a sequence of six 'X' characters, and containing no
5909 * directory components. If it is %NULL, a default template is used.
5911 * Unlike the other #GFile constructors, this will return %NULL if
5912 * a temporary file could not be created.
5914 * Returns: (transfer full): a new #GFile.
5915 * Free the returned object with g_object_unref().
5917 * Since: 2.32
5919 GFile *
5920 g_file_new_tmp (const char *tmpl,
5921 GFileIOStream **iostream,
5922 GError **error)
5924 gint fd;
5925 gchar *path;
5926 GFile *file;
5927 GFileOutputStream *output;
5929 g_return_val_if_fail (tmpl != NULL, NULL);
5930 g_return_val_if_fail (iostream != NULL, NULL);
5932 fd = g_file_open_tmp (tmpl, &path, error);
5933 if (fd == -1)
5934 return NULL;
5936 file = g_file_new_for_path (path);
5938 output = _g_local_file_output_stream_new (fd);
5939 *iostream = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
5941 g_object_unref (output);
5942 g_free (path);
5944 return file;
5948 * g_file_parse_name:
5949 * @parse_name: a file name or path to be parsed.
5951 * Constructs a #GFile with the given @parse_name (i.e. something given by g_file_get_parse_name()).
5952 * This operation never fails, but the returned object might not support any I/O
5953 * operation if the @parse_name cannot be parsed.
5955 * Returns: (transfer full): a new #GFile.
5957 GFile *
5958 g_file_parse_name (const char *parse_name)
5960 g_return_val_if_fail (parse_name != NULL, NULL);
5962 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
5965 static gboolean
5966 is_valid_scheme_character (char c)
5968 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
5971 /* Following RFC 2396, valid schemes are built like:
5972 * scheme = alpha *( alpha | digit | "+" | "-" | "." )
5974 static gboolean
5975 has_valid_scheme (const char *uri)
5977 const char *p;
5979 p = uri;
5981 if (!g_ascii_isalpha (*p))
5982 return FALSE;
5984 do {
5985 p++;
5986 } while (is_valid_scheme_character (*p));
5988 return *p == ':';
5992 * g_file_new_for_commandline_arg:
5993 * @arg: a command line string.
5995 * Creates a #GFile with the given argument from the command line. The value of
5996 * @arg can be either a URI, an absolute path or a relative path resolved
5997 * relative to the current working directory.
5998 * This operation never fails, but the returned object might not support any
5999 * I/O operation if @arg points to a malformed path.
6001 * Returns: (transfer full): a new #GFile.
6002 * Free the returned object with g_object_unref().
6004 GFile *
6005 g_file_new_for_commandline_arg (const char *arg)
6007 GFile *file;
6008 char *filename;
6009 char *current_dir;
6011 g_return_val_if_fail (arg != NULL, NULL);
6013 if (g_path_is_absolute (arg))
6014 return g_file_new_for_path (arg);
6016 if (has_valid_scheme (arg))
6017 return g_file_new_for_uri (arg);
6019 current_dir = g_get_current_dir ();
6020 filename = g_build_filename (current_dir, arg, NULL);
6021 g_free (current_dir);
6023 file = g_file_new_for_path (filename);
6024 g_free (filename);
6026 return file;
6030 * g_file_mount_enclosing_volume:
6031 * @location: input #GFile.
6032 * @flags: flags affecting the operation
6033 * @mount_operation: a #GMountOperation or %NULL to avoid user interaction.
6034 * @cancellable: optional #GCancellable object, %NULL to ignore.
6035 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
6036 * @user_data: the data to pass to callback function
6038 * Starts a @mount_operation, mounting the volume that contains the file @location.
6040 * When this operation has completed, @callback will be called with
6041 * @user_user data, and the operation can be finalized with
6042 * g_file_mount_enclosing_volume_finish().
6044 * If @cancellable is not %NULL, then the operation can be cancelled by
6045 * triggering the cancellable object from another thread. If the operation
6046 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6048 void
6049 g_file_mount_enclosing_volume (GFile *location,
6050 GMountMountFlags flags,
6051 GMountOperation *mount_operation,
6052 GCancellable *cancellable,
6053 GAsyncReadyCallback callback,
6054 gpointer user_data)
6056 GFileIface *iface;
6058 g_return_if_fail (G_IS_FILE (location));
6060 iface = G_FILE_GET_IFACE (location);
6062 if (iface->mount_enclosing_volume == NULL)
6064 g_simple_async_report_error_in_idle (G_OBJECT (location),
6065 callback, user_data,
6066 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
6067 _("volume doesn't implement mount"));
6069 return;
6072 (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
6077 * g_file_mount_enclosing_volume_finish:
6078 * @location: input #GFile.
6079 * @result: a #GAsyncResult.
6080 * @error: a #GError, or %NULL
6082 * Finishes a mount operation started by g_file_mount_enclosing_volume().
6084 * Returns: %TRUE if successful. If an error
6085 * has occurred, this function will return %FALSE and set @error
6086 * appropriately if present.
6088 gboolean
6089 g_file_mount_enclosing_volume_finish (GFile *location,
6090 GAsyncResult *result,
6091 GError **error)
6093 GFileIface *iface;
6095 g_return_val_if_fail (G_IS_FILE (location), FALSE);
6096 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
6098 if (G_IS_SIMPLE_ASYNC_RESULT (result))
6100 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
6101 if (g_simple_async_result_propagate_error (simple, error))
6102 return FALSE;
6105 iface = G_FILE_GET_IFACE (location);
6107 return (* iface->mount_enclosing_volume_finish) (location, result, error);
6110 /********************************************
6111 * Utility functions *
6112 ********************************************/
6115 * g_file_query_default_handler:
6116 * @file: a #GFile to open.
6117 * @cancellable: optional #GCancellable object, %NULL to ignore.
6118 * @error: a #GError, or %NULL
6120 * Returns the #GAppInfo that is registered as the default
6121 * application to handle the file specified by @file.
6123 * If @cancellable is not %NULL, then the operation can be cancelled by
6124 * triggering the cancellable object from another thread. If the operation
6125 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6127 * Returns: (transfer full): a #GAppInfo if the handle was found, %NULL if there were errors.
6128 * When you are done with it, release it with g_object_unref()
6130 GAppInfo *
6131 g_file_query_default_handler (GFile *file,
6132 GCancellable *cancellable,
6133 GError **error)
6135 char *uri_scheme;
6136 const char *content_type;
6137 GAppInfo *appinfo;
6138 GFileInfo *info;
6139 char *path;
6141 uri_scheme = g_file_get_uri_scheme (file);
6142 if (uri_scheme && uri_scheme[0] != '\0')
6144 appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
6145 g_free (uri_scheme);
6147 if (appinfo != NULL)
6148 return appinfo;
6151 info = g_file_query_info (file,
6152 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
6154 cancellable,
6155 error);
6156 if (info == NULL)
6157 return NULL;
6159 appinfo = NULL;
6161 content_type = g_file_info_get_content_type (info);
6162 if (content_type)
6164 /* Don't use is_native(), as we want to support fuse paths if available */
6165 path = g_file_get_path (file);
6166 appinfo = g_app_info_get_default_for_type (content_type,
6167 path == NULL);
6168 g_free (path);
6171 g_object_unref (info);
6173 if (appinfo != NULL)
6174 return appinfo;
6176 g_set_error_literal (error, G_IO_ERROR,
6177 G_IO_ERROR_NOT_SUPPORTED,
6178 _("No application is registered as handling this file"));
6179 return NULL;
6184 #define GET_CONTENT_BLOCK_SIZE 8192
6187 * g_file_load_contents:
6188 * @file: input #GFile.
6189 * @cancellable: optional #GCancellable object, %NULL to ignore.
6190 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file.
6191 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6192 * or %NULL if the length is not needed
6193 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6194 * or %NULL if the entity tag is not needed
6195 * @error: a #GError, or %NULL
6197 * Loads the content of the file into memory. The data is always
6198 * zero-terminated, but this is not included in the resultant @length.
6199 * The returned @content should be freed with g_free() when no longer
6200 * needed.
6202 * If @cancellable is not %NULL, then the operation can be cancelled by
6203 * triggering the cancellable object from another thread. If the operation
6204 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6206 * Returns: %TRUE if the @file's contents were successfully loaded.
6207 * %FALSE if there were errors.
6209 gboolean
6210 g_file_load_contents (GFile *file,
6211 GCancellable *cancellable,
6212 char **contents,
6213 gsize *length,
6214 char **etag_out,
6215 GError **error)
6217 GFileInputStream *in;
6218 GByteArray *content;
6219 gsize pos;
6220 gssize res;
6221 GFileInfo *info;
6223 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6224 g_return_val_if_fail (contents != NULL, FALSE);
6226 in = g_file_read (file, cancellable, error);
6227 if (in == NULL)
6228 return FALSE;
6230 content = g_byte_array_new ();
6231 pos = 0;
6233 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6234 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
6235 content->data + pos,
6236 GET_CONTENT_BLOCK_SIZE,
6237 cancellable, error)) > 0)
6239 pos += res;
6240 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6243 if (etag_out)
6245 *etag_out = NULL;
6247 info = g_file_input_stream_query_info (in,
6248 G_FILE_ATTRIBUTE_ETAG_VALUE,
6249 cancellable,
6250 NULL);
6251 if (info)
6253 *etag_out = g_strdup (g_file_info_get_etag (info));
6254 g_object_unref (info);
6258 /* Ignore errors on close */
6259 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
6260 g_object_unref (in);
6262 if (res < 0)
6264 /* error is set already */
6265 g_byte_array_free (content, TRUE);
6266 return FALSE;
6269 if (length)
6270 *length = pos;
6272 /* Zero terminate (we got an extra byte allocated for this */
6273 content->data[pos] = 0;
6275 *contents = (char *)g_byte_array_free (content, FALSE);
6277 return TRUE;
6280 typedef struct {
6281 GFile *file;
6282 GError *error;
6283 GCancellable *cancellable;
6284 GFileReadMoreCallback read_more_callback;
6285 GAsyncReadyCallback callback;
6286 gpointer user_data;
6287 GByteArray *content;
6288 gsize pos;
6289 char *etag;
6290 } LoadContentsData;
6293 static void
6294 load_contents_data_free (LoadContentsData *data)
6296 if (data->error)
6297 g_error_free (data->error);
6298 if (data->cancellable)
6299 g_object_unref (data->cancellable);
6300 if (data->content)
6301 g_byte_array_free (data->content, TRUE);
6302 g_free (data->etag);
6303 g_object_unref (data->file);
6304 g_free (data);
6307 static void
6308 load_contents_close_callback (GObject *obj,
6309 GAsyncResult *close_res,
6310 gpointer user_data)
6312 GInputStream *stream = G_INPUT_STREAM (obj);
6313 LoadContentsData *data = user_data;
6314 GSimpleAsyncResult *res;
6316 /* Ignore errors here, we're only reading anyway */
6317 g_input_stream_close_finish (stream, close_res, NULL);
6318 g_object_unref (stream);
6320 res = g_simple_async_result_new (G_OBJECT (data->file),
6321 data->callback,
6322 data->user_data,
6323 g_file_load_contents_async);
6324 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)load_contents_data_free);
6325 g_simple_async_result_complete (res);
6326 g_object_unref (res);
6329 static void
6330 load_contents_fstat_callback (GObject *obj,
6331 GAsyncResult *stat_res,
6332 gpointer user_data)
6334 GInputStream *stream = G_INPUT_STREAM (obj);
6335 LoadContentsData *data = user_data;
6336 GFileInfo *info;
6338 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
6339 stat_res, NULL);
6340 if (info)
6342 data->etag = g_strdup (g_file_info_get_etag (info));
6343 g_object_unref (info);
6346 g_input_stream_close_async (stream, 0,
6347 data->cancellable,
6348 load_contents_close_callback, data);
6351 static void
6352 load_contents_read_callback (GObject *obj,
6353 GAsyncResult *read_res,
6354 gpointer user_data)
6356 GInputStream *stream = G_INPUT_STREAM (obj);
6357 LoadContentsData *data = user_data;
6358 GError *error = NULL;
6359 gssize read_size;
6361 read_size = g_input_stream_read_finish (stream, read_res, &error);
6363 if (read_size < 0)
6365 /* Error or EOF, close the file */
6366 data->error = error;
6367 g_input_stream_close_async (stream, 0,
6368 data->cancellable,
6369 load_contents_close_callback, data);
6371 else if (read_size == 0)
6373 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
6374 G_FILE_ATTRIBUTE_ETAG_VALUE,
6376 data->cancellable,
6377 load_contents_fstat_callback,
6378 data);
6380 else if (read_size > 0)
6382 data->pos += read_size;
6384 g_byte_array_set_size (data->content,
6385 data->pos + GET_CONTENT_BLOCK_SIZE);
6388 if (data->read_more_callback &&
6389 !data->read_more_callback ((char *)data->content->data, data->pos, data->user_data))
6390 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
6391 G_FILE_ATTRIBUTE_ETAG_VALUE,
6393 data->cancellable,
6394 load_contents_fstat_callback,
6395 data);
6396 else
6397 g_input_stream_read_async (stream,
6398 data->content->data + data->pos,
6399 GET_CONTENT_BLOCK_SIZE,
6401 data->cancellable,
6402 load_contents_read_callback,
6403 data);
6407 static void
6408 load_contents_open_callback (GObject *obj,
6409 GAsyncResult *open_res,
6410 gpointer user_data)
6412 GFile *file = G_FILE (obj);
6413 GFileInputStream *stream;
6414 LoadContentsData *data = user_data;
6415 GError *error = NULL;
6416 GSimpleAsyncResult *res;
6418 stream = g_file_read_finish (file, open_res, &error);
6420 if (stream)
6422 g_byte_array_set_size (data->content,
6423 data->pos + GET_CONTENT_BLOCK_SIZE);
6424 g_input_stream_read_async (G_INPUT_STREAM (stream),
6425 data->content->data + data->pos,
6426 GET_CONTENT_BLOCK_SIZE,
6428 data->cancellable,
6429 load_contents_read_callback,
6430 data);
6433 else
6435 res = g_simple_async_result_new_take_error (G_OBJECT (data->file),
6436 data->callback,
6437 data->user_data,
6438 error);
6439 g_simple_async_result_complete (res);
6440 load_contents_data_free (data);
6441 g_object_unref (res);
6446 * g_file_load_partial_contents_async: (skip)
6447 * @file: input #GFile.
6448 * @cancellable: optional #GCancellable object, %NULL to ignore.
6449 * @read_more_callback: a #GFileReadMoreCallback to receive partial data and to specify whether further data should be read.
6450 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6451 * @user_data: the data to pass to the callback functions.
6453 * Reads the partial contents of a file. A #GFileReadMoreCallback should be
6454 * used to stop reading from the file when appropriate, else this function
6455 * will behave exactly as g_file_load_contents_async(). This operation
6456 * can be finished by g_file_load_partial_contents_finish().
6458 * Users of this function should be aware that @user_data is passed to
6459 * both the @read_more_callback and the @callback.
6461 * If @cancellable is not %NULL, then the operation can be cancelled by
6462 * triggering the cancellable object from another thread. If the operation
6463 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6465 void
6466 g_file_load_partial_contents_async (GFile *file,
6467 GCancellable *cancellable,
6468 GFileReadMoreCallback read_more_callback,
6469 GAsyncReadyCallback callback,
6470 gpointer user_data)
6472 LoadContentsData *data;
6474 g_return_if_fail (G_IS_FILE (file));
6476 data = g_new0 (LoadContentsData, 1);
6478 if (cancellable)
6479 data->cancellable = g_object_ref (cancellable);
6480 data->read_more_callback = read_more_callback;
6481 data->callback = callback;
6482 data->user_data = user_data;
6483 data->content = g_byte_array_new ();
6484 data->file = g_object_ref (file);
6486 g_file_read_async (file,
6488 cancellable,
6489 load_contents_open_callback,
6490 data);
6494 * g_file_load_partial_contents_finish:
6495 * @file: input #GFile.
6496 * @res: a #GAsyncResult.
6497 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file.
6498 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6499 * or %NULL if the length is not needed
6500 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6501 * or %NULL if the entity tag is not needed
6502 * @error: a #GError, or %NULL
6504 * Finishes an asynchronous partial load operation that was started
6505 * with g_file_load_partial_contents_async(). The data is always
6506 * zero-terminated, but this is not included in the resultant @length.
6507 * The returned @content should be freed with g_free() when no longer
6508 * needed.
6510 * Returns: %TRUE if the load was successful. If %FALSE and @error is
6511 * present, it will be set appropriately.
6513 gboolean
6514 g_file_load_partial_contents_finish (GFile *file,
6515 GAsyncResult *res,
6516 char **contents,
6517 gsize *length,
6518 char **etag_out,
6519 GError **error)
6521 GSimpleAsyncResult *simple;
6522 LoadContentsData *data;
6524 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6525 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
6526 g_return_val_if_fail (contents != NULL, FALSE);
6528 simple = G_SIMPLE_ASYNC_RESULT (res);
6530 if (g_simple_async_result_propagate_error (simple, error))
6531 return FALSE;
6533 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_load_contents_async);
6535 data = g_simple_async_result_get_op_res_gpointer (simple);
6537 if (data->error)
6539 g_propagate_error (error, data->error);
6540 data->error = NULL;
6541 *contents = NULL;
6542 if (length)
6543 *length = 0;
6544 return FALSE;
6547 if (length)
6548 *length = data->pos;
6550 if (etag_out)
6552 *etag_out = data->etag;
6553 data->etag = NULL;
6556 /* Zero terminate */
6557 g_byte_array_set_size (data->content, data->pos + 1);
6558 data->content->data[data->pos] = 0;
6560 *contents = (char *)g_byte_array_free (data->content, FALSE);
6561 data->content = NULL;
6563 return TRUE;
6567 * g_file_load_contents_async:
6568 * @file: input #GFile.
6569 * @cancellable: optional #GCancellable object, %NULL to ignore.
6570 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6571 * @user_data: the data to pass to callback function
6573 * Starts an asynchronous load of the @file's contents.
6575 * For more details, see g_file_load_contents() which is
6576 * the synchronous version of this call.
6578 * When the load operation has completed, @callback will be called
6579 * with @user data. To finish the operation, call
6580 * g_file_load_contents_finish() with the #GAsyncResult returned by
6581 * the @callback.
6583 * If @cancellable is not %NULL, then the operation can be cancelled by
6584 * triggering the cancellable object from another thread. If the operation
6585 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6587 void
6588 g_file_load_contents_async (GFile *file,
6589 GCancellable *cancellable,
6590 GAsyncReadyCallback callback,
6591 gpointer user_data)
6593 g_file_load_partial_contents_async (file,
6594 cancellable,
6595 NULL,
6596 callback, user_data);
6600 * g_file_load_contents_finish:
6601 * @file: input #GFile.
6602 * @res: a #GAsyncResult.
6603 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file.
6604 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6605 * or %NULL if the length is not needed
6606 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6607 * or %NULL if the entity tag is not needed
6608 * @error: a #GError, or %NULL
6610 * Finishes an asynchronous load of the @file's contents.
6611 * The contents are placed in @contents, and @length is set to the
6612 * size of the @contents string. The @content should be freed with
6613 * g_free() when no longer needed. If @etag_out is present, it will be
6614 * set to the new entity tag for the @file.
6616 * Returns: %TRUE if the load was successful. If %FALSE and @error is
6617 * present, it will be set appropriately.
6619 gboolean
6620 g_file_load_contents_finish (GFile *file,
6621 GAsyncResult *res,
6622 char **contents,
6623 gsize *length,
6624 char **etag_out,
6625 GError **error)
6627 return g_file_load_partial_contents_finish (file,
6628 res,
6629 contents,
6630 length,
6631 etag_out,
6632 error);
6636 * g_file_replace_contents:
6637 * @file: input #GFile.
6638 * @contents: (element-type guint8) (array length=length): a string containing the new contents for @file.
6639 * @length: the length of @contents in bytes.
6640 * @etag: (allow-none): the old <link linkend="gfile-etag">entity tag</link>
6641 * for the document, or %NULL
6642 * @make_backup: %TRUE if a backup should be created.
6643 * @flags: a set of #GFileCreateFlags.
6644 * @new_etag: (allow-none) (out): a location to a new <link linkend="gfile-etag">entity tag</link>
6645 * for the document. This should be freed with g_free() when no longer
6646 * needed, or %NULL
6647 * @cancellable: optional #GCancellable object, %NULL to ignore.
6648 * @error: a #GError, or %NULL
6650 * Replaces the contents of @file with @contents of @length bytes.
6652 * If @etag is specified (not %NULL) any existing file must have that etag, or
6653 * the error %G_IO_ERROR_WRONG_ETAG will be returned.
6655 * If @make_backup is %TRUE, this function will attempt to make a backup of @file.
6657 * If @cancellable is not %NULL, then the operation can be cancelled by
6658 * triggering the cancellable object from another thread. If the operation
6659 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6661 * The returned @new_etag can be used to verify that the file hasn't changed the
6662 * next time it is saved over.
6664 * Returns: %TRUE if successful. If an error
6665 * has occurred, this function will return %FALSE and set @error
6666 * appropriately if present.
6668 gboolean
6669 g_file_replace_contents (GFile *file,
6670 const char *contents,
6671 gsize length,
6672 const char *etag,
6673 gboolean make_backup,
6674 GFileCreateFlags flags,
6675 char **new_etag,
6676 GCancellable *cancellable,
6677 GError **error)
6679 GFileOutputStream *out;
6680 gsize pos, remainder;
6681 gssize res;
6682 gboolean ret;
6684 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6685 g_return_val_if_fail (contents != NULL, FALSE);
6687 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
6688 if (out == NULL)
6689 return FALSE;
6691 pos = 0;
6692 remainder = length;
6693 while (remainder > 0 &&
6694 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
6695 contents + pos,
6696 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
6697 cancellable,
6698 error)) > 0)
6700 pos += res;
6701 remainder -= res;
6704 if (remainder > 0 && res < 0)
6706 /* Ignore errors on close */
6707 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
6708 g_object_unref (out);
6710 /* error is set already */
6711 return FALSE;
6714 ret = g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error);
6716 if (new_etag)
6717 *new_etag = g_file_output_stream_get_etag (out);
6719 g_object_unref (out);
6721 return ret;
6724 typedef struct {
6725 GFile *file;
6726 GError *error;
6727 GCancellable *cancellable;
6728 GAsyncReadyCallback callback;
6729 gpointer user_data;
6730 const char *content;
6731 gsize length;
6732 gsize pos;
6733 char *etag;
6734 } ReplaceContentsData;
6736 static void
6737 replace_contents_data_free (ReplaceContentsData *data)
6739 if (data->error)
6740 g_error_free (data->error);
6741 if (data->cancellable)
6742 g_object_unref (data->cancellable);
6743 g_object_unref (data->file);
6744 g_free (data->etag);
6745 g_free (data);
6748 static void
6749 replace_contents_close_callback (GObject *obj,
6750 GAsyncResult *close_res,
6751 gpointer user_data)
6753 GOutputStream *stream = G_OUTPUT_STREAM (obj);
6754 ReplaceContentsData *data = user_data;
6755 GSimpleAsyncResult *res;
6757 /* Ignore errors here, we're only reading anyway */
6758 g_output_stream_close_finish (stream, close_res, NULL);
6759 g_object_unref (stream);
6761 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
6763 res = g_simple_async_result_new (G_OBJECT (data->file),
6764 data->callback,
6765 data->user_data,
6766 g_file_replace_contents_async);
6767 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_contents_data_free);
6768 g_simple_async_result_complete (res);
6769 g_object_unref (res);
6772 static void
6773 replace_contents_write_callback (GObject *obj,
6774 GAsyncResult *read_res,
6775 gpointer user_data)
6777 GOutputStream *stream = G_OUTPUT_STREAM (obj);
6778 ReplaceContentsData *data = user_data;
6779 GError *error = NULL;
6780 gssize write_size;
6782 write_size = g_output_stream_write_finish (stream, read_res, &error);
6784 if (write_size <= 0)
6786 /* Error or EOF, close the file */
6787 if (write_size < 0)
6788 data->error = error;
6789 g_output_stream_close_async (stream, 0,
6790 data->cancellable,
6791 replace_contents_close_callback, data);
6793 else if (write_size > 0)
6795 data->pos += write_size;
6797 if (data->pos >= data->length)
6798 g_output_stream_close_async (stream, 0,
6799 data->cancellable,
6800 replace_contents_close_callback, data);
6801 else
6802 g_output_stream_write_async (stream,
6803 data->content + data->pos,
6804 data->length - data->pos,
6806 data->cancellable,
6807 replace_contents_write_callback,
6808 data);
6812 static void
6813 replace_contents_open_callback (GObject *obj,
6814 GAsyncResult *open_res,
6815 gpointer user_data)
6817 GFile *file = G_FILE (obj);
6818 GFileOutputStream *stream;
6819 ReplaceContentsData *data = user_data;
6820 GError *error = NULL;
6821 GSimpleAsyncResult *res;
6823 stream = g_file_replace_finish (file, open_res, &error);
6825 if (stream)
6827 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
6828 data->content + data->pos,
6829 data->length - data->pos,
6831 data->cancellable,
6832 replace_contents_write_callback,
6833 data);
6836 else
6838 res = g_simple_async_result_new_take_error (G_OBJECT (data->file),
6839 data->callback,
6840 data->user_data,
6841 error);
6842 g_simple_async_result_complete (res);
6843 replace_contents_data_free (data);
6844 g_object_unref (res);
6849 * g_file_replace_contents_async:
6850 * @file: input #GFile.
6851 * @contents: (element-type guint8) (array length=length): string of contents to replace the file with.
6852 * @length: the length of @contents in bytes.
6853 * @etag: (allow-none): a new <link linkend="gfile-etag">entity tag</link> for the @file, or %NULL
6854 * @make_backup: %TRUE if a backup should be created.
6855 * @flags: a set of #GFileCreateFlags.
6856 * @cancellable: optional #GCancellable object, %NULL to ignore.
6857 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6858 * @user_data: the data to pass to callback function
6860 * Starts an asynchronous replacement of @file with the given
6861 * @contents of @length bytes. @etag will replace the document's
6862 * current entity tag.
6864 * When this operation has completed, @callback will be called with
6865 * @user_user data, and the operation can be finalized with
6866 * g_file_replace_contents_finish().
6868 * If @cancellable is not %NULL, then the operation can be cancelled by
6869 * triggering the cancellable object from another thread. If the operation
6870 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6872 * If @make_backup is %TRUE, this function will attempt to
6873 * make a backup of @file.
6875 void
6876 g_file_replace_contents_async (GFile *file,
6877 const char *contents,
6878 gsize length,
6879 const char *etag,
6880 gboolean make_backup,
6881 GFileCreateFlags flags,
6882 GCancellable *cancellable,
6883 GAsyncReadyCallback callback,
6884 gpointer user_data)
6886 ReplaceContentsData *data;
6888 g_return_if_fail (G_IS_FILE (file));
6889 g_return_if_fail (contents != NULL);
6891 data = g_new0 (ReplaceContentsData, 1);
6893 if (cancellable)
6894 data->cancellable = g_object_ref (cancellable);
6895 data->callback = callback;
6896 data->user_data = user_data;
6897 data->content = contents;
6898 data->length = length;
6899 data->pos = 0;
6900 data->file = g_object_ref (file);
6902 g_file_replace_async (file,
6903 etag,
6904 make_backup,
6905 flags,
6907 cancellable,
6908 replace_contents_open_callback,
6909 data);
6913 * g_file_replace_contents_finish:
6914 * @file: input #GFile.
6915 * @res: a #GAsyncResult.
6916 * @new_etag: (out) (allow-none): a location of a new <link linkend="gfile-etag">entity tag</link>
6917 * for the document. This should be freed with g_free() when it is no
6918 * longer needed, or %NULL
6919 * @error: a #GError, or %NULL
6921 * Finishes an asynchronous replace of the given @file. See
6922 * g_file_replace_contents_async(). Sets @new_etag to the new entity
6923 * tag for the document, if present.
6925 * Returns: %TRUE on success, %FALSE on failure.
6927 gboolean
6928 g_file_replace_contents_finish (GFile *file,
6929 GAsyncResult *res,
6930 char **new_etag,
6931 GError **error)
6933 GSimpleAsyncResult *simple;
6934 ReplaceContentsData *data;
6936 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6937 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
6939 simple = G_SIMPLE_ASYNC_RESULT (res);
6941 if (g_simple_async_result_propagate_error (simple, error))
6942 return FALSE;
6944 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_replace_contents_async);
6946 data = g_simple_async_result_get_op_res_gpointer (simple);
6948 if (data->error)
6950 g_propagate_error (error, data->error);
6951 data->error = NULL;
6952 return FALSE;
6956 if (new_etag)
6958 *new_etag = data->etag;
6959 data->etag = NULL; /* Take ownership */
6962 return TRUE;
6966 * g_file_start_mountable:
6967 * @file: input #GFile.
6968 * @flags: flags affecting the operation
6969 * @start_operation: a #GMountOperation, or %NULL to avoid user interaction.
6970 * @cancellable: optional #GCancellable object, %NULL to ignore.
6971 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
6972 * @user_data: the data to pass to callback function
6974 * Starts a file of type G_FILE_TYPE_MOUNTABLE.
6975 * Using @start_operation, you can request callbacks when, for instance,
6976 * passwords are needed during authentication.
6978 * If @cancellable is not %NULL, then the operation can be cancelled by
6979 * triggering the cancellable object from another thread. If the operation
6980 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6982 * When the operation is finished, @callback will be called. You can then call
6983 * g_file_mount_mountable_finish() to get the result of the operation.
6985 * Since: 2.22
6987 void
6988 g_file_start_mountable (GFile *file,
6989 GDriveStartFlags flags,
6990 GMountOperation *start_operation,
6991 GCancellable *cancellable,
6992 GAsyncReadyCallback callback,
6993 gpointer user_data)
6995 GFileIface *iface;
6997 g_return_if_fail (G_IS_FILE (file));
6999 iface = G_FILE_GET_IFACE (file);
7001 if (iface->start_mountable == NULL)
7003 g_simple_async_report_error_in_idle (G_OBJECT (file),
7004 callback,
7005 user_data,
7006 G_IO_ERROR,
7007 G_IO_ERROR_NOT_SUPPORTED,
7008 _("Operation not supported"));
7009 return;
7012 (* iface->start_mountable) (file,
7013 flags,
7014 start_operation,
7015 cancellable,
7016 callback,
7017 user_data);
7021 * g_file_start_mountable_finish:
7022 * @file: input #GFile.
7023 * @result: a #GAsyncResult.
7024 * @error: a #GError, or %NULL
7026 * Finishes a start operation. See g_file_start_mountable() for details.
7028 * Finish an asynchronous start operation that was started
7029 * with g_file_start_mountable().
7031 * Returns: %TRUE if the operation finished successfully. %FALSE
7032 * otherwise.
7034 * Since: 2.22
7036 gboolean
7037 g_file_start_mountable_finish (GFile *file,
7038 GAsyncResult *result,
7039 GError **error)
7041 GFileIface *iface;
7043 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7044 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7046 if (G_IS_SIMPLE_ASYNC_RESULT (result))
7048 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
7049 if (g_simple_async_result_propagate_error (simple, error))
7050 return FALSE;
7053 iface = G_FILE_GET_IFACE (file);
7054 return (* iface->start_mountable_finish) (file, result, error);
7058 * g_file_stop_mountable:
7059 * @file: input #GFile.
7060 * @flags: flags affecting the operation
7061 * @mount_operation: a #GMountOperation, or %NULL to avoid user interaction.
7062 * @cancellable: optional #GCancellable object, %NULL to ignore.
7063 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
7064 * @user_data: the data to pass to callback function
7066 * Stops a file of type G_FILE_TYPE_MOUNTABLE.
7068 * If @cancellable is not %NULL, then the operation can be cancelled by
7069 * triggering the cancellable object from another thread. If the operation
7070 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7072 * When the operation is finished, @callback will be called. You can then call
7073 * g_file_stop_mountable_finish() to get the result of the operation.
7075 * Since: 2.22
7077 void
7078 g_file_stop_mountable (GFile *file,
7079 GMountUnmountFlags flags,
7080 GMountOperation *mount_operation,
7081 GCancellable *cancellable,
7082 GAsyncReadyCallback callback,
7083 gpointer user_data)
7085 GFileIface *iface;
7087 g_return_if_fail (G_IS_FILE (file));
7089 iface = G_FILE_GET_IFACE (file);
7091 if (iface->stop_mountable == NULL)
7093 g_simple_async_report_error_in_idle (G_OBJECT (file),
7094 callback,
7095 user_data,
7096 G_IO_ERROR,
7097 G_IO_ERROR_NOT_SUPPORTED,
7098 _("Operation not supported"));
7099 return;
7102 (* iface->stop_mountable) (file,
7103 flags,
7104 mount_operation,
7105 cancellable,
7106 callback,
7107 user_data);
7111 * g_file_stop_mountable_finish:
7112 * @file: input #GFile.
7113 * @result: a #GAsyncResult.
7114 * @error: a #GError, or %NULL
7116 * Finishes an stop operation, see g_file_stop_mountable() for details.
7118 * Finish an asynchronous stop operation that was started
7119 * with g_file_stop_mountable().
7121 * Returns: %TRUE if the operation finished successfully. %FALSE
7122 * otherwise.
7124 * Since: 2.22
7126 gboolean
7127 g_file_stop_mountable_finish (GFile *file,
7128 GAsyncResult *result,
7129 GError **error)
7131 GFileIface *iface;
7133 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7134 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7136 if (G_IS_SIMPLE_ASYNC_RESULT (result))
7138 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
7139 if (g_simple_async_result_propagate_error (simple, error))
7140 return FALSE;
7143 iface = G_FILE_GET_IFACE (file);
7144 return (* iface->stop_mountable_finish) (file, result, error);
7148 * g_file_poll_mountable:
7149 * @file: input #GFile.
7150 * @cancellable: optional #GCancellable object, %NULL to ignore.
7151 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
7152 * @user_data: the data to pass to callback function
7154 * Polls a file of type G_FILE_TYPE_MOUNTABLE.
7156 * If @cancellable is not %NULL, then the operation can be cancelled by
7157 * triggering the cancellable object from another thread. If the operation
7158 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7160 * When the operation is finished, @callback will be called. You can then call
7161 * g_file_mount_mountable_finish() to get the result of the operation.
7163 * Since: 2.22
7165 void
7166 g_file_poll_mountable (GFile *file,
7167 GCancellable *cancellable,
7168 GAsyncReadyCallback callback,
7169 gpointer user_data)
7171 GFileIface *iface;
7173 g_return_if_fail (G_IS_FILE (file));
7175 iface = G_FILE_GET_IFACE (file);
7177 if (iface->poll_mountable == NULL)
7179 g_simple_async_report_error_in_idle (G_OBJECT (file),
7180 callback,
7181 user_data,
7182 G_IO_ERROR,
7183 G_IO_ERROR_NOT_SUPPORTED,
7184 _("Operation not supported"));
7185 return;
7188 (* iface->poll_mountable) (file,
7189 cancellable,
7190 callback,
7191 user_data);
7195 * g_file_poll_mountable_finish:
7196 * @file: input #GFile.
7197 * @result: a #GAsyncResult.
7198 * @error: a #GError, or %NULL
7200 * Finishes a poll operation. See g_file_poll_mountable() for details.
7202 * Finish an asynchronous poll operation that was polled
7203 * with g_file_poll_mountable().
7205 * Returns: %TRUE if the operation finished successfully. %FALSE
7206 * otherwise.
7208 * Since: 2.22
7210 gboolean
7211 g_file_poll_mountable_finish (GFile *file,
7212 GAsyncResult *result,
7213 GError **error)
7215 GFileIface *iface;
7217 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7218 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7220 if (G_IS_SIMPLE_ASYNC_RESULT (result))
7222 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
7223 if (g_simple_async_result_propagate_error (simple, error))
7224 return FALSE;
7227 iface = G_FILE_GET_IFACE (file);
7228 return (* iface->poll_mountable_finish) (file, result, error);
7232 * g_file_supports_thread_contexts:
7233 * @file: a #GFile.
7235 * Checks if @file supports <link
7236 * linkend="g-main-context-push-thread-default-context">thread-default
7237 * contexts</link>. If this returns %FALSE, you cannot perform
7238 * asynchronous operations on @file in a thread that has a
7239 * thread-default context.
7241 * Returns: Whether or not @file supports thread-default contexts.
7243 * Since: 2.22
7245 gboolean
7246 g_file_supports_thread_contexts (GFile *file)
7248 GFileIface *iface;
7250 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7252 iface = G_FILE_GET_IFACE (file);
7253 return iface->supports_thread_contexts;