1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
3 /* GIO - GLib Input, Output and Streaming Library
5 * Copyright (C) 2006-2007 Red Hat, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, 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>
27 #include <sys/types.h>
33 #include "gioscheduler.h"
34 #include "gsimpleasyncresult.h"
35 #include "gfileattribute-priv.h"
36 #include "gpollfilemonitor.h"
38 #include "gfileinputstream.h"
39 #include "gfileoutputstream.h"
40 #include "gcancellable.h"
41 #include "gasyncresult.h"
49 * @short_description: File and Directory Handling
51 * @see_also: #GFileInfo, #GFileEnumerator
53 * #GFile is a high level abstraction for manipulating files on a
54 * virtual file system. #GFile<!-- -->s are lightweight, immutable
55 * objects that do no I/O upon creation. It is necessary to understand that
56 * #GFile objects do not represent files, merely an identifier for a file. All
57 * file content I/O is implemented as streaming operations (see #GInputStream and
60 * To construct a #GFile, you can use:
61 * g_file_new_for_path() if you have a path.
62 * g_file_new_for_uri() if you have a URI.
63 * g_file_new_for_commandline_arg() for a command line argument.
64 * g_file_parse_name() from a utf8 string gotten from g_file_get_parse_name().
66 * One way to think of a #GFile is as an abstraction of a pathname. For normal
67 * files the system pathname is what is stored internally, but as #GFile<!-- -->s
68 * are extensible it could also be something else that corresponds to a pathname
69 * in a userspace implementation of a filesystem.
71 * #GFile<!-- -->s make up hierarchies of directories and files that correspond to the
72 * files on a filesystem. You can move through the file system with #GFile using
73 * g_file_get_parent() to get an identifier for the parent directory, g_file_get_child()
74 * to get a child within a directory, g_file_resolve_relative_path() to resolve a relative
75 * path between two #GFile<!-- -->s. There can be multiple hierarchies, so you may not
76 * end up at the same root if you repeatedly call g_file_get_parent() on two different
79 * All #GFile<!-- -->s have a basename (get with g_file_get_basename()). These names
80 * are byte strings that are used to identify the file on the filesystem (relative to
81 * its parent directory) and there is no guarantees that they have any particular charset
82 * encoding or even make any sense at all. If you want to use filenames in a user
83 * interface you should use the display name that you can get by requesting the
84 * %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME attribute with g_file_query_info().
85 * This is guaranteed to be in utf8 and can be used in a user interface. But always
86 * store the real basename or the #GFile to use to actually access the file, because
87 * there is no way to go from a display name to the actual name.
89 * Using #GFile as an identifier has the same weaknesses as using a path in that
90 * there may be multiple aliases for the same file. For instance, hard or
91 * soft links may cause two different #GFile<!-- -->s to refer to the same file.
92 * Other possible causes for aliases are: case insensitive filesystems, short
93 * and long names on Fat/NTFS, or bind mounts in Linux. If you want to check if
94 * two #GFile<!-- -->s point to the same file you can query for the
95 * %G_FILE_ATTRIBUTE_ID_FILE attribute. Note that #GFile does some trivial
96 * canonicalization of pathnames passed in, so that trivial differences in the
97 * path string used at creation (duplicated slashes, slash at end of path, "."
98 * or ".." path segments, etc) does not create different #GFile<!-- -->s.
100 * Many #GFile operations have both synchronous and asynchronous versions
101 * to suit your application. Asynchronous versions of synchronous functions
102 * simply have _async() appended to their function names. The asynchronous
103 * I/O functions call a #GAsyncReadyCallback which is then used to finalize
104 * the operation, producing a GAsyncResult which is then passed to the
105 * function's matching _finish() operation.
107 * Some #GFile operations do not have synchronous analogs, as they may
108 * take a very long time to finish, and blocking may leave an application
109 * unusable. Notable cases include:
110 * g_file_mount_mountable() to mount a mountable file.
111 * g_file_unmount_mountable_with_operation() to unmount a mountable file.
112 * g_file_eject_mountable_with_operation() to eject a mountable file.
114 * <para id="gfile-etag"><indexterm><primary>entity tag</primary></indexterm>
115 * One notable feature of #GFile<!-- -->s are entity tags, or "etags" for
116 * short. Entity tags are somewhat like a more abstract version of the
117 * traditional mtime, and can be used to quickly determine if the file has
118 * been modified from the version on the file system. See the HTTP 1.1
119 * <ulink url="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html">specification</ulink>
120 * for HTTP Etag headers, which are a very similar concept.
124 static void g_file_base_init (gpointer g_class);
125 static void g_file_class_init (gpointer g_class,
126 gpointer class_data);
128 static void g_file_real_query_info_async (GFile *file,
129 const char *attributes,
130 GFileQueryInfoFlags flags,
132 GCancellable *cancellable,
133 GAsyncReadyCallback callback,
135 static GFileInfo * g_file_real_query_info_finish (GFile *file,
138 static void g_file_real_query_filesystem_info_async (GFile *file,
139 const char *attributes,
141 GCancellable *cancellable,
142 GAsyncReadyCallback callback,
144 static GFileInfo * g_file_real_query_filesystem_info_finish (GFile *file,
147 static void g_file_real_enumerate_children_async (GFile *file,
148 const char *attributes,
149 GFileQueryInfoFlags flags,
151 GCancellable *cancellable,
152 GAsyncReadyCallback callback,
154 static GFileEnumerator * g_file_real_enumerate_children_finish (GFile *file,
157 static void g_file_real_read_async (GFile *file,
159 GCancellable *cancellable,
160 GAsyncReadyCallback callback,
162 static GFileInputStream * g_file_real_read_finish (GFile *file,
165 static void g_file_real_append_to_async (GFile *file,
166 GFileCreateFlags flags,
168 GCancellable *cancellable,
169 GAsyncReadyCallback callback,
171 static GFileOutputStream *g_file_real_append_to_finish (GFile *file,
174 static void g_file_real_create_async (GFile *file,
175 GFileCreateFlags flags,
177 GCancellable *cancellable,
178 GAsyncReadyCallback callback,
180 static GFileOutputStream *g_file_real_create_finish (GFile *file,
183 static void g_file_real_replace_async (GFile *file,
185 gboolean make_backup,
186 GFileCreateFlags flags,
188 GCancellable *cancellable,
189 GAsyncReadyCallback callback,
191 static GFileOutputStream *g_file_real_replace_finish (GFile *file,
194 static void g_file_real_open_readwrite_async (GFile *file,
196 GCancellable *cancellable,
197 GAsyncReadyCallback callback,
199 static GFileIOStream * g_file_real_open_readwrite_finish (GFile *file,
202 static void g_file_real_create_readwrite_async (GFile *file,
203 GFileCreateFlags flags,
205 GCancellable *cancellable,
206 GAsyncReadyCallback callback,
208 static GFileIOStream * g_file_real_create_readwrite_finish (GFile *file,
211 static void g_file_real_replace_readwrite_async (GFile *file,
213 gboolean make_backup,
214 GFileCreateFlags flags,
216 GCancellable *cancellable,
217 GAsyncReadyCallback callback,
219 static GFileIOStream * g_file_real_replace_readwrite_finish (GFile *file,
222 static gboolean g_file_real_set_attributes_from_info (GFile *file,
224 GFileQueryInfoFlags flags,
225 GCancellable *cancellable,
227 static void g_file_real_set_display_name_async (GFile *file,
228 const char *display_name,
230 GCancellable *cancellable,
231 GAsyncReadyCallback callback,
233 static GFile * g_file_real_set_display_name_finish (GFile *file,
236 static void g_file_real_set_attributes_async (GFile *file,
238 GFileQueryInfoFlags flags,
240 GCancellable *cancellable,
241 GAsyncReadyCallback callback,
243 static gboolean g_file_real_set_attributes_finish (GFile *file,
247 static void g_file_real_find_enclosing_mount_async (GFile *file,
249 GCancellable *cancellable,
250 GAsyncReadyCallback callback,
252 static GMount * g_file_real_find_enclosing_mount_finish (GFile *file,
255 static void g_file_real_copy_async (GFile *source,
257 GFileCopyFlags flags,
259 GCancellable *cancellable,
260 GFileProgressCallback progress_callback,
261 gpointer progress_callback_data,
262 GAsyncReadyCallback callback,
264 static gboolean g_file_real_copy_finish (GFile *file,
269 g_file_get_type (void)
271 static volatile gsize g_define_type_id__volatile = 0;
273 if (g_once_init_enter (&g_define_type_id__volatile))
275 const GTypeInfo file_info =
277 sizeof (GFileIface), /* class_size */
278 g_file_base_init, /* base_init */
279 NULL, /* base_finalize */
281 NULL, /* class_finalize */
282 NULL, /* class_data */
287 GType g_define_type_id =
288 g_type_register_static (G_TYPE_INTERFACE, I_("GFile"),
291 g_type_interface_add_prerequisite (g_define_type_id, G_TYPE_OBJECT);
293 g_once_init_leave (&g_define_type_id__volatile, g_define_type_id);
296 return g_define_type_id__volatile;
300 g_file_class_init (gpointer g_class,
303 GFileIface *iface = g_class;
305 iface->enumerate_children_async = g_file_real_enumerate_children_async;
306 iface->enumerate_children_finish = g_file_real_enumerate_children_finish;
307 iface->set_display_name_async = g_file_real_set_display_name_async;
308 iface->set_display_name_finish = g_file_real_set_display_name_finish;
309 iface->query_info_async = g_file_real_query_info_async;
310 iface->query_info_finish = g_file_real_query_info_finish;
311 iface->query_filesystem_info_async = g_file_real_query_filesystem_info_async;
312 iface->query_filesystem_info_finish = g_file_real_query_filesystem_info_finish;
313 iface->set_attributes_async = g_file_real_set_attributes_async;
314 iface->set_attributes_finish = g_file_real_set_attributes_finish;
315 iface->read_async = g_file_real_read_async;
316 iface->read_finish = g_file_real_read_finish;
317 iface->append_to_async = g_file_real_append_to_async;
318 iface->append_to_finish = g_file_real_append_to_finish;
319 iface->create_async = g_file_real_create_async;
320 iface->create_finish = g_file_real_create_finish;
321 iface->replace_async = g_file_real_replace_async;
322 iface->replace_finish = g_file_real_replace_finish;
323 iface->open_readwrite_async = g_file_real_open_readwrite_async;
324 iface->open_readwrite_finish = g_file_real_open_readwrite_finish;
325 iface->create_readwrite_async = g_file_real_create_readwrite_async;
326 iface->create_readwrite_finish = g_file_real_create_readwrite_finish;
327 iface->replace_readwrite_async = g_file_real_replace_readwrite_async;
328 iface->replace_readwrite_finish = g_file_real_replace_readwrite_finish;
329 iface->find_enclosing_mount_async = g_file_real_find_enclosing_mount_async;
330 iface->find_enclosing_mount_finish = g_file_real_find_enclosing_mount_finish;
331 iface->set_attributes_from_info = g_file_real_set_attributes_from_info;
332 iface->copy_async = g_file_real_copy_async;
333 iface->copy_finish = g_file_real_copy_finish;
337 g_file_base_init (gpointer g_class)
344 * @file: input #GFile.
346 * Checks to see if a file is native to the platform.
348 * A native file s one expressed in the platform-native filename format,
349 * e.g. "C:\Windows" or "/usr/bin/". This does not mean the file is local,
350 * as it might be on a locally mounted remote filesystem.
352 * On some systems non-native files may be available using
353 * the native filesystem via a userspace filesystem (FUSE), in
354 * these cases this call will return %FALSE, but g_file_get_path()
355 * will still return a native path.
357 * This call does no blocking i/o.
359 * Returns: %TRUE if file is native.
362 g_file_is_native (GFile *file)
366 g_return_val_if_fail (G_IS_FILE (file), FALSE);
368 iface = G_FILE_GET_IFACE (file);
370 return (* iface->is_native) (file);
375 * g_file_has_uri_scheme:
376 * @file: input #GFile.
377 * @uri_scheme: a string containing a URI scheme.
379 * Checks to see if a #GFile has a given URI scheme.
381 * This call does no blocking i/o.
383 * Returns: %TRUE if #GFile's backend supports the
384 * given URI scheme, %FALSE if URI scheme is %NULL,
385 * not supported, or #GFile is invalid.
388 g_file_has_uri_scheme (GFile *file,
389 const char *uri_scheme)
393 g_return_val_if_fail (G_IS_FILE (file), FALSE);
394 g_return_val_if_fail (uri_scheme != NULL, FALSE);
396 iface = G_FILE_GET_IFACE (file);
398 return (* iface->has_uri_scheme) (file, uri_scheme);
403 * g_file_get_uri_scheme:
404 * @file: input #GFile.
406 * Gets the URI scheme for a #GFile.
407 * RFC 3986 decodes the scheme as:
409 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
411 * Common schemes include "file", "http", "ftp", etc.
413 * This call does no blocking i/o.
415 * Returns: a string containing the URI scheme for the given
416 * #GFile. The returned string should be freed with g_free()
417 * when no longer needed.
420 g_file_get_uri_scheme (GFile *file)
424 g_return_val_if_fail (G_IS_FILE (file), NULL);
426 iface = G_FILE_GET_IFACE (file);
428 return (* iface->get_uri_scheme) (file);
433 * g_file_get_basename:
434 * @file: input #GFile.
436 * Gets the base name (the last component of the path) for a given #GFile.
438 * If called for the top level of a system (such as the filesystem root
439 * or a uri like sftp://host/) it will return a single directory separator
440 * (and on Windows, possibly a drive letter).
442 * The base name is a byte string (*not* UTF-8). It has no defined encoding
443 * or rules other than it may not contain zero bytes. If you want to use
444 * filenames in a user interface you should use the display name that you
445 * can get by requesting the %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME
446 * attribute with g_file_query_info().
448 * This call does no blocking i/o.
450 * Returns: string containing the #GFile's base name, or %NULL
451 * if given #GFile is invalid. The returned string should be
452 * freed with g_free() when no longer needed.
455 g_file_get_basename (GFile *file)
459 g_return_val_if_fail (G_IS_FILE (file), NULL);
461 iface = G_FILE_GET_IFACE (file);
463 return (* iface->get_basename) (file);
468 * @file: input #GFile.
470 * Gets the local pathname for #GFile, if one exists.
472 * This call does no blocking i/o.
474 * Returns: string containing the #GFile's path, or %NULL if
475 * no such path exists. The returned string should be
476 * freed with g_free() when no longer needed.
479 g_file_get_path (GFile *file)
483 g_return_val_if_fail (G_IS_FILE (file), NULL);
485 iface = G_FILE_GET_IFACE (file);
487 return (* iface->get_path) (file);
492 * @file: input #GFile.
494 * Gets the URI for the @file.
496 * This call does no blocking i/o.
498 * Returns: a string containing the #GFile's URI.
499 * The returned string should be freed with g_free() when no longer needed.
502 g_file_get_uri (GFile *file)
506 g_return_val_if_fail (G_IS_FILE (file), NULL);
508 iface = G_FILE_GET_IFACE (file);
510 return (* iface->get_uri) (file);
514 * g_file_get_parse_name:
515 * @file: input #GFile.
517 * Gets the parse name of the @file.
518 * A parse name is a UTF-8 string that describes the
519 * file such that one can get the #GFile back using
520 * g_file_parse_name().
522 * This is generally used to show the #GFile as a nice
523 * full-pathname kind of string in a user interface,
524 * like in a location entry.
526 * For local files with names that can safely be converted
527 * to UTF8 the pathname is used, otherwise the IRI is used
528 * (a form of URI that allows UTF8 characters unescaped).
530 * This call does no blocking i/o.
532 * Returns: a string containing the #GFile's parse name. The returned
533 * string should be freed with g_free() when no longer needed.
536 g_file_get_parse_name (GFile *file)
540 g_return_val_if_fail (G_IS_FILE (file), NULL);
542 iface = G_FILE_GET_IFACE (file);
544 return (* iface->get_parse_name) (file);
549 * @file: input #GFile.
551 * Duplicates a #GFile handle. This operation does not duplicate
552 * the actual file or directory represented by the #GFile; see
553 * g_file_copy() if attempting to copy a file.
555 * This call does no blocking i/o.
557 * Returns: a new #GFile that is a duplicate of the given #GFile.
560 g_file_dup (GFile *file)
564 g_return_val_if_fail (G_IS_FILE (file), NULL);
566 iface = G_FILE_GET_IFACE (file);
568 return (* iface->dup) (file);
573 * @file: #gconstpointer to a #GFile.
575 * Creates a hash value for a #GFile.
577 * This call does no blocking i/o.
579 * Returns: 0 if @file is not a valid #GFile, otherwise an
580 * integer that can be used as hash value for the #GFile.
581 * This function is intended for easily hashing a #GFile to
582 * add to a #GHashTable or similar data structure.
585 g_file_hash (gconstpointer file)
589 g_return_val_if_fail (G_IS_FILE (file), 0);
591 iface = G_FILE_GET_IFACE (file);
593 return (* iface->hash) ((GFile *)file);
598 * @file1: the first #GFile.
599 * @file2: the second #GFile.
601 * Checks equality of two given #GFile<!-- -->s. Note that two
602 * #GFile<!-- -->s that differ can still refer to the same
603 * file on the filesystem due to various forms of filename
606 * This call does no blocking i/o.
608 * Returns: %TRUE if @file1 and @file2 are equal.
609 * %FALSE if either is not a #GFile.
612 g_file_equal (GFile *file1,
617 g_return_val_if_fail (G_IS_FILE (file1), FALSE);
618 g_return_val_if_fail (G_IS_FILE (file2), FALSE);
620 if (G_TYPE_FROM_INSTANCE (file1) != G_TYPE_FROM_INSTANCE (file2))
623 iface = G_FILE_GET_IFACE (file1);
625 return (* iface->equal) (file1, file2);
631 * @file: input #GFile.
633 * Gets the parent directory for the @file.
634 * If the @file represents the root directory of the
635 * file system, then %NULL will be returned.
637 * This call does no blocking i/o.
639 * Returns: a #GFile structure to the parent of the given
640 * #GFile or %NULL if there is no parent.
641 * Free the returned object with g_object_unref().
644 g_file_get_parent (GFile *file)
648 g_return_val_if_fail (G_IS_FILE (file), NULL);
650 iface = G_FILE_GET_IFACE (file);
652 return (* iface->get_parent) (file);
657 * @file: input #GFile
658 * @parent: the parent to check for, or %NULL
660 * Checks if @file has a parent, and optionally, if it is @parent.
662 * If @parent is %NULL then this function returns %TRUE if @file has any
663 * parent at all. If @parent is non-%NULL then %TRUE is only returned
664 * if @file is a child of @parent.
666 * Returns: %TRUE if @file is a child of @parent (or any parent in the
667 * case that @parent is %NULL).
672 g_file_has_parent (GFile *file,
675 GFile *actual_parent;
678 g_return_val_if_fail (G_IS_FILE (file), FALSE);
679 g_return_val_if_fail (parent == NULL || G_IS_FILE (parent), FALSE);
681 actual_parent = g_file_get_parent (file);
683 if (actual_parent != NULL)
686 result = g_file_equal (parent, actual_parent);
690 g_object_unref (actual_parent);
700 * @file: input #GFile.
701 * @name: string containing the child's basename.
703 * Gets a child of @file with basename equal to @name.
705 * Note that the file with that specific name might not exist, but
706 * you can still have a #GFile that points to it. You can use this
707 * for instance to create that file.
709 * This call does no blocking i/o.
711 * Returns: a #GFile to a child specified by @name.
712 * Free the returned object with g_object_unref().
715 g_file_get_child (GFile *file,
718 g_return_val_if_fail (G_IS_FILE (file), NULL);
719 g_return_val_if_fail (name != NULL, NULL);
721 return g_file_resolve_relative_path (file, name);
725 * g_file_get_child_for_display_name:
726 * @file: input #GFile.
727 * @display_name: string to a possible child.
730 * Gets the child of @file for a given @display_name (i.e. a UTF8
731 * version of the name). If this function fails, it returns %NULL and @error will be
732 * set. This is very useful when constructing a GFile for a new file
733 * and the user entered the filename in the user interface, for instance
734 * when you select a directory and type a filename in the file selector.
736 * This call does no blocking i/o.
738 * Returns: a #GFile to the specified child, or
739 * %NULL if the display name couldn't be converted.
740 * Free the returned object with g_object_unref().
743 g_file_get_child_for_display_name (GFile *file,
744 const char *display_name,
749 g_return_val_if_fail (G_IS_FILE (file), NULL);
750 g_return_val_if_fail (display_name != NULL, NULL);
752 iface = G_FILE_GET_IFACE (file);
754 return (* iface->get_child_for_display_name) (file, display_name, error);
759 * @file: input #GFile.
760 * @prefix: input #GFile.
762 * Checks whether @file has the prefix specified by @prefix. In other word,
763 * if the names of inital elements of @file<!-- -->s pathname match @prefix.
764 * Only full pathname elements are matched, so a path like /foo is not
765 * considered a prefix of /foobar, only of /foo/bar.
767 * This call does no i/o, as it works purely on names. As such it can
768 * sometimes return %FALSE even if @file is inside a @prefix (from a
769 * filesystem point of view), because the prefix of @file is an alias
772 * Returns: %TRUE if the @files's parent, grandparent, etc is @prefix.
776 g_file_has_prefix (GFile *file,
781 g_return_val_if_fail (G_IS_FILE (file), FALSE);
782 g_return_val_if_fail (G_IS_FILE (prefix), FALSE);
784 if (G_TYPE_FROM_INSTANCE (file) != G_TYPE_FROM_INSTANCE (prefix))
787 iface = G_FILE_GET_IFACE (file);
789 /* The vtable function differs in arg order since we're
790 using the old contains_file call */
791 return (* iface->prefix_matches) (prefix, file);
795 * g_file_get_relative_path:
796 * @parent: input #GFile.
797 * @descendant: input #GFile.
799 * Gets the path for @descendant relative to @parent.
801 * This call does no blocking i/o.
803 * Returns: string with the relative path from @descendant
804 * to @parent, or %NULL if @descendant doesn't have @parent as prefix.
805 * The returned string should be freed with g_free() when no longer needed.
808 g_file_get_relative_path (GFile *parent,
813 g_return_val_if_fail (G_IS_FILE (parent), NULL);
814 g_return_val_if_fail (G_IS_FILE (descendant), NULL);
816 if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
819 iface = G_FILE_GET_IFACE (parent);
821 return (* iface->get_relative_path) (parent, descendant);
825 * g_file_resolve_relative_path:
826 * @file: input #GFile.
827 * @relative_path: a given relative path string.
829 * Resolves a relative path for @file to an absolute path.
831 * This call does no blocking i/o.
833 * Returns: #GFile to the resolved path. %NULL if @relative_path
834 * is %NULL or if @file is invalid.
835 * Free the returned object with g_object_unref().
838 g_file_resolve_relative_path (GFile *file,
839 const char *relative_path)
843 g_return_val_if_fail (G_IS_FILE (file), NULL);
844 g_return_val_if_fail (relative_path != NULL, NULL);
846 iface = G_FILE_GET_IFACE (file);
848 return (* iface->resolve_relative_path) (file, relative_path);
852 * g_file_enumerate_children:
853 * @file: input #GFile.
854 * @attributes: an attribute query string.
855 * @flags: a set of #GFileQueryInfoFlags.
856 * @cancellable: optional #GCancellable object, %NULL to ignore.
857 * @error: #GError for error reporting.
859 * Gets the requested information about the files in a directory. The result
860 * is a #GFileEnumerator object that will give out #GFileInfo objects for
861 * all the files in the directory.
863 * The @attribute value is a string that specifies the file attributes that
864 * should be gathered. It is not an error if it's not possible to read a particular
865 * requested attribute from a file - it just won't be set. @attribute should
866 * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
867 * means all attributes, and a wildcard like "standard::*" means all attributes in the standard
868 * namespace. An example attribute query be "standard::*,owner::user".
869 * The standard attributes are available as defines, like #G_FILE_ATTRIBUTE_STANDARD_NAME.
871 * If @cancellable is not %NULL, then the operation can be cancelled by
872 * triggering the cancellable object from another thread. If the operation
873 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
875 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
876 * If the file is not a directory, the G_FILE_ERROR_NOTDIR error will be returned.
877 * Other errors are possible too.
879 * Returns: A #GFileEnumerator if successful, %NULL on error.
880 * Free the returned object with g_object_unref().
883 g_file_enumerate_children (GFile *file,
884 const char *attributes,
885 GFileQueryInfoFlags flags,
886 GCancellable *cancellable,
892 g_return_val_if_fail (G_IS_FILE (file), NULL);
894 if (g_cancellable_set_error_if_cancelled (cancellable, error))
897 iface = G_FILE_GET_IFACE (file);
899 if (iface->enumerate_children == NULL)
901 g_set_error_literal (error, G_IO_ERROR,
902 G_IO_ERROR_NOT_SUPPORTED,
903 _("Operation not supported"));
907 return (* iface->enumerate_children) (file, attributes, flags,
912 * g_file_enumerate_children_async:
913 * @file: input #GFile.
914 * @attributes: an attribute query string.
915 * @flags: a set of #GFileQueryInfoFlags.
916 * @io_priority: the <link linkend="io-priority">I/O priority</link>
918 * @cancellable: optional #GCancellable object, %NULL to ignore.
919 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
920 * @user_data: the data to pass to callback function
922 * Asynchronously gets the requested information about the files in a directory. The result
923 * is a #GFileEnumerator object that will give out #GFileInfo objects for
924 * all the files in the directory.
926 * For more details, see g_file_enumerate_children() which is
927 * the synchronous version of this call.
929 * When the operation is finished, @callback will be called. You can then call
930 * g_file_enumerate_children_finish() to get the result of the operation.
933 g_file_enumerate_children_async (GFile *file,
934 const char *attributes,
935 GFileQueryInfoFlags flags,
937 GCancellable *cancellable,
938 GAsyncReadyCallback callback,
943 g_return_if_fail (G_IS_FILE (file));
945 iface = G_FILE_GET_IFACE (file);
946 (* iface->enumerate_children_async) (file,
956 * g_file_enumerate_children_finish:
957 * @file: input #GFile.
958 * @res: a #GAsyncResult.
961 * Finishes an async enumerate children operation.
962 * See g_file_enumerate_children_async().
964 * Returns: a #GFileEnumerator or %NULL if an error occurred.
965 * Free the returned object with g_object_unref().
968 g_file_enumerate_children_finish (GFile *file,
974 g_return_val_if_fail (G_IS_FILE (file), NULL);
975 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
977 if (G_IS_SIMPLE_ASYNC_RESULT (res))
979 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
980 if (g_simple_async_result_propagate_error (simple, error))
984 iface = G_FILE_GET_IFACE (file);
985 return (* iface->enumerate_children_finish) (file, res, error);
989 * g_file_query_exists:
990 * @file: input #GFile.
991 * @cancellable: optional #GCancellable object, %NULL to ignore.
993 * Utility function to check if a particular file exists. This is
994 * implemented using g_file_query_info() and as such does blocking I/O.
996 * Note that in many cases it is racy to first check for file existence
997 * and then execute something based on the outcome of that, because the
998 * file might have been created or removed in between the operations. The
999 * general approach to handling that is to not check, but just do the
1000 * operation and handle the errors as they come.
1002 * As an example of race-free checking, take the case of reading a file, and
1003 * if it doesn't exist, creating it. There are two racy versions: read it, and
1004 * on error create it; and: check if it exists, if not create it. These
1005 * can both result in two processes creating the file (with perhaps a partially
1006 * written file as the result). The correct approach is to always try to create
1007 * the file with g_file_create() which will either atomically create the file
1008 * or fail with a G_IO_ERROR_EXISTS error.
1010 * However, in many cases an existence check is useful in a user
1011 * interface, for instance to make a menu item sensitive/insensitive, so that
1012 * you don't have to fool users that something is possible and then just show
1013 * and error dialog. If you do this, you should make sure to also handle the
1014 * errors that can happen due to races when you execute the operation.
1016 * Returns: %TRUE if the file exists (and can be detected without error), %FALSE otherwise (or if cancelled).
1019 g_file_query_exists (GFile *file,
1020 GCancellable *cancellable)
1024 g_return_val_if_fail (G_IS_FILE(file), FALSE);
1026 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1027 G_FILE_QUERY_INFO_NONE, cancellable, NULL);
1030 g_object_unref (info);
1038 * g_file_query_file_type:
1039 * @file: input #GFile.
1040 * @flags: a set of #GFileQueryInfoFlags passed to g_file_query_info().
1041 * @cancellable: optional #GCancellable object, %NULL to ignore.
1043 * Utility function to inspect the #GFileType of a file. This is
1044 * implemented using g_file_query_info() and as such does blocking I/O.
1046 * The primary use case of this method is to check if a file is a regular file,
1047 * directory, or symlink.
1049 * Returns: The #GFileType of the file and #G_FILE_TYPE_UNKNOWN if the file
1055 g_file_query_file_type (GFile *file,
1056 GFileQueryInfoFlags flags,
1057 GCancellable *cancellable)
1060 GFileType file_type;
1062 g_return_val_if_fail (G_IS_FILE(file), G_FILE_TYPE_UNKNOWN);
1063 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE, flags,
1067 file_type = g_file_info_get_file_type (info);
1068 g_object_unref (info);
1071 file_type = G_FILE_TYPE_UNKNOWN;
1077 * g_file_query_info:
1078 * @file: input #GFile.
1079 * @attributes: an attribute query string.
1080 * @flags: a set of #GFileQueryInfoFlags.
1081 * @cancellable: optional #GCancellable object, %NULL to ignore.
1082 * @error: a #GError.
1084 * Gets the requested information about specified @file. The result
1085 * is a #GFileInfo object that contains key-value attributes (such as
1086 * the type or size of the file).
1088 * The @attribute value is a string that specifies the file attributes that
1089 * should be gathered. It is not an error if it's not possible to read a particular
1090 * requested attribute from a file - it just won't be set. @attribute should
1091 * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
1092 * means all attributes, and a wildcard like "standard::*" means all attributes in the standard
1093 * namespace. An example attribute query be "standard::*,owner::user".
1094 * The standard attributes are available as defines, like #G_FILE_ATTRIBUTE_STANDARD_NAME.
1096 * If @cancellable is not %NULL, then the operation can be cancelled by
1097 * triggering the cancellable object from another thread. If the operation
1098 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1100 * For symlinks, normally the information about the target of the
1101 * symlink is returned, rather than information about the symlink itself.
1102 * However if you pass #G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS in @flags the
1103 * information about the symlink itself will be returned. Also, for symlinks
1104 * that point to non-existing files the information about the symlink itself
1107 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1108 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1110 * Returns: a #GFileInfo for the given @file, or %NULL on error.
1111 * Free the returned object with g_object_unref().
1114 g_file_query_info (GFile *file,
1115 const char *attributes,
1116 GFileQueryInfoFlags flags,
1117 GCancellable *cancellable,
1122 g_return_val_if_fail (G_IS_FILE (file), NULL);
1124 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1127 iface = G_FILE_GET_IFACE (file);
1129 if (iface->query_info == NULL)
1131 g_set_error_literal (error, G_IO_ERROR,
1132 G_IO_ERROR_NOT_SUPPORTED,
1133 _("Operation not supported"));
1137 return (* iface->query_info) (file, attributes, flags, cancellable, error);
1141 * g_file_query_info_async:
1142 * @file: input #GFile.
1143 * @attributes: an attribute query string.
1144 * @flags: a set of #GFileQueryInfoFlags.
1145 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1147 * @cancellable: optional #GCancellable object, %NULL to ignore.
1148 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1149 * @user_data: the data to pass to callback function
1151 * Asynchronously gets the requested information about specified @file. The result
1152 * is a #GFileInfo object that contains key-value attributes (such as type or size
1155 * For more details, see g_file_query_info() which is
1156 * the synchronous version of this call.
1158 * When the operation is finished, @callback will be called. You can then call
1159 * g_file_query_info_finish() to get the result of the operation.
1162 g_file_query_info_async (GFile *file,
1163 const char *attributes,
1164 GFileQueryInfoFlags flags,
1166 GCancellable *cancellable,
1167 GAsyncReadyCallback callback,
1172 g_return_if_fail (G_IS_FILE (file));
1174 iface = G_FILE_GET_IFACE (file);
1175 (* iface->query_info_async) (file,
1185 * g_file_query_info_finish:
1186 * @file: input #GFile.
1187 * @res: a #GAsyncResult.
1188 * @error: a #GError.
1190 * Finishes an asynchronous file info query.
1191 * See g_file_query_info_async().
1193 * Returns: #GFileInfo for given @file or %NULL on error.
1194 * Free the returned object with g_object_unref().
1197 g_file_query_info_finish (GFile *file,
1203 g_return_val_if_fail (G_IS_FILE (file), NULL);
1204 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1206 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1208 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1209 if (g_simple_async_result_propagate_error (simple, error))
1213 iface = G_FILE_GET_IFACE (file);
1214 return (* iface->query_info_finish) (file, res, error);
1218 * g_file_query_filesystem_info:
1219 * @file: input #GFile.
1220 * @attributes: an attribute query string.
1221 * @cancellable: optional #GCancellable object, %NULL to ignore.
1222 * @error: a #GError.
1224 * Similar to g_file_query_info(), but obtains information
1225 * about the filesystem the @file is on, rather than the file itself.
1226 * For instance the amount of space available and the type of
1229 * The @attribute value is a string that specifies the file attributes that
1230 * should be gathered. It is not an error if it's not possible to read a particular
1231 * requested attribute from a file - it just won't be set. @attribute should
1232 * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
1233 * means all attributes, and a wildcard like "fs:*" means all attributes in the fs
1234 * namespace. The standard namespace for filesystem attributes is "fs".
1235 * Common attributes of interest are #G_FILE_ATTRIBUTE_FILESYSTEM_SIZE
1236 * (the total size of the filesystem in bytes), #G_FILE_ATTRIBUTE_FILESYSTEM_FREE (number of
1237 * bytes available), and #G_FILE_ATTRIBUTE_FILESYSTEM_TYPE (type of the filesystem).
1239 * If @cancellable is not %NULL, then the operation can be cancelled by
1240 * triggering the cancellable object from another thread. If the operation
1241 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1243 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1244 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1246 * Returns: a #GFileInfo or %NULL if there was an error.
1247 * Free the returned object with g_object_unref().
1250 g_file_query_filesystem_info (GFile *file,
1251 const char *attributes,
1252 GCancellable *cancellable,
1257 g_return_val_if_fail (G_IS_FILE (file), NULL);
1259 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1262 iface = G_FILE_GET_IFACE (file);
1264 if (iface->query_filesystem_info == NULL)
1266 g_set_error_literal (error, G_IO_ERROR,
1267 G_IO_ERROR_NOT_SUPPORTED,
1268 _("Operation not supported"));
1272 return (* iface->query_filesystem_info) (file, attributes, cancellable, error);
1276 * g_file_query_filesystem_info_async:
1277 * @file: input #GFile.
1278 * @attributes: an attribute query string.
1279 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1281 * @cancellable: optional #GCancellable object, %NULL to ignore.
1282 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1283 * @user_data: the data to pass to callback function
1285 * Asynchronously gets the requested information about the filesystem
1286 * that the specified @file is on. The result is a #GFileInfo object
1287 * that contains key-value attributes (such as type or size for the
1290 * For more details, see g_file_query_filesystem_info() which is the
1291 * synchronous version of this call.
1293 * When the operation is finished, @callback will be called. You can
1294 * then call g_file_query_info_finish() to get the result of the
1298 g_file_query_filesystem_info_async (GFile *file,
1299 const char *attributes,
1301 GCancellable *cancellable,
1302 GAsyncReadyCallback callback,
1307 g_return_if_fail (G_IS_FILE (file));
1309 iface = G_FILE_GET_IFACE (file);
1310 (* iface->query_filesystem_info_async) (file,
1319 * g_file_query_filesystem_info_finish:
1320 * @file: input #GFile.
1321 * @res: a #GAsyncResult.
1322 * @error: a #GError.
1324 * Finishes an asynchronous filesystem info query. See
1325 * g_file_query_filesystem_info_async().
1327 * Returns: #GFileInfo for given @file or %NULL on error.
1328 * Free the returned object with g_object_unref().
1331 g_file_query_filesystem_info_finish (GFile *file,
1337 g_return_val_if_fail (G_IS_FILE (file), NULL);
1338 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1340 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1342 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1343 if (g_simple_async_result_propagate_error (simple, error))
1347 iface = G_FILE_GET_IFACE (file);
1348 return (* iface->query_filesystem_info_finish) (file, res, error);
1352 * g_file_find_enclosing_mount:
1353 * @file: input #GFile.
1354 * @cancellable: optional #GCancellable object, %NULL to ignore.
1355 * @error: a #GError.
1357 * Gets a #GMount for the #GFile.
1359 * If the #GFileIface for @file does not have a mount (e.g. possibly a
1360 * remote share), @error will be set to %G_IO_ERROR_NOT_FOUND and %NULL
1363 * If @cancellable is not %NULL, then the operation can be cancelled by
1364 * triggering the cancellable object from another thread. If the operation
1365 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1367 * Returns: a #GMount where the @file is located or %NULL on error.
1368 * Free the returned object with g_object_unref().
1371 g_file_find_enclosing_mount (GFile *file,
1372 GCancellable *cancellable,
1377 g_return_val_if_fail (G_IS_FILE (file), NULL);
1379 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1382 iface = G_FILE_GET_IFACE (file);
1383 if (iface->find_enclosing_mount == NULL)
1386 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1387 /* Translators: This is an error message when trying to find the
1388 * enclosing (user visible) mount of a file, but none exists. */
1389 _("Containing mount does not exist"));
1393 return (* iface->find_enclosing_mount) (file, cancellable, error);
1397 * g_file_find_enclosing_mount_async:
1399 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1401 * @cancellable: optional #GCancellable object, %NULL to ignore.
1402 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1403 * @user_data: the data to pass to callback function
1405 * Asynchronously gets the mount for the file.
1407 * For more details, see g_file_find_enclosing_mount() which is
1408 * the synchronous version of this call.
1410 * When the operation is finished, @callback will be called. You can then call
1411 * g_file_find_enclosing_mount_finish() to get the result of the operation.
1414 g_file_find_enclosing_mount_async (GFile *file,
1416 GCancellable *cancellable,
1417 GAsyncReadyCallback callback,
1422 g_return_if_fail (G_IS_FILE (file));
1424 iface = G_FILE_GET_IFACE (file);
1425 (* iface->find_enclosing_mount_async) (file,
1433 * g_file_find_enclosing_mount_finish:
1435 * @res: a #GAsyncResult
1438 * Finishes an asynchronous find mount request.
1439 * See g_file_find_enclosing_mount_async().
1441 * Returns: #GMount for given @file or %NULL on error.
1442 * Free the returned object with g_object_unref().
1445 g_file_find_enclosing_mount_finish (GFile *file,
1451 g_return_val_if_fail (G_IS_FILE (file), NULL);
1452 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1454 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1456 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1457 if (g_simple_async_result_propagate_error (simple, error))
1461 iface = G_FILE_GET_IFACE (file);
1462 return (* iface->find_enclosing_mount_finish) (file, res, error);
1468 * @file: #GFile to read.
1469 * @cancellable: a #GCancellable
1470 * @error: a #GError, or %NULL
1472 * Opens a file for reading. The result is a #GFileInputStream that
1473 * can be used to read the contents of the file.
1475 * If @cancellable is not %NULL, then the operation can be cancelled by
1476 * triggering the cancellable object from another thread. If the operation
1477 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1479 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1480 * If the file is a directory, the G_IO_ERROR_IS_DIRECTORY error will be returned.
1481 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1483 * Returns: #GFileInputStream or %NULL on error.
1484 * Free the returned object with g_object_unref().
1487 g_file_read (GFile *file,
1488 GCancellable *cancellable,
1493 g_return_val_if_fail (G_IS_FILE (file), NULL);
1495 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1498 iface = G_FILE_GET_IFACE (file);
1500 if (iface->read_fn == NULL)
1502 g_set_error_literal (error, G_IO_ERROR,
1503 G_IO_ERROR_NOT_SUPPORTED,
1504 _("Operation not supported"));
1508 return (* iface->read_fn) (file, cancellable, error);
1513 * @file: input #GFile.
1514 * @flags: a set of #GFileCreateFlags.
1515 * @cancellable: optional #GCancellable object, %NULL to ignore.
1516 * @error: a #GError, or %NULL
1518 * Gets an output stream for appending data to the file. If
1519 * the file doesn't already exist it is created.
1521 * By default files created are generally readable by everyone,
1522 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1523 * will be made readable only to the current user, to the level that
1524 * is supported on the target filesystem.
1526 * If @cancellable is not %NULL, then the operation can be cancelled by
1527 * triggering the cancellable object from another thread. If the operation
1528 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1530 * Some file systems don't allow all file names, and may
1531 * return an %G_IO_ERROR_INVALID_FILENAME error.
1532 * If the file is a directory the %G_IO_ERROR_IS_DIRECTORY error will be
1533 * returned. Other errors are possible too, and depend on what kind of
1534 * filesystem the file is on.
1536 * Returns: a #GFileOutputStream, or %NULL on error.
1537 * Free the returned object with g_object_unref().
1540 g_file_append_to (GFile *file,
1541 GFileCreateFlags flags,
1542 GCancellable *cancellable,
1547 g_return_val_if_fail (G_IS_FILE (file), NULL);
1549 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1552 iface = G_FILE_GET_IFACE (file);
1554 if (iface->append_to == NULL)
1556 g_set_error_literal (error, G_IO_ERROR,
1557 G_IO_ERROR_NOT_SUPPORTED,
1558 _("Operation not supported"));
1562 return (* iface->append_to) (file, flags, cancellable, error);
1567 * @file: input #GFile.
1568 * @flags: a set of #GFileCreateFlags.
1569 * @cancellable: optional #GCancellable object, %NULL to ignore.
1570 * @error: a #GError, or %NULL
1572 * Creates a new file and returns an output stream for writing to it.
1573 * The file must not already exist.
1575 * By default files created are generally readable by everyone,
1576 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1577 * will be made readable only to the current user, to the level that
1578 * is supported on the target filesystem.
1580 * If @cancellable is not %NULL, then the operation can be cancelled by
1581 * triggering the cancellable object from another thread. If the operation
1582 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1584 * If a file or directory with this name already exists the G_IO_ERROR_EXISTS
1585 * error will be returned.
1586 * Some file systems don't allow all file names, and may
1587 * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1588 * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1589 * Other errors are possible too, and depend on what kind of
1590 * filesystem the file is on.
1592 * Returns: a #GFileOutputStream for the newly created file, or
1594 * Free the returned object with g_object_unref().
1597 g_file_create (GFile *file,
1598 GFileCreateFlags flags,
1599 GCancellable *cancellable,
1604 g_return_val_if_fail (G_IS_FILE (file), NULL);
1606 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1609 iface = G_FILE_GET_IFACE (file);
1611 if (iface->create == NULL)
1613 g_set_error_literal (error, G_IO_ERROR,
1614 G_IO_ERROR_NOT_SUPPORTED,
1615 _("Operation not supported"));
1619 return (* iface->create) (file, flags, cancellable, error);
1624 * @file: input #GFile.
1625 * @etag: an optional <link linkend="gfile-etag">entity tag</link> for the
1626 * current #GFile, or #NULL to ignore.
1627 * @make_backup: %TRUE if a backup should be created.
1628 * @flags: a set of #GFileCreateFlags.
1629 * @cancellable: optional #GCancellable object, %NULL to ignore.
1630 * @error: a #GError, or %NULL
1632 * Returns an output stream for overwriting the file, possibly
1633 * creating a backup copy of the file first. If the file doesn't exist,
1634 * it will be created.
1636 * This will try to replace the file in the safest way possible so
1637 * that any errors during the writing will not affect an already
1638 * existing copy of the file. For instance, for local files it
1639 * may write to a temporary file and then atomically rename over
1640 * the destination when the stream is closed.
1642 * By default files created are generally readable by everyone,
1643 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1644 * will be made readable only to the current user, to the level that
1645 * is supported on the target filesystem.
1647 * If @cancellable is not %NULL, then the operation can be cancelled by
1648 * triggering the cancellable object from another thread. If the operation
1649 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1651 * If you pass in a non-#NULL @etag value, then this value is
1652 * compared to the current entity tag of the file, and if they differ
1653 * an G_IO_ERROR_WRONG_ETAG error is returned. This generally means
1654 * that the file has been changed since you last read it. You can get
1655 * the new etag from g_file_output_stream_get_etag() after you've
1656 * finished writing and closed the #GFileOutputStream. When you load
1657 * a new file you can use g_file_input_stream_query_info() to get
1658 * the etag of the file.
1660 * If @make_backup is %TRUE, this function will attempt to make a backup
1661 * of the current file before overwriting it. If this fails a G_IO_ERROR_CANT_CREATE_BACKUP
1662 * error will be returned. If you want to replace anyway, try again with
1663 * @make_backup set to %FALSE.
1665 * If the file is a directory the G_IO_ERROR_IS_DIRECTORY error will be returned,
1666 * and if the file is some other form of non-regular file then a
1667 * G_IO_ERROR_NOT_REGULAR_FILE error will be returned.
1668 * Some file systems don't allow all file names, and may
1669 * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1670 * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1671 * Other errors are possible too, and depend on what kind of
1672 * filesystem the file is on.
1674 * Returns: a #GFileOutputStream or %NULL on error.
1675 * Free the returned object with g_object_unref().
1678 g_file_replace (GFile *file,
1680 gboolean make_backup,
1681 GFileCreateFlags flags,
1682 GCancellable *cancellable,
1687 g_return_val_if_fail (G_IS_FILE (file), NULL);
1689 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1692 iface = G_FILE_GET_IFACE (file);
1694 if (iface->replace == NULL)
1696 g_set_error_literal (error, G_IO_ERROR,
1697 G_IO_ERROR_NOT_SUPPORTED,
1698 _("Operation not supported"));
1703 /* Handle empty tag string as NULL in consistent way. */
1704 if (etag && *etag == 0)
1707 return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1711 * g_file_open_readwrite:
1712 * @file: #GFile to open
1713 * @cancellable: a #GCancellable
1714 * @error: a #GError, or %NULL
1716 * Opens an existing file for reading and writing. The result is
1717 * a #GFileIOStream that can be used to read and write the contents of the file.
1719 * If @cancellable is not %NULL, then the operation can be cancelled by
1720 * triggering the cancellable object from another thread. If the operation
1721 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1723 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1724 * If the file is a directory, the G_IO_ERROR_IS_DIRECTORY error will be returned.
1725 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1726 * Note that in many non-local file cases read and write streams are not supported,
1727 * so make sure you really need to do read and write streaming, rather than
1728 * just opening for reading or writing.
1730 * Returns: #GFileIOStream or %NULL on error.
1731 * Free the returned object with g_object_unref().
1736 g_file_open_readwrite (GFile *file,
1737 GCancellable *cancellable,
1742 g_return_val_if_fail (G_IS_FILE (file), NULL);
1744 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1747 iface = G_FILE_GET_IFACE (file);
1749 if (iface->open_readwrite == NULL)
1751 g_set_error_literal (error, G_IO_ERROR,
1752 G_IO_ERROR_NOT_SUPPORTED,
1753 _("Operation not supported"));
1757 return (* iface->open_readwrite) (file, cancellable, error);
1761 * g_file_create_readwrite:
1763 * @flags: a set of #GFileCreateFlags
1764 * @cancellable: optional #GCancellable object, %NULL to ignore
1765 * @error: return location for a #GError, or %NULL
1767 * Creates a new file and returns a stream for reading and writing to it.
1768 * The file must not already exist.
1770 * By default files created are generally readable by everyone,
1771 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1772 * will be made readable only to the current user, to the level that
1773 * is supported on the target filesystem.
1775 * If @cancellable is not %NULL, then the operation can be cancelled by
1776 * triggering the cancellable object from another thread. If the operation
1777 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1779 * If a file or directory with this name already exists the %G_IO_ERROR_EXISTS
1780 * error will be returned. Some file systems don't allow all file names,
1781 * and may return an %G_IO_ERROR_INVALID_FILENAME error, and if the name
1782 * is too long, %G_IO_ERROR_FILENAME_TOO_LONG will be returned. Other errors
1783 * are possible too, and depend on what kind of filesystem the file is on.
1785 * Note that in many non-local file cases read and write streams are not
1786 * supported, so make sure you really need to do read and write streaming,
1787 * rather than just opening for reading or writing.
1789 * Returns: a #GFileIOStream for the newly created file, or %NULL on error.
1790 * Free the returned object with g_object_unref().
1795 g_file_create_readwrite (GFile *file,
1796 GFileCreateFlags flags,
1797 GCancellable *cancellable,
1802 g_return_val_if_fail (G_IS_FILE (file), NULL);
1804 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1807 iface = G_FILE_GET_IFACE (file);
1809 if (iface->create_readwrite == NULL)
1811 g_set_error_literal (error, G_IO_ERROR,
1812 G_IO_ERROR_NOT_SUPPORTED,
1813 _("Operation not supported"));
1817 return (* iface->create_readwrite) (file, flags, cancellable, error);
1821 * g_file_replace_readwrite:
1823 * @etag: an optional <link linkend="gfile-etag">entity tag</link> for the
1824 * current #GFile, or #NULL to ignore
1825 * @make_backup: %TRUE if a backup should be created
1826 * @flags: a set of #GFileCreateFlags
1827 * @cancellable: optional #GCancellable object, %NULL to ignore
1828 * @error: return location for a #GError, or %NULL
1830 * Returns an output stream for overwriting the file in readwrite mode,
1831 * possibly creating a backup copy of the file first. If the file doesn't
1832 * exist, it will be created.
1834 * For details about the behaviour, see g_file_replace() which does the same
1835 * thing but returns an output stream only.
1837 * Note that in many non-local file cases read and write streams are not
1838 * supported, so make sure you really need to do read and write streaming,
1839 * rather than just opening for reading or writing.
1841 * Returns: a #GFileIOStream or %NULL on error.
1842 * Free the returned object with g_object_unref().
1847 g_file_replace_readwrite (GFile *file,
1849 gboolean make_backup,
1850 GFileCreateFlags flags,
1851 GCancellable *cancellable,
1856 g_return_val_if_fail (G_IS_FILE (file), NULL);
1858 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1861 iface = G_FILE_GET_IFACE (file);
1863 if (iface->replace_readwrite == NULL)
1865 g_set_error_literal (error, G_IO_ERROR,
1866 G_IO_ERROR_NOT_SUPPORTED,
1867 _("Operation not supported"));
1871 return (* iface->replace_readwrite) (file, etag, make_backup, flags, cancellable, error);
1875 * g_file_read_async:
1876 * @file: input #GFile
1877 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1879 * @cancellable: optional #GCancellable object, %NULL to ignore.
1880 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1881 * @user_data: the data to pass to callback function
1883 * Asynchronously opens @file for reading.
1885 * For more details, see g_file_read() which is
1886 * the synchronous version of this call.
1888 * When the operation is finished, @callback will be called. You can then call
1889 * g_file_read_finish() to get the result of the operation.
1892 g_file_read_async (GFile *file,
1894 GCancellable *cancellable,
1895 GAsyncReadyCallback callback,
1900 g_return_if_fail (G_IS_FILE (file));
1902 iface = G_FILE_GET_IFACE (file);
1903 (* iface->read_async) (file,
1911 * g_file_read_finish:
1912 * @file: input #GFile.
1913 * @res: a #GAsyncResult.
1914 * @error: a #GError, or %NULL
1916 * Finishes an asynchronous file read operation started with
1917 * g_file_read_async().
1919 * Returns: a #GFileInputStream or %NULL on error.
1920 * Free the returned object with g_object_unref().
1923 g_file_read_finish (GFile *file,
1929 g_return_val_if_fail (G_IS_FILE (file), NULL);
1930 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1932 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1934 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1935 if (g_simple_async_result_propagate_error (simple, error))
1939 iface = G_FILE_GET_IFACE (file);
1940 return (* iface->read_finish) (file, res, error);
1944 * g_file_append_to_async:
1945 * @file: input #GFile.
1946 * @flags: a set of #GFileCreateFlags.
1947 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1949 * @cancellable: optional #GCancellable object, %NULL to ignore.
1950 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1951 * @user_data: the data to pass to callback function
1953 * Asynchronously opens @file for appending.
1955 * For more details, see g_file_append_to() which is
1956 * the synchronous version of this call.
1958 * When the operation is finished, @callback will be called. You can then call
1959 * g_file_append_to_finish() to get the result of the operation.
1962 g_file_append_to_async (GFile *file,
1963 GFileCreateFlags flags,
1965 GCancellable *cancellable,
1966 GAsyncReadyCallback callback,
1971 g_return_if_fail (G_IS_FILE (file));
1973 iface = G_FILE_GET_IFACE (file);
1974 (* iface->append_to_async) (file,
1983 * g_file_append_to_finish:
1984 * @file: input #GFile.
1985 * @res: #GAsyncResult
1986 * @error: a #GError, or %NULL
1988 * Finishes an asynchronous file append operation started with
1989 * g_file_append_to_async().
1991 * Returns: a valid #GFileOutputStream or %NULL on error.
1992 * Free the returned object with g_object_unref().
1995 g_file_append_to_finish (GFile *file,
2001 g_return_val_if_fail (G_IS_FILE (file), NULL);
2002 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2004 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2006 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2007 if (g_simple_async_result_propagate_error (simple, error))
2011 iface = G_FILE_GET_IFACE (file);
2012 return (* iface->append_to_finish) (file, res, error);
2016 * g_file_create_async:
2017 * @file: input #GFile.
2018 * @flags: a set of #GFileCreateFlags.
2019 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2021 * @cancellable: optional #GCancellable object, %NULL to ignore.
2022 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2023 * @user_data: the data to pass to callback function
2025 * Asynchronously creates a new file and returns an output stream for writing to it.
2026 * The file must not already exist.
2028 * For more details, see g_file_create() which is
2029 * the synchronous version of this call.
2031 * When the operation is finished, @callback will be called. You can then call
2032 * g_file_create_finish() to get the result of the operation.
2035 g_file_create_async (GFile *file,
2036 GFileCreateFlags flags,
2038 GCancellable *cancellable,
2039 GAsyncReadyCallback callback,
2044 g_return_if_fail (G_IS_FILE (file));
2046 iface = G_FILE_GET_IFACE (file);
2047 (* iface->create_async) (file,
2056 * g_file_create_finish:
2057 * @file: input #GFile.
2058 * @res: a #GAsyncResult.
2059 * @error: a #GError, or %NULL
2061 * Finishes an asynchronous file create operation started with
2062 * g_file_create_async().
2064 * Returns: a #GFileOutputStream or %NULL on error.
2065 * Free the returned object with g_object_unref().
2068 g_file_create_finish (GFile *file,
2074 g_return_val_if_fail (G_IS_FILE (file), NULL);
2075 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2077 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2079 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2080 if (g_simple_async_result_propagate_error (simple, error))
2084 iface = G_FILE_GET_IFACE (file);
2085 return (* iface->create_finish) (file, res, error);
2089 * g_file_replace_async:
2090 * @file: input #GFile.
2091 * @etag: an <link linkend="gfile-etag">entity tag</link> for the
2092 * current #GFile, or NULL to ignore.
2093 * @make_backup: %TRUE if a backup should be created.
2094 * @flags: a set of #GFileCreateFlags.
2095 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2097 * @cancellable: optional #GCancellable object, %NULL to ignore.
2098 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2099 * @user_data: the data to pass to callback function
2101 * Asynchronously overwrites the file, replacing the contents, possibly
2102 * creating a backup copy of the file first.
2104 * For more details, see g_file_replace() which is
2105 * the synchronous version of this call.
2107 * When the operation is finished, @callback will be called. You can then call
2108 * g_file_replace_finish() to get the result of the operation.
2111 g_file_replace_async (GFile *file,
2113 gboolean make_backup,
2114 GFileCreateFlags flags,
2116 GCancellable *cancellable,
2117 GAsyncReadyCallback callback,
2122 g_return_if_fail (G_IS_FILE (file));
2124 iface = G_FILE_GET_IFACE (file);
2125 (* iface->replace_async) (file,
2136 * g_file_replace_finish:
2137 * @file: input #GFile.
2138 * @res: a #GAsyncResult.
2139 * @error: a #GError, or %NULL
2141 * Finishes an asynchronous file replace operation started with
2142 * g_file_replace_async().
2144 * Returns: a #GFileOutputStream, or %NULL on error.
2145 * Free the returned object with g_object_unref().
2148 g_file_replace_finish (GFile *file,
2154 g_return_val_if_fail (G_IS_FILE (file), NULL);
2155 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2157 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2159 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2160 if (g_simple_async_result_propagate_error (simple, error))
2164 iface = G_FILE_GET_IFACE (file);
2165 return (* iface->replace_finish) (file, res, error);
2170 * g_file_open_readwrite_async:
2171 * @file: input #GFile.
2172 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2174 * @cancellable: optional #GCancellable object, %NULL to ignore.
2175 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2176 * @user_data: the data to pass to callback function
2178 * Asynchronously opens @file for reading and writing.
2180 * For more details, see g_file_open_readwrite() which is
2181 * the synchronous version of this call.
2183 * When the operation is finished, @callback will be called. You can then call
2184 * g_file_open_readwrite_finish() to get the result of the operation.
2189 g_file_open_readwrite_async (GFile *file,
2191 GCancellable *cancellable,
2192 GAsyncReadyCallback callback,
2197 g_return_if_fail (G_IS_FILE (file));
2199 iface = G_FILE_GET_IFACE (file);
2200 (* iface->open_readwrite_async) (file,
2208 * g_file_open_readwrite_finish:
2209 * @file: input #GFile.
2210 * @res: a #GAsyncResult.
2211 * @error: a #GError, or %NULL
2213 * Finishes an asynchronous file read operation started with
2214 * g_file_open_readwrite_async().
2216 * Returns: a #GFileIOStream or %NULL on error.
2217 * Free the returned object with g_object_unref().
2222 g_file_open_readwrite_finish (GFile *file,
2228 g_return_val_if_fail (G_IS_FILE (file), NULL);
2229 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2231 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2233 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2234 if (g_simple_async_result_propagate_error (simple, error))
2238 iface = G_FILE_GET_IFACE (file);
2239 return (* iface->open_readwrite_finish) (file, res, error);
2244 * g_file_create_readwrite_async:
2245 * @file: input #GFile
2246 * @flags: a set of #GFileCreateFlags
2247 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2249 * @cancellable: optional #GCancellable object, %NULL to ignore
2250 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2251 * @user_data: the data to pass to callback function
2253 * Asynchronously creates a new file and returns a stream for reading and
2254 * writing to it. The file must not already exist.
2256 * For more details, see g_file_create_readwrite() which is
2257 * the synchronous version of this call.
2259 * When the operation is finished, @callback will be called. You can then
2260 * call g_file_create_readwrite_finish() to get the result of the operation.
2265 g_file_create_readwrite_async (GFile *file,
2266 GFileCreateFlags flags,
2268 GCancellable *cancellable,
2269 GAsyncReadyCallback callback,
2274 g_return_if_fail (G_IS_FILE (file));
2276 iface = G_FILE_GET_IFACE (file);
2277 (* iface->create_readwrite_async) (file,
2286 * g_file_create_readwrite_finish:
2287 * @file: input #GFile
2288 * @res: a #GAsyncResult
2289 * @error: a #GError, or %NULL
2291 * Finishes an asynchronous file create operation started with
2292 * g_file_create_readwrite_async().
2294 * Returns: a #GFileIOStream or %NULL on error.
2295 * Free the returned object with g_object_unref().
2300 g_file_create_readwrite_finish (GFile *file,
2306 g_return_val_if_fail (G_IS_FILE (file), NULL);
2307 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2309 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2311 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2312 if (g_simple_async_result_propagate_error (simple, error))
2316 iface = G_FILE_GET_IFACE (file);
2317 return (* iface->create_readwrite_finish) (file, res, error);
2321 * g_file_replace_readwrite_async:
2322 * @file: input #GFile.
2323 * @etag: an <link linkend="gfile-etag">entity tag</link> for the
2324 * current #GFile, or NULL to ignore.
2325 * @make_backup: %TRUE if a backup should be created.
2326 * @flags: a set of #GFileCreateFlags.
2327 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2329 * @cancellable: optional #GCancellable object, %NULL to ignore.
2330 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2331 * @user_data: the data to pass to callback function
2333 * Asynchronously overwrites the file in read-write mode, replacing the
2334 * contents, possibly creating a backup copy of the file first.
2336 * For more details, see g_file_replace_readwrite() which is
2337 * the synchronous version of this call.
2339 * When the operation is finished, @callback will be called. You can then
2340 * call g_file_replace_readwrite_finish() to get the result of the operation.
2345 g_file_replace_readwrite_async (GFile *file,
2347 gboolean make_backup,
2348 GFileCreateFlags flags,
2350 GCancellable *cancellable,
2351 GAsyncReadyCallback callback,
2356 g_return_if_fail (G_IS_FILE (file));
2358 iface = G_FILE_GET_IFACE (file);
2359 (* iface->replace_readwrite_async) (file,
2370 * g_file_replace_readwrite_finish:
2371 * @file: input #GFile.
2372 * @res: a #GAsyncResult.
2373 * @error: a #GError, or %NULL
2375 * Finishes an asynchronous file replace operation started with
2376 * g_file_replace_readwrite_async().
2378 * Returns: a #GFileIOStream, or %NULL on error.
2379 * Free the returned object with g_object_unref().
2384 g_file_replace_readwrite_finish (GFile *file,
2390 g_return_val_if_fail (G_IS_FILE (file), NULL);
2391 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2393 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2395 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2396 if (g_simple_async_result_propagate_error (simple, error))
2400 iface = G_FILE_GET_IFACE (file);
2401 return (* iface->replace_readwrite_finish) (file, res, error);
2405 copy_symlink (GFile *destination,
2406 GFileCopyFlags flags,
2407 GCancellable *cancellable,
2412 gboolean tried_delete;
2414 GFileType file_type;
2416 tried_delete = FALSE;
2420 if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
2422 /* Maybe it already existed, and we want to overwrite? */
2423 if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) &&
2424 my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
2426 g_error_free (my_error);
2429 /* Don't overwrite if the destination is a directory */
2430 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2431 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2432 cancellable, &my_error);
2435 file_type = g_file_info_get_file_type (info);
2436 g_object_unref (info);
2438 if (file_type == G_FILE_TYPE_DIRECTORY)
2440 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
2441 _("Can't copy over directory"));
2446 if (!g_file_delete (destination, cancellable, error))
2449 tried_delete = TRUE;
2453 g_propagate_error (error, my_error);
2460 static GInputStream *
2461 open_source_for_copy (GFile *source,
2463 GFileCopyFlags flags,
2464 GCancellable *cancellable,
2470 GFileType file_type;
2473 in = (GInputStream *)g_file_read (source, cancellable, &my_error);
2477 /* There was an error opening the source, try to set a good error for it: */
2479 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
2481 /* The source is a directory, don't fail with WOULD_RECURSE immediately,
2482 * as that is less useful to the app. Better check for errors on the
2485 g_error_free (my_error);
2488 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2489 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2490 cancellable, &my_error);
2493 file_type = g_file_info_get_file_type (info);
2494 g_object_unref (info);
2496 if (flags & G_FILE_COPY_OVERWRITE)
2498 if (file_type == G_FILE_TYPE_DIRECTORY)
2500 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
2501 _("Can't copy directory over directory"));
2504 /* continue to would_recurse error */
2508 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
2509 _("Target file exists"));
2515 /* Error getting info from target, return that error
2516 * (except for NOT_FOUND, which is no error here)
2518 if (my_error->domain != G_IO_ERROR && my_error->code != G_IO_ERROR_NOT_FOUND)
2520 g_propagate_error (error, my_error);
2523 g_error_free (my_error);
2526 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
2527 _("Can't recursively copy directory"));
2531 g_propagate_error (error, my_error);
2536 should_copy (GFileAttributeInfo *info,
2538 gboolean skip_perms)
2540 if (skip_perms && strcmp(info->name, "unix::mode") == 0)
2544 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
2545 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
2549 build_attribute_list_for_copy (GFileAttributeInfoList *attributes,
2550 GFileAttributeInfoList *namespaces,
2552 gboolean skip_perms)
2559 s = g_string_new ("");
2563 for (i = 0; i < attributes->n_infos; i++)
2565 if (should_copy (&attributes->infos[i], as_move, skip_perms))
2570 g_string_append_c (s, ',');
2572 g_string_append (s, attributes->infos[i].name);
2579 for (i = 0; i < namespaces->n_infos; i++)
2581 if (should_copy (&namespaces->infos[i], as_move, FALSE))
2586 g_string_append_c (s, ',');
2588 g_string_append (s, namespaces->infos[i].name);
2589 g_string_append (s, "::*");
2594 return g_string_free (s, FALSE);
2598 * g_file_copy_attributes:
2599 * @source: a #GFile with attributes.
2600 * @destination: a #GFile to copy attributes to.
2601 * @flags: a set of #GFileCopyFlags.
2602 * @cancellable: optional #GCancellable object, %NULL to ignore.
2603 * @error: a #GError, %NULL to ignore.
2605 * Copies the file attributes from @source to @destination.
2607 * Normally only a subset of the file attributes are copied,
2608 * those that are copies in a normal file copy operation
2609 * (which for instance does not include e.g. owner). However
2610 * if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
2611 * all the metadata that is possible to copy is copied. This
2612 * is useful when implementing move by copy + delete source.
2614 * Returns: %TRUE if the attributes were copied successfully, %FALSE otherwise.
2617 g_file_copy_attributes (GFile *source,
2619 GFileCopyFlags flags,
2620 GCancellable *cancellable,
2623 GFileAttributeInfoList *attributes, *namespaces;
2624 char *attrs_to_read;
2628 gboolean source_nofollow_symlinks;
2629 gboolean skip_perms;
2631 as_move = flags & G_FILE_COPY_ALL_METADATA;
2632 source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
2633 skip_perms = (flags & G_FILE_COPY_TARGET_DEFAULT_PERMS) != 0;
2635 /* Ignore errors here, if the target supports no attributes there is nothing to copy */
2636 attributes = g_file_query_settable_attributes (destination, cancellable, NULL);
2637 namespaces = g_file_query_writable_namespaces (destination, cancellable, NULL);
2639 if (attributes == NULL && namespaces == NULL)
2642 attrs_to_read = build_attribute_list_for_copy (attributes, namespaces, as_move, skip_perms);
2644 /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
2645 * we just don't copy it.
2647 info = g_file_query_info (source, attrs_to_read,
2648 source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
2652 g_free (attrs_to_read);
2657 res = g_file_set_attributes_from_info (destination,
2659 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2662 g_object_unref (info);
2665 g_file_attribute_info_list_unref (attributes);
2666 g_file_attribute_info_list_unref (namespaces);
2671 /* Closes the streams */
2673 copy_stream_with_progress (GInputStream *in,
2676 GCancellable *cancellable,
2677 GFileProgressCallback progress_callback,
2678 gpointer progress_callback_data,
2681 gssize n_read, n_written;
2682 goffset current_size;
2683 char buffer[1024*64], *p;
2689 /* avoid performance impact of querying total size when it's not needed */
2690 if (progress_callback)
2692 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
2693 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2697 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2698 total_size = g_file_info_get_size (info);
2699 g_object_unref (info);
2702 if (total_size == -1)
2704 info = g_file_query_info (source,
2705 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2706 G_FILE_QUERY_INFO_NONE,
2710 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2711 total_size = g_file_info_get_size (info);
2712 g_object_unref (info);
2717 if (total_size == -1)
2724 n_read = g_input_stream_read (in, buffer, sizeof (buffer), cancellable, error);
2734 current_size += n_read;
2739 n_written = g_output_stream_write (out, p, n_read, cancellable, error);
2740 if (n_written == -1)
2747 n_read -= n_written;
2753 if (progress_callback)
2754 progress_callback (current_size, total_size, progress_callback_data);
2758 error = NULL; /* Ignore further errors */
2760 /* Make sure we send full copied size */
2761 if (progress_callback)
2762 progress_callback (current_size, total_size, progress_callback_data);
2764 /* Don't care about errors in source here */
2765 g_input_stream_close (in, cancellable, NULL);
2767 /* But write errors on close are bad! */
2768 if (!g_output_stream_close (out, cancellable, error))
2771 g_object_unref (in);
2772 g_object_unref (out);
2778 file_copy_fallback (GFile *source,
2780 GFileCopyFlags flags,
2781 GCancellable *cancellable,
2782 GFileProgressCallback progress_callback,
2783 gpointer progress_callback_data,
2791 /* need to know the file type */
2792 info = g_file_query_info (source,
2793 G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
2794 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2801 /* Maybe copy the symlink? */
2802 if ((flags & G_FILE_COPY_NOFOLLOW_SYMLINKS) &&
2803 g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK)
2805 target = g_file_info_get_symlink_target (info);
2808 if (!copy_symlink (destination, flags, cancellable, target, error))
2810 g_object_unref (info);
2814 g_object_unref (info);
2817 /* ... else fall back on a regular file copy */
2818 g_object_unref (info);
2820 /* Handle "special" files (pipes, device nodes, ...)? */
2821 else if (g_file_info_get_file_type (info) == G_FILE_TYPE_SPECIAL)
2823 /* FIXME: could try to recreate device nodes and others? */
2825 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2826 _("Can't copy special file"));
2827 g_object_unref (info);
2830 /* Everything else should just fall back on a regular copy. */
2832 g_object_unref (info);
2834 in = open_source_for_copy (source, destination, flags, cancellable, error);
2838 if (flags & G_FILE_COPY_OVERWRITE)
2840 out = (GOutputStream *)g_file_replace (destination,
2842 flags & G_FILE_COPY_BACKUP,
2843 G_FILE_CREATE_REPLACE_DESTINATION,
2844 cancellable, error);
2848 out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
2853 g_object_unref (in);
2857 if (!copy_stream_with_progress (in, out, source, cancellable,
2858 progress_callback, progress_callback_data,
2864 /* Ignore errors here. Failure to copy metadata is not a hard error */
2865 g_file_copy_attributes (source, destination,
2866 flags, cancellable, NULL);
2873 * @source: input #GFile.
2874 * @destination: destination #GFile
2875 * @flags: set of #GFileCopyFlags
2876 * @cancellable: optional #GCancellable object, %NULL to ignore.
2877 * @progress_callback: function to callback with progress information
2878 * @progress_callback_data: user data to pass to @progress_callback
2879 * @error: #GError to set on error, or %NULL
2881 * Copies the file @source to the location specified by @destination.
2882 * Can not handle recursive copies of directories.
2884 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2885 * existing @destination file is overwritten.
2887 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2888 * will be copied as symlinks, otherwise the target of the
2889 * @source symlink will be copied.
2891 * If @cancellable is not %NULL, then the operation can be cancelled by
2892 * triggering the cancellable object from another thread. If the operation
2893 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2895 * If @progress_callback is not %NULL, then the operation can be monitored by
2896 * setting this to a #GFileProgressCallback function. @progress_callback_data
2897 * will be passed to this function. It is guaranteed that this callback will
2898 * be called after all data has been transferred with the total number of bytes
2899 * copied during the operation.
2901 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2902 * error is returned, independent on the status of the @destination.
2904 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2905 * error G_IO_ERROR_EXISTS is returned.
2907 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2908 * error is returned. If trying to overwrite a directory with a directory the
2909 * G_IO_ERROR_WOULD_MERGE error is returned.
2911 * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2912 * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2915 * If you are interested in copying the #GFile object itself (not the on-disk
2916 * file), see g_file_dup().
2918 * Returns: %TRUE on success, %FALSE otherwise.
2921 g_file_copy (GFile *source,
2923 GFileCopyFlags flags,
2924 GCancellable *cancellable,
2925 GFileProgressCallback progress_callback,
2926 gpointer progress_callback_data,
2933 g_return_val_if_fail (G_IS_FILE (source), FALSE);
2934 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2936 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2939 iface = G_FILE_GET_IFACE (destination);
2943 res = (* iface->copy) (source, destination,
2945 progress_callback, progress_callback_data,
2951 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2953 g_propagate_error (error, my_error);
2957 g_clear_error (&my_error);
2960 /* If the types are different, and the destination method failed
2961 also try the source method */
2962 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
2964 iface = G_FILE_GET_IFACE (source);
2969 res = (* iface->copy) (source, destination,
2971 progress_callback, progress_callback_data,
2977 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2979 g_propagate_error (error, my_error);
2983 g_clear_error (&my_error);
2987 return file_copy_fallback (source, destination, flags, cancellable,
2988 progress_callback, progress_callback_data,
2993 * g_file_copy_async:
2994 * @source: input #GFile.
2995 * @destination: destination #GFile
2996 * @flags: set of #GFileCopyFlags
2997 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2999 * @cancellable: optional #GCancellable object, %NULL to ignore.
3000 * @progress_callback: function to callback with progress information
3001 * @progress_callback_data: user data to pass to @progress_callback
3002 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
3003 * @user_data: the data to pass to callback function
3005 * Copies the file @source to the location specified by @destination
3006 * asynchronously. For details of the behaviour, see g_file_copy().
3008 * If @progress_callback is not %NULL, then that function that will be called
3009 * just like in g_file_copy(), however the callback will run in the main loop,
3010 * not in the thread that is doing the I/O operation.
3012 * When the operation is finished, @callback will be called. You can then call
3013 * g_file_copy_finish() to get the result of the operation.
3016 g_file_copy_async (GFile *source,
3018 GFileCopyFlags flags,
3020 GCancellable *cancellable,
3021 GFileProgressCallback progress_callback,
3022 gpointer progress_callback_data,
3023 GAsyncReadyCallback callback,
3028 g_return_if_fail (G_IS_FILE (source));
3029 g_return_if_fail (G_IS_FILE (destination));
3031 iface = G_FILE_GET_IFACE (source);
3032 (* iface->copy_async) (source,
3038 progress_callback_data,
3044 * g_file_copy_finish:
3045 * @file: input #GFile.
3046 * @res: a #GAsyncResult.
3047 * @error: a #GError, or %NULL
3049 * Finishes copying the file started with
3050 * g_file_copy_async().
3052 * Returns: a %TRUE on success, %FALSE on error.
3055 g_file_copy_finish (GFile *file,
3061 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3062 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
3064 if (G_IS_SIMPLE_ASYNC_RESULT (res))
3066 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3068 if (g_simple_async_result_propagate_error (simple, error))
3072 iface = G_FILE_GET_IFACE (file);
3073 return (* iface->copy_finish) (file, res, error);
3078 * @source: #GFile pointing to the source location.
3079 * @destination: #GFile pointing to the destination location.
3080 * @flags: set of #GFileCopyFlags.
3081 * @cancellable: optional #GCancellable object, %NULL to ignore.
3082 * @progress_callback: #GFileProgressCallback function for updates.
3083 * @progress_callback_data: gpointer to user data for the callback function.
3084 * @error: #GError for returning error conditions, or %NULL
3087 * Tries to move the file or directory @source to the location specified by @destination.
3088 * If native move operations are supported then this is used, otherwise a copy + delete
3089 * fallback is used. The native implementation may support moving directories (for instance
3090 * on moves inside the same filesystem), but the fallback code does not.
3092 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3093 * existing @destination file is overwritten.
3095 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3096 * will be copied as symlinks, otherwise the target of the
3097 * @source symlink will be copied.
3099 * If @cancellable is not %NULL, then the operation can be cancelled by
3100 * triggering the cancellable object from another thread. If the operation
3101 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3103 * If @progress_callback is not %NULL, then the operation can be monitored by
3104 * setting this to a #GFileProgressCallback function. @progress_callback_data
3105 * will be passed to this function. It is guaranteed that this callback will
3106 * be called after all data has been transferred with the total number of bytes
3107 * copied during the operation.
3109 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
3110 * error is returned, independent on the status of the @destination.
3112 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
3113 * error G_IO_ERROR_EXISTS is returned.
3115 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
3116 * error is returned. If trying to overwrite a directory with a directory the
3117 * G_IO_ERROR_WOULD_MERGE error is returned.
3119 * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
3120 * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
3121 * may be returned (if the native move operation isn't available).
3123 * Returns: %TRUE on successful move, %FALSE otherwise.
3126 g_file_move (GFile *source,
3128 GFileCopyFlags flags,
3129 GCancellable *cancellable,
3130 GFileProgressCallback progress_callback,
3131 gpointer progress_callback_data,
3138 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3139 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3141 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3144 iface = G_FILE_GET_IFACE (destination);
3148 res = (* iface->move) (source, destination,
3150 progress_callback, progress_callback_data,
3156 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3158 g_propagate_error (error, my_error);
3163 /* If the types are different, and the destination method failed
3164 also try the source method */
3165 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3167 iface = G_FILE_GET_IFACE (source);
3172 res = (* iface->move) (source, destination,
3174 progress_callback, progress_callback_data,
3180 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3182 g_propagate_error (error, my_error);
3188 if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
3190 g_set_error_literal (error, G_IO_ERROR,
3191 G_IO_ERROR_NOT_SUPPORTED,
3192 _("Operation not supported"));
3196 flags |= G_FILE_COPY_ALL_METADATA;
3197 if (!g_file_copy (source, destination, flags, cancellable,
3198 progress_callback, progress_callback_data,
3202 return g_file_delete (source, cancellable, error);
3206 * g_file_make_directory
3207 * @file: input #GFile.
3208 * @cancellable: optional #GCancellable object, %NULL to ignore.
3209 * @error: a #GError, or %NULL
3211 * Creates a directory. Note that this will only create a child directory of
3212 * the immediate parent directory of the path or URI given by the #GFile. To
3213 * recursively create directories, see g_file_make_directory_with_parents().
3214 * This function will fail if the parent directory does not exist, setting
3215 * @error to %G_IO_ERROR_NOT_FOUND. If the file system doesn't support creating
3216 * directories, this function will fail, setting @error to
3217 * %G_IO_ERROR_NOT_SUPPORTED.
3219 * For a local #GFile the newly created directory will have the default
3220 * (current) ownership and permissions of the current process.
3222 * If @cancellable is not %NULL, then the operation can be cancelled by
3223 * triggering the cancellable object from another thread. If the operation
3224 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3226 * Returns: %TRUE on successful creation, %FALSE otherwise.
3229 g_file_make_directory (GFile *file,
3230 GCancellable *cancellable,
3235 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3237 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3240 iface = G_FILE_GET_IFACE (file);
3242 if (iface->make_directory == NULL)
3244 g_set_error_literal (error, G_IO_ERROR,
3245 G_IO_ERROR_NOT_SUPPORTED,
3246 _("Operation not supported"));
3250 return (* iface->make_directory) (file, cancellable, error);
3254 * g_file_make_directory_with_parents:
3255 * @file: input #GFile.
3256 * @cancellable: optional #GCancellable object, %NULL to ignore.
3257 * @error: a #GError, or %NULL
3259 * Creates a directory and any parent directories that may not exist similar to
3260 * 'mkdir -p'. If the file system does not support creating directories, this
3261 * function will fail, setting @error to %G_IO_ERROR_NOT_SUPPORTED.
3263 * For a local #GFile the newly created directories will have the default
3264 * (current) ownership and permissions of the current process.
3266 * If @cancellable is not %NULL, then the operation can be cancelled by
3267 * triggering the cancellable object from another thread. If the operation
3268 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3270 * Returns: %TRUE if all directories have been successfully created, %FALSE
3276 g_file_make_directory_with_parents (GFile *file,
3277 GCancellable *cancellable,
3281 GFile *parent_file, *work_file;
3282 GList *list = NULL, *l;
3283 GError *my_error = NULL;
3285 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3288 result = g_file_make_directory (file, cancellable, &my_error);
3289 if (result || my_error->code != G_IO_ERROR_NOT_FOUND)
3292 g_propagate_error (error, my_error);
3298 while (!result && my_error->code == G_IO_ERROR_NOT_FOUND)
3300 g_clear_error (&my_error);
3302 parent_file = g_file_get_parent (work_file);
3303 if (parent_file == NULL)
3305 result = g_file_make_directory (parent_file, cancellable, &my_error);
3307 if (!result && my_error->code == G_IO_ERROR_NOT_FOUND)
3308 list = g_list_prepend (list, parent_file);
3310 work_file = parent_file;
3313 for (l = list; result && l; l = l->next)
3315 result = g_file_make_directory ((GFile *) l->data, cancellable, &my_error);
3319 while (list != NULL)
3321 g_object_unref ((GFile *) list->data);
3322 list = g_list_remove (list, list->data);
3327 g_propagate_error (error, my_error);
3331 return g_file_make_directory (file, cancellable, error);
3335 * g_file_make_symbolic_link:
3336 * @file: input #GFile.
3337 * @symlink_value: a string with the value of the new symlink.
3338 * @cancellable: optional #GCancellable object, %NULL to ignore.
3339 * @error: a #GError.
3341 * Creates a symbolic link.
3343 * If @cancellable is not %NULL, then the operation can be cancelled by
3344 * triggering the cancellable object from another thread. If the operation
3345 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3347 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
3350 g_file_make_symbolic_link (GFile *file,
3351 const char *symlink_value,
3352 GCancellable *cancellable,
3357 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3358 g_return_val_if_fail (symlink_value != NULL, FALSE);
3360 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3363 if (*symlink_value == '\0')
3365 g_set_error_literal (error, G_IO_ERROR,
3366 G_IO_ERROR_INVALID_ARGUMENT,
3367 _("Invalid symlink value given"));
3371 iface = G_FILE_GET_IFACE (file);
3373 if (iface->make_symbolic_link == NULL)
3375 g_set_error_literal (error, G_IO_ERROR,
3376 G_IO_ERROR_NOT_SUPPORTED,
3377 _("Operation not supported"));
3381 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
3386 * @file: input #GFile.
3387 * @cancellable: optional #GCancellable object, %NULL to ignore.
3388 * @error: a #GError, or %NULL
3390 * Deletes a file. If the @file is a directory, it will only be deleted if it
3393 * If @cancellable is not %NULL, then the operation can be cancelled by
3394 * triggering the cancellable object from another thread. If the operation
3395 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3397 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
3400 g_file_delete (GFile *file,
3401 GCancellable *cancellable,
3406 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3408 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3411 iface = G_FILE_GET_IFACE (file);
3413 if (iface->delete_file == NULL)
3415 g_set_error_literal (error, G_IO_ERROR,
3416 G_IO_ERROR_NOT_SUPPORTED,
3417 _("Operation not supported"));
3421 return (* iface->delete_file) (file, cancellable, error);
3426 * @file: #GFile to send to trash.
3427 * @cancellable: optional #GCancellable object, %NULL to ignore.
3428 * @error: a #GError, or %NULL
3430 * Sends @file to the "Trashcan", if possible. This is similar to
3431 * deleting it, but the user can recover it before emptying the trashcan.
3432 * Not all file systems support trashing, so this call can return the
3433 * %G_IO_ERROR_NOT_SUPPORTED error.
3436 * If @cancellable is not %NULL, then the operation can be cancelled by
3437 * triggering the cancellable object from another thread. If the operation
3438 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3440 * Returns: %TRUE on successful trash, %FALSE otherwise.
3443 g_file_trash (GFile *file,
3444 GCancellable *cancellable,
3449 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3451 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3454 iface = G_FILE_GET_IFACE (file);
3456 if (iface->trash == NULL)
3458 g_set_error_literal (error,
3459 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
3460 _("Trash not supported"));
3464 return (* iface->trash) (file, cancellable, error);
3468 * g_file_set_display_name:
3469 * @file: input #GFile.
3470 * @display_name: a string.
3471 * @cancellable: optional #GCancellable object, %NULL to ignore.
3472 * @error: a #GError, or %NULL
3474 * Renames @file to the specified display name.
3476 * The display name is converted from UTF8 to the correct encoding for the target
3477 * filesystem if possible and the @file is renamed to this.
3479 * If you want to implement a rename operation in the user interface the edit name
3480 * (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the initial value in the rename
3481 * widget, and then the result after editing should be passed to g_file_set_display_name().
3483 * On success the resulting converted filename is returned.
3485 * If @cancellable is not %NULL, then the operation can be cancelled by
3486 * triggering the cancellable object from another thread. If the operation
3487 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3489 * Returns: a #GFile specifying what @file was renamed to, or %NULL
3490 * if there was an error.
3491 * Free the returned object with g_object_unref().
3494 g_file_set_display_name (GFile *file,
3495 const char *display_name,
3496 GCancellable *cancellable,
3501 g_return_val_if_fail (G_IS_FILE (file), NULL);
3502 g_return_val_if_fail (display_name != NULL, NULL);
3504 if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
3508 G_IO_ERROR_INVALID_ARGUMENT,
3509 _("File names cannot contain '%c'"), G_DIR_SEPARATOR);
3513 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3516 iface = G_FILE_GET_IFACE (file);
3518 return (* iface->set_display_name) (file, display_name, cancellable, error);
3522 * g_file_set_display_name_async:
3523 * @file: input #GFile.
3524 * @display_name: a string.
3525 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3527 * @cancellable: optional #GCancellable object, %NULL to ignore.
3528 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
3529 * @user_data: the data to pass to callback function
3531 * Asynchronously sets the display name for a given #GFile.
3533 * For more details, see g_file_set_display_name() which is
3534 * the synchronous version of this call.
3536 * When the operation is finished, @callback will be called. You can then call
3537 * g_file_set_display_name_finish() to get the result of the operation.
3540 g_file_set_display_name_async (GFile *file,
3541 const char *display_name,
3543 GCancellable *cancellable,
3544 GAsyncReadyCallback callback,
3549 g_return_if_fail (G_IS_FILE (file));
3550 g_return_if_fail (display_name != NULL);
3552 iface = G_FILE_GET_IFACE (file);
3553 (* iface->set_display_name_async) (file,
3562 * g_file_set_display_name_finish:
3563 * @file: input #GFile.
3564 * @res: a #GAsyncResult.
3565 * @error: a #GError, or %NULL
3567 * Finishes setting a display name started with
3568 * g_file_set_display_name_async().
3570 * Returns: a #GFile or %NULL on error.
3571 * Free the returned object with g_object_unref().
3574 g_file_set_display_name_finish (GFile *file,
3580 g_return_val_if_fail (G_IS_FILE (file), NULL);
3581 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
3583 if (G_IS_SIMPLE_ASYNC_RESULT (res))
3585 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3586 if (g_simple_async_result_propagate_error (simple, error))
3590 iface = G_FILE_GET_IFACE (file);
3591 return (* iface->set_display_name_finish) (file, res, error);
3595 * g_file_query_settable_attributes:
3596 * @file: input #GFile.
3597 * @cancellable: optional #GCancellable object, %NULL to ignore.
3598 * @error: a #GError, or %NULL
3600 * Obtain the list of settable attributes for the file.
3602 * Returns the type and full attribute name of all the attributes
3603 * that can be set on this file. This doesn't mean setting it will always
3604 * succeed though, you might get an access failure, or some specific
3605 * file may not support a specific attribute.
3607 * If @cancellable is not %NULL, then the operation can be cancelled by
3608 * triggering the cancellable object from another thread. If the operation
3609 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3611 * Returns: a #GFileAttributeInfoList describing the settable attributes.
3612 * When you are done with it, release it with g_file_attribute_info_list_unref()
3614 GFileAttributeInfoList *
3615 g_file_query_settable_attributes (GFile *file,
3616 GCancellable *cancellable,
3621 GFileAttributeInfoList *list;
3623 g_return_val_if_fail (G_IS_FILE (file), NULL);
3625 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3628 iface = G_FILE_GET_IFACE (file);
3630 if (iface->query_settable_attributes == NULL)
3631 return g_file_attribute_info_list_new ();
3634 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
3638 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
3640 list = g_file_attribute_info_list_new ();
3641 g_error_free (my_error);
3644 g_propagate_error (error, my_error);
3651 * g_file_query_writable_namespaces:
3652 * @file: input #GFile.
3653 * @cancellable: optional #GCancellable object, %NULL to ignore.
3654 * @error: a #GError, or %NULL
3656 * Obtain the list of attribute namespaces where new attributes
3657 * can be created by a user. An example of this is extended
3658 * attributes (in the "xattr" namespace).
3660 * If @cancellable is not %NULL, then the operation can be cancelled by
3661 * triggering the cancellable object from another thread. If the operation
3662 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3664 * Returns: a #GFileAttributeInfoList describing the writable namespaces.
3665 * When you are done with it, release it with g_file_attribute_info_list_unref()
3667 GFileAttributeInfoList *
3668 g_file_query_writable_namespaces (GFile *file,
3669 GCancellable *cancellable,
3674 GFileAttributeInfoList *list;
3676 g_return_val_if_fail (G_IS_FILE (file), NULL);
3678 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3681 iface = G_FILE_GET_IFACE (file);
3683 if (iface->query_writable_namespaces == NULL)
3684 return g_file_attribute_info_list_new ();
3687 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
3691 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
3693 list = g_file_attribute_info_list_new ();
3694 g_error_free (my_error);
3697 g_propagate_error (error, my_error);
3704 * g_file_set_attribute:
3705 * @file: input #GFile.
3706 * @attribute: a string containing the attribute's name.
3707 * @type: The type of the attribute
3708 * @value_p: a pointer to the value (or the pointer itself if the type is a pointer type)
3709 * @flags: a set of #GFileQueryInfoFlags.
3710 * @cancellable: optional #GCancellable object, %NULL to ignore.
3711 * @error: a #GError, or %NULL
3713 * Sets an attribute in the file with attribute name @attribute to @value.
3715 * If @cancellable is not %NULL, then the operation can be cancelled by
3716 * triggering the cancellable object from another thread. If the operation
3717 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3719 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
3722 g_file_set_attribute (GFile *file,
3723 const char *attribute,
3724 GFileAttributeType type,
3726 GFileQueryInfoFlags flags,
3727 GCancellable *cancellable,
3732 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3733 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
3735 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3738 iface = G_FILE_GET_IFACE (file);
3740 if (iface->set_attribute == NULL)
3742 g_set_error_literal (error, G_IO_ERROR,
3743 G_IO_ERROR_NOT_SUPPORTED,
3744 _("Operation not supported"));
3748 return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
3752 * g_file_set_attributes_from_info:
3753 * @file: input #GFile.
3754 * @info: a #GFileInfo.
3755 * @flags: #GFileQueryInfoFlags
3756 * @cancellable: optional #GCancellable object, %NULL to ignore.
3757 * @error: a #GError, or %NULL
3759 * Tries to set all attributes in the #GFileInfo on the target values,
3760 * not stopping on the first error.
3762 * If there is any error during this operation then @error will be set to
3763 * the first error. Error on particular fields are flagged by setting
3764 * the "status" field in the attribute value to
3765 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can also detect
3768 * If @cancellable is not %NULL, then the operation can be cancelled by
3769 * triggering the cancellable object from another thread. If the operation
3770 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3772 * Returns: %TRUE if there was any error, %FALSE otherwise.
3775 g_file_set_attributes_from_info (GFile *file,
3777 GFileQueryInfoFlags flags,
3778 GCancellable *cancellable,
3783 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3784 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
3786 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3789 g_file_info_clear_status (info);
3791 iface = G_FILE_GET_IFACE (file);
3793 return (* iface->set_attributes_from_info) (file,
3802 g_file_real_set_attributes_from_info (GFile *file,
3804 GFileQueryInfoFlags flags,
3805 GCancellable *cancellable,
3811 GFileAttributeValue *value;
3815 attributes = g_file_info_list_attributes (info, NULL);
3817 for (i = 0; attributes[i] != NULL; i++)
3819 value = _g_file_info_get_attribute_value (info, attributes[i]);
3821 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
3824 if (!g_file_set_attribute (file, attributes[i],
3825 value->type, _g_file_attribute_value_peek_as_pointer (value),
3826 flags, cancellable, error))
3828 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
3830 /* Don't set error multiple times */
3834 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
3837 g_strfreev (attributes);
3843 * g_file_set_attributes_async:
3844 * @file: input #GFile.
3845 * @info: a #GFileInfo.
3846 * @flags: a #GFileQueryInfoFlags.
3847 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3849 * @cancellable: optional #GCancellable object, %NULL to ignore.
3850 * @callback: a #GAsyncReadyCallback.
3851 * @user_data: a #gpointer.
3853 * Asynchronously sets the attributes of @file with @info.
3855 * For more details, see g_file_set_attributes_from_info() which is
3856 * the synchronous version of this call.
3858 * When the operation is finished, @callback will be called. You can then call
3859 * g_file_set_attributes_finish() to get the result of the operation.
3862 g_file_set_attributes_async (GFile *file,
3864 GFileQueryInfoFlags flags,
3866 GCancellable *cancellable,
3867 GAsyncReadyCallback callback,
3872 g_return_if_fail (G_IS_FILE (file));
3873 g_return_if_fail (G_IS_FILE_INFO (info));
3875 iface = G_FILE_GET_IFACE (file);
3876 (* iface->set_attributes_async) (file,
3886 * g_file_set_attributes_finish:
3887 * @file: input #GFile.
3888 * @result: a #GAsyncResult.
3889 * @info: a #GFileInfo.
3890 * @error: a #GError, or %NULL
3892 * Finishes setting an attribute started in g_file_set_attributes_async().
3894 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
3897 g_file_set_attributes_finish (GFile *file,
3898 GAsyncResult *result,
3904 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3905 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3907 /* No standard handling of errors here, as we must set info even
3910 iface = G_FILE_GET_IFACE (file);
3911 return (* iface->set_attributes_finish) (file, result, info, error);
3915 * g_file_set_attribute_string:
3916 * @file: input #GFile.
3917 * @attribute: a string containing the attribute's name.
3918 * @value: a string containing the attribute's value.
3919 * @flags: #GFileQueryInfoFlags.
3920 * @cancellable: optional #GCancellable object, %NULL to ignore.
3921 * @error: a #GError, or %NULL
3923 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
3924 * If @attribute is of a different type, this operation will fail.
3926 * If @cancellable is not %NULL, then the operation can be cancelled by
3927 * triggering the cancellable object from another thread. If the operation
3928 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3930 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
3933 g_file_set_attribute_string (GFile *file,
3934 const char *attribute,
3936 GFileQueryInfoFlags flags,
3937 GCancellable *cancellable,
3940 return g_file_set_attribute (file, attribute,
3941 G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
3942 flags, cancellable, error);
3946 * g_file_set_attribute_byte_string:
3947 * @file: input #GFile.
3948 * @attribute: a string containing the attribute's name.
3949 * @value: a string containing the attribute's new value.
3950 * @flags: a #GFileQueryInfoFlags.
3951 * @cancellable: optional #GCancellable object, %NULL to ignore.
3952 * @error: a #GError, or %NULL
3954 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
3955 * If @attribute is of a different type, this operation will fail,
3958 * If @cancellable is not %NULL, then the operation can be cancelled by
3959 * triggering the cancellable object from another thread. If the operation
3960 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3962 * Returns: %TRUE if the @attribute was successfully set to @value
3963 * in the @file, %FALSE otherwise.
3966 g_file_set_attribute_byte_string (GFile *file,
3967 const char *attribute,
3969 GFileQueryInfoFlags flags,
3970 GCancellable *cancellable,
3973 return g_file_set_attribute (file, attribute,
3974 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
3975 flags, cancellable, error);
3979 * g_file_set_attribute_uint32:
3980 * @file: input #GFile.
3981 * @attribute: a string containing the attribute's name.
3982 * @value: a #guint32 containing the attribute's new value.
3983 * @flags: a #GFileQueryInfoFlags.
3984 * @cancellable: optional #GCancellable object, %NULL to ignore.
3985 * @error: a #GError, or %NULL
3987 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
3988 * If @attribute is of a different type, this operation will fail.
3990 * If @cancellable is not %NULL, then the operation can be cancelled by
3991 * triggering the cancellable object from another thread. If the operation
3992 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3994 * Returns: %TRUE if the @attribute was successfully set to @value
3995 * in the @file, %FALSE otherwise.
3998 g_file_set_attribute_uint32 (GFile *file,
3999 const char *attribute,
4001 GFileQueryInfoFlags flags,
4002 GCancellable *cancellable,
4005 return g_file_set_attribute (file, attribute,
4006 G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
4007 flags, cancellable, error);
4011 * g_file_set_attribute_int32:
4012 * @file: input #GFile.
4013 * @attribute: a string containing the attribute's name.
4014 * @value: a #gint32 containing the attribute's new value.
4015 * @flags: a #GFileQueryInfoFlags.
4016 * @cancellable: optional #GCancellable object, %NULL to ignore.
4017 * @error: a #GError, or %NULL
4019 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
4020 * If @attribute is of a different type, this operation will fail.
4022 * If @cancellable is not %NULL, then the operation can be cancelled by
4023 * triggering the cancellable object from another thread. If the operation
4024 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4026 * Returns: %TRUE if the @attribute was successfully set to @value
4027 * in the @file, %FALSE otherwise.
4030 g_file_set_attribute_int32 (GFile *file,
4031 const char *attribute,
4033 GFileQueryInfoFlags flags,
4034 GCancellable *cancellable,
4037 return g_file_set_attribute (file, attribute,
4038 G_FILE_ATTRIBUTE_TYPE_INT32, &value,
4039 flags, cancellable, error);
4043 * g_file_set_attribute_uint64:
4044 * @file: input #GFile.
4045 * @attribute: a string containing the attribute's name.
4046 * @value: a #guint64 containing the attribute's new value.
4047 * @flags: a #GFileQueryInfoFlags.
4048 * @cancellable: optional #GCancellable object, %NULL to ignore.
4049 * @error: a #GError, or %NULL
4051 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
4052 * If @attribute is of a different type, this operation will fail.
4054 * If @cancellable is not %NULL, then the operation can be cancelled by
4055 * triggering the cancellable object from another thread. If the operation
4056 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4058 * Returns: %TRUE if the @attribute was successfully set to @value
4059 * in the @file, %FALSE otherwise.
4062 g_file_set_attribute_uint64 (GFile *file,
4063 const char *attribute,
4065 GFileQueryInfoFlags flags,
4066 GCancellable *cancellable,
4069 return g_file_set_attribute (file, attribute,
4070 G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
4071 flags, cancellable, error);
4075 * g_file_set_attribute_int64:
4076 * @file: input #GFile.
4077 * @attribute: a string containing the attribute's name.
4078 * @value: a #guint64 containing the attribute's new value.
4079 * @flags: a #GFileQueryInfoFlags.
4080 * @cancellable: optional #GCancellable object, %NULL to ignore.
4081 * @error: a #GError, or %NULL
4083 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
4084 * If @attribute is of a different type, this operation will fail.
4086 * If @cancellable is not %NULL, then the operation can be cancelled by
4087 * triggering the cancellable object from another thread. If the operation
4088 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4090 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4093 g_file_set_attribute_int64 (GFile *file,
4094 const char *attribute,
4096 GFileQueryInfoFlags flags,
4097 GCancellable *cancellable,
4100 return g_file_set_attribute (file, attribute,
4101 G_FILE_ATTRIBUTE_TYPE_INT64, &value,
4102 flags, cancellable, error);
4106 * g_file_mount_mountable:
4107 * @file: input #GFile.
4108 * @flags: flags affecting the operation
4109 * @mount_operation: a #GMountOperation, or %NULL to avoid user interaction.
4110 * @cancellable: optional #GCancellable object, %NULL to ignore.
4111 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4112 * @user_data: the data to pass to callback function
4114 * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
4115 * Using @mount_operation, you can request callbacks when, for instance,
4116 * passwords are needed during authentication.
4118 * If @cancellable is not %NULL, then the operation can be cancelled by
4119 * triggering the cancellable object from another thread. If the operation
4120 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4122 * When the operation is finished, @callback will be called. You can then call
4123 * g_file_mount_mountable_finish() to get the result of the operation.
4126 g_file_mount_mountable (GFile *file,
4127 GMountMountFlags flags,
4128 GMountOperation *mount_operation,
4129 GCancellable *cancellable,
4130 GAsyncReadyCallback callback,
4135 g_return_if_fail (G_IS_FILE (file));
4137 iface = G_FILE_GET_IFACE (file);
4139 if (iface->mount_mountable == NULL)
4141 g_simple_async_report_error_in_idle (G_OBJECT (file),
4145 G_IO_ERROR_NOT_SUPPORTED,
4146 _("Operation not supported"));
4150 (* iface->mount_mountable) (file,
4159 * g_file_mount_mountable_finish:
4160 * @file: input #GFile.
4161 * @result: a #GAsyncResult.
4162 * @error: a #GError, or %NULL
4164 * Finishes a mount operation. See g_file_mount_mountable() for details.
4166 * Finish an asynchronous mount operation that was started
4167 * with g_file_mount_mountable().
4169 * Returns: a #GFile or %NULL on error.
4170 * Free the returned object with g_object_unref().
4173 g_file_mount_mountable_finish (GFile *file,
4174 GAsyncResult *result,
4179 g_return_val_if_fail (G_IS_FILE (file), NULL);
4180 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
4182 if (G_IS_SIMPLE_ASYNC_RESULT (result))
4184 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
4185 if (g_simple_async_result_propagate_error (simple, error))
4189 iface = G_FILE_GET_IFACE (file);
4190 return (* iface->mount_mountable_finish) (file, result, error);
4194 * g_file_unmount_mountable:
4195 * @file: input #GFile.
4196 * @flags: flags affecting the operation
4197 * @cancellable: optional #GCancellable object, %NULL to ignore.
4198 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4199 * @user_data: the data to pass to callback function
4201 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
4203 * If @cancellable is not %NULL, then the operation can be cancelled by
4204 * triggering the cancellable object from another thread. If the operation
4205 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4207 * When the operation is finished, @callback will be called. You can then call
4208 * g_file_unmount_mountable_finish() to get the result of the operation.
4210 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation() instead.
4213 g_file_unmount_mountable (GFile *file,
4214 GMountUnmountFlags flags,
4215 GCancellable *cancellable,
4216 GAsyncReadyCallback callback,
4221 g_return_if_fail (G_IS_FILE (file));
4223 iface = G_FILE_GET_IFACE (file);
4225 if (iface->unmount_mountable == NULL)
4227 g_simple_async_report_error_in_idle (G_OBJECT (file),
4231 G_IO_ERROR_NOT_SUPPORTED,
4232 _("Operation not supported"));
4236 (* iface->unmount_mountable) (file,
4244 * g_file_unmount_mountable_finish:
4245 * @file: input #GFile.
4246 * @result: a #GAsyncResult.
4247 * @error: a #GError, or %NULL
4249 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
4251 * Finish an asynchronous unmount operation that was started
4252 * with g_file_unmount_mountable().
4254 * Returns: %TRUE if the operation finished successfully. %FALSE
4257 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation_finish() instead.
4260 g_file_unmount_mountable_finish (GFile *file,
4261 GAsyncResult *result,
4266 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4267 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4269 if (G_IS_SIMPLE_ASYNC_RESULT (result))
4271 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
4272 if (g_simple_async_result_propagate_error (simple, error))
4276 iface = G_FILE_GET_IFACE (file);
4277 return (* iface->unmount_mountable_finish) (file, result, error);
4281 * g_file_unmount_mountable_with_operation:
4282 * @file: input #GFile.
4283 * @flags: flags affecting the operation
4284 * @mount_operation: a #GMountOperation, or %NULL to avoid user interaction.
4285 * @cancellable: optional #GCancellable object, %NULL to ignore.
4286 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4287 * @user_data: the data to pass to callback function
4289 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
4291 * If @cancellable is not %NULL, then the operation can be cancelled by
4292 * triggering the cancellable object from another thread. If the operation
4293 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4295 * When the operation is finished, @callback will be called. You can then call
4296 * g_file_unmount_mountable_finish() to get the result of the operation.
4301 g_file_unmount_mountable_with_operation (GFile *file,
4302 GMountUnmountFlags flags,
4303 GMountOperation *mount_operation,
4304 GCancellable *cancellable,
4305 GAsyncReadyCallback callback,
4310 g_return_if_fail (G_IS_FILE (file));
4312 iface = G_FILE_GET_IFACE (file);
4314 if (iface->unmount_mountable == NULL && iface->unmount_mountable_with_operation == NULL)
4316 g_simple_async_report_error_in_idle (G_OBJECT (file),
4320 G_IO_ERROR_NOT_SUPPORTED,
4321 _("Operation not supported"));
4325 if (iface->unmount_mountable_with_operation != NULL)
4326 (* iface->unmount_mountable_with_operation) (file,
4333 (* iface->unmount_mountable) (file,
4341 * g_file_unmount_mountable_with_operation_finish:
4342 * @file: input #GFile.
4343 * @result: a #GAsyncResult.
4344 * @error: a #GError, or %NULL
4346 * Finishes an unmount operation, see g_file_unmount_mountable_with_operation() for details.
4348 * Finish an asynchronous unmount operation that was started
4349 * with g_file_unmount_mountable_with_operation().
4351 * Returns: %TRUE if the operation finished successfully. %FALSE
4357 g_file_unmount_mountable_with_operation_finish (GFile *file,
4358 GAsyncResult *result,
4363 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4364 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4366 if (G_IS_SIMPLE_ASYNC_RESULT (result))
4368 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
4369 if (g_simple_async_result_propagate_error (simple, error))
4373 iface = G_FILE_GET_IFACE (file);
4374 if (iface->unmount_mountable_with_operation_finish != NULL)
4375 return (* iface->unmount_mountable_with_operation_finish) (file, result, error);
4377 return (* iface->unmount_mountable_finish) (file, result, error);
4381 * g_file_eject_mountable:
4382 * @file: input #GFile.
4383 * @flags: flags affecting the operation
4384 * @cancellable: optional #GCancellable object, %NULL to ignore.
4385 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4386 * @user_data: the data to pass to callback function
4388 * Starts an asynchronous eject on a mountable.
4389 * When this operation has completed, @callback will be called with
4390 * @user_user data, and the operation can be finalized with
4391 * g_file_eject_mountable_finish().
4393 * If @cancellable is not %NULL, then the operation can be cancelled by
4394 * triggering the cancellable object from another thread. If the operation
4395 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4397 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation() instead.
4400 g_file_eject_mountable (GFile *file,
4401 GMountUnmountFlags flags,
4402 GCancellable *cancellable,
4403 GAsyncReadyCallback callback,
4408 g_return_if_fail (G_IS_FILE (file));
4410 iface = G_FILE_GET_IFACE (file);
4412 if (iface->eject_mountable == NULL)
4414 g_simple_async_report_error_in_idle (G_OBJECT (file),
4418 G_IO_ERROR_NOT_SUPPORTED,
4419 _("Operation not supported"));
4423 (* iface->eject_mountable) (file,
4431 * g_file_eject_mountable_finish:
4432 * @file: input #GFile.
4433 * @result: a #GAsyncResult.
4434 * @error: a #GError, or %NULL
4436 * Finishes an asynchronous eject operation started by
4437 * g_file_eject_mountable().
4439 * Returns: %TRUE if the @file was ejected successfully. %FALSE
4442 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation_finish() instead.
4445 g_file_eject_mountable_finish (GFile *file,
4446 GAsyncResult *result,
4451 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4452 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4454 if (G_IS_SIMPLE_ASYNC_RESULT (result))
4456 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
4457 if (g_simple_async_result_propagate_error (simple, error))
4461 iface = G_FILE_GET_IFACE (file);
4462 return (* iface->eject_mountable_finish) (file, result, error);
4466 * g_file_eject_mountable_with_operation:
4467 * @file: input #GFile.
4468 * @flags: flags affecting the operation
4469 * @mount_operation: a #GMountOperation, or %NULL to avoid user interaction.
4470 * @cancellable: optional #GCancellable object, %NULL to ignore.
4471 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4472 * @user_data: the data to pass to callback function
4474 * Starts an asynchronous eject on a mountable.
4475 * When this operation has completed, @callback will be called with
4476 * @user_user data, and the operation can be finalized with
4477 * g_file_eject_mountable_with_operation_finish().
4479 * If @cancellable is not %NULL, then the operation can be cancelled by
4480 * triggering the cancellable object from another thread. If the operation
4481 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4486 g_file_eject_mountable_with_operation (GFile *file,
4487 GMountUnmountFlags flags,
4488 GMountOperation *mount_operation,
4489 GCancellable *cancellable,
4490 GAsyncReadyCallback callback,
4495 g_return_if_fail (G_IS_FILE (file));
4497 iface = G_FILE_GET_IFACE (file);
4499 if (iface->eject_mountable == NULL && iface->eject_mountable_with_operation == NULL)
4501 g_simple_async_report_error_in_idle (G_OBJECT (file),
4505 G_IO_ERROR_NOT_SUPPORTED,
4506 _("Operation not supported"));
4510 if (iface->eject_mountable_with_operation != NULL)
4511 (* iface->eject_mountable_with_operation) (file,
4518 (* iface->eject_mountable) (file,
4526 * g_file_eject_mountable_with_operation_finish:
4527 * @file: input #GFile.
4528 * @result: a #GAsyncResult.
4529 * @error: a #GError, or %NULL
4531 * Finishes an asynchronous eject operation started by
4532 * g_file_eject_mountable_with_operation().
4534 * Returns: %TRUE if the @file was ejected successfully. %FALSE
4540 g_file_eject_mountable_with_operation_finish (GFile *file,
4541 GAsyncResult *result,
4546 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4547 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4549 if (G_IS_SIMPLE_ASYNC_RESULT (result))
4551 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
4552 if (g_simple_async_result_propagate_error (simple, error))
4556 iface = G_FILE_GET_IFACE (file);
4557 if (iface->eject_mountable_with_operation_finish != NULL)
4558 return (* iface->eject_mountable_with_operation_finish) (file, result, error);
4560 return (* iface->eject_mountable_finish) (file, result, error);
4564 * g_file_monitor_directory:
4565 * @file: input #GFile.
4566 * @flags: a set of #GFileMonitorFlags.
4567 * @cancellable: optional #GCancellable object, %NULL to ignore.
4568 * @error: a #GError, or %NULL.
4570 * Obtains a directory monitor for the given file.
4571 * This may fail if directory monitoring is not supported.
4573 * If @cancellable is not %NULL, then the operation can be cancelled by
4574 * triggering the cancellable object from another thread. If the operation
4575 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4577 * Returns: a #GFileMonitor for the given @file, or %NULL on error.
4578 * Free the returned object with g_object_unref().
4581 g_file_monitor_directory (GFile *file,
4582 GFileMonitorFlags flags,
4583 GCancellable *cancellable,
4588 g_return_val_if_fail (G_IS_FILE (file), NULL);
4590 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4593 iface = G_FILE_GET_IFACE (file);
4595 if (iface->monitor_dir == NULL)
4597 g_set_error_literal (error, G_IO_ERROR,
4598 G_IO_ERROR_NOT_SUPPORTED,
4599 _("Operation not supported"));
4603 return (* iface->monitor_dir) (file, flags, cancellable, error);
4607 * g_file_monitor_file:
4608 * @file: input #GFile.
4609 * @flags: a set of #GFileMonitorFlags.
4610 * @cancellable: optional #GCancellable object, %NULL to ignore.
4611 * @error: a #GError, or %NULL.
4613 * Obtains a file monitor for the given file. If no file notification
4614 * mechanism exists, then regular polling of the file is used.
4616 * If @cancellable is not %NULL, then the operation can be cancelled by
4617 * triggering the cancellable object from another thread. If the operation
4618 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4620 * Returns: a #GFileMonitor for the given @file, or %NULL on error.
4621 * Free the returned object with g_object_unref().
4624 g_file_monitor_file (GFile *file,
4625 GFileMonitorFlags flags,
4626 GCancellable *cancellable,
4630 GFileMonitor *monitor;
4632 g_return_val_if_fail (G_IS_FILE (file), NULL);
4634 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4637 iface = G_FILE_GET_IFACE (file);
4641 if (iface->monitor_file)
4642 monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
4644 /* Fallback to polling */
4645 if (monitor == NULL)
4646 monitor = _g_poll_file_monitor_new (file);
4653 * @file: input #GFile
4654 * @flags: a set of #GFileMonitorFlags
4655 * @cancellable: optional #GCancellable object, %NULL to ignore
4656 * @error: a #GError, or %NULL
4658 * Obtains a file or directory monitor for the given file, depending
4659 * on the type of the file.
4661 * If @cancellable is not %NULL, then the operation can be cancelled by
4662 * triggering the cancellable object from another thread. If the operation
4663 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4665 * Returns: a #GFileMonitor for the given @file, or %NULL on error.
4666 * Free the returned object with g_object_unref().
4671 g_file_monitor (GFile *file,
4672 GFileMonitorFlags flags,
4673 GCancellable *cancellable,
4676 if (g_file_query_file_type (file, 0, cancellable) == G_FILE_TYPE_DIRECTORY)
4677 return g_file_monitor_directory (file, flags, cancellable, error);
4679 return g_file_monitor_file (file, flags, cancellable, error);
4682 /********************************************
4683 * Default implementation of async ops *
4684 ********************************************/
4688 GFileQueryInfoFlags flags;
4690 } QueryInfoAsyncData;
4693 query_info_data_free (QueryInfoAsyncData *data)
4696 g_object_unref (data->info);
4697 g_free (data->attributes);
4702 query_info_async_thread (GSimpleAsyncResult *res,
4704 GCancellable *cancellable)
4706 GError *error = NULL;
4707 QueryInfoAsyncData *data;
4710 data = g_simple_async_result_get_op_res_gpointer (res);
4712 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
4716 g_simple_async_result_set_from_error (res, error);
4717 g_error_free (error);
4724 g_file_real_query_info_async (GFile *file,
4725 const char *attributes,
4726 GFileQueryInfoFlags flags,
4728 GCancellable *cancellable,
4729 GAsyncReadyCallback callback,
4732 GSimpleAsyncResult *res;
4733 QueryInfoAsyncData *data;
4735 data = g_new0 (QueryInfoAsyncData, 1);
4736 data->attributes = g_strdup (attributes);
4737 data->flags = flags;
4739 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_info_async);
4740 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
4742 g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
4743 g_object_unref (res);
4747 g_file_real_query_info_finish (GFile *file,
4751 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4752 QueryInfoAsyncData *data;
4754 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_info_async);
4756 data = g_simple_async_result_get_op_res_gpointer (simple);
4758 return g_object_ref (data->info);
4766 } QueryFilesystemInfoAsyncData;
4769 query_filesystem_info_data_free (QueryFilesystemInfoAsyncData *data)
4772 g_object_unref (data->info);
4773 g_free (data->attributes);
4778 query_filesystem_info_async_thread (GSimpleAsyncResult *res,
4780 GCancellable *cancellable)
4782 GError *error = NULL;
4783 QueryFilesystemInfoAsyncData *data;
4786 data = g_simple_async_result_get_op_res_gpointer (res);
4788 info = g_file_query_filesystem_info (G_FILE (object), data->attributes, cancellable, &error);
4792 g_simple_async_result_set_from_error (res, error);
4793 g_error_free (error);
4800 g_file_real_query_filesystem_info_async (GFile *file,
4801 const char *attributes,
4803 GCancellable *cancellable,
4804 GAsyncReadyCallback callback,
4807 GSimpleAsyncResult *res;
4808 QueryFilesystemInfoAsyncData *data;
4810 data = g_new0 (QueryFilesystemInfoAsyncData, 1);
4811 data->attributes = g_strdup (attributes);
4813 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_filesystem_info_async);
4814 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_filesystem_info_data_free);
4816 g_simple_async_result_run_in_thread (res, query_filesystem_info_async_thread, io_priority, cancellable);
4817 g_object_unref (res);
4821 g_file_real_query_filesystem_info_finish (GFile *file,
4825 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4826 QueryFilesystemInfoAsyncData *data;
4828 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_filesystem_info_async);
4830 data = g_simple_async_result_get_op_res_gpointer (simple);
4832 return g_object_ref (data->info);
4839 GFileQueryInfoFlags flags;
4840 GFileEnumerator *enumerator;
4841 } EnumerateChildrenAsyncData;
4844 enumerate_children_data_free (EnumerateChildrenAsyncData *data)
4846 if (data->enumerator)
4847 g_object_unref (data->enumerator);
4848 g_free (data->attributes);
4853 enumerate_children_async_thread (GSimpleAsyncResult *res,
4855 GCancellable *cancellable)
4857 GError *error = NULL;
4858 EnumerateChildrenAsyncData *data;
4859 GFileEnumerator *enumerator;
4861 data = g_simple_async_result_get_op_res_gpointer (res);
4863 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
4865 if (enumerator == NULL)
4867 g_simple_async_result_set_from_error (res, error);
4868 g_error_free (error);
4871 data->enumerator = enumerator;
4875 g_file_real_enumerate_children_async (GFile *file,
4876 const char *attributes,
4877 GFileQueryInfoFlags flags,
4879 GCancellable *cancellable,
4880 GAsyncReadyCallback callback,
4883 GSimpleAsyncResult *res;
4884 EnumerateChildrenAsyncData *data;
4886 data = g_new0 (EnumerateChildrenAsyncData, 1);
4887 data->attributes = g_strdup (attributes);
4888 data->flags = flags;
4890 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_enumerate_children_async);
4891 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)enumerate_children_data_free);
4893 g_simple_async_result_run_in_thread (res, enumerate_children_async_thread, io_priority, cancellable);
4894 g_object_unref (res);
4897 static GFileEnumerator *
4898 g_file_real_enumerate_children_finish (GFile *file,
4902 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4903 EnumerateChildrenAsyncData *data;
4905 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_enumerate_children_async);
4907 data = g_simple_async_result_get_op_res_gpointer (simple);
4908 if (data->enumerator)
4909 return g_object_ref (data->enumerator);
4915 open_read_async_thread (GSimpleAsyncResult *res,
4917 GCancellable *cancellable)
4920 GFileInputStream *stream;
4921 GError *error = NULL;
4923 iface = G_FILE_GET_IFACE (object);
4925 if (iface->read_fn == NULL)
4927 g_set_error_literal (&error, G_IO_ERROR,
4928 G_IO_ERROR_NOT_SUPPORTED,
4929 _("Operation not supported"));
4931 g_simple_async_result_set_from_error (res, error);
4932 g_error_free (error);
4937 stream = iface->read_fn (G_FILE (object), cancellable, &error);
4941 g_simple_async_result_set_from_error (res, error);
4942 g_error_free (error);
4945 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4949 g_file_real_read_async (GFile *file,
4951 GCancellable *cancellable,
4952 GAsyncReadyCallback callback,
4955 GSimpleAsyncResult *res;
4957 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_read_async);
4959 g_simple_async_result_run_in_thread (res, open_read_async_thread, io_priority, cancellable);
4960 g_object_unref (res);
4963 static GFileInputStream *
4964 g_file_real_read_finish (GFile *file,
4968 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4971 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_read_async);
4973 op = g_simple_async_result_get_op_res_gpointer (simple);
4975 return g_object_ref (op);
4981 append_to_async_thread (GSimpleAsyncResult *res,
4983 GCancellable *cancellable)
4986 GFileCreateFlags *data;
4987 GFileOutputStream *stream;
4988 GError *error = NULL;
4990 iface = G_FILE_GET_IFACE (object);
4992 data = g_simple_async_result_get_op_res_gpointer (res);
4994 stream = iface->append_to (G_FILE (object), *data, cancellable, &error);
4998 g_simple_async_result_set_from_error (res, error);
4999 g_error_free (error);
5002 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
5006 g_file_real_append_to_async (GFile *file,
5007 GFileCreateFlags flags,
5009 GCancellable *cancellable,
5010 GAsyncReadyCallback callback,
5013 GFileCreateFlags *data;
5014 GSimpleAsyncResult *res;
5016 data = g_new0 (GFileCreateFlags, 1);
5019 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_append_to_async);
5020 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
5022 g_simple_async_result_run_in_thread (res, append_to_async_thread, io_priority, cancellable);
5023 g_object_unref (res);
5026 static GFileOutputStream *
5027 g_file_real_append_to_finish (GFile *file,
5031 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5034 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_append_to_async);
5036 op = g_simple_async_result_get_op_res_gpointer (simple);
5038 return g_object_ref (op);
5044 create_async_thread (GSimpleAsyncResult *res,
5046 GCancellable *cancellable)
5049 GFileCreateFlags *data;
5050 GFileOutputStream *stream;
5051 GError *error = NULL;
5053 iface = G_FILE_GET_IFACE (object);
5055 data = g_simple_async_result_get_op_res_gpointer (res);
5057 stream = iface->create (G_FILE (object), *data, cancellable, &error);
5061 g_simple_async_result_set_from_error (res, error);
5062 g_error_free (error);
5065 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
5069 g_file_real_create_async (GFile *file,
5070 GFileCreateFlags flags,
5072 GCancellable *cancellable,
5073 GAsyncReadyCallback callback,
5076 GFileCreateFlags *data;
5077 GSimpleAsyncResult *res;
5079 data = g_new0 (GFileCreateFlags, 1);
5082 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_create_async);
5083 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
5085 g_simple_async_result_run_in_thread (res, create_async_thread, io_priority, cancellable);
5086 g_object_unref (res);
5089 static GFileOutputStream *
5090 g_file_real_create_finish (GFile *file,
5094 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5097 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_create_async);
5099 op = g_simple_async_result_get_op_res_gpointer (simple);
5101 return g_object_ref (op);
5107 GFileOutputStream *stream;
5109 gboolean make_backup;
5110 GFileCreateFlags flags;
5114 replace_async_data_free (ReplaceAsyncData *data)
5117 g_object_unref (data->stream);
5118 g_free (data->etag);
5123 replace_async_thread (GSimpleAsyncResult *res,
5125 GCancellable *cancellable)
5128 GFileOutputStream *stream;
5129 GError *error = NULL;
5130 ReplaceAsyncData *data;
5132 iface = G_FILE_GET_IFACE (object);
5134 data = g_simple_async_result_get_op_res_gpointer (res);
5136 stream = iface->replace (G_FILE (object),
5145 g_simple_async_result_set_from_error (res, error);
5146 g_error_free (error);
5149 data->stream = stream;
5153 g_file_real_replace_async (GFile *file,
5155 gboolean make_backup,
5156 GFileCreateFlags flags,
5158 GCancellable *cancellable,
5159 GAsyncReadyCallback callback,
5162 GSimpleAsyncResult *res;
5163 ReplaceAsyncData *data;
5165 data = g_new0 (ReplaceAsyncData, 1);
5166 data->etag = g_strdup (etag);
5167 data->make_backup = make_backup;
5168 data->flags = flags;
5170 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_replace_async);
5171 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_async_data_free);
5173 g_simple_async_result_run_in_thread (res, replace_async_thread, io_priority, cancellable);
5174 g_object_unref (res);
5177 static GFileOutputStream *
5178 g_file_real_replace_finish (GFile *file,
5182 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5183 ReplaceAsyncData *data;
5185 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_replace_async);
5187 data = g_simple_async_result_get_op_res_gpointer (simple);
5189 return g_object_ref (data->stream);
5195 open_readwrite_async_thread (GSimpleAsyncResult *res,
5197 GCancellable *cancellable)
5200 GFileIOStream *stream;
5201 GError *error = NULL;
5203 iface = G_FILE_GET_IFACE (object);
5205 if (iface->open_readwrite == NULL)
5207 g_set_error_literal (&error, G_IO_ERROR,
5208 G_IO_ERROR_NOT_SUPPORTED,
5209 _("Operation not supported"));
5211 g_simple_async_result_set_from_error (res, error);
5212 g_error_free (error);
5217 stream = iface->open_readwrite (G_FILE (object), cancellable, &error);
5221 g_simple_async_result_set_from_error (res, error);
5222 g_error_free (error);
5225 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
5229 g_file_real_open_readwrite_async (GFile *file,
5231 GCancellable *cancellable,
5232 GAsyncReadyCallback callback,
5235 GSimpleAsyncResult *res;
5237 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_open_readwrite_async);
5239 g_simple_async_result_run_in_thread (res, open_readwrite_async_thread, io_priority, cancellable);
5240 g_object_unref (res);
5243 static GFileIOStream *
5244 g_file_real_open_readwrite_finish (GFile *file,
5248 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5251 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_open_readwrite_async);
5253 op = g_simple_async_result_get_op_res_gpointer (simple);
5255 return g_object_ref (op);
5261 create_readwrite_async_thread (GSimpleAsyncResult *res,
5263 GCancellable *cancellable)
5266 GFileCreateFlags *data;
5267 GFileIOStream *stream;
5268 GError *error = NULL;
5270 iface = G_FILE_GET_IFACE (object);
5272 data = g_simple_async_result_get_op_res_gpointer (res);
5274 if (iface->create_readwrite == NULL)
5276 g_set_error_literal (&error, G_IO_ERROR,
5277 G_IO_ERROR_NOT_SUPPORTED,
5278 _("Operation not supported"));
5280 g_simple_async_result_set_from_error (res, error);
5281 g_error_free (error);
5286 stream = iface->create_readwrite (G_FILE (object), *data, cancellable, &error);
5290 g_simple_async_result_set_from_error (res, error);
5291 g_error_free (error);
5294 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
5298 g_file_real_create_readwrite_async (GFile *file,
5299 GFileCreateFlags flags,
5301 GCancellable *cancellable,
5302 GAsyncReadyCallback callback,
5305 GFileCreateFlags *data;
5306 GSimpleAsyncResult *res;
5308 data = g_new0 (GFileCreateFlags, 1);
5311 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_create_readwrite_async);
5312 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
5314 g_simple_async_result_run_in_thread (res, create_readwrite_async_thread, io_priority, cancellable);
5315 g_object_unref (res);
5318 static GFileIOStream *
5319 g_file_real_create_readwrite_finish (GFile *file,
5323 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5326 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_create_readwrite_async);
5328 op = g_simple_async_result_get_op_res_gpointer (simple);
5330 return g_object_ref (op);
5336 GFileIOStream *stream;
5338 gboolean make_backup;
5339 GFileCreateFlags flags;
5340 } ReplaceRWAsyncData;
5343 replace_rw_async_data_free (ReplaceRWAsyncData *data)
5346 g_object_unref (data->stream);
5347 g_free (data->etag);
5352 replace_readwrite_async_thread (GSimpleAsyncResult *res,
5354 GCancellable *cancellable)
5357 GFileIOStream *stream;
5358 GError *error = NULL;
5359 ReplaceRWAsyncData *data;
5361 iface = G_FILE_GET_IFACE (object);
5363 data = g_simple_async_result_get_op_res_gpointer (res);
5365 stream = iface->replace_readwrite (G_FILE (object),
5374 g_simple_async_result_set_from_error (res, error);
5375 g_error_free (error);
5378 data->stream = stream;
5382 g_file_real_replace_readwrite_async (GFile *file,
5384 gboolean make_backup,
5385 GFileCreateFlags flags,
5387 GCancellable *cancellable,
5388 GAsyncReadyCallback callback,
5391 GSimpleAsyncResult *res;
5392 ReplaceRWAsyncData *data;
5394 data = g_new0 (ReplaceRWAsyncData, 1);
5395 data->etag = g_strdup (etag);
5396 data->make_backup = make_backup;
5397 data->flags = flags;
5399 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_replace_readwrite_async);
5400 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_rw_async_data_free);
5402 g_simple_async_result_run_in_thread (res, replace_readwrite_async_thread, io_priority, cancellable);
5403 g_object_unref (res);
5406 static GFileIOStream *
5407 g_file_real_replace_readwrite_finish (GFile *file,
5411 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5412 ReplaceRWAsyncData *data;
5414 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_replace_readwrite_async);
5416 data = g_simple_async_result_get_op_res_gpointer (simple);
5418 return g_object_ref (data->stream);
5426 } SetDisplayNameAsyncData;
5429 set_display_name_data_free (SetDisplayNameAsyncData *data)
5431 g_free (data->name);
5433 g_object_unref (data->file);
5438 set_display_name_async_thread (GSimpleAsyncResult *res,
5440 GCancellable *cancellable)
5442 GError *error = NULL;
5443 SetDisplayNameAsyncData *data;
5446 data = g_simple_async_result_get_op_res_gpointer (res);
5448 file = g_file_set_display_name (G_FILE (object), data->name, cancellable, &error);
5452 g_simple_async_result_set_from_error (res, error);
5453 g_error_free (error);
5460 g_file_real_set_display_name_async (GFile *file,
5461 const char *display_name,
5463 GCancellable *cancellable,
5464 GAsyncReadyCallback callback,
5467 GSimpleAsyncResult *res;
5468 SetDisplayNameAsyncData *data;
5470 data = g_new0 (SetDisplayNameAsyncData, 1);
5471 data->name = g_strdup (display_name);
5473 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_display_name_async);
5474 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_display_name_data_free);
5476 g_simple_async_result_run_in_thread (res, set_display_name_async_thread, io_priority, cancellable);
5477 g_object_unref (res);
5481 g_file_real_set_display_name_finish (GFile *file,
5485 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5486 SetDisplayNameAsyncData *data;
5488 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_display_name_async);
5490 data = g_simple_async_result_get_op_res_gpointer (simple);
5492 return g_object_ref (data->file);
5498 GFileQueryInfoFlags flags;
5505 set_info_data_free (SetInfoAsyncData *data)
5508 g_object_unref (data->info);
5510 g_error_free (data->error);
5515 set_info_async_thread (GSimpleAsyncResult *res,
5517 GCancellable *cancellable)
5519 SetInfoAsyncData *data;
5521 data = g_simple_async_result_get_op_res_gpointer (res);
5524 data->res = g_file_set_attributes_from_info (G_FILE (object),
5532 g_file_real_set_attributes_async (GFile *file,
5534 GFileQueryInfoFlags flags,
5536 GCancellable *cancellable,
5537 GAsyncReadyCallback callback,
5540 GSimpleAsyncResult *res;
5541 SetInfoAsyncData *data;
5543 data = g_new0 (SetInfoAsyncData, 1);
5544 data->info = g_file_info_dup (info);
5545 data->flags = flags;
5547 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_attributes_async);
5548 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_info_data_free);
5550 g_simple_async_result_run_in_thread (res, set_info_async_thread, io_priority, cancellable);
5551 g_object_unref (res);
5555 g_file_real_set_attributes_finish (GFile *file,
5560 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5561 SetInfoAsyncData *data;
5563 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_attributes_async);
5565 data = g_simple_async_result_get_op_res_gpointer (simple);
5568 *info = g_object_ref (data->info);
5570 if (error != NULL && data->error)
5571 *error = g_error_copy (data->error);
5577 find_enclosing_mount_async_thread (GSimpleAsyncResult *res,
5579 GCancellable *cancellable)
5581 GError *error = NULL;
5584 mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
5588 g_simple_async_result_set_from_error (res, error);
5589 g_error_free (error);
5592 g_simple_async_result_set_op_res_gpointer (res, mount, (GDestroyNotify)g_object_unref);
5596 g_file_real_find_enclosing_mount_async (GFile *file,
5598 GCancellable *cancellable,
5599 GAsyncReadyCallback callback,
5602 GSimpleAsyncResult *res;
5604 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_find_enclosing_mount_async);
5606 g_simple_async_result_run_in_thread (res, find_enclosing_mount_async_thread, io_priority, cancellable);
5607 g_object_unref (res);
5611 g_file_real_find_enclosing_mount_finish (GFile *file,
5615 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5618 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_find_enclosing_mount_async);
5620 mount = g_simple_async_result_get_op_res_gpointer (simple);
5621 return g_object_ref (mount);
5628 GFileCopyFlags flags;
5629 GFileProgressCallback progress_cb;
5630 gpointer progress_cb_data;
5631 GIOSchedulerJob *job;
5635 copy_async_data_free (CopyAsyncData *data)
5637 g_object_unref (data->source);
5638 g_object_unref (data->destination);
5643 CopyAsyncData *data;
5644 goffset current_num_bytes;
5645 goffset total_num_bytes;
5649 copy_async_progress_in_main (gpointer user_data)
5651 ProgressData *progress = user_data;
5652 CopyAsyncData *data = progress->data;
5654 data->progress_cb (progress->current_num_bytes,
5655 progress->total_num_bytes,
5656 data->progress_cb_data);
5662 mainloop_barrier (gpointer user_data)
5664 /* Does nothing, but ensures all queued idles before
5671 copy_async_progress_callback (goffset current_num_bytes,
5672 goffset total_num_bytes,
5675 CopyAsyncData *data = user_data;
5676 ProgressData *progress;
5678 progress = g_new (ProgressData, 1);
5679 progress->data = data;
5680 progress->current_num_bytes = current_num_bytes;
5681 progress->total_num_bytes = total_num_bytes;
5683 g_io_scheduler_job_send_to_mainloop_async (data->job,
5684 copy_async_progress_in_main,
5690 copy_async_thread (GIOSchedulerJob *job,
5691 GCancellable *cancellable,
5694 GSimpleAsyncResult *res;
5695 CopyAsyncData *data;
5700 data = g_simple_async_result_get_op_res_gpointer (res);
5704 result = g_file_copy (data->source,
5708 (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
5712 /* Ensure all progress callbacks are done running in main thread */
5713 if (data->progress_cb != NULL)
5714 g_io_scheduler_job_send_to_mainloop (job,
5720 g_simple_async_result_set_from_error (res, error);
5721 g_error_free (error);
5724 g_simple_async_result_complete_in_idle (res);
5730 g_file_real_copy_async (GFile *source,
5732 GFileCopyFlags flags,
5734 GCancellable *cancellable,
5735 GFileProgressCallback progress_callback,
5736 gpointer progress_callback_data,
5737 GAsyncReadyCallback callback,
5740 GSimpleAsyncResult *res;
5741 CopyAsyncData *data;
5743 data = g_new0 (CopyAsyncData, 1);
5744 data->source = g_object_ref (source);
5745 data->destination = g_object_ref (destination);
5746 data->flags = flags;
5747 data->progress_cb = progress_callback;
5748 data->progress_cb_data = progress_callback_data;
5750 res = g_simple_async_result_new (G_OBJECT (source), callback, user_data, g_file_real_copy_async);
5751 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)copy_async_data_free);
5753 g_io_scheduler_push_job (copy_async_thread, res, g_object_unref, io_priority, cancellable);
5757 g_file_real_copy_finish (GFile *file,
5761 /* Error handled in g_file_copy_finish() */
5766 /********************************************
5767 * Default VFS operations *
5768 ********************************************/
5771 * g_file_new_for_path:
5772 * @path: a string containing a relative or absolute path.
5774 * Constructs a #GFile for a given path. This operation never
5775 * fails, but the returned object might not support any I/O
5776 * operation if @path is malformed.
5778 * Returns: a new #GFile for the given @path.
5781 g_file_new_for_path (const char *path)
5783 g_return_val_if_fail (path != NULL, NULL);
5785 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
5789 * g_file_new_for_uri:
5790 * @uri: a string containing a URI.
5792 * Constructs a #GFile for a given URI. This operation never
5793 * fails, but the returned object might not support any I/O
5794 * operation if @uri is malformed or if the uri type is
5797 * Returns: a #GFile for the given @uri.
5800 g_file_new_for_uri (const char *uri)
5802 g_return_val_if_fail (uri != NULL, NULL);
5804 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
5808 * g_file_parse_name:
5809 * @parse_name: a file name or path to be parsed.
5811 * Constructs a #GFile with the given @parse_name (i.e. something given by g_file_get_parse_name()).
5812 * This operation never fails, but the returned object might not support any I/O
5813 * operation if the @parse_name cannot be parsed.
5815 * Returns: a new #GFile.
5818 g_file_parse_name (const char *parse_name)
5820 g_return_val_if_fail (parse_name != NULL, NULL);
5822 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
5826 is_valid_scheme_character (char c)
5828 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
5831 /* Following RFC 2396, valid schemes are built like:
5832 * scheme = alpha *( alpha | digit | "+" | "-" | "." )
5835 has_valid_scheme (const char *uri)
5841 if (!g_ascii_isalpha (*p))
5846 } while (is_valid_scheme_character (*p));
5852 * g_file_new_for_commandline_arg:
5853 * @arg: a command line string.
5855 * Creates a #GFile with the given argument from the command line. The value of
5856 * @arg can be either a URI, an absolute path or a relative path resolved
5857 * relative to the current working directory.
5858 * This operation never fails, but the returned object might not support any
5859 * I/O operation if @arg points to a malformed path.
5861 * Returns: a new #GFile.
5864 g_file_new_for_commandline_arg (const char *arg)
5870 g_return_val_if_fail (arg != NULL, NULL);
5872 if (g_path_is_absolute (arg))
5873 return g_file_new_for_path (arg);
5875 if (has_valid_scheme (arg))
5876 return g_file_new_for_uri (arg);
5878 current_dir = g_get_current_dir ();
5879 filename = g_build_filename (current_dir, arg, NULL);
5880 g_free (current_dir);
5882 file = g_file_new_for_path (filename);
5889 * g_file_mount_enclosing_volume:
5890 * @location: input #GFile.
5891 * @flags: flags affecting the operation
5892 * @mount_operation: a #GMountOperation or %NULL to avoid user interaction.
5893 * @cancellable: optional #GCancellable object, %NULL to ignore.
5894 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
5895 * @user_data: the data to pass to callback function
5897 * Starts a @mount_operation, mounting the volume that contains the file @location.
5899 * When this operation has completed, @callback will be called with
5900 * @user_user data, and the operation can be finalized with
5901 * g_file_mount_enclosing_volume_finish().
5903 * If @cancellable is not %NULL, then the operation can be cancelled by
5904 * triggering the cancellable object from another thread. If the operation
5905 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5908 g_file_mount_enclosing_volume (GFile *location,
5909 GMountMountFlags flags,
5910 GMountOperation *mount_operation,
5911 GCancellable *cancellable,
5912 GAsyncReadyCallback callback,
5917 g_return_if_fail (G_IS_FILE (location));
5919 iface = G_FILE_GET_IFACE (location);
5921 if (iface->mount_enclosing_volume == NULL)
5923 g_simple_async_report_error_in_idle (G_OBJECT (location),
5924 callback, user_data,
5925 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5926 _("volume doesn't implement mount"));
5931 (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
5936 * g_file_mount_enclosing_volume_finish:
5937 * @location: input #GFile.
5938 * @result: a #GAsyncResult.
5939 * @error: a #GError, or %NULL
5941 * Finishes a mount operation started by g_file_mount_enclosing_volume().
5943 * Returns: %TRUE if successful. If an error
5944 * has occurred, this function will return %FALSE and set @error
5945 * appropriately if present.
5948 g_file_mount_enclosing_volume_finish (GFile *location,
5949 GAsyncResult *result,
5954 g_return_val_if_fail (G_IS_FILE (location), FALSE);
5955 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5957 if (G_IS_SIMPLE_ASYNC_RESULT (result))
5959 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
5960 if (g_simple_async_result_propagate_error (simple, error))
5964 iface = G_FILE_GET_IFACE (location);
5966 return (* iface->mount_enclosing_volume_finish) (location, result, error);
5969 /********************************************
5970 * Utility functions *
5971 ********************************************/
5974 * g_file_query_default_handler:
5975 * @file: a #GFile to open.
5976 * @cancellable: optional #GCancellable object, %NULL to ignore.
5977 * @error: a #GError, or %NULL
5979 * Returns the #GAppInfo that is registered as the default
5980 * application to handle the file specified by @file.
5982 * If @cancellable is not %NULL, then the operation can be cancelled by
5983 * triggering the cancellable object from another thread. If the operation
5984 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5986 * Returns: a #GAppInfo if the handle was found, %NULL if there were errors.
5987 * When you are done with it, release it with g_object_unref()
5990 g_file_query_default_handler (GFile *file,
5991 GCancellable *cancellable,
5995 const char *content_type;
6000 uri_scheme = g_file_get_uri_scheme (file);
6001 if (uri_scheme && uri_scheme[0] != '\0')
6003 appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
6004 g_free (uri_scheme);
6006 if (appinfo != NULL)
6010 info = g_file_query_info (file,
6011 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
6020 content_type = g_file_info_get_content_type (info);
6023 /* Don't use is_native(), as we want to support fuse paths if availible */
6024 path = g_file_get_path (file);
6025 appinfo = g_app_info_get_default_for_type (content_type,
6030 g_object_unref (info);
6032 if (appinfo != NULL)
6035 g_set_error_literal (error, G_IO_ERROR,
6036 G_IO_ERROR_NOT_SUPPORTED,
6037 _("No application is registered as handling this file"));
6043 #define GET_CONTENT_BLOCK_SIZE 8192
6046 * g_file_load_contents:
6047 * @file: input #GFile.
6048 * @cancellable: optional #GCancellable object, %NULL to ignore.
6049 * @contents: a location to place the contents of the file.
6050 * @length: a location to place the length of the contents of the file,
6051 * or %NULL if the length is not needed
6052 * @etag_out: a location to place the current entity tag for the file,
6053 * or %NULL if the entity tag is not needed
6054 * @error: a #GError, or %NULL
6056 * Loads the content of the file into memory. The data is always
6057 * zero-terminated, but this is not included in the resultant @length.
6058 * The returned @content should be freed with g_free() when no longer
6061 * If @cancellable is not %NULL, then the operation can be cancelled by
6062 * triggering the cancellable object from another thread. If the operation
6063 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6065 * Returns: %TRUE if the @file's contents were successfully loaded.
6066 * %FALSE if there were errors.
6069 g_file_load_contents (GFile *file,
6070 GCancellable *cancellable,
6076 GFileInputStream *in;
6077 GByteArray *content;
6082 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6083 g_return_val_if_fail (contents != NULL, FALSE);
6085 in = g_file_read (file, cancellable, error);
6089 content = g_byte_array_new ();
6092 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6093 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
6094 content->data + pos,
6095 GET_CONTENT_BLOCK_SIZE,
6096 cancellable, error)) > 0)
6099 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6106 info = g_file_input_stream_query_info (in,
6107 G_FILE_ATTRIBUTE_ETAG_VALUE,
6112 *etag_out = g_strdup (g_file_info_get_etag (info));
6113 g_object_unref (info);
6117 /* Ignore errors on close */
6118 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
6119 g_object_unref (in);
6123 /* error is set already */
6124 g_byte_array_free (content, TRUE);
6131 /* Zero terminate (we got an extra byte allocated for this */
6132 content->data[pos] = 0;
6134 *contents = (char *)g_byte_array_free (content, FALSE);
6142 GCancellable *cancellable;
6143 GFileReadMoreCallback read_more_callback;
6144 GAsyncReadyCallback callback;
6146 GByteArray *content;
6153 load_contents_data_free (LoadContentsData *data)
6156 g_error_free (data->error);
6157 if (data->cancellable)
6158 g_object_unref (data->cancellable);
6160 g_byte_array_free (data->content, TRUE);
6161 g_free (data->etag);
6162 g_object_unref (data->file);
6167 load_contents_close_callback (GObject *obj,
6168 GAsyncResult *close_res,
6171 GInputStream *stream = G_INPUT_STREAM (obj);
6172 LoadContentsData *data = user_data;
6173 GSimpleAsyncResult *res;
6175 /* Ignore errors here, we're only reading anyway */
6176 g_input_stream_close_finish (stream, close_res, NULL);
6177 g_object_unref (stream);
6179 res = g_simple_async_result_new (G_OBJECT (data->file),
6182 g_file_load_contents_async);
6183 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)load_contents_data_free);
6184 g_simple_async_result_complete (res);
6185 g_object_unref (res);
6189 load_contents_fstat_callback (GObject *obj,
6190 GAsyncResult *stat_res,
6193 GInputStream *stream = G_INPUT_STREAM (obj);
6194 LoadContentsData *data = user_data;
6197 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
6201 data->etag = g_strdup (g_file_info_get_etag (info));
6202 g_object_unref (info);
6205 g_input_stream_close_async (stream, 0,
6207 load_contents_close_callback, data);
6211 load_contents_read_callback (GObject *obj,
6212 GAsyncResult *read_res,
6215 GInputStream *stream = G_INPUT_STREAM (obj);
6216 LoadContentsData *data = user_data;
6217 GError *error = NULL;
6220 read_size = g_input_stream_read_finish (stream, read_res, &error);
6224 /* Error or EOF, close the file */
6225 data->error = error;
6226 g_input_stream_close_async (stream, 0,
6228 load_contents_close_callback, data);
6230 else if (read_size == 0)
6232 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
6233 G_FILE_ATTRIBUTE_ETAG_VALUE,
6236 load_contents_fstat_callback,
6239 else if (read_size > 0)
6241 data->pos += read_size;
6243 g_byte_array_set_size (data->content,
6244 data->pos + GET_CONTENT_BLOCK_SIZE);
6247 if (data->read_more_callback &&
6248 !data->read_more_callback ((char *)data->content->data, data->pos, data->user_data))
6249 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
6250 G_FILE_ATTRIBUTE_ETAG_VALUE,
6253 load_contents_fstat_callback,
6256 g_input_stream_read_async (stream,
6257 data->content->data + data->pos,
6258 GET_CONTENT_BLOCK_SIZE,
6261 load_contents_read_callback,
6267 load_contents_open_callback (GObject *obj,
6268 GAsyncResult *open_res,
6271 GFile *file = G_FILE (obj);
6272 GFileInputStream *stream;
6273 LoadContentsData *data = user_data;
6274 GError *error = NULL;
6275 GSimpleAsyncResult *res;
6277 stream = g_file_read_finish (file, open_res, &error);
6281 g_byte_array_set_size (data->content,
6282 data->pos + GET_CONTENT_BLOCK_SIZE);
6283 g_input_stream_read_async (G_INPUT_STREAM (stream),
6284 data->content->data + data->pos,
6285 GET_CONTENT_BLOCK_SIZE,
6288 load_contents_read_callback,
6294 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
6298 g_simple_async_result_complete (res);
6299 g_error_free (error);
6300 load_contents_data_free (data);
6301 g_object_unref (res);
6306 * g_file_load_partial_contents_async:
6307 * @file: input #GFile.
6308 * @cancellable: optional #GCancellable object, %NULL to ignore.
6309 * @read_more_callback: a #GFileReadMoreCallback to receive partial data and to specify whether further data should be read.
6310 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6311 * @user_data: the data to pass to the callback functions.
6313 * Reads the partial contents of a file. A #GFileReadMoreCallback should be
6314 * used to stop reading from the file when appropriate, else this function
6315 * will behave exactly as g_file_load_contents_async(). This operation
6316 * can be finished by g_file_load_partial_contents_finish().
6318 * Users of this function should be aware that @user_data is passed to
6319 * both the @read_more_callback and the @callback.
6321 * If @cancellable is not %NULL, then the operation can be cancelled by
6322 * triggering the cancellable object from another thread. If the operation
6323 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6326 g_file_load_partial_contents_async (GFile *file,
6327 GCancellable *cancellable,
6328 GFileReadMoreCallback read_more_callback,
6329 GAsyncReadyCallback callback,
6332 LoadContentsData *data;
6334 g_return_if_fail (G_IS_FILE (file));
6336 data = g_new0 (LoadContentsData, 1);
6339 data->cancellable = g_object_ref (cancellable);
6340 data->read_more_callback = read_more_callback;
6341 data->callback = callback;
6342 data->user_data = user_data;
6343 data->content = g_byte_array_new ();
6344 data->file = g_object_ref (file);
6346 g_file_read_async (file,
6349 load_contents_open_callback,
6354 * g_file_load_partial_contents_finish:
6355 * @file: input #GFile.
6356 * @res: a #GAsyncResult.
6357 * @contents: a location to place the contents of the file.
6358 * @length: a location to place the length of the contents of the file,
6359 * or %NULL if the length is not needed
6360 * @etag_out: a location to place the current entity tag for the file,
6361 * or %NULL if the entity tag is not needed
6362 * @error: a #GError, or %NULL
6364 * Finishes an asynchronous partial load operation that was started
6365 * with g_file_load_partial_contents_async(). The data is always
6366 * zero-terminated, but this is not included in the resultant @length.
6367 * The returned @content should be freed with g_free() when no longer
6370 * Returns: %TRUE if the load was successful. If %FALSE and @error is
6371 * present, it will be set appropriately.
6374 g_file_load_partial_contents_finish (GFile *file,
6381 GSimpleAsyncResult *simple;
6382 LoadContentsData *data;
6384 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6385 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
6386 g_return_val_if_fail (contents != NULL, FALSE);
6388 simple = G_SIMPLE_ASYNC_RESULT (res);
6390 if (g_simple_async_result_propagate_error (simple, error))
6393 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_load_contents_async);
6395 data = g_simple_async_result_get_op_res_gpointer (simple);
6399 g_propagate_error (error, data->error);
6408 *length = data->pos;
6412 *etag_out = data->etag;
6416 /* Zero terminate */
6417 g_byte_array_set_size (data->content, data->pos + 1);
6418 data->content->data[data->pos] = 0;
6420 *contents = (char *)g_byte_array_free (data->content, FALSE);
6421 data->content = NULL;
6427 * g_file_load_contents_async:
6428 * @file: input #GFile.
6429 * @cancellable: optional #GCancellable object, %NULL to ignore.
6430 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6431 * @user_data: the data to pass to callback function
6433 * Starts an asynchronous load of the @file's contents.
6435 * For more details, see g_file_load_contents() which is
6436 * the synchronous version of this call.
6438 * When the load operation has completed, @callback will be called
6439 * with @user data. To finish the operation, call
6440 * g_file_load_contents_finish() with the #GAsyncResult returned by
6443 * If @cancellable is not %NULL, then the operation can be cancelled by
6444 * triggering the cancellable object from another thread. If the operation
6445 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6448 g_file_load_contents_async (GFile *file,
6449 GCancellable *cancellable,
6450 GAsyncReadyCallback callback,
6453 g_file_load_partial_contents_async (file,
6456 callback, user_data);
6460 * g_file_load_contents_finish:
6461 * @file: input #GFile.
6462 * @res: a #GAsyncResult.
6463 * @contents: a location to place the contents of the file.
6464 * @length: a location to place the length of the contents of the file,
6465 * or %NULL if the length is not needed
6466 * @etag_out: a location to place the current entity tag for the file,
6467 * or %NULL if the entity tag is not needed
6468 * @error: a #GError, or %NULL
6470 * Finishes an asynchronous load of the @file's contents.
6471 * The contents are placed in @contents, and @length is set to the
6472 * size of the @contents string. The @content should be freed with
6473 * g_free() when no longer needed. If @etag_out is present, it will be
6474 * set to the new entity tag for the @file.
6476 * Returns: %TRUE if the load was successful. If %FALSE and @error is
6477 * present, it will be set appropriately.
6480 g_file_load_contents_finish (GFile *file,
6487 return g_file_load_partial_contents_finish (file,
6496 * g_file_replace_contents:
6497 * @file: input #GFile.
6498 * @contents: a string containing the new contents for @file.
6499 * @length: the length of @contents in bytes.
6500 * @etag: the old <link linkend="gfile-etag">entity tag</link>
6501 * for the document, or %NULL
6502 * @make_backup: %TRUE if a backup should be created.
6503 * @flags: a set of #GFileCreateFlags.
6504 * @new_etag: a location to a new <link linkend="gfile-etag">entity tag</link>
6505 * for the document. This should be freed with g_free() when no longer
6507 * @cancellable: optional #GCancellable object, %NULL to ignore.
6508 * @error: a #GError, or %NULL
6510 * Replaces the contents of @file with @contents of @length bytes.
6512 * If @etag is specified (not %NULL) any existing file must have that etag, or
6513 * the error %G_IO_ERROR_WRONG_ETAG will be returned.
6515 * If @make_backup is %TRUE, this function will attempt to make a backup of @file.
6517 * If @cancellable is not %NULL, then the operation can be cancelled by
6518 * triggering the cancellable object from another thread. If the operation
6519 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6521 * The returned @new_etag can be used to verify that the file hasn't changed the
6522 * next time it is saved over.
6524 * Returns: %TRUE if successful. If an error
6525 * has occurred, this function will return %FALSE and set @error
6526 * appropriately if present.
6529 g_file_replace_contents (GFile *file,
6530 const char *contents,
6533 gboolean make_backup,
6534 GFileCreateFlags flags,
6536 GCancellable *cancellable,
6539 GFileOutputStream *out;
6540 gsize pos, remainder;
6544 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6545 g_return_val_if_fail (contents != NULL, FALSE);
6547 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
6553 while (remainder > 0 &&
6554 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
6556 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
6564 if (remainder > 0 && res < 0)
6566 /* Ignore errors on close */
6567 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
6568 g_object_unref (out);
6570 /* error is set already */
6574 ret = g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error);
6577 *new_etag = g_file_output_stream_get_etag (out);
6579 g_object_unref (out);
6587 GCancellable *cancellable;
6588 GAsyncReadyCallback callback;
6590 const char *content;
6594 } ReplaceContentsData;
6597 replace_contents_data_free (ReplaceContentsData *data)
6600 g_error_free (data->error);
6601 if (data->cancellable)
6602 g_object_unref (data->cancellable);
6603 g_object_unref (data->file);
6604 g_free (data->etag);
6609 replace_contents_close_callback (GObject *obj,
6610 GAsyncResult *close_res,
6613 GOutputStream *stream = G_OUTPUT_STREAM (obj);
6614 ReplaceContentsData *data = user_data;
6615 GSimpleAsyncResult *res;
6617 /* Ignore errors here, we're only reading anyway */
6618 g_output_stream_close_finish (stream, close_res, NULL);
6619 g_object_unref (stream);
6621 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
6623 res = g_simple_async_result_new (G_OBJECT (data->file),
6626 g_file_replace_contents_async);
6627 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_contents_data_free);
6628 g_simple_async_result_complete (res);
6629 g_object_unref (res);
6633 replace_contents_write_callback (GObject *obj,
6634 GAsyncResult *read_res,
6637 GOutputStream *stream = G_OUTPUT_STREAM (obj);
6638 ReplaceContentsData *data = user_data;
6639 GError *error = NULL;
6642 write_size = g_output_stream_write_finish (stream, read_res, &error);
6644 if (write_size <= 0)
6646 /* Error or EOF, close the file */
6648 data->error = error;
6649 g_output_stream_close_async (stream, 0,
6651 replace_contents_close_callback, data);
6653 else if (write_size > 0)
6655 data->pos += write_size;
6657 if (data->pos >= data->length)
6658 g_output_stream_close_async (stream, 0,
6660 replace_contents_close_callback, data);
6662 g_output_stream_write_async (stream,
6663 data->content + data->pos,
6664 data->length - data->pos,
6667 replace_contents_write_callback,
6673 replace_contents_open_callback (GObject *obj,
6674 GAsyncResult *open_res,
6677 GFile *file = G_FILE (obj);
6678 GFileOutputStream *stream;
6679 ReplaceContentsData *data = user_data;
6680 GError *error = NULL;
6681 GSimpleAsyncResult *res;
6683 stream = g_file_replace_finish (file, open_res, &error);
6687 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
6688 data->content + data->pos,
6689 data->length - data->pos,
6692 replace_contents_write_callback,
6698 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
6702 g_simple_async_result_complete (res);
6703 g_error_free (error);
6704 replace_contents_data_free (data);
6705 g_object_unref (res);
6710 * g_file_replace_contents_async:
6711 * @file: input #GFile.
6712 * @contents: string of contents to replace the file with.
6713 * @length: the length of @contents in bytes.
6714 * @etag: a new <link linkend="gfile-etag">entity tag</link> for the @file, or %NULL
6715 * @make_backup: %TRUE if a backup should be created.
6716 * @flags: a set of #GFileCreateFlags.
6717 * @cancellable: optional #GCancellable object, %NULL to ignore.
6718 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6719 * @user_data: the data to pass to callback function
6721 * Starts an asynchronous replacement of @file with the given
6722 * @contents of @length bytes. @etag will replace the document's
6723 * current entity tag.
6725 * When this operation has completed, @callback will be called with
6726 * @user_user data, and the operation can be finalized with
6727 * g_file_replace_contents_finish().
6729 * If @cancellable is not %NULL, then the operation can be cancelled by
6730 * triggering the cancellable object from another thread. If the operation
6731 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6733 * If @make_backup is %TRUE, this function will attempt to
6734 * make a backup of @file.
6737 g_file_replace_contents_async (GFile *file,
6738 const char *contents,
6741 gboolean make_backup,
6742 GFileCreateFlags flags,
6743 GCancellable *cancellable,
6744 GAsyncReadyCallback callback,
6747 ReplaceContentsData *data;
6749 g_return_if_fail (G_IS_FILE (file));
6750 g_return_if_fail (contents != NULL);
6752 data = g_new0 (ReplaceContentsData, 1);
6755 data->cancellable = g_object_ref (cancellable);
6756 data->callback = callback;
6757 data->user_data = user_data;
6758 data->content = contents;
6759 data->length = length;
6761 data->file = g_object_ref (file);
6763 g_file_replace_async (file,
6769 replace_contents_open_callback,
6774 * g_file_replace_contents_finish:
6775 * @file: input #GFile.
6776 * @res: a #GAsyncResult.
6777 * @new_etag: a location of a new <link linkend="gfile-etag">entity tag</link>
6778 * for the document. This should be freed with g_free() when it is no
6779 * longer needed, or %NULL
6780 * @error: a #GError, or %NULL
6782 * Finishes an asynchronous replace of the given @file. See
6783 * g_file_replace_contents_async(). Sets @new_etag to the new entity
6784 * tag for the document, if present.
6786 * Returns: %TRUE on success, %FALSE on failure.
6789 g_file_replace_contents_finish (GFile *file,
6794 GSimpleAsyncResult *simple;
6795 ReplaceContentsData *data;
6797 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6798 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
6800 simple = G_SIMPLE_ASYNC_RESULT (res);
6802 if (g_simple_async_result_propagate_error (simple, error))
6805 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_replace_contents_async);
6807 data = g_simple_async_result_get_op_res_gpointer (simple);
6811 g_propagate_error (error, data->error);
6819 *new_etag = data->etag;
6820 data->etag = NULL; /* Take ownership */
6827 * g_file_start_mountable:
6828 * @file: input #GFile.
6829 * @flags: flags affecting the operation
6830 * @start_operation: a #GMountOperation, or %NULL to avoid user interaction.
6831 * @cancellable: optional #GCancellable object, %NULL to ignore.
6832 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
6833 * @user_data: the data to pass to callback function
6835 * Starts a file of type G_FILE_TYPE_MOUNTABLE.
6836 * Using @start_operation, you can request callbacks when, for instance,
6837 * passwords are needed during authentication.
6839 * If @cancellable is not %NULL, then the operation can be cancelled by
6840 * triggering the cancellable object from another thread. If the operation
6841 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6843 * When the operation is finished, @callback will be called. You can then call
6844 * g_file_mount_mountable_finish() to get the result of the operation.
6849 g_file_start_mountable (GFile *file,
6850 GDriveStartFlags flags,
6851 GMountOperation *start_operation,
6852 GCancellable *cancellable,
6853 GAsyncReadyCallback callback,
6858 g_return_if_fail (G_IS_FILE (file));
6860 iface = G_FILE_GET_IFACE (file);
6862 if (iface->start_mountable == NULL)
6864 g_simple_async_report_error_in_idle (G_OBJECT (file),
6868 G_IO_ERROR_NOT_SUPPORTED,
6869 _("Operation not supported"));
6873 (* iface->start_mountable) (file,
6882 * g_file_start_mountable_finish:
6883 * @file: input #GFile.
6884 * @result: a #GAsyncResult.
6885 * @error: a #GError, or %NULL
6887 * Finishes a start operation. See g_file_start_mountable() for details.
6889 * Finish an asynchronous start operation that was started
6890 * with g_file_start_mountable().
6892 * Returns: %TRUE if the operation finished successfully. %FALSE
6898 g_file_start_mountable_finish (GFile *file,
6899 GAsyncResult *result,
6904 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6905 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
6907 if (G_IS_SIMPLE_ASYNC_RESULT (result))
6909 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
6910 if (g_simple_async_result_propagate_error (simple, error))
6914 iface = G_FILE_GET_IFACE (file);
6915 return (* iface->start_mountable_finish) (file, result, error);
6919 * g_file_stop_mountable:
6920 * @file: input #GFile.
6921 * @flags: flags affecting the operation
6922 * @mount_operation: a #GMountOperation, or %NULL to avoid user interaction.
6923 * @cancellable: optional #GCancellable object, %NULL to ignore.
6924 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
6925 * @user_data: the data to pass to callback function
6927 * Stops a file of type G_FILE_TYPE_MOUNTABLE.
6929 * If @cancellable is not %NULL, then the operation can be cancelled by
6930 * triggering the cancellable object from another thread. If the operation
6931 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6933 * When the operation is finished, @callback will be called. You can then call
6934 * g_file_stop_mountable_finish() to get the result of the operation.
6939 g_file_stop_mountable (GFile *file,
6940 GMountUnmountFlags flags,
6941 GMountOperation *mount_operation,
6942 GCancellable *cancellable,
6943 GAsyncReadyCallback callback,
6948 g_return_if_fail (G_IS_FILE (file));
6950 iface = G_FILE_GET_IFACE (file);
6952 if (iface->stop_mountable == NULL)
6954 g_simple_async_report_error_in_idle (G_OBJECT (file),
6958 G_IO_ERROR_NOT_SUPPORTED,
6959 _("Operation not supported"));
6963 (* iface->stop_mountable) (file,
6972 * g_file_stop_mountable_finish:
6973 * @file: input #GFile.
6974 * @result: a #GAsyncResult.
6975 * @error: a #GError, or %NULL
6977 * Finishes an stop operation, see g_file_stop_mountable() for details.
6979 * Finish an asynchronous stop operation that was started
6980 * with g_file_stop_mountable().
6982 * Returns: %TRUE if the operation finished successfully. %FALSE
6988 g_file_stop_mountable_finish (GFile *file,
6989 GAsyncResult *result,
6994 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6995 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
6997 if (G_IS_SIMPLE_ASYNC_RESULT (result))
6999 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
7000 if (g_simple_async_result_propagate_error (simple, error))
7004 iface = G_FILE_GET_IFACE (file);
7005 return (* iface->stop_mountable_finish) (file, result, error);
7009 * g_file_poll_mountable:
7010 * @file: input #GFile.
7011 * @cancellable: optional #GCancellable object, %NULL to ignore.
7012 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
7013 * @user_data: the data to pass to callback function
7015 * Polls a file of type G_FILE_TYPE_MOUNTABLE.
7017 * If @cancellable is not %NULL, then the operation can be cancelled by
7018 * triggering the cancellable object from another thread. If the operation
7019 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7021 * When the operation is finished, @callback will be called. You can then call
7022 * g_file_mount_mountable_finish() to get the result of the operation.
7027 g_file_poll_mountable (GFile *file,
7028 GCancellable *cancellable,
7029 GAsyncReadyCallback callback,
7034 g_return_if_fail (G_IS_FILE (file));
7036 iface = G_FILE_GET_IFACE (file);
7038 if (iface->poll_mountable == NULL)
7040 g_simple_async_report_error_in_idle (G_OBJECT (file),
7044 G_IO_ERROR_NOT_SUPPORTED,
7045 _("Operation not supported"));
7049 (* iface->poll_mountable) (file,
7056 * g_file_poll_mountable_finish:
7057 * @file: input #GFile.
7058 * @result: a #GAsyncResult.
7059 * @error: a #GError, or %NULL
7061 * Finishes a poll operation. See g_file_poll_mountable() for details.
7063 * Finish an asynchronous poll operation that was polled
7064 * with g_file_poll_mountable().
7066 * Returns: %TRUE if the operation finished successfully. %FALSE
7072 g_file_poll_mountable_finish (GFile *file,
7073 GAsyncResult *result,
7078 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7079 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7081 if (G_IS_SIMPLE_ASYNC_RESULT (result))
7083 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
7084 if (g_simple_async_result_propagate_error (simple, error))
7088 iface = G_FILE_GET_IFACE (file);
7089 return (* iface->poll_mountable_finish) (file, result, error);
7093 * g_file_supports_thread_contexts:
7096 * Checks if @file supports <link
7097 * linkend="g-main-context-push-thread-default-context">thread-default
7098 * contexts</link>. If this returns %FALSE, you cannot perform
7099 * asynchronous operations on @file in a thread that has a
7100 * thread-default context.
7102 * Returns: Whether or not @file supports thread-default contexts.
7107 g_file_supports_thread_contexts (GFile *file)
7111 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7113 iface = G_FILE_GET_IFACE (file);
7114 return iface->supports_thread_contexts;
7117 #define __G_FILE_C__
7118 #include "gioaliasdef.c"