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 "glocalfile.h"
35 #include "gsimpleasyncresult.h"
36 #include "gfileattribute-priv.h"
37 #include "gpollfilemonitor.h"
39 #include "gfileinputstream.h"
40 #include "gfileoutputstream.h"
41 #include "gcancellable.h"
42 #include "gasyncresult.h"
50 * @short_description: File and Directory Handling
52 * @see_also: #GFileInfo, #GFileEnumerator
54 * #GFile is a high level abstraction for manipulating files on a
55 * virtual file system. #GFile<!-- -->s are lightweight, immutable
56 * objects that do no I/O upon creation. It is necessary to understand that
57 * #GFile objects do not represent files, merely an identifier for a file. All
58 * file content I/O is implemented as streaming operations (see #GInputStream and
61 * To construct a #GFile, you can use:
62 * g_file_new_for_path() if you have a path.
63 * g_file_new_for_uri() if you have a URI.
64 * g_file_new_for_commandline_arg() for a command line argument.
65 * g_file_parse_name() from a utf8 string gotten from g_file_get_parse_name().
67 * One way to think of a #GFile is as an abstraction of a pathname. For normal
68 * files the system pathname is what is stored internally, but as #GFile<!-- -->s
69 * are extensible it could also be something else that corresponds to a pathname
70 * in a userspace implementation of a filesystem.
72 * #GFile<!-- -->s make up hierarchies of directories and files that correspond to the
73 * files on a filesystem. You can move through the file system with #GFile using
74 * g_file_get_parent() to get an identifier for the parent directory, g_file_get_child()
75 * to get a child within a directory, g_file_resolve_relative_path() to resolve a relative
76 * path between two #GFile<!-- -->s. There can be multiple hierarchies, so you may not
77 * end up at the same root if you repeatedly call g_file_get_parent() on two different
80 * All #GFile<!-- -->s have a basename (get with g_file_get_basename()). These names
81 * are byte strings that are used to identify the file on the filesystem (relative to
82 * its parent directory) and there is no guarantees that they have any particular charset
83 * encoding or even make any sense at all. If you want to use filenames in a user
84 * interface you should use the display name that you can get by requesting the
85 * %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME attribute with g_file_query_info().
86 * This is guaranteed to be in utf8 and can be used in a user interface. But always
87 * store the real basename or the #GFile to use to actually access the file, because
88 * there is no way to go from a display name to the actual name.
90 * Using #GFile as an identifier has the same weaknesses as using a path in that
91 * there may be multiple aliases for the same file. For instance, hard or
92 * soft links may cause two different #GFile<!-- -->s to refer to the same file.
93 * Other possible causes for aliases are: case insensitive filesystems, short
94 * and long names on Fat/NTFS, or bind mounts in Linux. If you want to check if
95 * two #GFile<!-- -->s point to the same file you can query for the
96 * %G_FILE_ATTRIBUTE_ID_FILE attribute. Note that #GFile does some trivial
97 * canonicalization of pathnames passed in, so that trivial differences in the
98 * path string used at creation (duplicated slashes, slash at end of path, "."
99 * or ".." path segments, etc) does not create different #GFile<!-- -->s.
101 * Many #GFile operations have both synchronous and asynchronous versions
102 * to suit your application. Asynchronous versions of synchronous functions
103 * simply have _async() appended to their function names. The asynchronous
104 * I/O functions call a #GAsyncReadyCallback which is then used to finalize
105 * the operation, producing a GAsyncResult which is then passed to the
106 * function's matching _finish() operation.
108 * Some #GFile operations do not have synchronous analogs, as they may
109 * take a very long time to finish, and blocking may leave an application
110 * unusable. Notable cases include:
111 * g_file_mount_mountable() to mount a mountable file.
112 * g_file_unmount_mountable() to unmount a mountable file.
113 * g_file_eject_mountable() to eject a mountable file.
115 * <para id="gfile-etag"><indexterm><primary>entity tag</primary></indexterm>
116 * One notable feature of #GFile<!-- -->s are entity tags, or "etags" for
117 * short. Entity tags are somewhat like a more abstract version of the
118 * traditional mtime, and can be used to quickly determine if the file has
119 * been modified from the version on the file system. See the HTTP 1.1
120 * <ulink url="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html">specification</ulink>
121 * for HTTP Etag headers, which are a very similar concept.
125 static void g_file_base_init (gpointer g_class);
126 static void g_file_class_init (gpointer g_class,
127 gpointer class_data);
129 static void g_file_real_query_info_async (GFile *file,
130 const char *attributes,
131 GFileQueryInfoFlags flags,
133 GCancellable *cancellable,
134 GAsyncReadyCallback callback,
136 static GFileInfo * g_file_real_query_info_finish (GFile *file,
139 static void g_file_real_query_filesystem_info_async (GFile *file,
140 const char *attributes,
142 GCancellable *cancellable,
143 GAsyncReadyCallback callback,
145 static GFileInfo * g_file_real_query_filesystem_info_finish (GFile *file,
148 static void g_file_real_enumerate_children_async (GFile *file,
149 const char *attributes,
150 GFileQueryInfoFlags flags,
152 GCancellable *cancellable,
153 GAsyncReadyCallback callback,
155 static GFileEnumerator * g_file_real_enumerate_children_finish (GFile *file,
158 static void g_file_real_read_async (GFile *file,
160 GCancellable *cancellable,
161 GAsyncReadyCallback callback,
163 static GFileInputStream * g_file_real_read_finish (GFile *file,
166 static void g_file_real_append_to_async (GFile *file,
167 GFileCreateFlags flags,
169 GCancellable *cancellable,
170 GAsyncReadyCallback callback,
172 static GFileOutputStream *g_file_real_append_to_finish (GFile *file,
175 static void g_file_real_create_async (GFile *file,
176 GFileCreateFlags flags,
178 GCancellable *cancellable,
179 GAsyncReadyCallback callback,
181 static GFileOutputStream *g_file_real_create_finish (GFile *file,
184 static void g_file_real_replace_async (GFile *file,
186 gboolean make_backup,
187 GFileCreateFlags flags,
189 GCancellable *cancellable,
190 GAsyncReadyCallback callback,
192 static GFileOutputStream *g_file_real_replace_finish (GFile *file,
195 static gboolean g_file_real_set_attributes_from_info (GFile *file,
197 GFileQueryInfoFlags flags,
198 GCancellable *cancellable,
200 static void g_file_real_set_display_name_async (GFile *file,
201 const char *display_name,
203 GCancellable *cancellable,
204 GAsyncReadyCallback callback,
206 static GFile * g_file_real_set_display_name_finish (GFile *file,
209 static void g_file_real_set_attributes_async (GFile *file,
211 GFileQueryInfoFlags flags,
213 GCancellable *cancellable,
214 GAsyncReadyCallback callback,
216 static gboolean g_file_real_set_attributes_finish (GFile *file,
220 static void g_file_real_find_enclosing_mount_async (GFile *file,
222 GCancellable *cancellable,
223 GAsyncReadyCallback callback,
225 static GMount * g_file_real_find_enclosing_mount_finish (GFile *file,
228 static void g_file_real_copy_async (GFile *source,
230 GFileCopyFlags flags,
232 GCancellable *cancellable,
233 GFileProgressCallback progress_callback,
234 gpointer progress_callback_data,
235 GAsyncReadyCallback callback,
237 static gboolean g_file_real_copy_finish (GFile *file,
242 g_file_get_type (void)
244 static volatile gsize g_define_type_id__volatile = 0;
246 if (g_once_init_enter (&g_define_type_id__volatile))
248 const GTypeInfo file_info =
250 sizeof (GFileIface), /* class_size */
251 g_file_base_init, /* base_init */
252 NULL, /* base_finalize */
254 NULL, /* class_finalize */
255 NULL, /* class_data */
260 GType g_define_type_id =
261 g_type_register_static (G_TYPE_INTERFACE, I_("GFile"),
264 g_type_interface_add_prerequisite (g_define_type_id, G_TYPE_OBJECT);
266 g_once_init_leave (&g_define_type_id__volatile, g_define_type_id);
269 return g_define_type_id__volatile;
273 g_file_class_init (gpointer g_class,
276 GFileIface *iface = g_class;
278 iface->enumerate_children_async = g_file_real_enumerate_children_async;
279 iface->enumerate_children_finish = g_file_real_enumerate_children_finish;
280 iface->set_display_name_async = g_file_real_set_display_name_async;
281 iface->set_display_name_finish = g_file_real_set_display_name_finish;
282 iface->query_info_async = g_file_real_query_info_async;
283 iface->query_info_finish = g_file_real_query_info_finish;
284 iface->query_filesystem_info_async = g_file_real_query_filesystem_info_async;
285 iface->query_filesystem_info_finish = g_file_real_query_filesystem_info_finish;
286 iface->set_attributes_async = g_file_real_set_attributes_async;
287 iface->set_attributes_finish = g_file_real_set_attributes_finish;
288 iface->read_async = g_file_real_read_async;
289 iface->read_finish = g_file_real_read_finish;
290 iface->append_to_async = g_file_real_append_to_async;
291 iface->append_to_finish = g_file_real_append_to_finish;
292 iface->create_async = g_file_real_create_async;
293 iface->create_finish = g_file_real_create_finish;
294 iface->replace_async = g_file_real_replace_async;
295 iface->replace_finish = g_file_real_replace_finish;
296 iface->find_enclosing_mount_async = g_file_real_find_enclosing_mount_async;
297 iface->find_enclosing_mount_finish = g_file_real_find_enclosing_mount_finish;
298 iface->set_attributes_from_info = g_file_real_set_attributes_from_info;
299 iface->copy_async = g_file_real_copy_async;
300 iface->copy_finish = g_file_real_copy_finish;
304 g_file_base_init (gpointer g_class)
311 * @file: input #GFile.
313 * Checks to see if a file is native to the platform.
315 * A native file s one expressed in the platform-native filename format,
316 * e.g. "C:\Windows" or "/usr/bin/". This does not mean the file is local,
317 * as it might be on a locally mounted remote filesystem.
319 * On some systems non-native files may be available using
320 * the native filesystem via a userspace filesystem (FUSE), in
321 * these cases this call will return %FALSE, but g_file_get_path()
322 * will still return a native path.
324 * This call does no blocking i/o.
326 * Returns: %TRUE if file is native.
329 g_file_is_native (GFile *file)
333 g_return_val_if_fail (G_IS_FILE (file), FALSE);
335 iface = G_FILE_GET_IFACE (file);
337 return (* iface->is_native) (file);
342 * g_file_has_uri_scheme:
343 * @file: input #GFile.
344 * @uri_scheme: a string containing a URI scheme.
346 * Checks to see if a #GFile has a given URI scheme.
348 * This call does no blocking i/o.
350 * Returns: %TRUE if #GFile's backend supports the
351 * given URI scheme, %FALSE if URI scheme is %NULL,
352 * not supported, or #GFile is invalid.
355 g_file_has_uri_scheme (GFile *file,
356 const char *uri_scheme)
360 g_return_val_if_fail (G_IS_FILE (file), FALSE);
361 g_return_val_if_fail (uri_scheme != NULL, FALSE);
363 iface = G_FILE_GET_IFACE (file);
365 return (* iface->has_uri_scheme) (file, uri_scheme);
370 * g_file_get_uri_scheme:
371 * @file: input #GFile.
373 * Gets the URI scheme for a #GFile.
374 * RFC 3986 decodes the scheme as:
376 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
378 * Common schemes include "file", "http", "ftp", etc.
380 * This call does no blocking i/o.
382 * Returns: a string containing the URI scheme for the given
383 * #GFile. The returned string should be freed with g_free()
384 * when no longer needed.
387 g_file_get_uri_scheme (GFile *file)
391 g_return_val_if_fail (G_IS_FILE (file), NULL);
393 iface = G_FILE_GET_IFACE (file);
395 return (* iface->get_uri_scheme) (file);
400 * g_file_get_basename:
401 * @file: input #GFile.
403 * Gets the base name (the last component of the path) for a given #GFile.
405 * If called for the top level of a system (such as the filesystem root
406 * or a uri like sftp://host/) it will return a single directory separator
407 * (and on Windows, possibly a drive letter).
409 * The base name is a byte string (*not* UTF-8). It has no defined encoding
410 * or rules other than it may not contain zero bytes. If you want to use
411 * filenames in a user interface you should use the display name that you
412 * can get by requesting the %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME
413 * attribute with g_file_query_info().
415 * This call does no blocking i/o.
417 * Returns: string containing the #GFile's base name, or %NULL
418 * if given #GFile is invalid. The returned string should be
419 * freed with g_free() when no longer needed.
422 g_file_get_basename (GFile *file)
426 g_return_val_if_fail (G_IS_FILE (file), NULL);
428 iface = G_FILE_GET_IFACE (file);
430 return (* iface->get_basename) (file);
435 * @file: input #GFile.
437 * Gets the local pathname for #GFile, if one exists.
439 * This call does no blocking i/o.
441 * Returns: string containing the #GFile's path, or %NULL if
442 * no such path exists. The returned string should be
443 * freed with g_free() when no longer needed.
446 g_file_get_path (GFile *file)
450 g_return_val_if_fail (G_IS_FILE (file), NULL);
452 iface = G_FILE_GET_IFACE (file);
454 return (* iface->get_path) (file);
459 * @file: input #GFile.
461 * Gets the URI for the @file.
463 * This call does no blocking i/o.
465 * Returns: a string containing the #GFile's URI.
466 * The returned string should be freed with g_free() when no longer needed.
469 g_file_get_uri (GFile *file)
473 g_return_val_if_fail (G_IS_FILE (file), NULL);
475 iface = G_FILE_GET_IFACE (file);
477 return (* iface->get_uri) (file);
481 * g_file_get_parse_name:
482 * @file: input #GFile.
484 * Gets the parse name of the @file.
485 * A parse name is a UTF-8 string that describes the
486 * file such that one can get the #GFile back using
487 * g_file_parse_name().
489 * This is generally used to show the #GFile as a nice
490 * full-pathname kind of string in a user interface,
491 * like in a location entry.
493 * For local files with names that can safely be converted
494 * to UTF8 the pathname is used, otherwise the IRI is used
495 * (a form of URI that allows UTF8 characters unescaped).
497 * This call does no blocking i/o.
499 * Returns: a string containing the #GFile's parse name. The returned
500 * string should be freed with g_free() when no longer needed.
503 g_file_get_parse_name (GFile *file)
507 g_return_val_if_fail (G_IS_FILE (file), NULL);
509 iface = G_FILE_GET_IFACE (file);
511 return (* iface->get_parse_name) (file);
516 * @file: input #GFile.
518 * Duplicates a #GFile handle. This operation does not duplicate
519 * the actual file or directory represented by the #GFile; see
520 * g_file_copy() if attempting to copy a file.
522 * This call does no blocking i/o.
524 * Returns: a new #GFile that is a duplicate of the given #GFile.
527 g_file_dup (GFile *file)
531 g_return_val_if_fail (G_IS_FILE (file), NULL);
533 iface = G_FILE_GET_IFACE (file);
535 return (* iface->dup) (file);
540 * @file: #gconstpointer to a #GFile.
542 * Creates a hash value for a #GFile.
544 * This call does no blocking i/o.
546 * Returns: 0 if @file is not a valid #GFile, otherwise an
547 * integer that can be used as hash value for the #GFile.
548 * This function is intended for easily hashing a #GFile to
549 * add to a #GHashTable or similar data structure.
552 g_file_hash (gconstpointer file)
556 g_return_val_if_fail (G_IS_FILE (file), 0);
558 iface = G_FILE_GET_IFACE (file);
560 return (* iface->hash) ((GFile *)file);
565 * @file1: the first #GFile.
566 * @file2: the second #GFile.
568 * Checks equality of two given #GFile<!-- -->s. Note that two
569 * #GFile<!-- -->s that differ can still refer to the same
570 * file on the filesystem due to various forms of filename
573 * This call does no blocking i/o.
575 * Returns: %TRUE if @file1 and @file2 are equal.
576 * %FALSE if either is not a #GFile.
579 g_file_equal (GFile *file1,
584 g_return_val_if_fail (G_IS_FILE (file1), FALSE);
585 g_return_val_if_fail (G_IS_FILE (file2), FALSE);
587 if (G_TYPE_FROM_INSTANCE (file1) != G_TYPE_FROM_INSTANCE (file2))
590 iface = G_FILE_GET_IFACE (file1);
592 return (* iface->equal) (file1, file2);
598 * @file: input #GFile.
600 * Gets the parent directory for the @file.
601 * If the @file represents the root directory of the
602 * file system, then %NULL will be returned.
604 * This call does no blocking i/o.
606 * Returns: a #GFile structure to the parent of the given
607 * #GFile or %NULL if there is no parent.
608 * Free the returned object with g_object_unref().
611 g_file_get_parent (GFile *file)
615 g_return_val_if_fail (G_IS_FILE (file), NULL);
617 iface = G_FILE_GET_IFACE (file);
619 return (* iface->get_parent) (file);
624 * @file: input #GFile.
625 * @name: string containing the child's basename.
627 * Gets a child of @file with basename equal to @name.
629 * Note that the file with that specific name might not exist, but
630 * you can still have a #GFile that points to it. You can use this
631 * for instance to create that file.
633 * This call does no blocking i/o.
635 * Returns: a #GFile to a child specified by @name.
636 * Free the returned object with g_object_unref().
639 g_file_get_child (GFile *file,
642 g_return_val_if_fail (G_IS_FILE (file), NULL);
643 g_return_val_if_fail (name != NULL, NULL);
645 return g_file_resolve_relative_path (file, name);
649 * g_file_get_child_for_display_name:
650 * @file: input #GFile.
651 * @display_name: string to a possible child.
654 * Gets the child of @file for a given @display_name (i.e. a UTF8
655 * version of the name). If this function fails, it returns %NULL and @error will be
656 * set. This is very useful when constructing a GFile for a new file
657 * and the user entered the filename in the user interface, for instance
658 * when you select a directory and type a filename in the file selector.
660 * This call does no blocking i/o.
662 * Returns: a #GFile to the specified child, or
663 * %NULL if the display name couldn't be converted.
664 * Free the returned object with g_object_unref().
667 g_file_get_child_for_display_name (GFile *file,
668 const char *display_name,
673 g_return_val_if_fail (G_IS_FILE (file), NULL);
674 g_return_val_if_fail (display_name != NULL, NULL);
676 iface = G_FILE_GET_IFACE (file);
678 return (* iface->get_child_for_display_name) (file, display_name, error);
683 * @file: input #GFile.
684 * @prefix: input #GFile.
686 * Checks whether @file has the prefix specified by @prefix. In other word,
687 * if the names of inital elements of @file<!-- -->s pathname match @prefix.
689 * This call does no i/o, as it works purely on names. As such it can
690 * sometimes return %FALSE even if @file is inside a @prefix (from a
691 * filesystem point of view), because the prefix of @file is an alias
694 * Returns: %TRUE if the @files's parent, grandparent, etc is @prefix.
698 g_file_has_prefix (GFile *file,
703 g_return_val_if_fail (G_IS_FILE (file), FALSE);
704 g_return_val_if_fail (G_IS_FILE (prefix), FALSE);
706 if (G_TYPE_FROM_INSTANCE (file) != G_TYPE_FROM_INSTANCE (prefix))
709 iface = G_FILE_GET_IFACE (file);
711 /* The vtable function differs in arg order since we're
712 using the old contains_file call */
713 return (* iface->prefix_matches) (prefix, file);
717 * g_file_get_relative_path:
718 * @parent: input #GFile.
719 * @descendant: input #GFile.
721 * Gets the path for @descendant relative to @parent.
723 * This call does no blocking i/o.
725 * Returns: string with the relative path from @descendant
726 * to @parent, or %NULL if @descendant doesn't have @parent as prefix.
727 * The returned string should be freed with g_free() when no longer needed.
730 g_file_get_relative_path (GFile *parent,
735 g_return_val_if_fail (G_IS_FILE (parent), NULL);
736 g_return_val_if_fail (G_IS_FILE (descendant), NULL);
738 if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
741 iface = G_FILE_GET_IFACE (parent);
743 return (* iface->get_relative_path) (parent, descendant);
747 * g_file_resolve_relative_path:
748 * @file: input #GFile.
749 * @relative_path: a given relative path string.
751 * Resolves a relative path for @file to an absolute path.
753 * This call does no blocking i/o.
755 * Returns: #GFile to the resolved path. %NULL if @relative_path
756 * is %NULL or if @file is invalid.
757 * Free the returned object with g_object_unref().
760 g_file_resolve_relative_path (GFile *file,
761 const char *relative_path)
765 g_return_val_if_fail (G_IS_FILE (file), NULL);
766 g_return_val_if_fail (relative_path != NULL, NULL);
768 iface = G_FILE_GET_IFACE (file);
770 return (* iface->resolve_relative_path) (file, relative_path);
774 * g_file_enumerate_children:
775 * @file: input #GFile.
776 * @attributes: an attribute query string.
777 * @flags: a set of #GFileQueryInfoFlags.
778 * @cancellable: optional #GCancellable object, %NULL to ignore.
779 * @error: #GError for error reporting.
781 * Gets the requested information about the files in a directory. The result
782 * is a #GFileEnumerator object that will give out #GFileInfo objects for
783 * all the files in the directory.
785 * The @attribute value is a string that specifies the file attributes that
786 * should be gathered. It is not an error if it's not possible to read a particular
787 * requested attribute from a file - it just won't be set. @attribute should
788 * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
789 * means all attributes, and a wildcard like "standard::*" means all attributes in the standard
790 * namespace. An example attribute query be "standard::*,owner::user".
791 * The standard attributes are available as defines, like #G_FILE_ATTRIBUTE_STANDARD_NAME.
793 * If @cancellable is not %NULL, then the operation can be cancelled by
794 * triggering the cancellable object from another thread. If the operation
795 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
797 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
798 * If the file is not a directory, the G_FILE_ERROR_NOTDIR error will be returned.
799 * Other errors are possible too.
801 * Returns: A #GFileEnumerator if successful, %NULL on error.
802 * Free the returned object with g_object_unref().
805 g_file_enumerate_children (GFile *file,
806 const char *attributes,
807 GFileQueryInfoFlags flags,
808 GCancellable *cancellable,
814 g_return_val_if_fail (G_IS_FILE (file), NULL);
816 if (g_cancellable_set_error_if_cancelled (cancellable, error))
819 iface = G_FILE_GET_IFACE (file);
821 if (iface->enumerate_children == NULL)
823 g_set_error_literal (error, G_IO_ERROR,
824 G_IO_ERROR_NOT_SUPPORTED,
825 _("Operation not supported"));
829 return (* iface->enumerate_children) (file, attributes, flags,
834 * g_file_enumerate_children_async:
835 * @file: input #GFile.
836 * @attributes: an attribute query string.
837 * @flags: a set of #GFileQueryInfoFlags.
838 * @io_priority: the <link linkend="io-priority">I/O priority</link>
840 * @cancellable: optional #GCancellable object, %NULL to ignore.
841 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
842 * @user_data: the data to pass to callback function
844 * Asynchronously gets the requested information about the files in a directory. The result
845 * is a #GFileEnumerator object that will give out #GFileInfo objects for
846 * all the files in the directory.
848 * For more details, see g_file_enumerate_children() which is
849 * the synchronous version of this call.
851 * When the operation is finished, @callback will be called. You can then call
852 * g_file_enumerate_children_finish() to get the result of the operation.
855 g_file_enumerate_children_async (GFile *file,
856 const char *attributes,
857 GFileQueryInfoFlags flags,
859 GCancellable *cancellable,
860 GAsyncReadyCallback callback,
865 g_return_if_fail (G_IS_FILE (file));
867 iface = G_FILE_GET_IFACE (file);
868 (* iface->enumerate_children_async) (file,
878 * g_file_enumerate_children_finish:
879 * @file: input #GFile.
880 * @res: a #GAsyncResult.
883 * Finishes an async enumerate children operation.
884 * See g_file_enumerate_children_async().
886 * Returns: a #GFileEnumerator or %NULL if an error occurred.
887 * Free the returned object with g_object_unref().
890 g_file_enumerate_children_finish (GFile *file,
896 g_return_val_if_fail (G_IS_FILE (file), NULL);
897 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
899 if (G_IS_SIMPLE_ASYNC_RESULT (res))
901 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
902 if (g_simple_async_result_propagate_error (simple, error))
906 iface = G_FILE_GET_IFACE (file);
907 return (* iface->enumerate_children_finish) (file, res, error);
911 * g_file_query_exists:
912 * @file: input #GFile.
913 * @cancellable: optional #GCancellable object, %NULL to ignore.
915 * Utility function to check if a particular file exists. This is
916 * implemented using g_file_query_info() and as such does blocking I/O.
918 * Note that in many cases it is racy to first check for file existence
919 * and then execute something based on the outcome of that, because the
920 * file might have been created or removed in between the operations. The
921 * general approach to handling that is to not check, but just do the
922 * operation and handle the errors as they come.
924 * As an example of race-free checking, take the case of reading a file, and
925 * if it doesn't exist, creating it. There are two racy versions: read it, and
926 * on error create it; and: check if it exists, if not create it. These
927 * can both result in two processes creating the file (with perhaps a partially
928 * written file as the result). The correct approach is to always try to create
929 * the file with g_file_create() which will either atomically create the file
930 * or fail with a G_IO_ERROR_EXISTS error.
932 * However, in many cases an existence check is useful in a user
933 * interface, for instance to make a menu item sensitive/insensitive, so that
934 * you don't have to fool users that something is possible and then just show
935 * and error dialog. If you do this, you should make sure to also handle the
936 * errors that can happen due to races when you execute the operation.
938 * Returns: %TRUE if the file exists (and can be detected without error), %FALSE otherwise (or if cancelled).
941 g_file_query_exists (GFile *file,
942 GCancellable *cancellable)
946 g_return_val_if_fail (G_IS_FILE(file), FALSE);
948 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE,
949 G_FILE_QUERY_INFO_NONE, cancellable, NULL);
952 g_object_unref (info);
960 * g_file_query_file_type:
961 * @file: input #GFile.
962 * @flags: a set of #GFileQueryInfoFlags passed to g_file_query_info().
963 * @cancellable: optional #GCancellable object, %NULL to ignore.
965 * Utility function to inspect the #GFileType of a file. This is
966 * implemented using g_file_query_info() and as such does blocking I/O.
968 * The primary use case of this method is to check if a file is a regular file,
969 * directory, or symlink.
971 * Returns: The #GFileType of the file and #G_FILE_TYPE_UNKNOWN if the file
977 g_file_query_file_type (GFile *file,
978 GFileQueryInfoFlags flags,
979 GCancellable *cancellable)
984 g_return_val_if_fail (G_IS_FILE(file), G_FILE_TYPE_UNKNOWN);
985 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE, flags,
989 file_type = g_file_info_get_file_type (info);
990 g_object_unref (info);
993 file_type = G_FILE_TYPE_UNKNOWN;
1000 * @file: input #GFile.
1001 * @attributes: an attribute query string.
1002 * @flags: a set of #GFileQueryInfoFlags.
1003 * @cancellable: optional #GCancellable object, %NULL to ignore.
1004 * @error: a #GError.
1006 * Gets the requested information about specified @file. The result
1007 * is a #GFileInfo object that contains key-value attributes (such as
1008 * the type or size of the file).
1010 * The @attribute value is a string that specifies the file attributes that
1011 * should be gathered. It is not an error if it's not possible to read a particular
1012 * requested attribute from a file - it just won't be set. @attribute should
1013 * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
1014 * means all attributes, and a wildcard like "standard::*" means all attributes in the standard
1015 * namespace. An example attribute query be "standard::*,owner::user".
1016 * The standard attributes are available as defines, like #G_FILE_ATTRIBUTE_STANDARD_NAME.
1018 * If @cancellable is not %NULL, then the operation can be cancelled by
1019 * triggering the cancellable object from another thread. If the operation
1020 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1022 * For symlinks, normally the information about the target of the
1023 * symlink is returned, rather than information about the symlink itself.
1024 * However if you pass #G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS in @flags the
1025 * information about the symlink itself will be returned. Also, for symlinks
1026 * that point to non-existing files the information about the symlink itself
1029 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1030 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1032 * Returns: a #GFileInfo for the given @file, or %NULL on error.
1033 * Free the returned object with g_object_unref().
1036 g_file_query_info (GFile *file,
1037 const char *attributes,
1038 GFileQueryInfoFlags flags,
1039 GCancellable *cancellable,
1044 g_return_val_if_fail (G_IS_FILE (file), NULL);
1046 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1049 iface = G_FILE_GET_IFACE (file);
1051 if (iface->query_info == NULL)
1053 g_set_error_literal (error, G_IO_ERROR,
1054 G_IO_ERROR_NOT_SUPPORTED,
1055 _("Operation not supported"));
1059 return (* iface->query_info) (file, attributes, flags, cancellable, error);
1063 * g_file_query_info_async:
1064 * @file: input #GFile.
1065 * @attributes: an attribute query string.
1066 * @flags: a set of #GFileQueryInfoFlags.
1067 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1069 * @cancellable: optional #GCancellable object, %NULL to ignore.
1070 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1071 * @user_data: the data to pass to callback function
1073 * Asynchronously gets the requested information about specified @file. The result
1074 * is a #GFileInfo object that contains key-value attributes (such as type or size
1077 * For more details, see g_file_query_info() which is
1078 * the synchronous version of this call.
1080 * When the operation is finished, @callback will be called. You can then call
1081 * g_file_query_info_finish() to get the result of the operation.
1084 g_file_query_info_async (GFile *file,
1085 const char *attributes,
1086 GFileQueryInfoFlags flags,
1088 GCancellable *cancellable,
1089 GAsyncReadyCallback callback,
1094 g_return_if_fail (G_IS_FILE (file));
1096 iface = G_FILE_GET_IFACE (file);
1097 (* iface->query_info_async) (file,
1107 * g_file_query_info_finish:
1108 * @file: input #GFile.
1109 * @res: a #GAsyncResult.
1110 * @error: a #GError.
1112 * Finishes an asynchronous file info query.
1113 * See g_file_query_info_async().
1115 * Returns: #GFileInfo for given @file or %NULL on error.
1116 * Free the returned object with g_object_unref().
1119 g_file_query_info_finish (GFile *file,
1125 g_return_val_if_fail (G_IS_FILE (file), NULL);
1126 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1128 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1130 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1131 if (g_simple_async_result_propagate_error (simple, error))
1135 iface = G_FILE_GET_IFACE (file);
1136 return (* iface->query_info_finish) (file, res, error);
1140 * g_file_query_filesystem_info:
1141 * @file: input #GFile.
1142 * @attributes: an attribute query string.
1143 * @cancellable: optional #GCancellable object, %NULL to ignore.
1144 * @error: a #GError.
1146 * Similar to g_file_query_info(), but obtains information
1147 * about the filesystem the @file is on, rather than the file itself.
1148 * For instance the amount of space available and the type of
1151 * The @attribute value is a string that specifies the file attributes that
1152 * should be gathered. It is not an error if it's not possible to read a particular
1153 * requested attribute from a file - it just won't be set. @attribute should
1154 * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
1155 * means all attributes, and a wildcard like "fs:*" means all attributes in the fs
1156 * namespace. The standard namespace for filesystem attributes is "fs".
1157 * Common attributes of interest are #G_FILE_ATTRIBUTE_FILESYSTEM_SIZE
1158 * (the total size of the filesystem in bytes), #G_FILE_ATTRIBUTE_FILESYSTEM_FREE (number of
1159 * bytes available), and #G_FILE_ATTRIBUTE_FILESYSTEM_TYPE (type of the filesystem).
1161 * If @cancellable is not %NULL, then the operation can be cancelled by
1162 * triggering the cancellable object from another thread. If the operation
1163 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1165 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1166 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1168 * Returns: a #GFileInfo or %NULL if there was an error.
1169 * Free the returned object with g_object_unref().
1172 g_file_query_filesystem_info (GFile *file,
1173 const char *attributes,
1174 GCancellable *cancellable,
1179 g_return_val_if_fail (G_IS_FILE (file), NULL);
1181 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1184 iface = G_FILE_GET_IFACE (file);
1186 if (iface->query_filesystem_info == NULL)
1188 g_set_error_literal (error, G_IO_ERROR,
1189 G_IO_ERROR_NOT_SUPPORTED,
1190 _("Operation not supported"));
1194 return (* iface->query_filesystem_info) (file, attributes, cancellable, error);
1198 * g_file_query_filesystem_info_async:
1199 * @file: input #GFile.
1200 * @attributes: an attribute query string.
1201 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1203 * @cancellable: optional #GCancellable object, %NULL to ignore.
1204 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1205 * @user_data: the data to pass to callback function
1207 * Asynchronously gets the requested information about the filesystem
1208 * that the specified @file is on. The result is a #GFileInfo object
1209 * that contains key-value attributes (such as type or size for the
1212 * For more details, see g_file_query_filesystem_info() which is the
1213 * synchronous version of this call.
1215 * When the operation is finished, @callback will be called. You can
1216 * then call g_file_query_info_finish() to get the result of the
1220 g_file_query_filesystem_info_async (GFile *file,
1221 const char *attributes,
1223 GCancellable *cancellable,
1224 GAsyncReadyCallback callback,
1229 g_return_if_fail (G_IS_FILE (file));
1231 iface = G_FILE_GET_IFACE (file);
1232 (* iface->query_filesystem_info_async) (file,
1241 * g_file_query_filesystem_info_finish:
1242 * @file: input #GFile.
1243 * @res: a #GAsyncResult.
1244 * @error: a #GError.
1246 * Finishes an asynchronous filesystem info query. See
1247 * g_file_query_filesystem_info_async().
1249 * Returns: #GFileInfo for given @file or %NULL on error.
1250 * Free the returned object with g_object_unref().
1253 g_file_query_filesystem_info_finish (GFile *file,
1259 g_return_val_if_fail (G_IS_FILE (file), NULL);
1260 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1262 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1264 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1265 if (g_simple_async_result_propagate_error (simple, error))
1269 iface = G_FILE_GET_IFACE (file);
1270 return (* iface->query_filesystem_info_finish) (file, res, error);
1274 * g_file_find_enclosing_mount:
1275 * @file: input #GFile.
1276 * @cancellable: optional #GCancellable object, %NULL to ignore.
1277 * @error: a #GError.
1279 * Gets a #GMount for the #GFile.
1281 * If the #GFileIface for @file does not have a mount (e.g. possibly a
1282 * remote share), @error will be set to %G_IO_ERROR_NOT_FOUND and %NULL
1285 * If @cancellable is not %NULL, then the operation can be cancelled by
1286 * triggering the cancellable object from another thread. If the operation
1287 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1289 * Returns: a #GMount where the @file is located or %NULL on error.
1290 * Free the returned object with g_object_unref().
1293 g_file_find_enclosing_mount (GFile *file,
1294 GCancellable *cancellable,
1299 g_return_val_if_fail (G_IS_FILE (file), NULL);
1301 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1304 iface = G_FILE_GET_IFACE (file);
1305 if (iface->find_enclosing_mount == NULL)
1308 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1309 /* Translators: This is an error message when trying to find the
1310 * enclosing (user visible) mount of a file, but none exists. */
1311 _("Containing mount does not exist"));
1315 return (* iface->find_enclosing_mount) (file, cancellable, error);
1319 * g_file_find_enclosing_mount_async:
1321 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1323 * @cancellable: optional #GCancellable object, %NULL to ignore.
1324 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1325 * @user_data: the data to pass to callback function
1327 * Asynchronously gets the mount for the file.
1329 * For more details, see g_file_find_enclosing_mount() which is
1330 * the synchronous version of this call.
1332 * When the operation is finished, @callback will be called. You can then call
1333 * g_file_find_enclosing_mount_finish() to get the result of the operation.
1336 g_file_find_enclosing_mount_async (GFile *file,
1338 GCancellable *cancellable,
1339 GAsyncReadyCallback callback,
1344 g_return_if_fail (G_IS_FILE (file));
1346 iface = G_FILE_GET_IFACE (file);
1347 (* iface->find_enclosing_mount_async) (file,
1355 * g_file_find_enclosing_mount_finish:
1357 * @res: a #GAsyncResult
1360 * Finishes an asynchronous find mount request.
1361 * See g_file_find_enclosing_mount_async().
1363 * Returns: #GMount for given @file or %NULL on error.
1364 * Free the returned object with g_object_unref().
1367 g_file_find_enclosing_mount_finish (GFile *file,
1373 g_return_val_if_fail (G_IS_FILE (file), NULL);
1374 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1376 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1378 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1379 if (g_simple_async_result_propagate_error (simple, error))
1383 iface = G_FILE_GET_IFACE (file);
1384 return (* iface->find_enclosing_mount_finish) (file, res, error);
1390 * @file: #GFile to read.
1391 * @cancellable: a #GCancellable
1392 * @error: a #GError, or %NULL
1394 * Opens a file for reading. The result is a #GFileInputStream that
1395 * can be used to read the contents of the file.
1397 * If @cancellable is not %NULL, then the operation can be cancelled by
1398 * triggering the cancellable object from another thread. If the operation
1399 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1401 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1402 * If the file is a directory, the G_IO_ERROR_IS_DIRECTORY error will be returned.
1403 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1405 * Returns: #GFileInputStream or %NULL on error.
1406 * Free the returned object with g_object_unref().
1409 g_file_read (GFile *file,
1410 GCancellable *cancellable,
1415 g_return_val_if_fail (G_IS_FILE (file), NULL);
1417 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1420 iface = G_FILE_GET_IFACE (file);
1422 if (iface->read_fn == NULL)
1424 g_set_error_literal (error, G_IO_ERROR,
1425 G_IO_ERROR_NOT_SUPPORTED,
1426 _("Operation not supported"));
1430 return (* iface->read_fn) (file, cancellable, error);
1435 * @file: input #GFile.
1436 * @flags: a set of #GFileCreateFlags.
1437 * @cancellable: optional #GCancellable object, %NULL to ignore.
1438 * @error: a #GError, or %NULL
1440 * Gets an output stream for appending data to the file. If
1441 * the file doesn't already exist it is created.
1443 * By default files created are generally readable by everyone,
1444 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1445 * will be made readable only to the current user, to the level that
1446 * is supported on the target filesystem.
1448 * If @cancellable is not %NULL, then the operation can be cancelled by
1449 * triggering the cancellable object from another thread. If the operation
1450 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1452 * Some file systems don't allow all file names, and may
1453 * return an %G_IO_ERROR_INVALID_FILENAME error.
1454 * If the file is a directory the %G_IO_ERROR_IS_DIRECTORY error will be
1455 * returned. Other errors are possible too, and depend on what kind of
1456 * filesystem the file is on.
1458 * Returns: a #GFileOutputStream, or %NULL on error.
1459 * Free the returned object with g_object_unref().
1462 g_file_append_to (GFile *file,
1463 GFileCreateFlags flags,
1464 GCancellable *cancellable,
1469 g_return_val_if_fail (G_IS_FILE (file), NULL);
1471 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1474 iface = G_FILE_GET_IFACE (file);
1476 if (iface->append_to == NULL)
1478 g_set_error_literal (error, G_IO_ERROR,
1479 G_IO_ERROR_NOT_SUPPORTED,
1480 _("Operation not supported"));
1484 return (* iface->append_to) (file, flags, cancellable, error);
1489 * @file: input #GFile.
1490 * @flags: a set of #GFileCreateFlags.
1491 * @cancellable: optional #GCancellable object, %NULL to ignore.
1492 * @error: a #GError, or %NULL
1494 * Creates a new file and returns an output stream for writing to it.
1495 * The file must not already exist.
1497 * By default files created are generally readable by everyone,
1498 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1499 * will be made readable only to the current user, to the level that
1500 * is supported on the target filesystem.
1502 * If @cancellable is not %NULL, then the operation can be cancelled by
1503 * triggering the cancellable object from another thread. If the operation
1504 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1506 * If a file or directory with this name already exists the G_IO_ERROR_EXISTS
1507 * error will be returned.
1508 * Some file systems don't allow all file names, and may
1509 * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1510 * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1511 * Other errors are possible too, and depend on what kind of
1512 * filesystem the file is on.
1514 * Returns: a #GFileOutputStream for the newly created file, or
1516 * Free the returned object with g_object_unref().
1519 g_file_create (GFile *file,
1520 GFileCreateFlags flags,
1521 GCancellable *cancellable,
1526 g_return_val_if_fail (G_IS_FILE (file), NULL);
1528 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1531 iface = G_FILE_GET_IFACE (file);
1533 if (iface->create == NULL)
1535 g_set_error_literal (error, G_IO_ERROR,
1536 G_IO_ERROR_NOT_SUPPORTED,
1537 _("Operation not supported"));
1541 return (* iface->create) (file, flags, cancellable, error);
1546 * @file: input #GFile.
1547 * @etag: an optional <link linkend="gfile-etag">entity tag</link> for the
1548 * current #GFile, or #NULL to ignore.
1549 * @make_backup: %TRUE if a backup should be created.
1550 * @flags: a set of #GFileCreateFlags.
1551 * @cancellable: optional #GCancellable object, %NULL to ignore.
1552 * @error: a #GError, or %NULL
1554 * Returns an output stream for overwriting the file, possibly
1555 * creating a backup copy of the file first. If the file doesn't exist,
1556 * it will be created.
1558 * This will try to replace the file in the safest way possible so
1559 * that any errors during the writing will not affect an already
1560 * existing copy of the file. For instance, for local files it
1561 * may write to a temporary file and then atomically rename over
1562 * the destination when the stream is closed.
1564 * By default files created are generally readable by everyone,
1565 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1566 * will be made readable only to the current user, to the level that
1567 * is supported on the target filesystem.
1569 * If @cancellable is not %NULL, then the operation can be cancelled by
1570 * triggering the cancellable object from another thread. If the operation
1571 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1573 * If you pass in a non-#NULL @etag value, then this value is
1574 * compared to the current entity tag of the file, and if they differ
1575 * an G_IO_ERROR_WRONG_ETAG error is returned. This generally means
1576 * that the file has been changed since you last read it. You can get
1577 * the new etag from g_file_output_stream_get_etag() after you've
1578 * finished writing and closed the #GFileOutputStream. When you load
1579 * a new file you can use g_file_input_stream_query_info() to get
1580 * the etag of the file.
1582 * If @make_backup is %TRUE, this function will attempt to make a backup
1583 * of the current file before overwriting it. If this fails a G_IO_ERROR_CANT_CREATE_BACKUP
1584 * error will be returned. If you want to replace anyway, try again with
1585 * @make_backup set to %FALSE.
1587 * If the file is a directory the G_IO_ERROR_IS_DIRECTORY error will be returned,
1588 * and if the file is some other form of non-regular file then a
1589 * G_IO_ERROR_NOT_REGULAR_FILE error will be returned.
1590 * Some file systems don't allow all file names, and may
1591 * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1592 * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1593 * Other errors are possible too, and depend on what kind of
1594 * filesystem the file is on.
1596 * Returns: a #GFileOutputStream or %NULL on error.
1597 * Free the returned object with g_object_unref().
1600 g_file_replace (GFile *file,
1602 gboolean make_backup,
1603 GFileCreateFlags flags,
1604 GCancellable *cancellable,
1609 g_return_val_if_fail (G_IS_FILE (file), NULL);
1611 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1614 iface = G_FILE_GET_IFACE (file);
1616 if (iface->replace == NULL)
1618 g_set_error_literal (error, G_IO_ERROR,
1619 G_IO_ERROR_NOT_SUPPORTED,
1620 _("Operation not supported"));
1625 /* Handle empty tag string as NULL in consistent way. */
1626 if (etag && *etag == 0)
1629 return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1633 * g_file_read_async:
1634 * @file: input #GFile.
1635 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1637 * @cancellable: optional #GCancellable object, %NULL to ignore.
1638 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1639 * @user_data: the data to pass to callback function
1641 * Asynchronously opens @file for reading.
1643 * For more details, see g_file_read() which is
1644 * the synchronous version of this call.
1646 * When the operation is finished, @callback will be called. You can then call
1647 * g_file_read_finish() to get the result of the operation.
1650 g_file_read_async (GFile *file,
1652 GCancellable *cancellable,
1653 GAsyncReadyCallback callback,
1658 g_return_if_fail (G_IS_FILE (file));
1660 iface = G_FILE_GET_IFACE (file);
1661 (* iface->read_async) (file,
1669 * g_file_read_finish:
1670 * @file: input #GFile.
1671 * @res: a #GAsyncResult.
1672 * @error: a #GError, or %NULL
1674 * Finishes an asynchronous file read operation started with
1675 * g_file_read_async().
1677 * Returns: a #GFileInputStream or %NULL on error.
1678 * Free the returned object with g_object_unref().
1681 g_file_read_finish (GFile *file,
1687 g_return_val_if_fail (G_IS_FILE (file), NULL);
1688 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1690 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1692 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1693 if (g_simple_async_result_propagate_error (simple, error))
1697 iface = G_FILE_GET_IFACE (file);
1698 return (* iface->read_finish) (file, res, error);
1702 * g_file_append_to_async:
1703 * @file: input #GFile.
1704 * @flags: a set of #GFileCreateFlags.
1705 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1707 * @cancellable: optional #GCancellable object, %NULL to ignore.
1708 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1709 * @user_data: the data to pass to callback function
1711 * Asynchronously opens @file for appending.
1713 * For more details, see g_file_append_to() which is
1714 * the synchronous version of this call.
1716 * When the operation is finished, @callback will be called. You can then call
1717 * g_file_append_to_finish() to get the result of the operation.
1720 g_file_append_to_async (GFile *file,
1721 GFileCreateFlags flags,
1723 GCancellable *cancellable,
1724 GAsyncReadyCallback callback,
1729 g_return_if_fail (G_IS_FILE (file));
1731 iface = G_FILE_GET_IFACE (file);
1732 (* iface->append_to_async) (file,
1741 * g_file_append_to_finish:
1742 * @file: input #GFile.
1743 * @res: #GAsyncResult
1744 * @error: a #GError, or %NULL
1746 * Finishes an asynchronous file append operation started with
1747 * g_file_append_to_async().
1749 * Returns: a valid #GFileOutputStream or %NULL on error.
1750 * Free the returned object with g_object_unref().
1753 g_file_append_to_finish (GFile *file,
1759 g_return_val_if_fail (G_IS_FILE (file), NULL);
1760 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1762 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1764 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1765 if (g_simple_async_result_propagate_error (simple, error))
1769 iface = G_FILE_GET_IFACE (file);
1770 return (* iface->append_to_finish) (file, res, error);
1774 * g_file_create_async:
1775 * @file: input #GFile.
1776 * @flags: a set of #GFileCreateFlags.
1777 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1779 * @cancellable: optional #GCancellable object, %NULL to ignore.
1780 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1781 * @user_data: the data to pass to callback function
1783 * Asynchronously creates a new file and returns an output stream for writing to it.
1784 * The file must not already exist.
1786 * For more details, see g_file_create() which is
1787 * the synchronous version of this call.
1789 * When the operation is finished, @callback will be called. You can then call
1790 * g_file_create_finish() to get the result of the operation.
1793 g_file_create_async (GFile *file,
1794 GFileCreateFlags flags,
1796 GCancellable *cancellable,
1797 GAsyncReadyCallback callback,
1802 g_return_if_fail (G_IS_FILE (file));
1804 iface = G_FILE_GET_IFACE (file);
1805 (* iface->create_async) (file,
1814 * g_file_create_finish:
1815 * @file: input #GFile.
1816 * @res: a #GAsyncResult.
1817 * @error: a #GError, or %NULL
1819 * Finishes an asynchronous file create operation started with
1820 * g_file_create_async().
1822 * Returns: a #GFileOutputStream or %NULL on error.
1823 * Free the returned object with g_object_unref().
1826 g_file_create_finish (GFile *file,
1832 g_return_val_if_fail (G_IS_FILE (file), NULL);
1833 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1835 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1837 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1838 if (g_simple_async_result_propagate_error (simple, error))
1842 iface = G_FILE_GET_IFACE (file);
1843 return (* iface->create_finish) (file, res, error);
1847 * g_file_replace_async:
1848 * @file: input #GFile.
1849 * @etag: an <link linkend="gfile-etag">entity tag</link> for the
1850 * current #GFile, or NULL to ignore.
1851 * @make_backup: %TRUE if a backup should be created.
1852 * @flags: a set of #GFileCreateFlags.
1853 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1855 * @cancellable: optional #GCancellable object, %NULL to ignore.
1856 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1857 * @user_data: the data to pass to callback function
1859 * Asynchronously overwrites the file, replacing the contents, possibly
1860 * creating a backup copy of the file first.
1862 * For more details, see g_file_replace() which is
1863 * the synchronous version of this call.
1865 * When the operation is finished, @callback will be called. You can then call
1866 * g_file_replace_finish() to get the result of the operation.
1869 g_file_replace_async (GFile *file,
1871 gboolean make_backup,
1872 GFileCreateFlags flags,
1874 GCancellable *cancellable,
1875 GAsyncReadyCallback callback,
1880 g_return_if_fail (G_IS_FILE (file));
1882 iface = G_FILE_GET_IFACE (file);
1883 (* iface->replace_async) (file,
1894 * g_file_replace_finish:
1895 * @file: input #GFile.
1896 * @res: a #GAsyncResult.
1897 * @error: a #GError, or %NULL
1899 * Finishes an asynchronous file replace operation started with
1900 * g_file_replace_async().
1902 * Returns: a #GFileOutputStream, or %NULL on error.
1903 * Free the returned object with g_object_unref().
1906 g_file_replace_finish (GFile *file,
1912 g_return_val_if_fail (G_IS_FILE (file), NULL);
1913 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1915 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1917 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1918 if (g_simple_async_result_propagate_error (simple, error))
1922 iface = G_FILE_GET_IFACE (file);
1923 return (* iface->replace_finish) (file, res, error);
1927 copy_symlink (GFile *destination,
1928 GFileCopyFlags flags,
1929 GCancellable *cancellable,
1934 gboolean tried_delete;
1936 GFileType file_type;
1938 tried_delete = FALSE;
1942 if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
1944 /* Maybe it already existed, and we want to overwrite? */
1945 if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) &&
1946 my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
1948 g_error_free (my_error);
1951 /* Don't overwrite if the destination is a directory */
1952 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1953 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1954 cancellable, &my_error);
1957 file_type = g_file_info_get_file_type (info);
1958 g_object_unref (info);
1960 if (file_type == G_FILE_TYPE_DIRECTORY)
1962 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
1963 _("Can't copy over directory"));
1968 if (!g_file_delete (destination, cancellable, error))
1971 tried_delete = TRUE;
1975 g_propagate_error (error, my_error);
1982 static GInputStream *
1983 open_source_for_copy (GFile *source,
1985 GFileCopyFlags flags,
1986 GCancellable *cancellable,
1992 GFileType file_type;
1995 in = (GInputStream *)g_file_read (source, cancellable, &my_error);
1999 /* There was an error opening the source, try to set a good error for it: */
2001 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
2003 /* The source is a directory, don't fail with WOULD_RECURSE immediately,
2004 * as that is less useful to the app. Better check for errors on the
2007 g_error_free (my_error);
2010 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2011 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2012 cancellable, &my_error);
2015 file_type = g_file_info_get_file_type (info);
2016 g_object_unref (info);
2018 if (flags & G_FILE_COPY_OVERWRITE)
2020 if (file_type == G_FILE_TYPE_DIRECTORY)
2022 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
2023 _("Can't copy directory over directory"));
2026 /* continue to would_recurse error */
2030 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
2031 _("Target file exists"));
2037 /* Error getting info from target, return that error
2038 * (except for NOT_FOUND, which is no error here)
2040 if (my_error->domain != G_IO_ERROR && my_error->code != G_IO_ERROR_NOT_FOUND)
2042 g_propagate_error (error, my_error);
2045 g_error_free (my_error);
2048 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
2049 _("Can't recursively copy directory"));
2053 g_propagate_error (error, my_error);
2058 should_copy (GFileAttributeInfo *info,
2062 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
2063 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
2067 build_attribute_list_for_copy (GFileAttributeInfoList *attributes,
2068 GFileAttributeInfoList *namespaces,
2076 s = g_string_new ("");
2080 for (i = 0; i < attributes->n_infos; i++)
2082 if (should_copy (&attributes->infos[i], as_move))
2087 g_string_append_c (s, ',');
2089 g_string_append (s, attributes->infos[i].name);
2096 for (i = 0; i < namespaces->n_infos; i++)
2098 if (should_copy (&namespaces->infos[i], as_move))
2103 g_string_append_c (s, ',');
2105 g_string_append (s, namespaces->infos[i].name);
2106 g_string_append (s, ":*");
2111 return g_string_free (s, FALSE);
2115 * g_file_copy_attributes:
2116 * @source: a #GFile with attributes.
2117 * @destination: a #GFile to copy attributes to.
2118 * @flags: a set of #GFileCopyFlags.
2119 * @cancellable: optional #GCancellable object, %NULL to ignore.
2120 * @error: a #GError, %NULL to ignore.
2122 * Copies the file attributes from @source to @destination.
2124 * Normally only a subset of the file attributes are copied,
2125 * those that are copies in a normal file copy operation
2126 * (which for instance does not include e.g. mtime). However
2127 * if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
2128 * all the metadata that is possible to copy is copied.
2130 * Returns: %TRUE if the attributes were copied successfully, %FALSE otherwise.
2133 g_file_copy_attributes (GFile *source,
2135 GFileCopyFlags flags,
2136 GCancellable *cancellable,
2139 GFileAttributeInfoList *attributes, *namespaces;
2140 char *attrs_to_read;
2144 gboolean source_nofollow_symlinks;
2146 as_move = flags & G_FILE_COPY_ALL_METADATA;
2147 source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
2149 /* Ignore errors here, if the target supports no attributes there is nothing to copy */
2150 attributes = g_file_query_settable_attributes (destination, cancellable, NULL);
2151 namespaces = g_file_query_writable_namespaces (destination, cancellable, NULL);
2153 if (attributes == NULL && namespaces == NULL)
2156 attrs_to_read = build_attribute_list_for_copy (attributes, namespaces, as_move);
2158 /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
2159 * we just don't copy it.
2161 info = g_file_query_info (source, attrs_to_read,
2162 source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
2166 g_free (attrs_to_read);
2171 res = g_file_set_attributes_from_info (destination,
2173 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2176 g_object_unref (info);
2179 g_file_attribute_info_list_unref (attributes);
2180 g_file_attribute_info_list_unref (namespaces);
2185 /* Closes the streams */
2187 copy_stream_with_progress (GInputStream *in,
2190 GCancellable *cancellable,
2191 GFileProgressCallback progress_callback,
2192 gpointer progress_callback_data,
2195 gssize n_read, n_written;
2196 goffset current_size;
2197 char buffer[1024*64], *p;
2203 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
2204 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2208 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2209 total_size = g_file_info_get_size (info);
2210 g_object_unref (info);
2213 if (total_size == -1)
2215 info = g_file_query_info (source,
2216 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2217 G_FILE_QUERY_INFO_NONE,
2221 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2222 total_size = g_file_info_get_size (info);
2223 g_object_unref (info);
2227 if (total_size == -1)
2234 n_read = g_input_stream_read (in, buffer, sizeof (buffer), cancellable, error);
2244 current_size += n_read;
2249 n_written = g_output_stream_write (out, p, n_read, cancellable, error);
2250 if (n_written == -1)
2257 n_read -= n_written;
2263 if (progress_callback)
2264 progress_callback (current_size, total_size, progress_callback_data);
2268 error = NULL; /* Ignore further errors */
2270 /* Make sure we send full copied size */
2271 if (progress_callback)
2272 progress_callback (current_size, total_size, progress_callback_data);
2274 /* Don't care about errors in source here */
2275 g_input_stream_close (in, cancellable, NULL);
2277 /* But write errors on close are bad! */
2278 if (!g_output_stream_close (out, cancellable, error))
2281 g_object_unref (in);
2282 g_object_unref (out);
2288 file_copy_fallback (GFile *source,
2290 GFileCopyFlags flags,
2291 GCancellable *cancellable,
2292 GFileProgressCallback progress_callback,
2293 gpointer progress_callback_data,
2301 /* Maybe copy the symlink? */
2302 if (flags & G_FILE_COPY_NOFOLLOW_SYMLINKS)
2304 info = g_file_query_info (source,
2305 G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
2306 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2312 if (g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK &&
2313 (target = g_file_info_get_symlink_target (info)) != NULL)
2315 if (!copy_symlink (destination, flags, cancellable, target, error))
2317 g_object_unref (info);
2321 g_object_unref (info);
2325 g_object_unref (info);
2328 in = open_source_for_copy (source, destination, flags, cancellable, error);
2332 if (flags & G_FILE_COPY_OVERWRITE)
2334 out = (GOutputStream *)g_file_replace (destination,
2336 flags & G_FILE_COPY_BACKUP,
2338 cancellable, error);
2342 out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
2347 g_object_unref (in);
2351 if (!copy_stream_with_progress (in, out, source, cancellable,
2352 progress_callback, progress_callback_data,
2358 /* Ignore errors here. Failure to copy metadata is not a hard error */
2359 g_file_copy_attributes (source, destination,
2360 flags, cancellable, NULL);
2367 * @source: input #GFile.
2368 * @destination: destination #GFile
2369 * @flags: set of #GFileCopyFlags
2370 * @cancellable: optional #GCancellable object, %NULL to ignore.
2371 * @progress_callback: function to callback with progress information
2372 * @progress_callback_data: user data to pass to @progress_callback
2373 * @error: #GError to set on error, or %NULL
2375 * Copies the file @source to the location specified by @destination.
2376 * Can not handle recursive copies of directories.
2378 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2379 * existing @destination file is overwritten.
2381 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2382 * will be copied as symlinks, otherwise the target of the
2383 * @source symlink will be copied.
2385 * If @cancellable is not %NULL, then the operation can be cancelled by
2386 * triggering the cancellable object from another thread. If the operation
2387 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2389 * If @progress_callback is not %NULL, then the operation can be monitored by
2390 * setting this to a #GFileProgressCallback function. @progress_callback_data
2391 * will be passed to this function. It is guaranteed that this callback will
2392 * be called after all data has been transferred with the total number of bytes
2393 * copied during the operation.
2395 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2396 * error is returned, independent on the status of the @destination.
2398 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2399 * error G_IO_ERROR_EXISTS is returned.
2401 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2402 * error is returned. If trying to overwrite a directory with a directory the
2403 * G_IO_ERROR_WOULD_MERGE error is returned.
2405 * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2406 * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2409 * If you are interested in copying the #GFile object itself (not the on-disk
2410 * file), see g_file_dup().
2412 * Returns: %TRUE on success, %FALSE otherwise.
2415 g_file_copy (GFile *source,
2417 GFileCopyFlags flags,
2418 GCancellable *cancellable,
2419 GFileProgressCallback progress_callback,
2420 gpointer progress_callback_data,
2427 g_return_val_if_fail (G_IS_FILE (source), FALSE);
2428 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2430 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2433 iface = G_FILE_GET_IFACE (destination);
2437 res = (* iface->copy) (source, destination,
2439 progress_callback, progress_callback_data,
2445 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2447 g_propagate_error (error, my_error);
2452 /* If the types are different, and the destination method failed
2453 also try the source method */
2454 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
2456 iface = G_FILE_GET_IFACE (source);
2461 res = (* iface->copy) (source, destination,
2463 progress_callback, progress_callback_data,
2469 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2471 g_propagate_error (error, my_error);
2477 return file_copy_fallback (source, destination, flags, cancellable,
2478 progress_callback, progress_callback_data,
2483 * g_file_copy_async:
2484 * @source: input #GFile.
2485 * @destination: destination #GFile
2486 * @flags: set of #GFileCopyFlags
2487 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2489 * @cancellable: optional #GCancellable object, %NULL to ignore.
2490 * @progress_callback: function to callback with progress information
2491 * @progress_callback_data: user data to pass to @progress_callback
2492 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2493 * @user_data: the data to pass to callback function
2495 * Copies the file @source to the location specified by @destination
2496 * asynchronously. For details of the behaviour, see g_file_copy().
2498 * If @progress_callback is not %NULL, then that function that will be called
2499 * just like in g_file_copy(), however the callback will run in the main loop,
2500 * not in the thread that is doing the I/O operation.
2502 * When the operation is finished, @callback will be called. You can then call
2503 * g_file_copy_finish() to get the result of the operation.
2506 g_file_copy_async (GFile *source,
2508 GFileCopyFlags flags,
2510 GCancellable *cancellable,
2511 GFileProgressCallback progress_callback,
2512 gpointer progress_callback_data,
2513 GAsyncReadyCallback callback,
2518 g_return_if_fail (G_IS_FILE (source));
2519 g_return_if_fail (G_IS_FILE (destination));
2521 iface = G_FILE_GET_IFACE (source);
2522 (* iface->copy_async) (source,
2528 progress_callback_data,
2534 * g_file_copy_finish:
2535 * @file: input #GFile.
2536 * @res: a #GAsyncResult.
2537 * @error: a #GError, or %NULL
2539 * Finishes copying the file started with
2540 * g_file_copy_async().
2542 * Returns: a %TRUE on success, %FALSE on error.
2545 g_file_copy_finish (GFile *file,
2551 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2552 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
2554 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2556 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2558 if (g_simple_async_result_propagate_error (simple, error))
2562 iface = G_FILE_GET_IFACE (file);
2563 return (* iface->copy_finish) (file, res, error);
2568 * @source: #GFile pointing to the source location.
2569 * @destination: #GFile pointing to the destination location.
2570 * @flags: set of #GFileCopyFlags.
2571 * @cancellable: optional #GCancellable object, %NULL to ignore.
2572 * @progress_callback: #GFileProgressCallback function for updates.
2573 * @progress_callback_data: gpointer to user data for the callback function.
2574 * @error: #GError for returning error conditions, or %NULL
2577 * Tries to move the file or directory @source to the location specified by @destination.
2578 * If native move operations are supported then this is used, otherwise a copy + delete
2579 * fallback is used. The native implementation may support moving directories (for instance
2580 * on moves inside the same filesystem), but the fallback code does not.
2582 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2583 * existing @destination file is overwritten.
2585 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2586 * will be copied as symlinks, otherwise the target of the
2587 * @source symlink will be copied.
2589 * If @cancellable is not %NULL, then the operation can be cancelled by
2590 * triggering the cancellable object from another thread. If the operation
2591 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2593 * If @progress_callback is not %NULL, then the operation can be monitored by
2594 * setting this to a #GFileProgressCallback function. @progress_callback_data
2595 * will be passed to this function. It is guaranteed that this callback will
2596 * be called after all data has been transferred with the total number of bytes
2597 * copied during the operation.
2599 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2600 * error is returned, independent on the status of the @destination.
2602 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2603 * error G_IO_ERROR_EXISTS is returned.
2605 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2606 * error is returned. If trying to overwrite a directory with a directory the
2607 * G_IO_ERROR_WOULD_MERGE error is returned.
2609 * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2610 * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2611 * may be returned (if the native move operation isn't available).
2613 * Returns: %TRUE on successful move, %FALSE otherwise.
2616 g_file_move (GFile *source,
2618 GFileCopyFlags flags,
2619 GCancellable *cancellable,
2620 GFileProgressCallback progress_callback,
2621 gpointer progress_callback_data,
2628 g_return_val_if_fail (G_IS_FILE (source), FALSE);
2629 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2631 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2634 iface = G_FILE_GET_IFACE (destination);
2638 res = (* iface->move) (source, destination,
2640 progress_callback, progress_callback_data,
2646 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2648 g_propagate_error (error, my_error);
2653 /* If the types are different, and the destination method failed
2654 also try the source method */
2655 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
2657 iface = G_FILE_GET_IFACE (source);
2662 res = (* iface->move) (source, destination,
2664 progress_callback, progress_callback_data,
2670 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2672 g_propagate_error (error, my_error);
2678 if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
2680 g_set_error_literal (error, G_IO_ERROR,
2681 G_IO_ERROR_NOT_SUPPORTED,
2682 _("Operation not supported"));
2686 flags |= G_FILE_COPY_ALL_METADATA;
2687 if (!g_file_copy (source, destination, flags, cancellable,
2688 progress_callback, progress_callback_data,
2692 return g_file_delete (source, cancellable, error);
2696 * g_file_make_directory
2697 * @file: input #GFile.
2698 * @cancellable: optional #GCancellable object, %NULL to ignore.
2699 * @error: a #GError, or %NULL
2701 * Creates a directory. Note that this will only create a child directory of
2702 * the immediate parent directory of the path or URI given by the #GFile. To
2703 * recursively create directories, see g_file_make_directory_with_parents().
2704 * This function will fail if the parent directory does not exist, setting
2705 * @error to %G_IO_ERROR_NOT_FOUND. If the file system doesn't support creating
2706 * directories, this function will fail, setting @error to
2707 * %G_IO_ERROR_NOT_SUPPORTED.
2709 * If @cancellable is not %NULL, then the operation can be cancelled by
2710 * triggering the cancellable object from another thread. If the operation
2711 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2713 * Returns: %TRUE on successful creation, %FALSE otherwise.
2716 g_file_make_directory (GFile *file,
2717 GCancellable *cancellable,
2722 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2724 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2727 iface = G_FILE_GET_IFACE (file);
2729 if (iface->make_directory == NULL)
2731 g_set_error_literal (error, G_IO_ERROR,
2732 G_IO_ERROR_NOT_SUPPORTED,
2733 _("Operation not supported"));
2737 return (* iface->make_directory) (file, cancellable, error);
2741 * g_file_make_directory_with_parents:
2742 * @file: input #GFile.
2743 * @cancellable: optional #GCancellable object, %NULL to ignore.
2744 * @error: a #GError, or %NULL
2746 * Creates a directory and any parent directories that may not exist similar to
2747 * 'mkdir -p'. If the file system does not support creating directories, this
2748 * function will fail, setting @error to %G_IO_ERROR_NOT_SUPPORTED.
2750 * If @cancellable is not %NULL, then the operation can be cancelled by
2751 * triggering the cancellable object from another thread. If the operation
2752 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2754 * Returns: %TRUE if all directories have been successfully created, %FALSE
2760 g_file_make_directory_with_parents (GFile *file,
2761 GCancellable *cancellable,
2765 GFile *parent_file, *work_file;
2766 GList *list = NULL, *l;
2767 GError *my_error = NULL;
2769 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2772 result = g_file_make_directory (file, cancellable, &my_error);
2773 if (result || my_error->code != G_IO_ERROR_NOT_FOUND)
2776 g_propagate_error (error, my_error);
2782 while (!result && my_error->code == G_IO_ERROR_NOT_FOUND)
2784 g_clear_error (&my_error);
2786 parent_file = g_file_get_parent (work_file);
2787 if (parent_file == NULL)
2789 result = g_file_make_directory (parent_file, cancellable, &my_error);
2791 if (!result && my_error->code == G_IO_ERROR_NOT_FOUND)
2792 list = g_list_prepend (list, parent_file);
2794 work_file = parent_file;
2797 for (l = list; result && l; l = l->next)
2799 result = g_file_make_directory ((GFile *) l->data, cancellable, &my_error);
2803 while (list != NULL)
2805 g_object_unref ((GFile *) list->data);
2806 list = g_list_remove (list, list->data);
2811 g_propagate_error (error, my_error);
2815 return g_file_make_directory (file, cancellable, error);
2819 * g_file_make_symbolic_link:
2820 * @file: input #GFile.
2821 * @symlink_value: a string with the value of the new symlink.
2822 * @cancellable: optional #GCancellable object, %NULL to ignore.
2823 * @error: a #GError.
2825 * Creates a symbolic link.
2827 * If @cancellable is not %NULL, then the operation can be cancelled by
2828 * triggering the cancellable object from another thread. If the operation
2829 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2831 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
2834 g_file_make_symbolic_link (GFile *file,
2835 const char *symlink_value,
2836 GCancellable *cancellable,
2841 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2842 g_return_val_if_fail (symlink_value != NULL, FALSE);
2844 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2847 if (*symlink_value == '\0')
2849 g_set_error_literal (error, G_IO_ERROR,
2850 G_IO_ERROR_INVALID_ARGUMENT,
2851 _("Invalid symlink value given"));
2855 iface = G_FILE_GET_IFACE (file);
2857 if (iface->make_symbolic_link == NULL)
2859 g_set_error_literal (error, G_IO_ERROR,
2860 G_IO_ERROR_NOT_SUPPORTED,
2861 _("Operation not supported"));
2865 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
2870 * @file: input #GFile.
2871 * @cancellable: optional #GCancellable object, %NULL to ignore.
2872 * @error: a #GError, or %NULL
2874 * Deletes a file. If the @file is a directory, it will only be deleted if it
2877 * If @cancellable is not %NULL, then the operation can be cancelled by
2878 * triggering the cancellable object from another thread. If the operation
2879 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2881 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
2884 g_file_delete (GFile *file,
2885 GCancellable *cancellable,
2890 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2892 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2895 iface = G_FILE_GET_IFACE (file);
2897 if (iface->delete_file == NULL)
2899 g_set_error_literal (error, G_IO_ERROR,
2900 G_IO_ERROR_NOT_SUPPORTED,
2901 _("Operation not supported"));
2905 return (* iface->delete_file) (file, cancellable, error);
2910 * @file: #GFile to send to trash.
2911 * @cancellable: optional #GCancellable object, %NULL to ignore.
2912 * @error: a #GError, or %NULL
2914 * Sends @file to the "Trashcan", if possible. This is similar to
2915 * deleting it, but the user can recover it before emptying the trashcan.
2916 * Not all file systems support trashing, so this call can return the
2917 * %G_IO_ERROR_NOT_SUPPORTED error.
2920 * If @cancellable is not %NULL, then the operation can be cancelled by
2921 * triggering the cancellable object from another thread. If the operation
2922 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2924 * Returns: %TRUE on successful trash, %FALSE otherwise.
2927 g_file_trash (GFile *file,
2928 GCancellable *cancellable,
2933 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2935 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2938 iface = G_FILE_GET_IFACE (file);
2940 if (iface->trash == NULL)
2942 g_set_error_literal (error,
2943 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2944 _("Trash not supported"));
2948 return (* iface->trash) (file, cancellable, error);
2952 * g_file_set_display_name:
2953 * @file: input #GFile.
2954 * @display_name: a string.
2955 * @cancellable: optional #GCancellable object, %NULL to ignore.
2956 * @error: a #GError, or %NULL
2958 * Renames @file to the specified display name.
2960 * The display name is converted from UTF8 to the correct encoding for the target
2961 * filesystem if possible and the @file is renamed to this.
2963 * If you want to implement a rename operation in the user interface the edit name
2964 * (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the initial value in the rename
2965 * widget, and then the result after editing should be passed to g_file_set_display_name().
2967 * On success the resulting converted filename is returned.
2969 * If @cancellable is not %NULL, then the operation can be cancelled by
2970 * triggering the cancellable object from another thread. If the operation
2971 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2973 * Returns: a #GFile specifying what @file was renamed to, or %NULL
2974 * if there was an error.
2975 * Free the returned object with g_object_unref().
2978 g_file_set_display_name (GFile *file,
2979 const char *display_name,
2980 GCancellable *cancellable,
2985 g_return_val_if_fail (G_IS_FILE (file), NULL);
2986 g_return_val_if_fail (display_name != NULL, NULL);
2988 if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
2992 G_IO_ERROR_INVALID_ARGUMENT,
2993 _("File names cannot contain '%c'"), G_DIR_SEPARATOR);
2997 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3000 iface = G_FILE_GET_IFACE (file);
3002 return (* iface->set_display_name) (file, display_name, cancellable, error);
3006 * g_file_set_display_name_async:
3007 * @file: input #GFile.
3008 * @display_name: a string.
3009 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3011 * @cancellable: optional #GCancellable object, %NULL to ignore.
3012 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
3013 * @user_data: the data to pass to callback function
3015 * Asynchronously sets the display name for a given #GFile.
3017 * For more details, see g_set_display_name() which is
3018 * the synchronous version of this call.
3020 * When the operation is finished, @callback will be called. You can then call
3021 * g_file_set_display_name_finish() to get the result of the operation.
3024 g_file_set_display_name_async (GFile *file,
3025 const char *display_name,
3027 GCancellable *cancellable,
3028 GAsyncReadyCallback callback,
3033 g_return_if_fail (G_IS_FILE (file));
3034 g_return_if_fail (display_name != NULL);
3036 iface = G_FILE_GET_IFACE (file);
3037 (* iface->set_display_name_async) (file,
3046 * g_file_set_display_name_finish:
3047 * @file: input #GFile.
3048 * @res: a #GAsyncResult.
3049 * @error: a #GError, or %NULL
3051 * Finishes setting a display name started with
3052 * g_file_set_display_name_async().
3054 * Returns: a #GFile or %NULL on error.
3055 * Free the returned object with g_object_unref().
3058 g_file_set_display_name_finish (GFile *file,
3064 g_return_val_if_fail (G_IS_FILE (file), NULL);
3065 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
3067 if (G_IS_SIMPLE_ASYNC_RESULT (res))
3069 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3070 if (g_simple_async_result_propagate_error (simple, error))
3074 iface = G_FILE_GET_IFACE (file);
3075 return (* iface->set_display_name_finish) (file, res, error);
3079 * g_file_query_settable_attributes:
3080 * @file: input #GFile.
3081 * @cancellable: optional #GCancellable object, %NULL to ignore.
3082 * @error: a #GError, or %NULL
3084 * Obtain the list of settable attributes for the file.
3086 * Returns the type and full attribute name of all the attributes
3087 * that can be set on this file. This doesn't mean setting it will always
3088 * succeed though, you might get an access failure, or some specific
3089 * file may not support a specific attribute.
3091 * If @cancellable is not %NULL, then the operation can be cancelled by
3092 * triggering the cancellable object from another thread. If the operation
3093 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3095 * Returns: a #GFileAttributeInfoList describing the settable attributes.
3096 * When you are done with it, release it with g_file_attribute_info_list_unref()
3098 GFileAttributeInfoList *
3099 g_file_query_settable_attributes (GFile *file,
3100 GCancellable *cancellable,
3105 GFileAttributeInfoList *list;
3107 g_return_val_if_fail (G_IS_FILE (file), NULL);
3109 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3112 iface = G_FILE_GET_IFACE (file);
3114 if (iface->query_settable_attributes == NULL)
3115 return g_file_attribute_info_list_new ();
3118 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
3122 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
3124 list = g_file_attribute_info_list_new ();
3125 g_error_free (my_error);
3128 g_propagate_error (error, my_error);
3135 * g_file_query_writable_namespaces:
3136 * @file: input #GFile.
3137 * @cancellable: optional #GCancellable object, %NULL to ignore.
3138 * @error: a #GError, or %NULL
3140 * Obtain the list of attribute namespaces where new attributes
3141 * can be created by a user. An example of this is extended
3142 * attributes (in the "xattr" namespace).
3144 * If @cancellable is not %NULL, then the operation can be cancelled by
3145 * triggering the cancellable object from another thread. If the operation
3146 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3148 * Returns: a #GFileAttributeInfoList describing the writable namespaces.
3149 * When you are done with it, release it with g_file_attribute_info_list_unref()
3151 GFileAttributeInfoList *
3152 g_file_query_writable_namespaces (GFile *file,
3153 GCancellable *cancellable,
3158 GFileAttributeInfoList *list;
3160 g_return_val_if_fail (G_IS_FILE (file), NULL);
3162 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3165 iface = G_FILE_GET_IFACE (file);
3167 if (iface->query_writable_namespaces == NULL)
3168 return g_file_attribute_info_list_new ();
3171 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
3175 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
3177 list = g_file_attribute_info_list_new ();
3178 g_error_free (my_error);
3181 g_propagate_error (error, my_error);
3188 * g_file_set_attribute:
3189 * @file: input #GFile.
3190 * @attribute: a string containing the attribute's name.
3191 * @type: The type of the attribute
3192 * @value_p: a pointer to the value (or the pointer itself if the type is a pointer type)
3193 * @flags: a set of #GFileQueryInfoFlags.
3194 * @cancellable: optional #GCancellable object, %NULL to ignore.
3195 * @error: a #GError, or %NULL
3197 * Sets an attribute in the file with attribute name @attribute to @value.
3199 * If @cancellable is not %NULL, then the operation can be cancelled by
3200 * triggering the cancellable object from another thread. If the operation
3201 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3203 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
3206 g_file_set_attribute (GFile *file,
3207 const char *attribute,
3208 GFileAttributeType type,
3210 GFileQueryInfoFlags flags,
3211 GCancellable *cancellable,
3216 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3217 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
3219 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3222 iface = G_FILE_GET_IFACE (file);
3224 if (iface->set_attribute == NULL)
3226 g_set_error_literal (error, G_IO_ERROR,
3227 G_IO_ERROR_NOT_SUPPORTED,
3228 _("Operation not supported"));
3232 return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
3236 * g_file_set_attributes_from_info:
3237 * @file: input #GFile.
3238 * @info: a #GFileInfo.
3239 * @flags: #GFileQueryInfoFlags
3240 * @cancellable: optional #GCancellable object, %NULL to ignore.
3241 * @error: a #GError, or %NULL
3243 * Tries to set all attributes in the #GFileInfo on the target values,
3244 * not stopping on the first error.
3246 * If there is any error during this operation then @error will be set to
3247 * the first error. Error on particular fields are flagged by setting
3248 * the "status" field in the attribute value to
3249 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can also detect
3252 * If @cancellable is not %NULL, then the operation can be cancelled by
3253 * triggering the cancellable object from another thread. If the operation
3254 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3256 * Returns: %TRUE if there was any error, %FALSE otherwise.
3259 g_file_set_attributes_from_info (GFile *file,
3261 GFileQueryInfoFlags flags,
3262 GCancellable *cancellable,
3267 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3268 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
3270 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3273 g_file_info_clear_status (info);
3275 iface = G_FILE_GET_IFACE (file);
3277 return (* iface->set_attributes_from_info) (file,
3286 g_file_real_set_attributes_from_info (GFile *file,
3288 GFileQueryInfoFlags flags,
3289 GCancellable *cancellable,
3295 GFileAttributeValue *value;
3299 attributes = g_file_info_list_attributes (info, NULL);
3301 for (i = 0; attributes[i] != NULL; i++)
3303 value = _g_file_info_get_attribute_value (info, attributes[i]);
3305 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
3308 if (!g_file_set_attribute (file, attributes[i],
3309 value->type, _g_file_attribute_value_peek_as_pointer (value),
3310 flags, cancellable, error))
3312 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
3314 /* Don't set error multiple times */
3318 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
3321 g_strfreev (attributes);
3327 * g_file_set_attributes_async:
3328 * @file: input #GFile.
3329 * @info: a #GFileInfo.
3330 * @flags: a #GFileQueryInfoFlags.
3331 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3333 * @cancellable: optional #GCancellable object, %NULL to ignore.
3334 * @callback: a #GAsyncReadyCallback.
3335 * @user_data: a #gpointer.
3337 * Asynchronously sets the attributes of @file with @info.
3339 * For more details, see g_file_set_attributes_from_info() which is
3340 * the synchronous version of this call.
3342 * When the operation is finished, @callback will be called. You can then call
3343 * g_file_set_attributes_finish() to get the result of the operation.
3346 g_file_set_attributes_async (GFile *file,
3348 GFileQueryInfoFlags flags,
3350 GCancellable *cancellable,
3351 GAsyncReadyCallback callback,
3356 g_return_if_fail (G_IS_FILE (file));
3357 g_return_if_fail (G_IS_FILE_INFO (info));
3359 iface = G_FILE_GET_IFACE (file);
3360 (* iface->set_attributes_async) (file,
3370 * g_file_set_attributes_finish:
3371 * @file: input #GFile.
3372 * @result: a #GAsyncResult.
3373 * @info: a #GFileInfo.
3374 * @error: a #GError, or %NULL
3376 * Finishes setting an attribute started in g_file_set_attributes_async().
3378 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
3381 g_file_set_attributes_finish (GFile *file,
3382 GAsyncResult *result,
3388 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3389 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3391 /* No standard handling of errors here, as we must set info even
3394 iface = G_FILE_GET_IFACE (file);
3395 return (* iface->set_attributes_finish) (file, result, info, error);
3399 * g_file_set_attribute_string:
3400 * @file: input #GFile.
3401 * @attribute: a string containing the attribute's name.
3402 * @value: a string containing the attribute's value.
3403 * @flags: #GFileQueryInfoFlags.
3404 * @cancellable: optional #GCancellable object, %NULL to ignore.
3405 * @error: a #GError, or %NULL
3407 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
3408 * If @attribute is of a different type, this operation will fail.
3410 * If @cancellable is not %NULL, then the operation can be cancelled by
3411 * triggering the cancellable object from another thread. If the operation
3412 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3414 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
3417 g_file_set_attribute_string (GFile *file,
3418 const char *attribute,
3420 GFileQueryInfoFlags flags,
3421 GCancellable *cancellable,
3424 return g_file_set_attribute (file, attribute,
3425 G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
3426 flags, cancellable, error);
3430 * g_file_set_attribute_byte_string:
3431 * @file: input #GFile.
3432 * @attribute: a string containing the attribute's name.
3433 * @value: a string containing the attribute's new value.
3434 * @flags: a #GFileQueryInfoFlags.
3435 * @cancellable: optional #GCancellable object, %NULL to ignore.
3436 * @error: a #GError, or %NULL
3438 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
3439 * If @attribute is of a different type, this operation will fail,
3442 * If @cancellable is not %NULL, then the operation can be cancelled by
3443 * triggering the cancellable object from another thread. If the operation
3444 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3446 * Returns: %TRUE if the @attribute was successfully set to @value
3447 * in the @file, %FALSE otherwise.
3450 g_file_set_attribute_byte_string (GFile *file,
3451 const char *attribute,
3453 GFileQueryInfoFlags flags,
3454 GCancellable *cancellable,
3457 return g_file_set_attribute (file, attribute,
3458 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
3459 flags, cancellable, error);
3463 * g_file_set_attribute_uint32:
3464 * @file: input #GFile.
3465 * @attribute: a string containing the attribute's name.
3466 * @value: a #guint32 containing the attribute's new value.
3467 * @flags: a #GFileQueryInfoFlags.
3468 * @cancellable: optional #GCancellable object, %NULL to ignore.
3469 * @error: a #GError, or %NULL
3471 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
3472 * If @attribute is of a different type, this operation will fail.
3474 * If @cancellable is not %NULL, then the operation can be cancelled by
3475 * triggering the cancellable object from another thread. If the operation
3476 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3478 * Returns: %TRUE if the @attribute was successfully set to @value
3479 * in the @file, %FALSE otherwise.
3482 g_file_set_attribute_uint32 (GFile *file,
3483 const char *attribute,
3485 GFileQueryInfoFlags flags,
3486 GCancellable *cancellable,
3489 return g_file_set_attribute (file, attribute,
3490 G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
3491 flags, cancellable, error);
3495 * g_file_set_attribute_int32:
3496 * @file: input #GFile.
3497 * @attribute: a string containing the attribute's name.
3498 * @value: a #gint32 containing the attribute's new value.
3499 * @flags: a #GFileQueryInfoFlags.
3500 * @cancellable: optional #GCancellable object, %NULL to ignore.
3501 * @error: a #GError, or %NULL
3503 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
3504 * If @attribute is of a different type, this operation will fail.
3506 * If @cancellable is not %NULL, then the operation can be cancelled by
3507 * triggering the cancellable object from another thread. If the operation
3508 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3510 * Returns: %TRUE if the @attribute was successfully set to @value
3511 * in the @file, %FALSE otherwise.
3514 g_file_set_attribute_int32 (GFile *file,
3515 const char *attribute,
3517 GFileQueryInfoFlags flags,
3518 GCancellable *cancellable,
3521 return g_file_set_attribute (file, attribute,
3522 G_FILE_ATTRIBUTE_TYPE_INT32, &value,
3523 flags, cancellable, error);
3527 * g_file_set_attribute_uint64:
3528 * @file: input #GFile.
3529 * @attribute: a string containing the attribute's name.
3530 * @value: a #guint64 containing the attribute's new value.
3531 * @flags: a #GFileQueryInfoFlags.
3532 * @cancellable: optional #GCancellable object, %NULL to ignore.
3533 * @error: a #GError, or %NULL
3535 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
3536 * If @attribute is of a different type, this operation will fail.
3538 * If @cancellable is not %NULL, then the operation can be cancelled by
3539 * triggering the cancellable object from another thread. If the operation
3540 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3542 * Returns: %TRUE if the @attribute was successfully set to @value
3543 * in the @file, %FALSE otherwise.
3546 g_file_set_attribute_uint64 (GFile *file,
3547 const char *attribute,
3549 GFileQueryInfoFlags flags,
3550 GCancellable *cancellable,
3553 return g_file_set_attribute (file, attribute,
3554 G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
3555 flags, cancellable, error);
3559 * g_file_set_attribute_int64:
3560 * @file: input #GFile.
3561 * @attribute: a string containing the attribute's name.
3562 * @value: a #guint64 containing the attribute's new value.
3563 * @flags: a #GFileQueryInfoFlags.
3564 * @cancellable: optional #GCancellable object, %NULL to ignore.
3565 * @error: a #GError, or %NULL
3567 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
3568 * If @attribute is of a different type, this operation will fail.
3570 * If @cancellable is not %NULL, then the operation can be cancelled by
3571 * triggering the cancellable object from another thread. If the operation
3572 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3574 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
3577 g_file_set_attribute_int64 (GFile *file,
3578 const char *attribute,
3580 GFileQueryInfoFlags flags,
3581 GCancellable *cancellable,
3584 return g_file_set_attribute (file, attribute,
3585 G_FILE_ATTRIBUTE_TYPE_INT64, &value,
3586 flags, cancellable, error);
3590 * g_file_mount_mountable:
3591 * @file: input #GFile.
3592 * @flags: flags affecting the operation
3593 * @mount_operation: a #GMountOperation, or %NULL to avoid user interaction.
3594 * @cancellable: optional #GCancellable object, %NULL to ignore.
3595 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3596 * @user_data: the data to pass to callback function
3598 * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
3599 * Using @mount_operation, you can request callbacks when, for instance,
3600 * passwords are needed during authentication.
3602 * If @cancellable is not %NULL, then the operation can be cancelled by
3603 * triggering the cancellable object from another thread. If the operation
3604 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3606 * When the operation is finished, @callback will be called. You can then call
3607 * g_file_mount_mountable_finish() to get the result of the operation.
3610 g_file_mount_mountable (GFile *file,
3611 GMountMountFlags flags,
3612 GMountOperation *mount_operation,
3613 GCancellable *cancellable,
3614 GAsyncReadyCallback callback,
3619 g_return_if_fail (G_IS_FILE (file));
3621 iface = G_FILE_GET_IFACE (file);
3623 if (iface->mount_mountable == NULL)
3625 g_simple_async_report_error_in_idle (G_OBJECT (file),
3629 G_IO_ERROR_NOT_SUPPORTED,
3630 _("Operation not supported"));
3634 (* iface->mount_mountable) (file,
3643 * g_file_mount_mountable_finish:
3644 * @file: input #GFile.
3645 * @result: a #GAsyncResult.
3646 * @error: a #GError, or %NULL
3648 * Finishes a mount operation. See g_file_mount_mountable() for details.
3650 * Finish an asynchronous mount operation that was started
3651 * with g_file_mount_mountable().
3653 * Returns: a #GFile or %NULL on error.
3654 * Free the returned object with g_object_unref().
3657 g_file_mount_mountable_finish (GFile *file,
3658 GAsyncResult *result,
3663 g_return_val_if_fail (G_IS_FILE (file), NULL);
3664 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
3666 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3668 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3669 if (g_simple_async_result_propagate_error (simple, error))
3673 iface = G_FILE_GET_IFACE (file);
3674 return (* iface->mount_mountable_finish) (file, result, error);
3678 * g_file_unmount_mountable:
3679 * @file: input #GFile.
3680 * @flags: flags affecting the operation
3681 * @cancellable: optional #GCancellable object, %NULL to ignore.
3682 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3683 * @user_data: the data to pass to callback function
3685 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
3687 * If @cancellable is not %NULL, then the operation can be cancelled by
3688 * triggering the cancellable object from another thread. If the operation
3689 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3691 * When the operation is finished, @callback will be called. You can then call
3692 * g_file_unmount_mountable_finish() to get the result of the operation.
3695 g_file_unmount_mountable (GFile *file,
3696 GMountUnmountFlags flags,
3697 GCancellable *cancellable,
3698 GAsyncReadyCallback callback,
3703 g_return_if_fail (G_IS_FILE (file));
3705 iface = G_FILE_GET_IFACE (file);
3707 if (iface->unmount_mountable == NULL)
3709 g_simple_async_report_error_in_idle (G_OBJECT (file),
3713 G_IO_ERROR_NOT_SUPPORTED,
3714 _("Operation not supported"));
3718 (* iface->unmount_mountable) (file,
3726 * g_file_unmount_mountable_finish:
3727 * @file: input #GFile.
3728 * @result: a #GAsyncResult.
3729 * @error: a #GError, or %NULL
3731 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
3733 * Finish an asynchronous unmount operation that was started
3734 * with g_file_unmount_mountable().
3736 * Returns: %TRUE if the operation finished successfully. %FALSE
3740 g_file_unmount_mountable_finish (GFile *file,
3741 GAsyncResult *result,
3746 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3747 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3749 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3751 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3752 if (g_simple_async_result_propagate_error (simple, error))
3756 iface = G_FILE_GET_IFACE (file);
3757 return (* iface->unmount_mountable_finish) (file, result, error);
3761 * g_file_eject_mountable:
3762 * @file: input #GFile.
3763 * @flags: flags affecting the operation
3764 * @cancellable: optional #GCancellable object, %NULL to ignore.
3765 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3766 * @user_data: the data to pass to callback function
3768 * Starts an asynchronous eject on a mountable.
3769 * When this operation has completed, @callback will be called with
3770 * @user_user data, and the operation can be finalized with
3771 * g_file_eject_mountable_finish().
3773 * If @cancellable is not %NULL, then the operation can be cancelled by
3774 * triggering the cancellable object from another thread. If the operation
3775 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3778 g_file_eject_mountable (GFile *file,
3779 GMountUnmountFlags flags,
3780 GCancellable *cancellable,
3781 GAsyncReadyCallback callback,
3786 g_return_if_fail (G_IS_FILE (file));
3788 iface = G_FILE_GET_IFACE (file);
3790 if (iface->eject_mountable == NULL)
3792 g_simple_async_report_error_in_idle (G_OBJECT (file),
3796 G_IO_ERROR_NOT_SUPPORTED,
3797 _("Operation not supported"));
3801 (* iface->eject_mountable) (file,
3809 * g_file_eject_mountable_finish:
3810 * @file: input #GFile.
3811 * @result: a #GAsyncResult.
3812 * @error: a #GError, or %NULL
3814 * Finishes an asynchronous eject operation started by
3815 * g_file_eject_mountable().
3817 * Returns: %TRUE if the @file was ejected successfully. %FALSE
3821 g_file_eject_mountable_finish (GFile *file,
3822 GAsyncResult *result,
3827 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3828 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3830 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3832 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3833 if (g_simple_async_result_propagate_error (simple, error))
3837 iface = G_FILE_GET_IFACE (file);
3838 return (* iface->eject_mountable_finish) (file, result, error);
3842 * g_file_monitor_directory:
3843 * @file: input #GFile.
3844 * @flags: a set of #GFileMonitorFlags.
3845 * @cancellable: optional #GCancellable object, %NULL to ignore.
3846 * @error: a #GError, or %NULL.
3848 * Obtains a directory monitor for the given file.
3849 * This may fail if directory monitoring is not supported.
3851 * If @cancellable is not %NULL, then the operation can be cancelled by
3852 * triggering the cancellable object from another thread. If the operation
3853 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3855 * Returns: a #GFileMonitor for the given @file, or %NULL on error.
3856 * Free the returned object with g_object_unref().
3859 g_file_monitor_directory (GFile *file,
3860 GFileMonitorFlags flags,
3861 GCancellable *cancellable,
3866 g_return_val_if_fail (G_IS_FILE (file), NULL);
3868 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3871 iface = G_FILE_GET_IFACE (file);
3873 if (iface->monitor_dir == NULL)
3875 g_set_error_literal (error, G_IO_ERROR,
3876 G_IO_ERROR_NOT_SUPPORTED,
3877 _("Operation not supported"));
3881 return (* iface->monitor_dir) (file, flags, cancellable, error);
3885 * g_file_monitor_file:
3886 * @file: input #GFile.
3887 * @flags: a set of #GFileMonitorFlags.
3888 * @cancellable: optional #GCancellable object, %NULL to ignore.
3889 * @error: a #GError, or %NULL.
3891 * Obtains a file monitor for the given file. If no file notification
3892 * mechanism exists, then regular polling of the file is used.
3894 * If @cancellable is not %NULL, then the operation can be cancelled by
3895 * triggering the cancellable object from another thread. If the operation
3896 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3898 * Returns: a #GFileMonitor for the given @file, or %NULL on error.
3899 * Free the returned object with g_object_unref().
3902 g_file_monitor_file (GFile *file,
3903 GFileMonitorFlags flags,
3904 GCancellable *cancellable,
3908 GFileMonitor *monitor;
3910 g_return_val_if_fail (G_IS_FILE (file), NULL);
3912 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3915 iface = G_FILE_GET_IFACE (file);
3919 if (iface->monitor_file)
3920 monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
3922 /* Fallback to polling */
3923 if (monitor == NULL)
3924 monitor = _g_poll_file_monitor_new (file);
3931 * @file: input #GFile
3932 * @flags: a set of #GFileMonitorFlags
3933 * @cancellable: optional #GCancellable object, %NULL to ignore
3934 * @error: a #GError, or %NULL
3936 * Obtains a file or directory monitor for the given file, depending
3937 * on the type of the file.
3939 * If @cancellable is not %NULL, then the operation can be cancelled by
3940 * triggering the cancellable object from another thread. If the operation
3941 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3943 * Returns: a #GFileMonitor for the given @file, or %NULL on error.
3944 * Free the returned object with g_object_unref().
3949 g_file_monitor (GFile *file,
3950 GFileMonitorFlags flags,
3951 GCancellable *cancellable,
3954 if (g_file_query_file_type (file, 0, cancellable) == G_FILE_TYPE_DIRECTORY)
3955 return g_file_monitor_directory (file, flags, cancellable, error);
3957 return g_file_monitor_file (file, flags, cancellable, error);
3960 /********************************************
3961 * Default implementation of async ops *
3962 ********************************************/
3966 GFileQueryInfoFlags flags;
3968 } QueryInfoAsyncData;
3971 query_info_data_free (QueryInfoAsyncData *data)
3974 g_object_unref (data->info);
3975 g_free (data->attributes);
3980 query_info_async_thread (GSimpleAsyncResult *res,
3982 GCancellable *cancellable)
3984 GError *error = NULL;
3985 QueryInfoAsyncData *data;
3988 data = g_simple_async_result_get_op_res_gpointer (res);
3990 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3994 g_simple_async_result_set_from_error (res, error);
3995 g_error_free (error);
4002 g_file_real_query_info_async (GFile *file,
4003 const char *attributes,
4004 GFileQueryInfoFlags flags,
4006 GCancellable *cancellable,
4007 GAsyncReadyCallback callback,
4010 GSimpleAsyncResult *res;
4011 QueryInfoAsyncData *data;
4013 data = g_new0 (QueryInfoAsyncData, 1);
4014 data->attributes = g_strdup (attributes);
4015 data->flags = flags;
4017 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_info_async);
4018 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
4020 g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
4021 g_object_unref (res);
4025 g_file_real_query_info_finish (GFile *file,
4029 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4030 QueryInfoAsyncData *data;
4032 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_info_async);
4034 data = g_simple_async_result_get_op_res_gpointer (simple);
4036 return g_object_ref (data->info);
4044 } QueryFilesystemInfoAsyncData;
4047 query_filesystem_info_data_free (QueryFilesystemInfoAsyncData *data)
4050 g_object_unref (data->info);
4051 g_free (data->attributes);
4056 query_filesystem_info_async_thread (GSimpleAsyncResult *res,
4058 GCancellable *cancellable)
4060 GError *error = NULL;
4061 QueryFilesystemInfoAsyncData *data;
4064 data = g_simple_async_result_get_op_res_gpointer (res);
4066 info = g_file_query_filesystem_info (G_FILE (object), data->attributes, cancellable, &error);
4070 g_simple_async_result_set_from_error (res, error);
4071 g_error_free (error);
4078 g_file_real_query_filesystem_info_async (GFile *file,
4079 const char *attributes,
4081 GCancellable *cancellable,
4082 GAsyncReadyCallback callback,
4085 GSimpleAsyncResult *res;
4086 QueryFilesystemInfoAsyncData *data;
4088 data = g_new0 (QueryFilesystemInfoAsyncData, 1);
4089 data->attributes = g_strdup (attributes);
4091 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_filesystem_info_async);
4092 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_filesystem_info_data_free);
4094 g_simple_async_result_run_in_thread (res, query_filesystem_info_async_thread, io_priority, cancellable);
4095 g_object_unref (res);
4099 g_file_real_query_filesystem_info_finish (GFile *file,
4103 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4104 QueryFilesystemInfoAsyncData *data;
4106 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_filesystem_info_async);
4108 data = g_simple_async_result_get_op_res_gpointer (simple);
4110 return g_object_ref (data->info);
4117 GFileQueryInfoFlags flags;
4118 GFileEnumerator *enumerator;
4119 } EnumerateChildrenAsyncData;
4122 enumerate_children_data_free (EnumerateChildrenAsyncData *data)
4124 if (data->enumerator)
4125 g_object_unref (data->enumerator);
4126 g_free (data->attributes);
4131 enumerate_children_async_thread (GSimpleAsyncResult *res,
4133 GCancellable *cancellable)
4135 GError *error = NULL;
4136 EnumerateChildrenAsyncData *data;
4137 GFileEnumerator *enumerator;
4139 data = g_simple_async_result_get_op_res_gpointer (res);
4141 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
4143 if (enumerator == NULL)
4145 g_simple_async_result_set_from_error (res, error);
4146 g_error_free (error);
4149 data->enumerator = enumerator;
4153 g_file_real_enumerate_children_async (GFile *file,
4154 const char *attributes,
4155 GFileQueryInfoFlags flags,
4157 GCancellable *cancellable,
4158 GAsyncReadyCallback callback,
4161 GSimpleAsyncResult *res;
4162 EnumerateChildrenAsyncData *data;
4164 data = g_new0 (EnumerateChildrenAsyncData, 1);
4165 data->attributes = g_strdup (attributes);
4166 data->flags = flags;
4168 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_enumerate_children_async);
4169 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)enumerate_children_data_free);
4171 g_simple_async_result_run_in_thread (res, enumerate_children_async_thread, io_priority, cancellable);
4172 g_object_unref (res);
4175 static GFileEnumerator *
4176 g_file_real_enumerate_children_finish (GFile *file,
4180 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4181 EnumerateChildrenAsyncData *data;
4183 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_enumerate_children_async);
4185 data = g_simple_async_result_get_op_res_gpointer (simple);
4186 if (data->enumerator)
4187 return g_object_ref (data->enumerator);
4193 open_read_async_thread (GSimpleAsyncResult *res,
4195 GCancellable *cancellable)
4198 GFileInputStream *stream;
4199 GError *error = NULL;
4201 iface = G_FILE_GET_IFACE (object);
4203 if (iface->read_fn == NULL)
4205 g_set_error_literal (&error, G_IO_ERROR,
4206 G_IO_ERROR_NOT_SUPPORTED,
4207 _("Operation not supported"));
4209 g_simple_async_result_set_from_error (res, error);
4210 g_error_free (error);
4215 stream = iface->read_fn (G_FILE (object), cancellable, &error);
4219 g_simple_async_result_set_from_error (res, error);
4220 g_error_free (error);
4223 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4227 g_file_real_read_async (GFile *file,
4229 GCancellable *cancellable,
4230 GAsyncReadyCallback callback,
4233 GSimpleAsyncResult *res;
4235 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_read_async);
4237 g_simple_async_result_run_in_thread (res, open_read_async_thread, io_priority, cancellable);
4238 g_object_unref (res);
4241 static GFileInputStream *
4242 g_file_real_read_finish (GFile *file,
4246 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4249 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_read_async);
4251 op = g_simple_async_result_get_op_res_gpointer (simple);
4253 return g_object_ref (op);
4259 append_to_async_thread (GSimpleAsyncResult *res,
4261 GCancellable *cancellable)
4264 GFileCreateFlags *data;
4265 GFileOutputStream *stream;
4266 GError *error = NULL;
4268 iface = G_FILE_GET_IFACE (object);
4270 data = g_simple_async_result_get_op_res_gpointer (res);
4272 stream = iface->append_to (G_FILE (object), *data, cancellable, &error);
4276 g_simple_async_result_set_from_error (res, error);
4277 g_error_free (error);
4280 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4284 g_file_real_append_to_async (GFile *file,
4285 GFileCreateFlags flags,
4287 GCancellable *cancellable,
4288 GAsyncReadyCallback callback,
4291 GFileCreateFlags *data;
4292 GSimpleAsyncResult *res;
4294 data = g_new0 (GFileCreateFlags, 1);
4297 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_append_to_async);
4298 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
4300 g_simple_async_result_run_in_thread (res, append_to_async_thread, io_priority, cancellable);
4301 g_object_unref (res);
4304 static GFileOutputStream *
4305 g_file_real_append_to_finish (GFile *file,
4309 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4312 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_append_to_async);
4314 op = g_simple_async_result_get_op_res_gpointer (simple);
4316 return g_object_ref (op);
4322 create_async_thread (GSimpleAsyncResult *res,
4324 GCancellable *cancellable)
4327 GFileCreateFlags *data;
4328 GFileOutputStream *stream;
4329 GError *error = NULL;
4331 iface = G_FILE_GET_IFACE (object);
4333 data = g_simple_async_result_get_op_res_gpointer (res);
4335 stream = iface->create (G_FILE (object), *data, cancellable, &error);
4339 g_simple_async_result_set_from_error (res, error);
4340 g_error_free (error);
4343 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4347 g_file_real_create_async (GFile *file,
4348 GFileCreateFlags flags,
4350 GCancellable *cancellable,
4351 GAsyncReadyCallback callback,
4354 GFileCreateFlags *data;
4355 GSimpleAsyncResult *res;
4357 data = g_new0 (GFileCreateFlags, 1);
4360 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_create_async);
4361 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
4363 g_simple_async_result_run_in_thread (res, create_async_thread, io_priority, cancellable);
4364 g_object_unref (res);
4367 static GFileOutputStream *
4368 g_file_real_create_finish (GFile *file,
4372 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4375 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_create_async);
4377 op = g_simple_async_result_get_op_res_gpointer (simple);
4379 return g_object_ref (op);
4385 GFileOutputStream *stream;
4387 gboolean make_backup;
4388 GFileCreateFlags flags;
4392 replace_async_data_free (ReplaceAsyncData *data)
4395 g_object_unref (data->stream);
4396 g_free (data->etag);
4401 replace_async_thread (GSimpleAsyncResult *res,
4403 GCancellable *cancellable)
4406 GFileOutputStream *stream;
4407 GError *error = NULL;
4408 ReplaceAsyncData *data;
4410 iface = G_FILE_GET_IFACE (object);
4412 data = g_simple_async_result_get_op_res_gpointer (res);
4414 stream = iface->replace (G_FILE (object),
4423 g_simple_async_result_set_from_error (res, error);
4424 g_error_free (error);
4427 data->stream = stream;
4431 g_file_real_replace_async (GFile *file,
4433 gboolean make_backup,
4434 GFileCreateFlags flags,
4436 GCancellable *cancellable,
4437 GAsyncReadyCallback callback,
4440 GSimpleAsyncResult *res;
4441 ReplaceAsyncData *data;
4443 data = g_new0 (ReplaceAsyncData, 1);
4444 data->etag = g_strdup (etag);
4445 data->make_backup = make_backup;
4446 data->flags = flags;
4448 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_replace_async);
4449 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_async_data_free);
4451 g_simple_async_result_run_in_thread (res, replace_async_thread, io_priority, cancellable);
4452 g_object_unref (res);
4455 static GFileOutputStream *
4456 g_file_real_replace_finish (GFile *file,
4460 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4461 ReplaceAsyncData *data;
4463 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_replace_async);
4465 data = g_simple_async_result_get_op_res_gpointer (simple);
4467 return g_object_ref (data->stream);
4475 } SetDisplayNameAsyncData;
4478 set_display_name_data_free (SetDisplayNameAsyncData *data)
4480 g_free (data->name);
4482 g_object_unref (data->file);
4487 set_display_name_async_thread (GSimpleAsyncResult *res,
4489 GCancellable *cancellable)
4491 GError *error = NULL;
4492 SetDisplayNameAsyncData *data;
4495 data = g_simple_async_result_get_op_res_gpointer (res);
4497 file = g_file_set_display_name (G_FILE (object), data->name, cancellable, &error);
4501 g_simple_async_result_set_from_error (res, error);
4502 g_error_free (error);
4509 g_file_real_set_display_name_async (GFile *file,
4510 const char *display_name,
4512 GCancellable *cancellable,
4513 GAsyncReadyCallback callback,
4516 GSimpleAsyncResult *res;
4517 SetDisplayNameAsyncData *data;
4519 data = g_new0 (SetDisplayNameAsyncData, 1);
4520 data->name = g_strdup (display_name);
4522 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_display_name_async);
4523 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_display_name_data_free);
4525 g_simple_async_result_run_in_thread (res, set_display_name_async_thread, io_priority, cancellable);
4526 g_object_unref (res);
4530 g_file_real_set_display_name_finish (GFile *file,
4534 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4535 SetDisplayNameAsyncData *data;
4537 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_display_name_async);
4539 data = g_simple_async_result_get_op_res_gpointer (simple);
4541 return g_object_ref (data->file);
4547 GFileQueryInfoFlags flags;
4554 set_info_data_free (SetInfoAsyncData *data)
4557 g_object_unref (data->info);
4559 g_error_free (data->error);
4564 set_info_async_thread (GSimpleAsyncResult *res,
4566 GCancellable *cancellable)
4568 SetInfoAsyncData *data;
4570 data = g_simple_async_result_get_op_res_gpointer (res);
4573 data->res = g_file_set_attributes_from_info (G_FILE (object),
4581 g_file_real_set_attributes_async (GFile *file,
4583 GFileQueryInfoFlags flags,
4585 GCancellable *cancellable,
4586 GAsyncReadyCallback callback,
4589 GSimpleAsyncResult *res;
4590 SetInfoAsyncData *data;
4592 data = g_new0 (SetInfoAsyncData, 1);
4593 data->info = g_file_info_dup (info);
4594 data->flags = flags;
4596 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_attributes_async);
4597 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_info_data_free);
4599 g_simple_async_result_run_in_thread (res, set_info_async_thread, io_priority, cancellable);
4600 g_object_unref (res);
4604 g_file_real_set_attributes_finish (GFile *file,
4609 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4610 SetInfoAsyncData *data;
4612 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_attributes_async);
4614 data = g_simple_async_result_get_op_res_gpointer (simple);
4617 *info = g_object_ref (data->info);
4619 if (error != NULL && data->error)
4620 *error = g_error_copy (data->error);
4626 find_enclosing_mount_async_thread (GSimpleAsyncResult *res,
4628 GCancellable *cancellable)
4630 GError *error = NULL;
4633 mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
4637 g_simple_async_result_set_from_error (res, error);
4638 g_error_free (error);
4641 g_simple_async_result_set_op_res_gpointer (res, mount, (GDestroyNotify)g_object_unref);
4645 g_file_real_find_enclosing_mount_async (GFile *file,
4647 GCancellable *cancellable,
4648 GAsyncReadyCallback callback,
4651 GSimpleAsyncResult *res;
4653 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_find_enclosing_mount_async);
4655 g_simple_async_result_run_in_thread (res, find_enclosing_mount_async_thread, io_priority, cancellable);
4656 g_object_unref (res);
4660 g_file_real_find_enclosing_mount_finish (GFile *file,
4664 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4667 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_find_enclosing_mount_async);
4669 mount = g_simple_async_result_get_op_res_gpointer (simple);
4670 return g_object_ref (mount);
4677 GFileCopyFlags flags;
4678 GFileProgressCallback progress_cb;
4679 gpointer progress_cb_data;
4680 GIOSchedulerJob *job;
4684 copy_async_data_free (CopyAsyncData *data)
4686 g_object_unref (data->source);
4687 g_object_unref (data->destination);
4692 CopyAsyncData *data;
4693 goffset current_num_bytes;
4694 goffset total_num_bytes;
4698 copy_async_progress_in_main (gpointer user_data)
4700 ProgressData *progress = user_data;
4701 CopyAsyncData *data = progress->data;
4703 data->progress_cb (progress->current_num_bytes,
4704 progress->total_num_bytes,
4705 data->progress_cb_data);
4711 mainloop_barrier (gpointer user_data)
4713 /* Does nothing, but ensures all queued idles before
4720 copy_async_progress_callback (goffset current_num_bytes,
4721 goffset total_num_bytes,
4724 CopyAsyncData *data = user_data;
4725 ProgressData *progress;
4727 progress = g_new (ProgressData, 1);
4728 progress->data = data;
4729 progress->current_num_bytes = current_num_bytes;
4730 progress->total_num_bytes = total_num_bytes;
4732 g_io_scheduler_job_send_to_mainloop_async (data->job,
4733 copy_async_progress_in_main,
4739 copy_async_thread (GIOSchedulerJob *job,
4740 GCancellable *cancellable,
4743 GSimpleAsyncResult *res;
4744 CopyAsyncData *data;
4749 data = g_simple_async_result_get_op_res_gpointer (res);
4753 result = g_file_copy (data->source,
4757 (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
4761 /* Ensure all progress callbacks are done running in main thread */
4762 if (data->progress_cb != NULL)
4763 g_io_scheduler_job_send_to_mainloop (job,
4769 g_simple_async_result_set_from_error (res, error);
4770 g_error_free (error);
4773 g_simple_async_result_complete_in_idle (res);
4779 g_file_real_copy_async (GFile *source,
4781 GFileCopyFlags flags,
4783 GCancellable *cancellable,
4784 GFileProgressCallback progress_callback,
4785 gpointer progress_callback_data,
4786 GAsyncReadyCallback callback,
4789 GSimpleAsyncResult *res;
4790 CopyAsyncData *data;
4792 data = g_new0 (CopyAsyncData, 1);
4793 data->source = g_object_ref (source);
4794 data->destination = g_object_ref (destination);
4795 data->flags = flags;
4796 data->progress_cb = progress_callback;
4797 data->progress_cb_data = progress_callback_data;
4799 res = g_simple_async_result_new (G_OBJECT (source), callback, user_data, g_file_real_copy_async);
4800 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)copy_async_data_free);
4802 g_io_scheduler_push_job (copy_async_thread, res, g_object_unref, io_priority, cancellable);
4806 g_file_real_copy_finish (GFile *file,
4810 /* Error handled in g_file_copy_finish() */
4815 /********************************************
4816 * Default VFS operations *
4817 ********************************************/
4820 * g_file_new_for_path:
4821 * @path: a string containing a relative or absolute path.
4823 * Constructs a #GFile for a given path. This operation never
4824 * fails, but the returned object might not support any I/O
4825 * operation if @path is malformed.
4827 * Returns: a new #GFile for the given @path.
4830 g_file_new_for_path (const char *path)
4832 g_return_val_if_fail (path != NULL, NULL);
4834 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
4838 * g_file_new_for_uri:
4839 * @uri: a string containing a URI.
4841 * Constructs a #GFile for a given URI. This operation never
4842 * fails, but the returned object might not support any I/O
4843 * operation if @uri is malformed or if the uri type is
4846 * Returns: a #GFile for the given @uri.
4849 g_file_new_for_uri (const char *uri)
4851 g_return_val_if_fail (uri != NULL, NULL);
4853 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
4857 * g_file_parse_name:
4858 * @parse_name: a file name or path to be parsed.
4860 * Constructs a #GFile with the given @parse_name (i.e. something given by g_file_get_parse_name()).
4861 * This operation never fails, but the returned object might not support any I/O
4862 * operation if the @parse_name cannot be parsed.
4864 * Returns: a new #GFile.
4867 g_file_parse_name (const char *parse_name)
4869 g_return_val_if_fail (parse_name != NULL, NULL);
4871 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
4875 is_valid_scheme_character (char c)
4877 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
4880 /* Following RFC 2396, valid schemes are built like:
4881 * scheme = alpha *( alpha | digit | "+" | "-" | "." )
4884 has_valid_scheme (const char *uri)
4890 if (!g_ascii_isalpha (*p))
4895 } while (is_valid_scheme_character (*p));
4901 * g_file_new_for_commandline_arg:
4902 * @arg: a command line string.
4904 * Creates a #GFile with the given argument from the command line. The value of
4905 * @arg can be either a URI, an absolute path or a relative path resolved
4906 * relative to the current working directory.
4907 * This operation never fails, but the returned object might not support any
4908 * I/O operation if @arg points to a malformed path.
4910 * Returns: a new #GFile.
4913 g_file_new_for_commandline_arg (const char *arg)
4919 g_return_val_if_fail (arg != NULL, NULL);
4921 if (g_path_is_absolute (arg))
4922 return g_file_new_for_path (arg);
4924 if (has_valid_scheme (arg))
4925 return g_file_new_for_uri (arg);
4927 current_dir = g_get_current_dir ();
4928 filename = g_build_filename (current_dir, arg, NULL);
4929 g_free (current_dir);
4931 file = g_file_new_for_path (filename);
4938 * g_file_mount_enclosing_volume:
4939 * @location: input #GFile.
4940 * @flags: flags affecting the operation
4941 * @mount_operation: a #GMountOperation or %NULL to avoid user interaction.
4942 * @cancellable: optional #GCancellable object, %NULL to ignore.
4943 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4944 * @user_data: the data to pass to callback function
4946 * Starts a @mount_operation, mounting the volume that contains the file @location.
4948 * When this operation has completed, @callback will be called with
4949 * @user_user data, and the operation can be finalized with
4950 * g_file_mount_enclosing_volume_finish().
4952 * If @cancellable is not %NULL, then the operation can be cancelled by
4953 * triggering the cancellable object from another thread. If the operation
4954 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4957 g_file_mount_enclosing_volume (GFile *location,
4958 GMountMountFlags flags,
4959 GMountOperation *mount_operation,
4960 GCancellable *cancellable,
4961 GAsyncReadyCallback callback,
4966 g_return_if_fail (G_IS_FILE (location));
4968 iface = G_FILE_GET_IFACE (location);
4970 if (iface->mount_enclosing_volume == NULL)
4972 g_simple_async_report_error_in_idle (G_OBJECT (location),
4973 callback, user_data,
4974 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4975 _("volume doesn't implement mount"));
4980 (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
4985 * g_file_mount_enclosing_volume_finish:
4986 * @location: input #GFile.
4987 * @result: a #GAsyncResult.
4988 * @error: a #GError, or %NULL
4990 * Finishes a mount operation started by g_file_mount_enclosing_volume().
4992 * Returns: %TRUE if successful. If an error
4993 * has occurred, this function will return %FALSE and set @error
4994 * appropriately if present.
4997 g_file_mount_enclosing_volume_finish (GFile *location,
4998 GAsyncResult *result,
5003 g_return_val_if_fail (G_IS_FILE (location), FALSE);
5004 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5006 if (G_IS_SIMPLE_ASYNC_RESULT (result))
5008 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
5009 if (g_simple_async_result_propagate_error (simple, error))
5013 iface = G_FILE_GET_IFACE (location);
5015 return (* iface->mount_enclosing_volume_finish) (location, result, error);
5018 /********************************************
5019 * Utility functions *
5020 ********************************************/
5023 * g_file_query_default_handler:
5024 * @file: a #GFile to open.
5025 * @cancellable: optional #GCancellable object, %NULL to ignore.
5026 * @error: a #GError, or %NULL
5028 * Returns the #GAppInfo that is registered as the default
5029 * application to handle the file specified by @file.
5031 * If @cancellable is not %NULL, then the operation can be cancelled by
5032 * triggering the cancellable object from another thread. If the operation
5033 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5035 * Returns: a #GAppInfo if the handle was found, %NULL if there were errors.
5036 * When you are done with it, release it with g_object_unref()
5039 g_file_query_default_handler (GFile *file,
5040 GCancellable *cancellable,
5044 const char *content_type;
5049 uri_scheme = g_file_get_uri_scheme (file);
5050 appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
5051 g_free (uri_scheme);
5053 if (appinfo != NULL)
5056 info = g_file_query_info (file,
5057 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
5066 content_type = g_file_info_get_content_type (info);
5069 /* Don't use is_native(), as we want to support fuse paths if availible */
5070 path = g_file_get_path (file);
5071 appinfo = g_app_info_get_default_for_type (content_type,
5076 g_object_unref (info);
5078 if (appinfo != NULL)
5081 g_set_error_literal (error, G_IO_ERROR,
5082 G_IO_ERROR_NOT_SUPPORTED,
5083 _("No application is registered as handling this file"));
5089 #define GET_CONTENT_BLOCK_SIZE 8192
5092 * g_file_load_contents:
5093 * @file: input #GFile.
5094 * @cancellable: optional #GCancellable object, %NULL to ignore.
5095 * @contents: a location to place the contents of the file.
5096 * @length: a location to place the length of the contents of the file,
5097 * or %NULL if the length is not needed
5098 * @etag_out: a location to place the current entity tag for the file,
5099 * or %NULL if the entity tag is not needed
5100 * @error: a #GError, or %NULL
5102 * Loads the content of the file into memory. The data is always
5103 * zero-terminated, but this is not included in the resultant @length.
5104 * The returned @content should be freed with g_free() when no longer
5107 * If @cancellable is not %NULL, then the operation can be cancelled by
5108 * triggering the cancellable object from another thread. If the operation
5109 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5111 * Returns: %TRUE if the @file's contents were successfully loaded.
5112 * %FALSE if there were errors.
5115 g_file_load_contents (GFile *file,
5116 GCancellable *cancellable,
5122 GFileInputStream *in;
5123 GByteArray *content;
5128 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5129 g_return_val_if_fail (contents != NULL, FALSE);
5131 in = g_file_read (file, cancellable, error);
5135 content = g_byte_array_new ();
5138 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
5139 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
5140 content->data + pos,
5141 GET_CONTENT_BLOCK_SIZE,
5142 cancellable, error)) > 0)
5145 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
5152 info = g_file_input_stream_query_info (in,
5153 G_FILE_ATTRIBUTE_ETAG_VALUE,
5158 *etag_out = g_strdup (g_file_info_get_etag (info));
5159 g_object_unref (info);
5163 /* Ignore errors on close */
5164 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
5165 g_object_unref (in);
5169 /* error is set already */
5170 g_byte_array_free (content, TRUE);
5177 /* Zero terminate (we got an extra byte allocated for this */
5178 content->data[pos] = 0;
5180 *contents = (char *)g_byte_array_free (content, FALSE);
5188 GCancellable *cancellable;
5189 GFileReadMoreCallback read_more_callback;
5190 GAsyncReadyCallback callback;
5192 GByteArray *content;
5199 load_contents_data_free (LoadContentsData *data)
5202 g_error_free (data->error);
5203 if (data->cancellable)
5204 g_object_unref (data->cancellable);
5206 g_byte_array_free (data->content, TRUE);
5207 g_free (data->etag);
5208 g_object_unref (data->file);
5213 load_contents_close_callback (GObject *obj,
5214 GAsyncResult *close_res,
5217 GInputStream *stream = G_INPUT_STREAM (obj);
5218 LoadContentsData *data = user_data;
5219 GSimpleAsyncResult *res;
5221 /* Ignore errors here, we're only reading anyway */
5222 g_input_stream_close_finish (stream, close_res, NULL);
5223 g_object_unref (stream);
5225 res = g_simple_async_result_new (G_OBJECT (data->file),
5228 g_file_load_contents_async);
5229 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)load_contents_data_free);
5230 g_simple_async_result_complete (res);
5231 g_object_unref (res);
5235 load_contents_fstat_callback (GObject *obj,
5236 GAsyncResult *stat_res,
5239 GInputStream *stream = G_INPUT_STREAM (obj);
5240 LoadContentsData *data = user_data;
5243 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
5247 data->etag = g_strdup (g_file_info_get_etag (info));
5248 g_object_unref (info);
5251 g_input_stream_close_async (stream, 0,
5253 load_contents_close_callback, data);
5257 load_contents_read_callback (GObject *obj,
5258 GAsyncResult *read_res,
5261 GInputStream *stream = G_INPUT_STREAM (obj);
5262 LoadContentsData *data = user_data;
5263 GError *error = NULL;
5266 read_size = g_input_stream_read_finish (stream, read_res, &error);
5270 /* Error or EOF, close the file */
5271 data->error = error;
5272 g_input_stream_close_async (stream, 0,
5274 load_contents_close_callback, data);
5276 else if (read_size == 0)
5278 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
5279 G_FILE_ATTRIBUTE_ETAG_VALUE,
5282 load_contents_fstat_callback,
5285 else if (read_size > 0)
5287 data->pos += read_size;
5289 g_byte_array_set_size (data->content,
5290 data->pos + GET_CONTENT_BLOCK_SIZE);
5293 if (data->read_more_callback &&
5294 !data->read_more_callback ((char *)data->content->data, data->pos, data->user_data))
5295 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
5296 G_FILE_ATTRIBUTE_ETAG_VALUE,
5299 load_contents_fstat_callback,
5302 g_input_stream_read_async (stream,
5303 data->content->data + data->pos,
5304 GET_CONTENT_BLOCK_SIZE,
5307 load_contents_read_callback,
5313 load_contents_open_callback (GObject *obj,
5314 GAsyncResult *open_res,
5317 GFile *file = G_FILE (obj);
5318 GFileInputStream *stream;
5319 LoadContentsData *data = user_data;
5320 GError *error = NULL;
5321 GSimpleAsyncResult *res;
5323 stream = g_file_read_finish (file, open_res, &error);
5327 g_byte_array_set_size (data->content,
5328 data->pos + GET_CONTENT_BLOCK_SIZE);
5329 g_input_stream_read_async (G_INPUT_STREAM (stream),
5330 data->content->data + data->pos,
5331 GET_CONTENT_BLOCK_SIZE,
5334 load_contents_read_callback,
5340 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
5344 g_simple_async_result_complete (res);
5345 g_error_free (error);
5346 load_contents_data_free (data);
5347 g_object_unref (res);
5352 * g_file_load_partial_contents_async:
5353 * @file: input #GFile.
5354 * @cancellable: optional #GCancellable object, %NULL to ignore.
5355 * @read_more_callback: a #GFileReadMoreCallback to receive partial data and to specify whether further data should be read.
5356 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5357 * @user_data: the data to pass to the callback functions.
5359 * Reads the partial contents of a file. A #GFileReadMoreCallback should be
5360 * used to stop reading from the file when appropriate, else this function
5361 * will behave exactly as g_file_load_contents_async(). This operation
5362 * can be finished by g_file_load_partial_contents_finish().
5364 * Users of this function should be aware that @user_data is passed to
5365 * both the @read_more_callback and the @callback.
5367 * If @cancellable is not %NULL, then the operation can be cancelled by
5368 * triggering the cancellable object from another thread. If the operation
5369 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5372 g_file_load_partial_contents_async (GFile *file,
5373 GCancellable *cancellable,
5374 GFileReadMoreCallback read_more_callback,
5375 GAsyncReadyCallback callback,
5378 LoadContentsData *data;
5380 g_return_if_fail (G_IS_FILE (file));
5382 data = g_new0 (LoadContentsData, 1);
5385 data->cancellable = g_object_ref (cancellable);
5386 data->read_more_callback = read_more_callback;
5387 data->callback = callback;
5388 data->user_data = user_data;
5389 data->content = g_byte_array_new ();
5390 data->file = g_object_ref (file);
5392 g_file_read_async (file,
5395 load_contents_open_callback,
5400 * g_file_load_partial_contents_finish:
5401 * @file: input #GFile.
5402 * @res: a #GAsyncResult.
5403 * @contents: a location to place the contents of the file.
5404 * @length: a location to place the length of the contents of the file,
5405 * or %NULL if the length is not needed
5406 * @etag_out: a location to place the current entity tag for the file,
5407 * or %NULL if the entity tag is not needed
5408 * @error: a #GError, or %NULL
5410 * Finishes an asynchronous partial load operation that was started
5411 * with g_file_load_partial_contents_async(). The data is always
5412 * zero-terminated, but this is not included in the resultant @length.
5413 * The returned @content should be freed with g_free() when no longer
5416 * Returns: %TRUE if the load was successful. If %FALSE and @error is
5417 * present, it will be set appropriately.
5420 g_file_load_partial_contents_finish (GFile *file,
5427 GSimpleAsyncResult *simple;
5428 LoadContentsData *data;
5430 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5431 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
5432 g_return_val_if_fail (contents != NULL, FALSE);
5434 simple = G_SIMPLE_ASYNC_RESULT (res);
5436 if (g_simple_async_result_propagate_error (simple, error))
5439 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_load_contents_async);
5441 data = g_simple_async_result_get_op_res_gpointer (simple);
5445 g_propagate_error (error, data->error);
5454 *length = data->pos;
5458 *etag_out = data->etag;
5462 /* Zero terminate */
5463 g_byte_array_set_size (data->content, data->pos + 1);
5464 data->content->data[data->pos] = 0;
5466 *contents = (char *)g_byte_array_free (data->content, FALSE);
5467 data->content = NULL;
5473 * g_file_load_contents_async:
5474 * @file: input #GFile.
5475 * @cancellable: optional #GCancellable object, %NULL to ignore.
5476 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5477 * @user_data: the data to pass to callback function
5479 * Starts an asynchronous load of the @file's contents.
5481 * For more details, see g_file_load_contents() which is
5482 * the synchronous version of this call.
5484 * When the load operation has completed, @callback will be called
5485 * with @user data. To finish the operation, call
5486 * g_file_load_contents_finish() with the #GAsyncResult returned by
5489 * If @cancellable is not %NULL, then the operation can be cancelled by
5490 * triggering the cancellable object from another thread. If the operation
5491 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5494 g_file_load_contents_async (GFile *file,
5495 GCancellable *cancellable,
5496 GAsyncReadyCallback callback,
5499 g_file_load_partial_contents_async (file,
5502 callback, user_data);
5506 * g_file_load_contents_finish:
5507 * @file: input #GFile.
5508 * @res: a #GAsyncResult.
5509 * @contents: a location to place the contents of the file.
5510 * @length: a location to place the length of the contents of the file,
5511 * or %NULL if the length is not needed
5512 * @etag_out: a location to place the current entity tag for the file,
5513 * or %NULL if the entity tag is not needed
5514 * @error: a #GError, or %NULL
5516 * Finishes an asynchronous load of the @file's contents.
5517 * The contents are placed in @contents, and @length is set to the
5518 * size of the @contents string. The @content should be freed with
5519 * g_free() when no longer needed. If @etag_out is present, it will be
5520 * set to the new entity tag for the @file.
5522 * Returns: %TRUE if the load was successful. If %FALSE and @error is
5523 * present, it will be set appropriately.
5526 g_file_load_contents_finish (GFile *file,
5533 return g_file_load_partial_contents_finish (file,
5542 * g_file_replace_contents:
5543 * @file: input #GFile.
5544 * @contents: a string containing the new contents for @file.
5545 * @length: the length of @contents in bytes.
5546 * @etag: the old <link linkend="gfile-etag">entity tag</link>
5547 * for the document, or %NULL
5548 * @make_backup: %TRUE if a backup should be created.
5549 * @flags: a set of #GFileCreateFlags.
5550 * @new_etag: a location to a new <link linkend="gfile-etag">entity tag</link>
5551 * for the document. This should be freed with g_free() when no longer
5553 * @cancellable: optional #GCancellable object, %NULL to ignore.
5554 * @error: a #GError, or %NULL
5556 * Replaces the contents of @file with @contents of @length bytes.
5558 * If @etag is specified (not %NULL) any existing file must have that etag, or
5559 * the error %G_IO_ERROR_WRONG_ETAG will be returned.
5561 * If @make_backup is %TRUE, this function will attempt to make a backup of @file.
5563 * If @cancellable is not %NULL, then the operation can be cancelled by
5564 * triggering the cancellable object from another thread. If the operation
5565 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5567 * The returned @new_etag can be used to verify that the file hasn't changed the
5568 * next time it is saved over.
5570 * Returns: %TRUE if successful. If an error
5571 * has occurred, this function will return %FALSE and set @error
5572 * appropriately if present.
5575 g_file_replace_contents (GFile *file,
5576 const char *contents,
5579 gboolean make_backup,
5580 GFileCreateFlags flags,
5582 GCancellable *cancellable,
5585 GFileOutputStream *out;
5586 gsize pos, remainder;
5590 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5591 g_return_val_if_fail (contents != NULL, FALSE);
5593 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
5599 while (remainder > 0 &&
5600 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
5602 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
5610 if (remainder > 0 && res < 0)
5612 /* Ignore errors on close */
5613 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
5614 g_object_unref (out);
5616 /* error is set already */
5620 ret = g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error);
5623 *new_etag = g_file_output_stream_get_etag (out);
5625 g_object_unref (out);
5633 GCancellable *cancellable;
5634 GAsyncReadyCallback callback;
5636 const char *content;
5640 } ReplaceContentsData;
5643 replace_contents_data_free (ReplaceContentsData *data)
5646 g_error_free (data->error);
5647 if (data->cancellable)
5648 g_object_unref (data->cancellable);
5649 g_object_unref (data->file);
5650 g_free (data->etag);
5655 replace_contents_close_callback (GObject *obj,
5656 GAsyncResult *close_res,
5659 GOutputStream *stream = G_OUTPUT_STREAM (obj);
5660 ReplaceContentsData *data = user_data;
5661 GSimpleAsyncResult *res;
5663 /* Ignore errors here, we're only reading anyway */
5664 g_output_stream_close_finish (stream, close_res, NULL);
5665 g_object_unref (stream);
5667 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
5669 res = g_simple_async_result_new (G_OBJECT (data->file),
5672 g_file_replace_contents_async);
5673 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_contents_data_free);
5674 g_simple_async_result_complete (res);
5675 g_object_unref (res);
5679 replace_contents_write_callback (GObject *obj,
5680 GAsyncResult *read_res,
5683 GOutputStream *stream = G_OUTPUT_STREAM (obj);
5684 ReplaceContentsData *data = user_data;
5685 GError *error = NULL;
5688 write_size = g_output_stream_write_finish (stream, read_res, &error);
5690 if (write_size <= 0)
5692 /* Error or EOF, close the file */
5694 data->error = error;
5695 g_output_stream_close_async (stream, 0,
5697 replace_contents_close_callback, data);
5699 else if (write_size > 0)
5701 data->pos += write_size;
5703 if (data->pos >= data->length)
5704 g_output_stream_close_async (stream, 0,
5706 replace_contents_close_callback, data);
5708 g_output_stream_write_async (stream,
5709 data->content + data->pos,
5710 data->length - data->pos,
5713 replace_contents_write_callback,
5719 replace_contents_open_callback (GObject *obj,
5720 GAsyncResult *open_res,
5723 GFile *file = G_FILE (obj);
5724 GFileOutputStream *stream;
5725 ReplaceContentsData *data = user_data;
5726 GError *error = NULL;
5727 GSimpleAsyncResult *res;
5729 stream = g_file_replace_finish (file, open_res, &error);
5733 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
5734 data->content + data->pos,
5735 data->length - data->pos,
5738 replace_contents_write_callback,
5744 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
5748 g_simple_async_result_complete (res);
5749 g_error_free (error);
5750 replace_contents_data_free (data);
5751 g_object_unref (res);
5756 * g_file_replace_contents_async:
5757 * @file: input #GFile.
5758 * @contents: string of contents to replace the file with.
5759 * @length: the length of @contents in bytes.
5760 * @etag: a new <link linkend="gfile-etag">entity tag</link> for the @file, or %NULL
5761 * @make_backup: %TRUE if a backup should be created.
5762 * @flags: a set of #GFileCreateFlags.
5763 * @cancellable: optional #GCancellable object, %NULL to ignore.
5764 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5765 * @user_data: the data to pass to callback function
5767 * Starts an asynchronous replacement of @file with the given
5768 * @contents of @length bytes. @etag will replace the document's
5769 * current entity tag.
5771 * When this operation has completed, @callback will be called with
5772 * @user_user data, and the operation can be finalized with
5773 * g_file_replace_contents_finish().
5775 * If @cancellable is not %NULL, then the operation can be cancelled by
5776 * triggering the cancellable object from another thread. If the operation
5777 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5779 * If @make_backup is %TRUE, this function will attempt to
5780 * make a backup of @file.
5783 g_file_replace_contents_async (GFile *file,
5784 const char *contents,
5787 gboolean make_backup,
5788 GFileCreateFlags flags,
5789 GCancellable *cancellable,
5790 GAsyncReadyCallback callback,
5793 ReplaceContentsData *data;
5795 g_return_if_fail (G_IS_FILE (file));
5796 g_return_if_fail (contents != NULL);
5798 data = g_new0 (ReplaceContentsData, 1);
5801 data->cancellable = g_object_ref (cancellable);
5802 data->callback = callback;
5803 data->user_data = user_data;
5804 data->content = contents;
5805 data->length = length;
5807 data->file = g_object_ref (file);
5809 g_file_replace_async (file,
5815 replace_contents_open_callback,
5820 * g_file_replace_contents_finish:
5821 * @file: input #GFile.
5822 * @res: a #GAsyncResult.
5823 * @new_etag: a location of a new <link linkend="gfile-etag">entity tag</link>
5824 * for the document. This should be freed with g_free() when it is no
5825 * longer needed, or %NULL
5826 * @error: a #GError, or %NULL
5828 * Finishes an asynchronous replace of the given @file. See
5829 * g_file_replace_contents_async(). Sets @new_etag to the new entity
5830 * tag for the document, if present.
5832 * Returns: %TRUE on success, %FALSE on failure.
5835 g_file_replace_contents_finish (GFile *file,
5840 GSimpleAsyncResult *simple;
5841 ReplaceContentsData *data;
5843 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5844 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
5846 simple = G_SIMPLE_ASYNC_RESULT (res);
5848 if (g_simple_async_result_propagate_error (simple, error))
5851 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_replace_contents_async);
5853 data = g_simple_async_result_get_op_res_gpointer (simple);
5857 g_propagate_error (error, data->error);
5865 *new_etag = data->etag;
5866 data->etag = NULL; /* Take ownership */
5872 #define __G_FILE_C__
5873 #include "gioaliasdef.c"