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.
688 * Only full pathname elements are matched, so a path like /foo is not
689 * considered a prefix of /foobar, only of /foo/bar.
691 * This call does no i/o, as it works purely on names. As such it can
692 * sometimes return %FALSE even if @file is inside a @prefix (from a
693 * filesystem point of view), because the prefix of @file is an alias
696 * Returns: %TRUE if the @files's parent, grandparent, etc is @prefix.
700 g_file_has_prefix (GFile *file,
705 g_return_val_if_fail (G_IS_FILE (file), FALSE);
706 g_return_val_if_fail (G_IS_FILE (prefix), FALSE);
708 if (G_TYPE_FROM_INSTANCE (file) != G_TYPE_FROM_INSTANCE (prefix))
711 iface = G_FILE_GET_IFACE (file);
713 /* The vtable function differs in arg order since we're
714 using the old contains_file call */
715 return (* iface->prefix_matches) (prefix, file);
719 * g_file_get_relative_path:
720 * @parent: input #GFile.
721 * @descendant: input #GFile.
723 * Gets the path for @descendant relative to @parent.
725 * This call does no blocking i/o.
727 * Returns: string with the relative path from @descendant
728 * to @parent, or %NULL if @descendant doesn't have @parent as prefix.
729 * The returned string should be freed with g_free() when no longer needed.
732 g_file_get_relative_path (GFile *parent,
737 g_return_val_if_fail (G_IS_FILE (parent), NULL);
738 g_return_val_if_fail (G_IS_FILE (descendant), NULL);
740 if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
743 iface = G_FILE_GET_IFACE (parent);
745 return (* iface->get_relative_path) (parent, descendant);
749 * g_file_resolve_relative_path:
750 * @file: input #GFile.
751 * @relative_path: a given relative path string.
753 * Resolves a relative path for @file to an absolute path.
755 * This call does no blocking i/o.
757 * Returns: #GFile to the resolved path. %NULL if @relative_path
758 * is %NULL or if @file is invalid.
759 * Free the returned object with g_object_unref().
762 g_file_resolve_relative_path (GFile *file,
763 const char *relative_path)
767 g_return_val_if_fail (G_IS_FILE (file), NULL);
768 g_return_val_if_fail (relative_path != NULL, NULL);
770 iface = G_FILE_GET_IFACE (file);
772 return (* iface->resolve_relative_path) (file, relative_path);
776 * g_file_enumerate_children:
777 * @file: input #GFile.
778 * @attributes: an attribute query string.
779 * @flags: a set of #GFileQueryInfoFlags.
780 * @cancellable: optional #GCancellable object, %NULL to ignore.
781 * @error: #GError for error reporting.
783 * Gets the requested information about the files in a directory. The result
784 * is a #GFileEnumerator object that will give out #GFileInfo objects for
785 * all the files in the directory.
787 * The @attribute value is a string that specifies the file attributes that
788 * should be gathered. It is not an error if it's not possible to read a particular
789 * requested attribute from a file - it just won't be set. @attribute should
790 * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
791 * means all attributes, and a wildcard like "standard::*" means all attributes in the standard
792 * namespace. An example attribute query be "standard::*,owner::user".
793 * The standard attributes are available as defines, like #G_FILE_ATTRIBUTE_STANDARD_NAME.
795 * If @cancellable is not %NULL, then the operation can be cancelled by
796 * triggering the cancellable object from another thread. If the operation
797 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
799 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
800 * If the file is not a directory, the G_FILE_ERROR_NOTDIR error will be returned.
801 * Other errors are possible too.
803 * Returns: A #GFileEnumerator if successful, %NULL on error.
804 * Free the returned object with g_object_unref().
807 g_file_enumerate_children (GFile *file,
808 const char *attributes,
809 GFileQueryInfoFlags flags,
810 GCancellable *cancellable,
816 g_return_val_if_fail (G_IS_FILE (file), NULL);
818 if (g_cancellable_set_error_if_cancelled (cancellable, error))
821 iface = G_FILE_GET_IFACE (file);
823 if (iface->enumerate_children == NULL)
825 g_set_error_literal (error, G_IO_ERROR,
826 G_IO_ERROR_NOT_SUPPORTED,
827 _("Operation not supported"));
831 return (* iface->enumerate_children) (file, attributes, flags,
836 * g_file_enumerate_children_async:
837 * @file: input #GFile.
838 * @attributes: an attribute query string.
839 * @flags: a set of #GFileQueryInfoFlags.
840 * @io_priority: the <link linkend="io-priority">I/O priority</link>
842 * @cancellable: optional #GCancellable object, %NULL to ignore.
843 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
844 * @user_data: the data to pass to callback function
846 * Asynchronously gets the requested information about the files in a directory. The result
847 * is a #GFileEnumerator object that will give out #GFileInfo objects for
848 * all the files in the directory.
850 * For more details, see g_file_enumerate_children() which is
851 * the synchronous version of this call.
853 * When the operation is finished, @callback will be called. You can then call
854 * g_file_enumerate_children_finish() to get the result of the operation.
857 g_file_enumerate_children_async (GFile *file,
858 const char *attributes,
859 GFileQueryInfoFlags flags,
861 GCancellable *cancellable,
862 GAsyncReadyCallback callback,
867 g_return_if_fail (G_IS_FILE (file));
869 iface = G_FILE_GET_IFACE (file);
870 (* iface->enumerate_children_async) (file,
880 * g_file_enumerate_children_finish:
881 * @file: input #GFile.
882 * @res: a #GAsyncResult.
885 * Finishes an async enumerate children operation.
886 * See g_file_enumerate_children_async().
888 * Returns: a #GFileEnumerator or %NULL if an error occurred.
889 * Free the returned object with g_object_unref().
892 g_file_enumerate_children_finish (GFile *file,
898 g_return_val_if_fail (G_IS_FILE (file), NULL);
899 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
901 if (G_IS_SIMPLE_ASYNC_RESULT (res))
903 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
904 if (g_simple_async_result_propagate_error (simple, error))
908 iface = G_FILE_GET_IFACE (file);
909 return (* iface->enumerate_children_finish) (file, res, error);
913 * g_file_query_exists:
914 * @file: input #GFile.
915 * @cancellable: optional #GCancellable object, %NULL to ignore.
917 * Utility function to check if a particular file exists. This is
918 * implemented using g_file_query_info() and as such does blocking I/O.
920 * Note that in many cases it is racy to first check for file existence
921 * and then execute something based on the outcome of that, because the
922 * file might have been created or removed in between the operations. The
923 * general approach to handling that is to not check, but just do the
924 * operation and handle the errors as they come.
926 * As an example of race-free checking, take the case of reading a file, and
927 * if it doesn't exist, creating it. There are two racy versions: read it, and
928 * on error create it; and: check if it exists, if not create it. These
929 * can both result in two processes creating the file (with perhaps a partially
930 * written file as the result). The correct approach is to always try to create
931 * the file with g_file_create() which will either atomically create the file
932 * or fail with a G_IO_ERROR_EXISTS error.
934 * However, in many cases an existence check is useful in a user
935 * interface, for instance to make a menu item sensitive/insensitive, so that
936 * you don't have to fool users that something is possible and then just show
937 * and error dialog. If you do this, you should make sure to also handle the
938 * errors that can happen due to races when you execute the operation.
940 * Returns: %TRUE if the file exists (and can be detected without error), %FALSE otherwise (or if cancelled).
943 g_file_query_exists (GFile *file,
944 GCancellable *cancellable)
948 g_return_val_if_fail (G_IS_FILE(file), FALSE);
950 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE,
951 G_FILE_QUERY_INFO_NONE, cancellable, NULL);
954 g_object_unref (info);
962 * g_file_query_file_type:
963 * @file: input #GFile.
964 * @flags: a set of #GFileQueryInfoFlags passed to g_file_query_info().
965 * @cancellable: optional #GCancellable object, %NULL to ignore.
967 * Utility function to inspect the #GFileType of a file. This is
968 * implemented using g_file_query_info() and as such does blocking I/O.
970 * The primary use case of this method is to check if a file is a regular file,
971 * directory, or symlink.
973 * Returns: The #GFileType of the file and #G_FILE_TYPE_UNKNOWN if the file
979 g_file_query_file_type (GFile *file,
980 GFileQueryInfoFlags flags,
981 GCancellable *cancellable)
986 g_return_val_if_fail (G_IS_FILE(file), G_FILE_TYPE_UNKNOWN);
987 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE, flags,
991 file_type = g_file_info_get_file_type (info);
992 g_object_unref (info);
995 file_type = G_FILE_TYPE_UNKNOWN;
1001 * g_file_query_info:
1002 * @file: input #GFile.
1003 * @attributes: an attribute query string.
1004 * @flags: a set of #GFileQueryInfoFlags.
1005 * @cancellable: optional #GCancellable object, %NULL to ignore.
1006 * @error: a #GError.
1008 * Gets the requested information about specified @file. The result
1009 * is a #GFileInfo object that contains key-value attributes (such as
1010 * the type or size of the file).
1012 * The @attribute value is a string that specifies the file attributes that
1013 * should be gathered. It is not an error if it's not possible to read a particular
1014 * requested attribute from a file - it just won't be set. @attribute should
1015 * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
1016 * means all attributes, and a wildcard like "standard::*" means all attributes in the standard
1017 * namespace. An example attribute query be "standard::*,owner::user".
1018 * The standard attributes are available as defines, like #G_FILE_ATTRIBUTE_STANDARD_NAME.
1020 * If @cancellable is not %NULL, then the operation can be cancelled by
1021 * triggering the cancellable object from another thread. If the operation
1022 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1024 * For symlinks, normally the information about the target of the
1025 * symlink is returned, rather than information about the symlink itself.
1026 * However if you pass #G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS in @flags the
1027 * information about the symlink itself will be returned. Also, for symlinks
1028 * that point to non-existing files the information about the symlink itself
1031 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1032 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1034 * Returns: a #GFileInfo for the given @file, or %NULL on error.
1035 * Free the returned object with g_object_unref().
1038 g_file_query_info (GFile *file,
1039 const char *attributes,
1040 GFileQueryInfoFlags flags,
1041 GCancellable *cancellable,
1046 g_return_val_if_fail (G_IS_FILE (file), NULL);
1048 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1051 iface = G_FILE_GET_IFACE (file);
1053 if (iface->query_info == NULL)
1055 g_set_error_literal (error, G_IO_ERROR,
1056 G_IO_ERROR_NOT_SUPPORTED,
1057 _("Operation not supported"));
1061 return (* iface->query_info) (file, attributes, flags, cancellable, error);
1065 * g_file_query_info_async:
1066 * @file: input #GFile.
1067 * @attributes: an attribute query string.
1068 * @flags: a set of #GFileQueryInfoFlags.
1069 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1071 * @cancellable: optional #GCancellable object, %NULL to ignore.
1072 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1073 * @user_data: the data to pass to callback function
1075 * Asynchronously gets the requested information about specified @file. The result
1076 * is a #GFileInfo object that contains key-value attributes (such as type or size
1079 * For more details, see g_file_query_info() which is
1080 * the synchronous version of this call.
1082 * When the operation is finished, @callback will be called. You can then call
1083 * g_file_query_info_finish() to get the result of the operation.
1086 g_file_query_info_async (GFile *file,
1087 const char *attributes,
1088 GFileQueryInfoFlags flags,
1090 GCancellable *cancellable,
1091 GAsyncReadyCallback callback,
1096 g_return_if_fail (G_IS_FILE (file));
1098 iface = G_FILE_GET_IFACE (file);
1099 (* iface->query_info_async) (file,
1109 * g_file_query_info_finish:
1110 * @file: input #GFile.
1111 * @res: a #GAsyncResult.
1112 * @error: a #GError.
1114 * Finishes an asynchronous file info query.
1115 * See g_file_query_info_async().
1117 * Returns: #GFileInfo for given @file or %NULL on error.
1118 * Free the returned object with g_object_unref().
1121 g_file_query_info_finish (GFile *file,
1127 g_return_val_if_fail (G_IS_FILE (file), NULL);
1128 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1130 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1132 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1133 if (g_simple_async_result_propagate_error (simple, error))
1137 iface = G_FILE_GET_IFACE (file);
1138 return (* iface->query_info_finish) (file, res, error);
1142 * g_file_query_filesystem_info:
1143 * @file: input #GFile.
1144 * @attributes: an attribute query string.
1145 * @cancellable: optional #GCancellable object, %NULL to ignore.
1146 * @error: a #GError.
1148 * Similar to g_file_query_info(), but obtains information
1149 * about the filesystem the @file is on, rather than the file itself.
1150 * For instance the amount of space available and the type of
1153 * The @attribute value is a string that specifies the file attributes that
1154 * should be gathered. It is not an error if it's not possible to read a particular
1155 * requested attribute from a file - it just won't be set. @attribute should
1156 * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
1157 * means all attributes, and a wildcard like "fs:*" means all attributes in the fs
1158 * namespace. The standard namespace for filesystem attributes is "fs".
1159 * Common attributes of interest are #G_FILE_ATTRIBUTE_FILESYSTEM_SIZE
1160 * (the total size of the filesystem in bytes), #G_FILE_ATTRIBUTE_FILESYSTEM_FREE (number of
1161 * bytes available), and #G_FILE_ATTRIBUTE_FILESYSTEM_TYPE (type of the filesystem).
1163 * If @cancellable is not %NULL, then the operation can be cancelled by
1164 * triggering the cancellable object from another thread. If the operation
1165 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1167 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1168 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1170 * Returns: a #GFileInfo or %NULL if there was an error.
1171 * Free the returned object with g_object_unref().
1174 g_file_query_filesystem_info (GFile *file,
1175 const char *attributes,
1176 GCancellable *cancellable,
1181 g_return_val_if_fail (G_IS_FILE (file), NULL);
1183 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1186 iface = G_FILE_GET_IFACE (file);
1188 if (iface->query_filesystem_info == NULL)
1190 g_set_error_literal (error, G_IO_ERROR,
1191 G_IO_ERROR_NOT_SUPPORTED,
1192 _("Operation not supported"));
1196 return (* iface->query_filesystem_info) (file, attributes, cancellable, error);
1200 * g_file_query_filesystem_info_async:
1201 * @file: input #GFile.
1202 * @attributes: an attribute query string.
1203 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1205 * @cancellable: optional #GCancellable object, %NULL to ignore.
1206 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1207 * @user_data: the data to pass to callback function
1209 * Asynchronously gets the requested information about the filesystem
1210 * that the specified @file is on. The result is a #GFileInfo object
1211 * that contains key-value attributes (such as type or size for the
1214 * For more details, see g_file_query_filesystem_info() which is the
1215 * synchronous version of this call.
1217 * When the operation is finished, @callback will be called. You can
1218 * then call g_file_query_info_finish() to get the result of the
1222 g_file_query_filesystem_info_async (GFile *file,
1223 const char *attributes,
1225 GCancellable *cancellable,
1226 GAsyncReadyCallback callback,
1231 g_return_if_fail (G_IS_FILE (file));
1233 iface = G_FILE_GET_IFACE (file);
1234 (* iface->query_filesystem_info_async) (file,
1243 * g_file_query_filesystem_info_finish:
1244 * @file: input #GFile.
1245 * @res: a #GAsyncResult.
1246 * @error: a #GError.
1248 * Finishes an asynchronous filesystem info query. See
1249 * g_file_query_filesystem_info_async().
1251 * Returns: #GFileInfo for given @file or %NULL on error.
1252 * Free the returned object with g_object_unref().
1255 g_file_query_filesystem_info_finish (GFile *file,
1261 g_return_val_if_fail (G_IS_FILE (file), NULL);
1262 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1264 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1266 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1267 if (g_simple_async_result_propagate_error (simple, error))
1271 iface = G_FILE_GET_IFACE (file);
1272 return (* iface->query_filesystem_info_finish) (file, res, error);
1276 * g_file_find_enclosing_mount:
1277 * @file: input #GFile.
1278 * @cancellable: optional #GCancellable object, %NULL to ignore.
1279 * @error: a #GError.
1281 * Gets a #GMount for the #GFile.
1283 * If the #GFileIface for @file does not have a mount (e.g. possibly a
1284 * remote share), @error will be set to %G_IO_ERROR_NOT_FOUND and %NULL
1287 * If @cancellable is not %NULL, then the operation can be cancelled by
1288 * triggering the cancellable object from another thread. If the operation
1289 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1291 * Returns: a #GMount where the @file is located or %NULL on error.
1292 * Free the returned object with g_object_unref().
1295 g_file_find_enclosing_mount (GFile *file,
1296 GCancellable *cancellable,
1301 g_return_val_if_fail (G_IS_FILE (file), NULL);
1303 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1306 iface = G_FILE_GET_IFACE (file);
1307 if (iface->find_enclosing_mount == NULL)
1310 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1311 /* Translators: This is an error message when trying to find the
1312 * enclosing (user visible) mount of a file, but none exists. */
1313 _("Containing mount does not exist"));
1317 return (* iface->find_enclosing_mount) (file, cancellable, error);
1321 * g_file_find_enclosing_mount_async:
1323 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1325 * @cancellable: optional #GCancellable object, %NULL to ignore.
1326 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1327 * @user_data: the data to pass to callback function
1329 * Asynchronously gets the mount for the file.
1331 * For more details, see g_file_find_enclosing_mount() which is
1332 * the synchronous version of this call.
1334 * When the operation is finished, @callback will be called. You can then call
1335 * g_file_find_enclosing_mount_finish() to get the result of the operation.
1338 g_file_find_enclosing_mount_async (GFile *file,
1340 GCancellable *cancellable,
1341 GAsyncReadyCallback callback,
1346 g_return_if_fail (G_IS_FILE (file));
1348 iface = G_FILE_GET_IFACE (file);
1349 (* iface->find_enclosing_mount_async) (file,
1357 * g_file_find_enclosing_mount_finish:
1359 * @res: a #GAsyncResult
1362 * Finishes an asynchronous find mount request.
1363 * See g_file_find_enclosing_mount_async().
1365 * Returns: #GMount for given @file or %NULL on error.
1366 * Free the returned object with g_object_unref().
1369 g_file_find_enclosing_mount_finish (GFile *file,
1375 g_return_val_if_fail (G_IS_FILE (file), NULL);
1376 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1378 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1380 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1381 if (g_simple_async_result_propagate_error (simple, error))
1385 iface = G_FILE_GET_IFACE (file);
1386 return (* iface->find_enclosing_mount_finish) (file, res, error);
1392 * @file: #GFile to read.
1393 * @cancellable: a #GCancellable
1394 * @error: a #GError, or %NULL
1396 * Opens a file for reading. The result is a #GFileInputStream that
1397 * can be used to read the contents of the file.
1399 * If @cancellable is not %NULL, then the operation can be cancelled by
1400 * triggering the cancellable object from another thread. If the operation
1401 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1403 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1404 * If the file is a directory, the G_IO_ERROR_IS_DIRECTORY error will be returned.
1405 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1407 * Returns: #GFileInputStream or %NULL on error.
1408 * Free the returned object with g_object_unref().
1411 g_file_read (GFile *file,
1412 GCancellable *cancellable,
1417 g_return_val_if_fail (G_IS_FILE (file), NULL);
1419 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1422 iface = G_FILE_GET_IFACE (file);
1424 if (iface->read_fn == NULL)
1426 g_set_error_literal (error, G_IO_ERROR,
1427 G_IO_ERROR_NOT_SUPPORTED,
1428 _("Operation not supported"));
1432 return (* iface->read_fn) (file, cancellable, error);
1437 * @file: input #GFile.
1438 * @flags: a set of #GFileCreateFlags.
1439 * @cancellable: optional #GCancellable object, %NULL to ignore.
1440 * @error: a #GError, or %NULL
1442 * Gets an output stream for appending data to the file. If
1443 * the file doesn't already exist it is created.
1445 * By default files created are generally readable by everyone,
1446 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1447 * will be made readable only to the current user, to the level that
1448 * is supported on the target filesystem.
1450 * If @cancellable is not %NULL, then the operation can be cancelled by
1451 * triggering the cancellable object from another thread. If the operation
1452 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1454 * Some file systems don't allow all file names, and may
1455 * return an %G_IO_ERROR_INVALID_FILENAME error.
1456 * If the file is a directory the %G_IO_ERROR_IS_DIRECTORY error will be
1457 * returned. Other errors are possible too, and depend on what kind of
1458 * filesystem the file is on.
1460 * Returns: a #GFileOutputStream, or %NULL on error.
1461 * Free the returned object with g_object_unref().
1464 g_file_append_to (GFile *file,
1465 GFileCreateFlags flags,
1466 GCancellable *cancellable,
1471 g_return_val_if_fail (G_IS_FILE (file), NULL);
1473 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1476 iface = G_FILE_GET_IFACE (file);
1478 if (iface->append_to == NULL)
1480 g_set_error_literal (error, G_IO_ERROR,
1481 G_IO_ERROR_NOT_SUPPORTED,
1482 _("Operation not supported"));
1486 return (* iface->append_to) (file, flags, cancellable, error);
1491 * @file: input #GFile.
1492 * @flags: a set of #GFileCreateFlags.
1493 * @cancellable: optional #GCancellable object, %NULL to ignore.
1494 * @error: a #GError, or %NULL
1496 * Creates a new file and returns an output stream for writing to it.
1497 * The file must not already exist.
1499 * By default files created are generally readable by everyone,
1500 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1501 * will be made readable only to the current user, to the level that
1502 * is supported on the target filesystem.
1504 * If @cancellable is not %NULL, then the operation can be cancelled by
1505 * triggering the cancellable object from another thread. If the operation
1506 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1508 * If a file or directory with this name already exists the G_IO_ERROR_EXISTS
1509 * error will be returned.
1510 * Some file systems don't allow all file names, and may
1511 * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1512 * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1513 * Other errors are possible too, and depend on what kind of
1514 * filesystem the file is on.
1516 * Returns: a #GFileOutputStream for the newly created file, or
1518 * Free the returned object with g_object_unref().
1521 g_file_create (GFile *file,
1522 GFileCreateFlags flags,
1523 GCancellable *cancellable,
1528 g_return_val_if_fail (G_IS_FILE (file), NULL);
1530 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1533 iface = G_FILE_GET_IFACE (file);
1535 if (iface->create == NULL)
1537 g_set_error_literal (error, G_IO_ERROR,
1538 G_IO_ERROR_NOT_SUPPORTED,
1539 _("Operation not supported"));
1543 return (* iface->create) (file, flags, cancellable, error);
1548 * @file: input #GFile.
1549 * @etag: an optional <link linkend="gfile-etag">entity tag</link> for the
1550 * current #GFile, or #NULL to ignore.
1551 * @make_backup: %TRUE if a backup should be created.
1552 * @flags: a set of #GFileCreateFlags.
1553 * @cancellable: optional #GCancellable object, %NULL to ignore.
1554 * @error: a #GError, or %NULL
1556 * Returns an output stream for overwriting the file, possibly
1557 * creating a backup copy of the file first. If the file doesn't exist,
1558 * it will be created.
1560 * This will try to replace the file in the safest way possible so
1561 * that any errors during the writing will not affect an already
1562 * existing copy of the file. For instance, for local files it
1563 * may write to a temporary file and then atomically rename over
1564 * the destination when the stream is closed.
1566 * By default files created are generally readable by everyone,
1567 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1568 * will be made readable only to the current user, to the level that
1569 * is supported on the target filesystem.
1571 * If @cancellable is not %NULL, then the operation can be cancelled by
1572 * triggering the cancellable object from another thread. If the operation
1573 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1575 * If you pass in a non-#NULL @etag value, then this value is
1576 * compared to the current entity tag of the file, and if they differ
1577 * an G_IO_ERROR_WRONG_ETAG error is returned. This generally means
1578 * that the file has been changed since you last read it. You can get
1579 * the new etag from g_file_output_stream_get_etag() after you've
1580 * finished writing and closed the #GFileOutputStream. When you load
1581 * a new file you can use g_file_input_stream_query_info() to get
1582 * the etag of the file.
1584 * If @make_backup is %TRUE, this function will attempt to make a backup
1585 * of the current file before overwriting it. If this fails a G_IO_ERROR_CANT_CREATE_BACKUP
1586 * error will be returned. If you want to replace anyway, try again with
1587 * @make_backup set to %FALSE.
1589 * If the file is a directory the G_IO_ERROR_IS_DIRECTORY error will be returned,
1590 * and if the file is some other form of non-regular file then a
1591 * G_IO_ERROR_NOT_REGULAR_FILE error will be returned.
1592 * Some file systems don't allow all file names, and may
1593 * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1594 * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1595 * Other errors are possible too, and depend on what kind of
1596 * filesystem the file is on.
1598 * Returns: a #GFileOutputStream or %NULL on error.
1599 * Free the returned object with g_object_unref().
1602 g_file_replace (GFile *file,
1604 gboolean make_backup,
1605 GFileCreateFlags flags,
1606 GCancellable *cancellable,
1611 g_return_val_if_fail (G_IS_FILE (file), NULL);
1613 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1616 iface = G_FILE_GET_IFACE (file);
1618 if (iface->replace == NULL)
1620 g_set_error_literal (error, G_IO_ERROR,
1621 G_IO_ERROR_NOT_SUPPORTED,
1622 _("Operation not supported"));
1627 /* Handle empty tag string as NULL in consistent way. */
1628 if (etag && *etag == 0)
1631 return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1635 * g_file_read_async:
1636 * @file: input #GFile.
1637 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1639 * @cancellable: optional #GCancellable object, %NULL to ignore.
1640 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1641 * @user_data: the data to pass to callback function
1643 * Asynchronously opens @file for reading.
1645 * For more details, see g_file_read() which is
1646 * the synchronous version of this call.
1648 * When the operation is finished, @callback will be called. You can then call
1649 * g_file_read_finish() to get the result of the operation.
1652 g_file_read_async (GFile *file,
1654 GCancellable *cancellable,
1655 GAsyncReadyCallback callback,
1660 g_return_if_fail (G_IS_FILE (file));
1662 iface = G_FILE_GET_IFACE (file);
1663 (* iface->read_async) (file,
1671 * g_file_read_finish:
1672 * @file: input #GFile.
1673 * @res: a #GAsyncResult.
1674 * @error: a #GError, or %NULL
1676 * Finishes an asynchronous file read operation started with
1677 * g_file_read_async().
1679 * Returns: a #GFileInputStream or %NULL on error.
1680 * Free the returned object with g_object_unref().
1683 g_file_read_finish (GFile *file,
1689 g_return_val_if_fail (G_IS_FILE (file), NULL);
1690 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1692 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1694 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1695 if (g_simple_async_result_propagate_error (simple, error))
1699 iface = G_FILE_GET_IFACE (file);
1700 return (* iface->read_finish) (file, res, error);
1704 * g_file_append_to_async:
1705 * @file: input #GFile.
1706 * @flags: a set of #GFileCreateFlags.
1707 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1709 * @cancellable: optional #GCancellable object, %NULL to ignore.
1710 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1711 * @user_data: the data to pass to callback function
1713 * Asynchronously opens @file for appending.
1715 * For more details, see g_file_append_to() which is
1716 * the synchronous version of this call.
1718 * When the operation is finished, @callback will be called. You can then call
1719 * g_file_append_to_finish() to get the result of the operation.
1722 g_file_append_to_async (GFile *file,
1723 GFileCreateFlags flags,
1725 GCancellable *cancellable,
1726 GAsyncReadyCallback callback,
1731 g_return_if_fail (G_IS_FILE (file));
1733 iface = G_FILE_GET_IFACE (file);
1734 (* iface->append_to_async) (file,
1743 * g_file_append_to_finish:
1744 * @file: input #GFile.
1745 * @res: #GAsyncResult
1746 * @error: a #GError, or %NULL
1748 * Finishes an asynchronous file append operation started with
1749 * g_file_append_to_async().
1751 * Returns: a valid #GFileOutputStream or %NULL on error.
1752 * Free the returned object with g_object_unref().
1755 g_file_append_to_finish (GFile *file,
1761 g_return_val_if_fail (G_IS_FILE (file), NULL);
1762 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1764 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1766 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1767 if (g_simple_async_result_propagate_error (simple, error))
1771 iface = G_FILE_GET_IFACE (file);
1772 return (* iface->append_to_finish) (file, res, error);
1776 * g_file_create_async:
1777 * @file: input #GFile.
1778 * @flags: a set of #GFileCreateFlags.
1779 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1781 * @cancellable: optional #GCancellable object, %NULL to ignore.
1782 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1783 * @user_data: the data to pass to callback function
1785 * Asynchronously creates a new file and returns an output stream for writing to it.
1786 * The file must not already exist.
1788 * For more details, see g_file_create() which is
1789 * the synchronous version of this call.
1791 * When the operation is finished, @callback will be called. You can then call
1792 * g_file_create_finish() to get the result of the operation.
1795 g_file_create_async (GFile *file,
1796 GFileCreateFlags flags,
1798 GCancellable *cancellable,
1799 GAsyncReadyCallback callback,
1804 g_return_if_fail (G_IS_FILE (file));
1806 iface = G_FILE_GET_IFACE (file);
1807 (* iface->create_async) (file,
1816 * g_file_create_finish:
1817 * @file: input #GFile.
1818 * @res: a #GAsyncResult.
1819 * @error: a #GError, or %NULL
1821 * Finishes an asynchronous file create operation started with
1822 * g_file_create_async().
1824 * Returns: a #GFileOutputStream or %NULL on error.
1825 * Free the returned object with g_object_unref().
1828 g_file_create_finish (GFile *file,
1834 g_return_val_if_fail (G_IS_FILE (file), NULL);
1835 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1837 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1839 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1840 if (g_simple_async_result_propagate_error (simple, error))
1844 iface = G_FILE_GET_IFACE (file);
1845 return (* iface->create_finish) (file, res, error);
1849 * g_file_replace_async:
1850 * @file: input #GFile.
1851 * @etag: an <link linkend="gfile-etag">entity tag</link> for the
1852 * current #GFile, or NULL to ignore.
1853 * @make_backup: %TRUE if a backup should be created.
1854 * @flags: a set of #GFileCreateFlags.
1855 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1857 * @cancellable: optional #GCancellable object, %NULL to ignore.
1858 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1859 * @user_data: the data to pass to callback function
1861 * Asynchronously overwrites the file, replacing the contents, possibly
1862 * creating a backup copy of the file first.
1864 * For more details, see g_file_replace() which is
1865 * the synchronous version of this call.
1867 * When the operation is finished, @callback will be called. You can then call
1868 * g_file_replace_finish() to get the result of the operation.
1871 g_file_replace_async (GFile *file,
1873 gboolean make_backup,
1874 GFileCreateFlags flags,
1876 GCancellable *cancellable,
1877 GAsyncReadyCallback callback,
1882 g_return_if_fail (G_IS_FILE (file));
1884 iface = G_FILE_GET_IFACE (file);
1885 (* iface->replace_async) (file,
1896 * g_file_replace_finish:
1897 * @file: input #GFile.
1898 * @res: a #GAsyncResult.
1899 * @error: a #GError, or %NULL
1901 * Finishes an asynchronous file replace operation started with
1902 * g_file_replace_async().
1904 * Returns: a #GFileOutputStream, or %NULL on error.
1905 * Free the returned object with g_object_unref().
1908 g_file_replace_finish (GFile *file,
1914 g_return_val_if_fail (G_IS_FILE (file), NULL);
1915 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1917 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1919 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1920 if (g_simple_async_result_propagate_error (simple, error))
1924 iface = G_FILE_GET_IFACE (file);
1925 return (* iface->replace_finish) (file, res, error);
1929 copy_symlink (GFile *destination,
1930 GFileCopyFlags flags,
1931 GCancellable *cancellable,
1936 gboolean tried_delete;
1938 GFileType file_type;
1940 tried_delete = FALSE;
1944 if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
1946 /* Maybe it already existed, and we want to overwrite? */
1947 if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) &&
1948 my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
1950 g_error_free (my_error);
1953 /* Don't overwrite if the destination is a directory */
1954 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1955 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1956 cancellable, &my_error);
1959 file_type = g_file_info_get_file_type (info);
1960 g_object_unref (info);
1962 if (file_type == G_FILE_TYPE_DIRECTORY)
1964 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
1965 _("Can't copy over directory"));
1970 if (!g_file_delete (destination, cancellable, error))
1973 tried_delete = TRUE;
1977 g_propagate_error (error, my_error);
1984 static GInputStream *
1985 open_source_for_copy (GFile *source,
1987 GFileCopyFlags flags,
1988 GCancellable *cancellable,
1994 GFileType file_type;
1997 in = (GInputStream *)g_file_read (source, cancellable, &my_error);
2001 /* There was an error opening the source, try to set a good error for it: */
2003 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
2005 /* The source is a directory, don't fail with WOULD_RECURSE immediately,
2006 * as that is less useful to the app. Better check for errors on the
2009 g_error_free (my_error);
2012 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2013 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2014 cancellable, &my_error);
2017 file_type = g_file_info_get_file_type (info);
2018 g_object_unref (info);
2020 if (flags & G_FILE_COPY_OVERWRITE)
2022 if (file_type == G_FILE_TYPE_DIRECTORY)
2024 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
2025 _("Can't copy directory over directory"));
2028 /* continue to would_recurse error */
2032 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
2033 _("Target file exists"));
2039 /* Error getting info from target, return that error
2040 * (except for NOT_FOUND, which is no error here)
2042 if (my_error->domain != G_IO_ERROR && my_error->code != G_IO_ERROR_NOT_FOUND)
2044 g_propagate_error (error, my_error);
2047 g_error_free (my_error);
2050 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
2051 _("Can't recursively copy directory"));
2055 g_propagate_error (error, my_error);
2060 should_copy (GFileAttributeInfo *info,
2062 gboolean skip_perms)
2064 if (skip_perms && strcmp(info->name, "unix::mode") == 0)
2068 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
2069 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
2073 build_attribute_list_for_copy (GFileAttributeInfoList *attributes,
2074 GFileAttributeInfoList *namespaces,
2076 gboolean skip_perms)
2083 s = g_string_new ("");
2087 for (i = 0; i < attributes->n_infos; i++)
2089 if (should_copy (&attributes->infos[i], as_move, skip_perms))
2094 g_string_append_c (s, ',');
2096 g_string_append (s, attributes->infos[i].name);
2103 for (i = 0; i < namespaces->n_infos; i++)
2105 if (should_copy (&namespaces->infos[i], as_move, FALSE))
2110 g_string_append_c (s, ',');
2112 g_string_append (s, namespaces->infos[i].name);
2113 g_string_append (s, ":*");
2118 return g_string_free (s, FALSE);
2122 * g_file_copy_attributes:
2123 * @source: a #GFile with attributes.
2124 * @destination: a #GFile to copy attributes to.
2125 * @flags: a set of #GFileCopyFlags.
2126 * @cancellable: optional #GCancellable object, %NULL to ignore.
2127 * @error: a #GError, %NULL to ignore.
2129 * Copies the file attributes from @source to @destination.
2131 * Normally only a subset of the file attributes are copied,
2132 * those that are copies in a normal file copy operation
2133 * (which for instance does not include e.g. owner). However
2134 * if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
2135 * all the metadata that is possible to copy is copied. This
2136 * is useful when implementing move by copy + delete source.
2138 * Returns: %TRUE if the attributes were copied successfully, %FALSE otherwise.
2141 g_file_copy_attributes (GFile *source,
2143 GFileCopyFlags flags,
2144 GCancellable *cancellable,
2147 GFileAttributeInfoList *attributes, *namespaces;
2148 char *attrs_to_read;
2152 gboolean source_nofollow_symlinks;
2153 gboolean skip_perms;
2155 as_move = flags & G_FILE_COPY_ALL_METADATA;
2156 source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
2157 skip_perms = (flags & G_FILE_COPY_TARGET_DEFAULT_PERMS) != 0;
2159 /* Ignore errors here, if the target supports no attributes there is nothing to copy */
2160 attributes = g_file_query_settable_attributes (destination, cancellable, NULL);
2161 namespaces = g_file_query_writable_namespaces (destination, cancellable, NULL);
2163 if (attributes == NULL && namespaces == NULL)
2166 attrs_to_read = build_attribute_list_for_copy (attributes, namespaces, as_move, skip_perms);
2168 /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
2169 * we just don't copy it.
2171 info = g_file_query_info (source, attrs_to_read,
2172 source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
2176 g_free (attrs_to_read);
2181 res = g_file_set_attributes_from_info (destination,
2183 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2186 g_object_unref (info);
2189 g_file_attribute_info_list_unref (attributes);
2190 g_file_attribute_info_list_unref (namespaces);
2195 /* Closes the streams */
2197 copy_stream_with_progress (GInputStream *in,
2200 GCancellable *cancellable,
2201 GFileProgressCallback progress_callback,
2202 gpointer progress_callback_data,
2205 gssize n_read, n_written;
2206 goffset current_size;
2207 char buffer[1024*64], *p;
2213 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
2214 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2218 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2219 total_size = g_file_info_get_size (info);
2220 g_object_unref (info);
2223 if (total_size == -1)
2225 info = g_file_query_info (source,
2226 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2227 G_FILE_QUERY_INFO_NONE,
2231 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2232 total_size = g_file_info_get_size (info);
2233 g_object_unref (info);
2237 if (total_size == -1)
2244 n_read = g_input_stream_read (in, buffer, sizeof (buffer), cancellable, error);
2254 current_size += n_read;
2259 n_written = g_output_stream_write (out, p, n_read, cancellable, error);
2260 if (n_written == -1)
2267 n_read -= n_written;
2273 if (progress_callback)
2274 progress_callback (current_size, total_size, progress_callback_data);
2278 error = NULL; /* Ignore further errors */
2280 /* Make sure we send full copied size */
2281 if (progress_callback)
2282 progress_callback (current_size, total_size, progress_callback_data);
2284 /* Don't care about errors in source here */
2285 g_input_stream_close (in, cancellable, NULL);
2287 /* But write errors on close are bad! */
2288 if (!g_output_stream_close (out, cancellable, error))
2291 g_object_unref (in);
2292 g_object_unref (out);
2298 file_copy_fallback (GFile *source,
2300 GFileCopyFlags flags,
2301 GCancellable *cancellable,
2302 GFileProgressCallback progress_callback,
2303 gpointer progress_callback_data,
2311 /* need to know the file type */
2312 info = g_file_query_info (source,
2313 G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
2314 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2321 /* Maybe copy the symlink? */
2322 if ((flags & G_FILE_COPY_NOFOLLOW_SYMLINKS) &&
2323 g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK)
2325 target = g_file_info_get_symlink_target (info);
2328 if (!copy_symlink (destination, flags, cancellable, target, error))
2330 g_object_unref (info);
2334 g_object_unref (info);
2337 /* ... else fall back on a regular file copy */
2338 g_object_unref (info);
2340 /* Handle "special" files (pipes, device nodes, ...)? */
2341 else if (g_file_info_get_file_type (info) == G_FILE_TYPE_SPECIAL)
2343 /* FIXME: could try to recreate device nodes and others? */
2345 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2346 _("Can't copy special file"));
2347 g_object_unref (info);
2350 /* Everything else should just fall back on a regular copy. */
2352 g_object_unref (info);
2354 in = open_source_for_copy (source, destination, flags, cancellable, error);
2358 if (flags & G_FILE_COPY_OVERWRITE)
2360 out = (GOutputStream *)g_file_replace (destination,
2362 flags & G_FILE_COPY_BACKUP,
2363 G_FILE_CREATE_REPLACE_DESTINATION,
2364 cancellable, error);
2368 out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
2373 g_object_unref (in);
2377 if (!copy_stream_with_progress (in, out, source, cancellable,
2378 progress_callback, progress_callback_data,
2384 /* Ignore errors here. Failure to copy metadata is not a hard error */
2385 g_file_copy_attributes (source, destination,
2386 flags, cancellable, NULL);
2393 * @source: input #GFile.
2394 * @destination: destination #GFile
2395 * @flags: set of #GFileCopyFlags
2396 * @cancellable: optional #GCancellable object, %NULL to ignore.
2397 * @progress_callback: function to callback with progress information
2398 * @progress_callback_data: user data to pass to @progress_callback
2399 * @error: #GError to set on error, or %NULL
2401 * Copies the file @source to the location specified by @destination.
2402 * Can not handle recursive copies of directories.
2404 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2405 * existing @destination file is overwritten.
2407 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2408 * will be copied as symlinks, otherwise the target of the
2409 * @source symlink will be copied.
2411 * If @cancellable is not %NULL, then the operation can be cancelled by
2412 * triggering the cancellable object from another thread. If the operation
2413 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2415 * If @progress_callback is not %NULL, then the operation can be monitored by
2416 * setting this to a #GFileProgressCallback function. @progress_callback_data
2417 * will be passed to this function. It is guaranteed that this callback will
2418 * be called after all data has been transferred with the total number of bytes
2419 * copied during the operation.
2421 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2422 * error is returned, independent on the status of the @destination.
2424 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2425 * error G_IO_ERROR_EXISTS is returned.
2427 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2428 * error is returned. If trying to overwrite a directory with a directory the
2429 * G_IO_ERROR_WOULD_MERGE error is returned.
2431 * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2432 * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2435 * If you are interested in copying the #GFile object itself (not the on-disk
2436 * file), see g_file_dup().
2438 * Returns: %TRUE on success, %FALSE otherwise.
2441 g_file_copy (GFile *source,
2443 GFileCopyFlags flags,
2444 GCancellable *cancellable,
2445 GFileProgressCallback progress_callback,
2446 gpointer progress_callback_data,
2453 g_return_val_if_fail (G_IS_FILE (source), FALSE);
2454 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2456 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2459 iface = G_FILE_GET_IFACE (destination);
2463 res = (* iface->copy) (source, destination,
2465 progress_callback, progress_callback_data,
2471 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2473 g_propagate_error (error, my_error);
2477 g_clear_error (&my_error);
2480 /* If the types are different, and the destination method failed
2481 also try the source method */
2482 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
2484 iface = G_FILE_GET_IFACE (source);
2489 res = (* iface->copy) (source, destination,
2491 progress_callback, progress_callback_data,
2497 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2499 g_propagate_error (error, my_error);
2503 g_clear_error (&my_error);
2507 return file_copy_fallback (source, destination, flags, cancellable,
2508 progress_callback, progress_callback_data,
2513 * g_file_copy_async:
2514 * @source: input #GFile.
2515 * @destination: destination #GFile
2516 * @flags: set of #GFileCopyFlags
2517 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2519 * @cancellable: optional #GCancellable object, %NULL to ignore.
2520 * @progress_callback: function to callback with progress information
2521 * @progress_callback_data: user data to pass to @progress_callback
2522 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2523 * @user_data: the data to pass to callback function
2525 * Copies the file @source to the location specified by @destination
2526 * asynchronously. For details of the behaviour, see g_file_copy().
2528 * If @progress_callback is not %NULL, then that function that will be called
2529 * just like in g_file_copy(), however the callback will run in the main loop,
2530 * not in the thread that is doing the I/O operation.
2532 * When the operation is finished, @callback will be called. You can then call
2533 * g_file_copy_finish() to get the result of the operation.
2536 g_file_copy_async (GFile *source,
2538 GFileCopyFlags flags,
2540 GCancellable *cancellable,
2541 GFileProgressCallback progress_callback,
2542 gpointer progress_callback_data,
2543 GAsyncReadyCallback callback,
2548 g_return_if_fail (G_IS_FILE (source));
2549 g_return_if_fail (G_IS_FILE (destination));
2551 iface = G_FILE_GET_IFACE (source);
2552 (* iface->copy_async) (source,
2558 progress_callback_data,
2564 * g_file_copy_finish:
2565 * @file: input #GFile.
2566 * @res: a #GAsyncResult.
2567 * @error: a #GError, or %NULL
2569 * Finishes copying the file started with
2570 * g_file_copy_async().
2572 * Returns: a %TRUE on success, %FALSE on error.
2575 g_file_copy_finish (GFile *file,
2581 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2582 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
2584 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2586 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2588 if (g_simple_async_result_propagate_error (simple, error))
2592 iface = G_FILE_GET_IFACE (file);
2593 return (* iface->copy_finish) (file, res, error);
2598 * @source: #GFile pointing to the source location.
2599 * @destination: #GFile pointing to the destination location.
2600 * @flags: set of #GFileCopyFlags.
2601 * @cancellable: optional #GCancellable object, %NULL to ignore.
2602 * @progress_callback: #GFileProgressCallback function for updates.
2603 * @progress_callback_data: gpointer to user data for the callback function.
2604 * @error: #GError for returning error conditions, or %NULL
2607 * Tries to move the file or directory @source to the location specified by @destination.
2608 * If native move operations are supported then this is used, otherwise a copy + delete
2609 * fallback is used. The native implementation may support moving directories (for instance
2610 * on moves inside the same filesystem), but the fallback code does not.
2612 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2613 * existing @destination file is overwritten.
2615 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2616 * will be copied as symlinks, otherwise the target of the
2617 * @source symlink will be copied.
2619 * If @cancellable is not %NULL, then the operation can be cancelled by
2620 * triggering the cancellable object from another thread. If the operation
2621 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2623 * If @progress_callback is not %NULL, then the operation can be monitored by
2624 * setting this to a #GFileProgressCallback function. @progress_callback_data
2625 * will be passed to this function. It is guaranteed that this callback will
2626 * be called after all data has been transferred with the total number of bytes
2627 * copied during the operation.
2629 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2630 * error is returned, independent on the status of the @destination.
2632 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2633 * error G_IO_ERROR_EXISTS is returned.
2635 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2636 * error is returned. If trying to overwrite a directory with a directory the
2637 * G_IO_ERROR_WOULD_MERGE error is returned.
2639 * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2640 * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2641 * may be returned (if the native move operation isn't available).
2643 * Returns: %TRUE on successful move, %FALSE otherwise.
2646 g_file_move (GFile *source,
2648 GFileCopyFlags flags,
2649 GCancellable *cancellable,
2650 GFileProgressCallback progress_callback,
2651 gpointer progress_callback_data,
2658 g_return_val_if_fail (G_IS_FILE (source), FALSE);
2659 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2661 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2664 iface = G_FILE_GET_IFACE (destination);
2668 res = (* iface->move) (source, destination,
2670 progress_callback, progress_callback_data,
2676 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2678 g_propagate_error (error, my_error);
2683 /* If the types are different, and the destination method failed
2684 also try the source method */
2685 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
2687 iface = G_FILE_GET_IFACE (source);
2692 res = (* iface->move) (source, destination,
2694 progress_callback, progress_callback_data,
2700 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2702 g_propagate_error (error, my_error);
2708 if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
2710 g_set_error_literal (error, G_IO_ERROR,
2711 G_IO_ERROR_NOT_SUPPORTED,
2712 _("Operation not supported"));
2716 flags |= G_FILE_COPY_ALL_METADATA;
2717 if (!g_file_copy (source, destination, flags, cancellable,
2718 progress_callback, progress_callback_data,
2722 return g_file_delete (source, cancellable, error);
2726 * g_file_make_directory
2727 * @file: input #GFile.
2728 * @cancellable: optional #GCancellable object, %NULL to ignore.
2729 * @error: a #GError, or %NULL
2731 * Creates a directory. Note that this will only create a child directory of
2732 * the immediate parent directory of the path or URI given by the #GFile. To
2733 * recursively create directories, see g_file_make_directory_with_parents().
2734 * This function will fail if the parent directory does not exist, setting
2735 * @error to %G_IO_ERROR_NOT_FOUND. If the file system doesn't support creating
2736 * directories, this function will fail, setting @error to
2737 * %G_IO_ERROR_NOT_SUPPORTED.
2739 * For a local #GFile the newly created directory will have the default
2740 * (current) ownership and permissions of the current process.
2742 * If @cancellable is not %NULL, then the operation can be cancelled by
2743 * triggering the cancellable object from another thread. If the operation
2744 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2746 * Returns: %TRUE on successful creation, %FALSE otherwise.
2749 g_file_make_directory (GFile *file,
2750 GCancellable *cancellable,
2755 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2757 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2760 iface = G_FILE_GET_IFACE (file);
2762 if (iface->make_directory == NULL)
2764 g_set_error_literal (error, G_IO_ERROR,
2765 G_IO_ERROR_NOT_SUPPORTED,
2766 _("Operation not supported"));
2770 return (* iface->make_directory) (file, cancellable, error);
2774 * g_file_make_directory_with_parents:
2775 * @file: input #GFile.
2776 * @cancellable: optional #GCancellable object, %NULL to ignore.
2777 * @error: a #GError, or %NULL
2779 * Creates a directory and any parent directories that may not exist similar to
2780 * 'mkdir -p'. If the file system does not support creating directories, this
2781 * function will fail, setting @error to %G_IO_ERROR_NOT_SUPPORTED.
2783 * For a local #GFile the newly created directories will have the default
2784 * (current) ownership and permissions of the current process.
2786 * If @cancellable is not %NULL, then the operation can be cancelled by
2787 * triggering the cancellable object from another thread. If the operation
2788 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2790 * Returns: %TRUE if all directories have been successfully created, %FALSE
2796 g_file_make_directory_with_parents (GFile *file,
2797 GCancellable *cancellable,
2801 GFile *parent_file, *work_file;
2802 GList *list = NULL, *l;
2803 GError *my_error = NULL;
2805 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2808 result = g_file_make_directory (file, cancellable, &my_error);
2809 if (result || my_error->code != G_IO_ERROR_NOT_FOUND)
2812 g_propagate_error (error, my_error);
2818 while (!result && my_error->code == G_IO_ERROR_NOT_FOUND)
2820 g_clear_error (&my_error);
2822 parent_file = g_file_get_parent (work_file);
2823 if (parent_file == NULL)
2825 result = g_file_make_directory (parent_file, cancellable, &my_error);
2827 if (!result && my_error->code == G_IO_ERROR_NOT_FOUND)
2828 list = g_list_prepend (list, parent_file);
2830 work_file = parent_file;
2833 for (l = list; result && l; l = l->next)
2835 result = g_file_make_directory ((GFile *) l->data, cancellable, &my_error);
2839 while (list != NULL)
2841 g_object_unref ((GFile *) list->data);
2842 list = g_list_remove (list, list->data);
2847 g_propagate_error (error, my_error);
2851 return g_file_make_directory (file, cancellable, error);
2855 * g_file_make_symbolic_link:
2856 * @file: input #GFile.
2857 * @symlink_value: a string with the value of the new symlink.
2858 * @cancellable: optional #GCancellable object, %NULL to ignore.
2859 * @error: a #GError.
2861 * Creates a symbolic link.
2863 * If @cancellable is not %NULL, then the operation can be cancelled by
2864 * triggering the cancellable object from another thread. If the operation
2865 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2867 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
2870 g_file_make_symbolic_link (GFile *file,
2871 const char *symlink_value,
2872 GCancellable *cancellable,
2877 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2878 g_return_val_if_fail (symlink_value != NULL, FALSE);
2880 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2883 if (*symlink_value == '\0')
2885 g_set_error_literal (error, G_IO_ERROR,
2886 G_IO_ERROR_INVALID_ARGUMENT,
2887 _("Invalid symlink value given"));
2891 iface = G_FILE_GET_IFACE (file);
2893 if (iface->make_symbolic_link == NULL)
2895 g_set_error_literal (error, G_IO_ERROR,
2896 G_IO_ERROR_NOT_SUPPORTED,
2897 _("Operation not supported"));
2901 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
2906 * @file: input #GFile.
2907 * @cancellable: optional #GCancellable object, %NULL to ignore.
2908 * @error: a #GError, or %NULL
2910 * Deletes a file. If the @file is a directory, it will only be deleted if it
2913 * If @cancellable is not %NULL, then the operation can be cancelled by
2914 * triggering the cancellable object from another thread. If the operation
2915 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2917 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
2920 g_file_delete (GFile *file,
2921 GCancellable *cancellable,
2926 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2928 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2931 iface = G_FILE_GET_IFACE (file);
2933 if (iface->delete_file == NULL)
2935 g_set_error_literal (error, G_IO_ERROR,
2936 G_IO_ERROR_NOT_SUPPORTED,
2937 _("Operation not supported"));
2941 return (* iface->delete_file) (file, cancellable, error);
2946 * @file: #GFile to send to trash.
2947 * @cancellable: optional #GCancellable object, %NULL to ignore.
2948 * @error: a #GError, or %NULL
2950 * Sends @file to the "Trashcan", if possible. This is similar to
2951 * deleting it, but the user can recover it before emptying the trashcan.
2952 * Not all file systems support trashing, so this call can return the
2953 * %G_IO_ERROR_NOT_SUPPORTED error.
2956 * If @cancellable is not %NULL, then the operation can be cancelled by
2957 * triggering the cancellable object from another thread. If the operation
2958 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2960 * Returns: %TRUE on successful trash, %FALSE otherwise.
2963 g_file_trash (GFile *file,
2964 GCancellable *cancellable,
2969 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2971 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2974 iface = G_FILE_GET_IFACE (file);
2976 if (iface->trash == NULL)
2978 g_set_error_literal (error,
2979 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2980 _("Trash not supported"));
2984 return (* iface->trash) (file, cancellable, error);
2988 * g_file_set_display_name:
2989 * @file: input #GFile.
2990 * @display_name: a string.
2991 * @cancellable: optional #GCancellable object, %NULL to ignore.
2992 * @error: a #GError, or %NULL
2994 * Renames @file to the specified display name.
2996 * The display name is converted from UTF8 to the correct encoding for the target
2997 * filesystem if possible and the @file is renamed to this.
2999 * If you want to implement a rename operation in the user interface the edit name
3000 * (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the initial value in the rename
3001 * widget, and then the result after editing should be passed to g_file_set_display_name().
3003 * On success the resulting converted filename is returned.
3005 * If @cancellable is not %NULL, then the operation can be cancelled by
3006 * triggering the cancellable object from another thread. If the operation
3007 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3009 * Returns: a #GFile specifying what @file was renamed to, or %NULL
3010 * if there was an error.
3011 * Free the returned object with g_object_unref().
3014 g_file_set_display_name (GFile *file,
3015 const char *display_name,
3016 GCancellable *cancellable,
3021 g_return_val_if_fail (G_IS_FILE (file), NULL);
3022 g_return_val_if_fail (display_name != NULL, NULL);
3024 if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
3028 G_IO_ERROR_INVALID_ARGUMENT,
3029 _("File names cannot contain '%c'"), G_DIR_SEPARATOR);
3033 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3036 iface = G_FILE_GET_IFACE (file);
3038 return (* iface->set_display_name) (file, display_name, cancellable, error);
3042 * g_file_set_display_name_async:
3043 * @file: input #GFile.
3044 * @display_name: a string.
3045 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3047 * @cancellable: optional #GCancellable object, %NULL to ignore.
3048 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
3049 * @user_data: the data to pass to callback function
3051 * Asynchronously sets the display name for a given #GFile.
3053 * For more details, see g_file_set_display_name() which is
3054 * the synchronous version of this call.
3056 * When the operation is finished, @callback will be called. You can then call
3057 * g_file_set_display_name_finish() to get the result of the operation.
3060 g_file_set_display_name_async (GFile *file,
3061 const char *display_name,
3063 GCancellable *cancellable,
3064 GAsyncReadyCallback callback,
3069 g_return_if_fail (G_IS_FILE (file));
3070 g_return_if_fail (display_name != NULL);
3072 iface = G_FILE_GET_IFACE (file);
3073 (* iface->set_display_name_async) (file,
3082 * g_file_set_display_name_finish:
3083 * @file: input #GFile.
3084 * @res: a #GAsyncResult.
3085 * @error: a #GError, or %NULL
3087 * Finishes setting a display name started with
3088 * g_file_set_display_name_async().
3090 * Returns: a #GFile or %NULL on error.
3091 * Free the returned object with g_object_unref().
3094 g_file_set_display_name_finish (GFile *file,
3100 g_return_val_if_fail (G_IS_FILE (file), NULL);
3101 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
3103 if (G_IS_SIMPLE_ASYNC_RESULT (res))
3105 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3106 if (g_simple_async_result_propagate_error (simple, error))
3110 iface = G_FILE_GET_IFACE (file);
3111 return (* iface->set_display_name_finish) (file, res, error);
3115 * g_file_query_settable_attributes:
3116 * @file: input #GFile.
3117 * @cancellable: optional #GCancellable object, %NULL to ignore.
3118 * @error: a #GError, or %NULL
3120 * Obtain the list of settable attributes for the file.
3122 * Returns the type and full attribute name of all the attributes
3123 * that can be set on this file. This doesn't mean setting it will always
3124 * succeed though, you might get an access failure, or some specific
3125 * file may not support a specific attribute.
3127 * If @cancellable is not %NULL, then the operation can be cancelled by
3128 * triggering the cancellable object from another thread. If the operation
3129 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3131 * Returns: a #GFileAttributeInfoList describing the settable attributes.
3132 * When you are done with it, release it with g_file_attribute_info_list_unref()
3134 GFileAttributeInfoList *
3135 g_file_query_settable_attributes (GFile *file,
3136 GCancellable *cancellable,
3141 GFileAttributeInfoList *list;
3143 g_return_val_if_fail (G_IS_FILE (file), NULL);
3145 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3148 iface = G_FILE_GET_IFACE (file);
3150 if (iface->query_settable_attributes == NULL)
3151 return g_file_attribute_info_list_new ();
3154 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
3158 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
3160 list = g_file_attribute_info_list_new ();
3161 g_error_free (my_error);
3164 g_propagate_error (error, my_error);
3171 * g_file_query_writable_namespaces:
3172 * @file: input #GFile.
3173 * @cancellable: optional #GCancellable object, %NULL to ignore.
3174 * @error: a #GError, or %NULL
3176 * Obtain the list of attribute namespaces where new attributes
3177 * can be created by a user. An example of this is extended
3178 * attributes (in the "xattr" namespace).
3180 * If @cancellable is not %NULL, then the operation can be cancelled by
3181 * triggering the cancellable object from another thread. If the operation
3182 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3184 * Returns: a #GFileAttributeInfoList describing the writable namespaces.
3185 * When you are done with it, release it with g_file_attribute_info_list_unref()
3187 GFileAttributeInfoList *
3188 g_file_query_writable_namespaces (GFile *file,
3189 GCancellable *cancellable,
3194 GFileAttributeInfoList *list;
3196 g_return_val_if_fail (G_IS_FILE (file), NULL);
3198 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3201 iface = G_FILE_GET_IFACE (file);
3203 if (iface->query_writable_namespaces == NULL)
3204 return g_file_attribute_info_list_new ();
3207 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
3211 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
3213 list = g_file_attribute_info_list_new ();
3214 g_error_free (my_error);
3217 g_propagate_error (error, my_error);
3224 * g_file_set_attribute:
3225 * @file: input #GFile.
3226 * @attribute: a string containing the attribute's name.
3227 * @type: The type of the attribute
3228 * @value_p: a pointer to the value (or the pointer itself if the type is a pointer type)
3229 * @flags: a set of #GFileQueryInfoFlags.
3230 * @cancellable: optional #GCancellable object, %NULL to ignore.
3231 * @error: a #GError, or %NULL
3233 * Sets an attribute in the file with attribute name @attribute to @value.
3235 * If @cancellable is not %NULL, then the operation can be cancelled by
3236 * triggering the cancellable object from another thread. If the operation
3237 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3239 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
3242 g_file_set_attribute (GFile *file,
3243 const char *attribute,
3244 GFileAttributeType type,
3246 GFileQueryInfoFlags flags,
3247 GCancellable *cancellable,
3252 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3253 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
3255 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3258 iface = G_FILE_GET_IFACE (file);
3260 if (iface->set_attribute == NULL)
3262 g_set_error_literal (error, G_IO_ERROR,
3263 G_IO_ERROR_NOT_SUPPORTED,
3264 _("Operation not supported"));
3268 return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
3272 * g_file_set_attributes_from_info:
3273 * @file: input #GFile.
3274 * @info: a #GFileInfo.
3275 * @flags: #GFileQueryInfoFlags
3276 * @cancellable: optional #GCancellable object, %NULL to ignore.
3277 * @error: a #GError, or %NULL
3279 * Tries to set all attributes in the #GFileInfo on the target values,
3280 * not stopping on the first error.
3282 * If there is any error during this operation then @error will be set to
3283 * the first error. Error on particular fields are flagged by setting
3284 * the "status" field in the attribute value to
3285 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can also detect
3288 * If @cancellable is not %NULL, then the operation can be cancelled by
3289 * triggering the cancellable object from another thread. If the operation
3290 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3292 * Returns: %TRUE if there was any error, %FALSE otherwise.
3295 g_file_set_attributes_from_info (GFile *file,
3297 GFileQueryInfoFlags flags,
3298 GCancellable *cancellable,
3303 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3304 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
3306 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3309 g_file_info_clear_status (info);
3311 iface = G_FILE_GET_IFACE (file);
3313 return (* iface->set_attributes_from_info) (file,
3322 g_file_real_set_attributes_from_info (GFile *file,
3324 GFileQueryInfoFlags flags,
3325 GCancellable *cancellable,
3331 GFileAttributeValue *value;
3335 attributes = g_file_info_list_attributes (info, NULL);
3337 for (i = 0; attributes[i] != NULL; i++)
3339 value = _g_file_info_get_attribute_value (info, attributes[i]);
3341 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
3344 if (!g_file_set_attribute (file, attributes[i],
3345 value->type, _g_file_attribute_value_peek_as_pointer (value),
3346 flags, cancellable, error))
3348 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
3350 /* Don't set error multiple times */
3354 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
3357 g_strfreev (attributes);
3363 * g_file_set_attributes_async:
3364 * @file: input #GFile.
3365 * @info: a #GFileInfo.
3366 * @flags: a #GFileQueryInfoFlags.
3367 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3369 * @cancellable: optional #GCancellable object, %NULL to ignore.
3370 * @callback: a #GAsyncReadyCallback.
3371 * @user_data: a #gpointer.
3373 * Asynchronously sets the attributes of @file with @info.
3375 * For more details, see g_file_set_attributes_from_info() which is
3376 * the synchronous version of this call.
3378 * When the operation is finished, @callback will be called. You can then call
3379 * g_file_set_attributes_finish() to get the result of the operation.
3382 g_file_set_attributes_async (GFile *file,
3384 GFileQueryInfoFlags flags,
3386 GCancellable *cancellable,
3387 GAsyncReadyCallback callback,
3392 g_return_if_fail (G_IS_FILE (file));
3393 g_return_if_fail (G_IS_FILE_INFO (info));
3395 iface = G_FILE_GET_IFACE (file);
3396 (* iface->set_attributes_async) (file,
3406 * g_file_set_attributes_finish:
3407 * @file: input #GFile.
3408 * @result: a #GAsyncResult.
3409 * @info: a #GFileInfo.
3410 * @error: a #GError, or %NULL
3412 * Finishes setting an attribute started in g_file_set_attributes_async().
3414 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
3417 g_file_set_attributes_finish (GFile *file,
3418 GAsyncResult *result,
3424 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3425 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3427 /* No standard handling of errors here, as we must set info even
3430 iface = G_FILE_GET_IFACE (file);
3431 return (* iface->set_attributes_finish) (file, result, info, error);
3435 * g_file_set_attribute_string:
3436 * @file: input #GFile.
3437 * @attribute: a string containing the attribute's name.
3438 * @value: a string containing the attribute's value.
3439 * @flags: #GFileQueryInfoFlags.
3440 * @cancellable: optional #GCancellable object, %NULL to ignore.
3441 * @error: a #GError, or %NULL
3443 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
3444 * If @attribute is of a different type, this operation will fail.
3446 * If @cancellable is not %NULL, then the operation can be cancelled by
3447 * triggering the cancellable object from another thread. If the operation
3448 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3450 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
3453 g_file_set_attribute_string (GFile *file,
3454 const char *attribute,
3456 GFileQueryInfoFlags flags,
3457 GCancellable *cancellable,
3460 return g_file_set_attribute (file, attribute,
3461 G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
3462 flags, cancellable, error);
3466 * g_file_set_attribute_byte_string:
3467 * @file: input #GFile.
3468 * @attribute: a string containing the attribute's name.
3469 * @value: a string containing the attribute's new value.
3470 * @flags: a #GFileQueryInfoFlags.
3471 * @cancellable: optional #GCancellable object, %NULL to ignore.
3472 * @error: a #GError, or %NULL
3474 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
3475 * If @attribute is of a different type, this operation will fail,
3478 * If @cancellable is not %NULL, then the operation can be cancelled by
3479 * triggering the cancellable object from another thread. If the operation
3480 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3482 * Returns: %TRUE if the @attribute was successfully set to @value
3483 * in the @file, %FALSE otherwise.
3486 g_file_set_attribute_byte_string (GFile *file,
3487 const char *attribute,
3489 GFileQueryInfoFlags flags,
3490 GCancellable *cancellable,
3493 return g_file_set_attribute (file, attribute,
3494 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
3495 flags, cancellable, error);
3499 * g_file_set_attribute_uint32:
3500 * @file: input #GFile.
3501 * @attribute: a string containing the attribute's name.
3502 * @value: a #guint32 containing the attribute's new value.
3503 * @flags: a #GFileQueryInfoFlags.
3504 * @cancellable: optional #GCancellable object, %NULL to ignore.
3505 * @error: a #GError, or %NULL
3507 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
3508 * If @attribute is of a different type, this operation will fail.
3510 * If @cancellable is not %NULL, then the operation can be cancelled by
3511 * triggering the cancellable object from another thread. If the operation
3512 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3514 * Returns: %TRUE if the @attribute was successfully set to @value
3515 * in the @file, %FALSE otherwise.
3518 g_file_set_attribute_uint32 (GFile *file,
3519 const char *attribute,
3521 GFileQueryInfoFlags flags,
3522 GCancellable *cancellable,
3525 return g_file_set_attribute (file, attribute,
3526 G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
3527 flags, cancellable, error);
3531 * g_file_set_attribute_int32:
3532 * @file: input #GFile.
3533 * @attribute: a string containing the attribute's name.
3534 * @value: a #gint32 containing the attribute's new value.
3535 * @flags: a #GFileQueryInfoFlags.
3536 * @cancellable: optional #GCancellable object, %NULL to ignore.
3537 * @error: a #GError, or %NULL
3539 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
3540 * If @attribute is of a different type, this operation will fail.
3542 * If @cancellable is not %NULL, then the operation can be cancelled by
3543 * triggering the cancellable object from another thread. If the operation
3544 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3546 * Returns: %TRUE if the @attribute was successfully set to @value
3547 * in the @file, %FALSE otherwise.
3550 g_file_set_attribute_int32 (GFile *file,
3551 const char *attribute,
3553 GFileQueryInfoFlags flags,
3554 GCancellable *cancellable,
3557 return g_file_set_attribute (file, attribute,
3558 G_FILE_ATTRIBUTE_TYPE_INT32, &value,
3559 flags, cancellable, error);
3563 * g_file_set_attribute_uint64:
3564 * @file: input #GFile.
3565 * @attribute: a string containing the attribute's name.
3566 * @value: a #guint64 containing the attribute's new value.
3567 * @flags: a #GFileQueryInfoFlags.
3568 * @cancellable: optional #GCancellable object, %NULL to ignore.
3569 * @error: a #GError, or %NULL
3571 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
3572 * If @attribute is of a different type, this operation will fail.
3574 * If @cancellable is not %NULL, then the operation can be cancelled by
3575 * triggering the cancellable object from another thread. If the operation
3576 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3578 * Returns: %TRUE if the @attribute was successfully set to @value
3579 * in the @file, %FALSE otherwise.
3582 g_file_set_attribute_uint64 (GFile *file,
3583 const char *attribute,
3585 GFileQueryInfoFlags flags,
3586 GCancellable *cancellable,
3589 return g_file_set_attribute (file, attribute,
3590 G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
3591 flags, cancellable, error);
3595 * g_file_set_attribute_int64:
3596 * @file: input #GFile.
3597 * @attribute: a string containing the attribute's name.
3598 * @value: a #guint64 containing the attribute's new value.
3599 * @flags: a #GFileQueryInfoFlags.
3600 * @cancellable: optional #GCancellable object, %NULL to ignore.
3601 * @error: a #GError, or %NULL
3603 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
3604 * If @attribute is of a different type, this operation will fail.
3606 * If @cancellable is not %NULL, then the operation can be cancelled by
3607 * triggering the cancellable object from another thread. If the operation
3608 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3610 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
3613 g_file_set_attribute_int64 (GFile *file,
3614 const char *attribute,
3616 GFileQueryInfoFlags flags,
3617 GCancellable *cancellable,
3620 return g_file_set_attribute (file, attribute,
3621 G_FILE_ATTRIBUTE_TYPE_INT64, &value,
3622 flags, cancellable, error);
3626 * g_file_mount_mountable:
3627 * @file: input #GFile.
3628 * @flags: flags affecting the operation
3629 * @mount_operation: a #GMountOperation, or %NULL to avoid user interaction.
3630 * @cancellable: optional #GCancellable object, %NULL to ignore.
3631 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3632 * @user_data: the data to pass to callback function
3634 * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
3635 * Using @mount_operation, you can request callbacks when, for instance,
3636 * passwords are needed during authentication.
3638 * If @cancellable is not %NULL, then the operation can be cancelled by
3639 * triggering the cancellable object from another thread. If the operation
3640 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3642 * When the operation is finished, @callback will be called. You can then call
3643 * g_file_mount_mountable_finish() to get the result of the operation.
3646 g_file_mount_mountable (GFile *file,
3647 GMountMountFlags flags,
3648 GMountOperation *mount_operation,
3649 GCancellable *cancellable,
3650 GAsyncReadyCallback callback,
3655 g_return_if_fail (G_IS_FILE (file));
3657 iface = G_FILE_GET_IFACE (file);
3659 if (iface->mount_mountable == NULL)
3661 g_simple_async_report_error_in_idle (G_OBJECT (file),
3665 G_IO_ERROR_NOT_SUPPORTED,
3666 _("Operation not supported"));
3670 (* iface->mount_mountable) (file,
3679 * g_file_mount_mountable_finish:
3680 * @file: input #GFile.
3681 * @result: a #GAsyncResult.
3682 * @error: a #GError, or %NULL
3684 * Finishes a mount operation. See g_file_mount_mountable() for details.
3686 * Finish an asynchronous mount operation that was started
3687 * with g_file_mount_mountable().
3689 * Returns: a #GFile or %NULL on error.
3690 * Free the returned object with g_object_unref().
3693 g_file_mount_mountable_finish (GFile *file,
3694 GAsyncResult *result,
3699 g_return_val_if_fail (G_IS_FILE (file), NULL);
3700 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
3702 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3704 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3705 if (g_simple_async_result_propagate_error (simple, error))
3709 iface = G_FILE_GET_IFACE (file);
3710 return (* iface->mount_mountable_finish) (file, result, error);
3714 * g_file_unmount_mountable:
3715 * @file: input #GFile.
3716 * @flags: flags affecting the operation
3717 * @cancellable: optional #GCancellable object, %NULL to ignore.
3718 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3719 * @user_data: the data to pass to callback function
3721 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
3723 * If @cancellable is not %NULL, then the operation can be cancelled by
3724 * triggering the cancellable object from another thread. If the operation
3725 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3727 * When the operation is finished, @callback will be called. You can then call
3728 * g_file_unmount_mountable_finish() to get the result of the operation.
3731 g_file_unmount_mountable (GFile *file,
3732 GMountUnmountFlags flags,
3733 GCancellable *cancellable,
3734 GAsyncReadyCallback callback,
3739 g_return_if_fail (G_IS_FILE (file));
3741 iface = G_FILE_GET_IFACE (file);
3743 if (iface->unmount_mountable == NULL)
3745 g_simple_async_report_error_in_idle (G_OBJECT (file),
3749 G_IO_ERROR_NOT_SUPPORTED,
3750 _("Operation not supported"));
3754 (* iface->unmount_mountable) (file,
3762 * g_file_unmount_mountable_finish:
3763 * @file: input #GFile.
3764 * @result: a #GAsyncResult.
3765 * @error: a #GError, or %NULL
3767 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
3769 * Finish an asynchronous unmount operation that was started
3770 * with g_file_unmount_mountable().
3772 * Returns: %TRUE if the operation finished successfully. %FALSE
3776 g_file_unmount_mountable_finish (GFile *file,
3777 GAsyncResult *result,
3782 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3783 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3785 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3787 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3788 if (g_simple_async_result_propagate_error (simple, error))
3792 iface = G_FILE_GET_IFACE (file);
3793 return (* iface->unmount_mountable_finish) (file, result, error);
3797 * g_file_eject_mountable:
3798 * @file: input #GFile.
3799 * @flags: flags affecting the operation
3800 * @cancellable: optional #GCancellable object, %NULL to ignore.
3801 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3802 * @user_data: the data to pass to callback function
3804 * Starts an asynchronous eject on a mountable.
3805 * When this operation has completed, @callback will be called with
3806 * @user_user data, and the operation can be finalized with
3807 * g_file_eject_mountable_finish().
3809 * If @cancellable is not %NULL, then the operation can be cancelled by
3810 * triggering the cancellable object from another thread. If the operation
3811 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3814 g_file_eject_mountable (GFile *file,
3815 GMountUnmountFlags flags,
3816 GCancellable *cancellable,
3817 GAsyncReadyCallback callback,
3822 g_return_if_fail (G_IS_FILE (file));
3824 iface = G_FILE_GET_IFACE (file);
3826 if (iface->eject_mountable == NULL)
3828 g_simple_async_report_error_in_idle (G_OBJECT (file),
3832 G_IO_ERROR_NOT_SUPPORTED,
3833 _("Operation not supported"));
3837 (* iface->eject_mountable) (file,
3845 * g_file_eject_mountable_finish:
3846 * @file: input #GFile.
3847 * @result: a #GAsyncResult.
3848 * @error: a #GError, or %NULL
3850 * Finishes an asynchronous eject operation started by
3851 * g_file_eject_mountable().
3853 * Returns: %TRUE if the @file was ejected successfully. %FALSE
3857 g_file_eject_mountable_finish (GFile *file,
3858 GAsyncResult *result,
3863 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3864 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3866 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3868 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3869 if (g_simple_async_result_propagate_error (simple, error))
3873 iface = G_FILE_GET_IFACE (file);
3874 return (* iface->eject_mountable_finish) (file, result, error);
3878 * g_file_monitor_directory:
3879 * @file: input #GFile.
3880 * @flags: a set of #GFileMonitorFlags.
3881 * @cancellable: optional #GCancellable object, %NULL to ignore.
3882 * @error: a #GError, or %NULL.
3884 * Obtains a directory monitor for the given file.
3885 * This may fail if directory monitoring is not supported.
3887 * If @cancellable is not %NULL, then the operation can be cancelled by
3888 * triggering the cancellable object from another thread. If the operation
3889 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3891 * Returns: a #GFileMonitor for the given @file, or %NULL on error.
3892 * Free the returned object with g_object_unref().
3895 g_file_monitor_directory (GFile *file,
3896 GFileMonitorFlags flags,
3897 GCancellable *cancellable,
3902 g_return_val_if_fail (G_IS_FILE (file), NULL);
3904 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3907 iface = G_FILE_GET_IFACE (file);
3909 if (iface->monitor_dir == NULL)
3911 g_set_error_literal (error, G_IO_ERROR,
3912 G_IO_ERROR_NOT_SUPPORTED,
3913 _("Operation not supported"));
3917 return (* iface->monitor_dir) (file, flags, cancellable, error);
3921 * g_file_monitor_file:
3922 * @file: input #GFile.
3923 * @flags: a set of #GFileMonitorFlags.
3924 * @cancellable: optional #GCancellable object, %NULL to ignore.
3925 * @error: a #GError, or %NULL.
3927 * Obtains a file monitor for the given file. If no file notification
3928 * mechanism exists, then regular polling of the file is used.
3930 * If @cancellable is not %NULL, then the operation can be cancelled by
3931 * triggering the cancellable object from another thread. If the operation
3932 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3934 * Returns: a #GFileMonitor for the given @file, or %NULL on error.
3935 * Free the returned object with g_object_unref().
3938 g_file_monitor_file (GFile *file,
3939 GFileMonitorFlags flags,
3940 GCancellable *cancellable,
3944 GFileMonitor *monitor;
3946 g_return_val_if_fail (G_IS_FILE (file), NULL);
3948 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3951 iface = G_FILE_GET_IFACE (file);
3955 if (iface->monitor_file)
3956 monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
3958 /* Fallback to polling */
3959 if (monitor == NULL)
3960 monitor = _g_poll_file_monitor_new (file);
3967 * @file: input #GFile
3968 * @flags: a set of #GFileMonitorFlags
3969 * @cancellable: optional #GCancellable object, %NULL to ignore
3970 * @error: a #GError, or %NULL
3972 * Obtains a file or directory monitor for the given file, depending
3973 * on the type of the file.
3975 * If @cancellable is not %NULL, then the operation can be cancelled by
3976 * triggering the cancellable object from another thread. If the operation
3977 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3979 * Returns: a #GFileMonitor for the given @file, or %NULL on error.
3980 * Free the returned object with g_object_unref().
3985 g_file_monitor (GFile *file,
3986 GFileMonitorFlags flags,
3987 GCancellable *cancellable,
3990 if (g_file_query_file_type (file, 0, cancellable) == G_FILE_TYPE_DIRECTORY)
3991 return g_file_monitor_directory (file, flags, cancellable, error);
3993 return g_file_monitor_file (file, flags, cancellable, error);
3996 /********************************************
3997 * Default implementation of async ops *
3998 ********************************************/
4002 GFileQueryInfoFlags flags;
4004 } QueryInfoAsyncData;
4007 query_info_data_free (QueryInfoAsyncData *data)
4010 g_object_unref (data->info);
4011 g_free (data->attributes);
4016 query_info_async_thread (GSimpleAsyncResult *res,
4018 GCancellable *cancellable)
4020 GError *error = NULL;
4021 QueryInfoAsyncData *data;
4024 data = g_simple_async_result_get_op_res_gpointer (res);
4026 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
4030 g_simple_async_result_set_from_error (res, error);
4031 g_error_free (error);
4038 g_file_real_query_info_async (GFile *file,
4039 const char *attributes,
4040 GFileQueryInfoFlags flags,
4042 GCancellable *cancellable,
4043 GAsyncReadyCallback callback,
4046 GSimpleAsyncResult *res;
4047 QueryInfoAsyncData *data;
4049 data = g_new0 (QueryInfoAsyncData, 1);
4050 data->attributes = g_strdup (attributes);
4051 data->flags = flags;
4053 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_info_async);
4054 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
4056 g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
4057 g_object_unref (res);
4061 g_file_real_query_info_finish (GFile *file,
4065 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4066 QueryInfoAsyncData *data;
4068 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_info_async);
4070 data = g_simple_async_result_get_op_res_gpointer (simple);
4072 return g_object_ref (data->info);
4080 } QueryFilesystemInfoAsyncData;
4083 query_filesystem_info_data_free (QueryFilesystemInfoAsyncData *data)
4086 g_object_unref (data->info);
4087 g_free (data->attributes);
4092 query_filesystem_info_async_thread (GSimpleAsyncResult *res,
4094 GCancellable *cancellable)
4096 GError *error = NULL;
4097 QueryFilesystemInfoAsyncData *data;
4100 data = g_simple_async_result_get_op_res_gpointer (res);
4102 info = g_file_query_filesystem_info (G_FILE (object), data->attributes, cancellable, &error);
4106 g_simple_async_result_set_from_error (res, error);
4107 g_error_free (error);
4114 g_file_real_query_filesystem_info_async (GFile *file,
4115 const char *attributes,
4117 GCancellable *cancellable,
4118 GAsyncReadyCallback callback,
4121 GSimpleAsyncResult *res;
4122 QueryFilesystemInfoAsyncData *data;
4124 data = g_new0 (QueryFilesystemInfoAsyncData, 1);
4125 data->attributes = g_strdup (attributes);
4127 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_filesystem_info_async);
4128 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_filesystem_info_data_free);
4130 g_simple_async_result_run_in_thread (res, query_filesystem_info_async_thread, io_priority, cancellable);
4131 g_object_unref (res);
4135 g_file_real_query_filesystem_info_finish (GFile *file,
4139 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4140 QueryFilesystemInfoAsyncData *data;
4142 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_filesystem_info_async);
4144 data = g_simple_async_result_get_op_res_gpointer (simple);
4146 return g_object_ref (data->info);
4153 GFileQueryInfoFlags flags;
4154 GFileEnumerator *enumerator;
4155 } EnumerateChildrenAsyncData;
4158 enumerate_children_data_free (EnumerateChildrenAsyncData *data)
4160 if (data->enumerator)
4161 g_object_unref (data->enumerator);
4162 g_free (data->attributes);
4167 enumerate_children_async_thread (GSimpleAsyncResult *res,
4169 GCancellable *cancellable)
4171 GError *error = NULL;
4172 EnumerateChildrenAsyncData *data;
4173 GFileEnumerator *enumerator;
4175 data = g_simple_async_result_get_op_res_gpointer (res);
4177 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
4179 if (enumerator == NULL)
4181 g_simple_async_result_set_from_error (res, error);
4182 g_error_free (error);
4185 data->enumerator = enumerator;
4189 g_file_real_enumerate_children_async (GFile *file,
4190 const char *attributes,
4191 GFileQueryInfoFlags flags,
4193 GCancellable *cancellable,
4194 GAsyncReadyCallback callback,
4197 GSimpleAsyncResult *res;
4198 EnumerateChildrenAsyncData *data;
4200 data = g_new0 (EnumerateChildrenAsyncData, 1);
4201 data->attributes = g_strdup (attributes);
4202 data->flags = flags;
4204 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_enumerate_children_async);
4205 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)enumerate_children_data_free);
4207 g_simple_async_result_run_in_thread (res, enumerate_children_async_thread, io_priority, cancellable);
4208 g_object_unref (res);
4211 static GFileEnumerator *
4212 g_file_real_enumerate_children_finish (GFile *file,
4216 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4217 EnumerateChildrenAsyncData *data;
4219 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_enumerate_children_async);
4221 data = g_simple_async_result_get_op_res_gpointer (simple);
4222 if (data->enumerator)
4223 return g_object_ref (data->enumerator);
4229 open_read_async_thread (GSimpleAsyncResult *res,
4231 GCancellable *cancellable)
4234 GFileInputStream *stream;
4235 GError *error = NULL;
4237 iface = G_FILE_GET_IFACE (object);
4239 if (iface->read_fn == NULL)
4241 g_set_error_literal (&error, G_IO_ERROR,
4242 G_IO_ERROR_NOT_SUPPORTED,
4243 _("Operation not supported"));
4245 g_simple_async_result_set_from_error (res, error);
4246 g_error_free (error);
4251 stream = iface->read_fn (G_FILE (object), cancellable, &error);
4255 g_simple_async_result_set_from_error (res, error);
4256 g_error_free (error);
4259 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4263 g_file_real_read_async (GFile *file,
4265 GCancellable *cancellable,
4266 GAsyncReadyCallback callback,
4269 GSimpleAsyncResult *res;
4271 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_read_async);
4273 g_simple_async_result_run_in_thread (res, open_read_async_thread, io_priority, cancellable);
4274 g_object_unref (res);
4277 static GFileInputStream *
4278 g_file_real_read_finish (GFile *file,
4282 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4285 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_read_async);
4287 op = g_simple_async_result_get_op_res_gpointer (simple);
4289 return g_object_ref (op);
4295 append_to_async_thread (GSimpleAsyncResult *res,
4297 GCancellable *cancellable)
4300 GFileCreateFlags *data;
4301 GFileOutputStream *stream;
4302 GError *error = NULL;
4304 iface = G_FILE_GET_IFACE (object);
4306 data = g_simple_async_result_get_op_res_gpointer (res);
4308 stream = iface->append_to (G_FILE (object), *data, cancellable, &error);
4312 g_simple_async_result_set_from_error (res, error);
4313 g_error_free (error);
4316 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4320 g_file_real_append_to_async (GFile *file,
4321 GFileCreateFlags flags,
4323 GCancellable *cancellable,
4324 GAsyncReadyCallback callback,
4327 GFileCreateFlags *data;
4328 GSimpleAsyncResult *res;
4330 data = g_new0 (GFileCreateFlags, 1);
4333 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_append_to_async);
4334 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
4336 g_simple_async_result_run_in_thread (res, append_to_async_thread, io_priority, cancellable);
4337 g_object_unref (res);
4340 static GFileOutputStream *
4341 g_file_real_append_to_finish (GFile *file,
4345 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4348 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_append_to_async);
4350 op = g_simple_async_result_get_op_res_gpointer (simple);
4352 return g_object_ref (op);
4358 create_async_thread (GSimpleAsyncResult *res,
4360 GCancellable *cancellable)
4363 GFileCreateFlags *data;
4364 GFileOutputStream *stream;
4365 GError *error = NULL;
4367 iface = G_FILE_GET_IFACE (object);
4369 data = g_simple_async_result_get_op_res_gpointer (res);
4371 stream = iface->create (G_FILE (object), *data, cancellable, &error);
4375 g_simple_async_result_set_from_error (res, error);
4376 g_error_free (error);
4379 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4383 g_file_real_create_async (GFile *file,
4384 GFileCreateFlags flags,
4386 GCancellable *cancellable,
4387 GAsyncReadyCallback callback,
4390 GFileCreateFlags *data;
4391 GSimpleAsyncResult *res;
4393 data = g_new0 (GFileCreateFlags, 1);
4396 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_create_async);
4397 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
4399 g_simple_async_result_run_in_thread (res, create_async_thread, io_priority, cancellable);
4400 g_object_unref (res);
4403 static GFileOutputStream *
4404 g_file_real_create_finish (GFile *file,
4408 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4411 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_create_async);
4413 op = g_simple_async_result_get_op_res_gpointer (simple);
4415 return g_object_ref (op);
4421 GFileOutputStream *stream;
4423 gboolean make_backup;
4424 GFileCreateFlags flags;
4428 replace_async_data_free (ReplaceAsyncData *data)
4431 g_object_unref (data->stream);
4432 g_free (data->etag);
4437 replace_async_thread (GSimpleAsyncResult *res,
4439 GCancellable *cancellable)
4442 GFileOutputStream *stream;
4443 GError *error = NULL;
4444 ReplaceAsyncData *data;
4446 iface = G_FILE_GET_IFACE (object);
4448 data = g_simple_async_result_get_op_res_gpointer (res);
4450 stream = iface->replace (G_FILE (object),
4459 g_simple_async_result_set_from_error (res, error);
4460 g_error_free (error);
4463 data->stream = stream;
4467 g_file_real_replace_async (GFile *file,
4469 gboolean make_backup,
4470 GFileCreateFlags flags,
4472 GCancellable *cancellable,
4473 GAsyncReadyCallback callback,
4476 GSimpleAsyncResult *res;
4477 ReplaceAsyncData *data;
4479 data = g_new0 (ReplaceAsyncData, 1);
4480 data->etag = g_strdup (etag);
4481 data->make_backup = make_backup;
4482 data->flags = flags;
4484 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_replace_async);
4485 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_async_data_free);
4487 g_simple_async_result_run_in_thread (res, replace_async_thread, io_priority, cancellable);
4488 g_object_unref (res);
4491 static GFileOutputStream *
4492 g_file_real_replace_finish (GFile *file,
4496 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4497 ReplaceAsyncData *data;
4499 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_replace_async);
4501 data = g_simple_async_result_get_op_res_gpointer (simple);
4503 return g_object_ref (data->stream);
4511 } SetDisplayNameAsyncData;
4514 set_display_name_data_free (SetDisplayNameAsyncData *data)
4516 g_free (data->name);
4518 g_object_unref (data->file);
4523 set_display_name_async_thread (GSimpleAsyncResult *res,
4525 GCancellable *cancellable)
4527 GError *error = NULL;
4528 SetDisplayNameAsyncData *data;
4531 data = g_simple_async_result_get_op_res_gpointer (res);
4533 file = g_file_set_display_name (G_FILE (object), data->name, cancellable, &error);
4537 g_simple_async_result_set_from_error (res, error);
4538 g_error_free (error);
4545 g_file_real_set_display_name_async (GFile *file,
4546 const char *display_name,
4548 GCancellable *cancellable,
4549 GAsyncReadyCallback callback,
4552 GSimpleAsyncResult *res;
4553 SetDisplayNameAsyncData *data;
4555 data = g_new0 (SetDisplayNameAsyncData, 1);
4556 data->name = g_strdup (display_name);
4558 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_display_name_async);
4559 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_display_name_data_free);
4561 g_simple_async_result_run_in_thread (res, set_display_name_async_thread, io_priority, cancellable);
4562 g_object_unref (res);
4566 g_file_real_set_display_name_finish (GFile *file,
4570 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4571 SetDisplayNameAsyncData *data;
4573 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_display_name_async);
4575 data = g_simple_async_result_get_op_res_gpointer (simple);
4577 return g_object_ref (data->file);
4583 GFileQueryInfoFlags flags;
4590 set_info_data_free (SetInfoAsyncData *data)
4593 g_object_unref (data->info);
4595 g_error_free (data->error);
4600 set_info_async_thread (GSimpleAsyncResult *res,
4602 GCancellable *cancellable)
4604 SetInfoAsyncData *data;
4606 data = g_simple_async_result_get_op_res_gpointer (res);
4609 data->res = g_file_set_attributes_from_info (G_FILE (object),
4617 g_file_real_set_attributes_async (GFile *file,
4619 GFileQueryInfoFlags flags,
4621 GCancellable *cancellable,
4622 GAsyncReadyCallback callback,
4625 GSimpleAsyncResult *res;
4626 SetInfoAsyncData *data;
4628 data = g_new0 (SetInfoAsyncData, 1);
4629 data->info = g_file_info_dup (info);
4630 data->flags = flags;
4632 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_attributes_async);
4633 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_info_data_free);
4635 g_simple_async_result_run_in_thread (res, set_info_async_thread, io_priority, cancellable);
4636 g_object_unref (res);
4640 g_file_real_set_attributes_finish (GFile *file,
4645 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4646 SetInfoAsyncData *data;
4648 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_attributes_async);
4650 data = g_simple_async_result_get_op_res_gpointer (simple);
4653 *info = g_object_ref (data->info);
4655 if (error != NULL && data->error)
4656 *error = g_error_copy (data->error);
4662 find_enclosing_mount_async_thread (GSimpleAsyncResult *res,
4664 GCancellable *cancellable)
4666 GError *error = NULL;
4669 mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
4673 g_simple_async_result_set_from_error (res, error);
4674 g_error_free (error);
4677 g_simple_async_result_set_op_res_gpointer (res, mount, (GDestroyNotify)g_object_unref);
4681 g_file_real_find_enclosing_mount_async (GFile *file,
4683 GCancellable *cancellable,
4684 GAsyncReadyCallback callback,
4687 GSimpleAsyncResult *res;
4689 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_find_enclosing_mount_async);
4691 g_simple_async_result_run_in_thread (res, find_enclosing_mount_async_thread, io_priority, cancellable);
4692 g_object_unref (res);
4696 g_file_real_find_enclosing_mount_finish (GFile *file,
4700 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4703 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_find_enclosing_mount_async);
4705 mount = g_simple_async_result_get_op_res_gpointer (simple);
4706 return g_object_ref (mount);
4713 GFileCopyFlags flags;
4714 GFileProgressCallback progress_cb;
4715 gpointer progress_cb_data;
4716 GIOSchedulerJob *job;
4720 copy_async_data_free (CopyAsyncData *data)
4722 g_object_unref (data->source);
4723 g_object_unref (data->destination);
4728 CopyAsyncData *data;
4729 goffset current_num_bytes;
4730 goffset total_num_bytes;
4734 copy_async_progress_in_main (gpointer user_data)
4736 ProgressData *progress = user_data;
4737 CopyAsyncData *data = progress->data;
4739 data->progress_cb (progress->current_num_bytes,
4740 progress->total_num_bytes,
4741 data->progress_cb_data);
4747 mainloop_barrier (gpointer user_data)
4749 /* Does nothing, but ensures all queued idles before
4756 copy_async_progress_callback (goffset current_num_bytes,
4757 goffset total_num_bytes,
4760 CopyAsyncData *data = user_data;
4761 ProgressData *progress;
4763 progress = g_new (ProgressData, 1);
4764 progress->data = data;
4765 progress->current_num_bytes = current_num_bytes;
4766 progress->total_num_bytes = total_num_bytes;
4768 g_io_scheduler_job_send_to_mainloop_async (data->job,
4769 copy_async_progress_in_main,
4775 copy_async_thread (GIOSchedulerJob *job,
4776 GCancellable *cancellable,
4779 GSimpleAsyncResult *res;
4780 CopyAsyncData *data;
4785 data = g_simple_async_result_get_op_res_gpointer (res);
4789 result = g_file_copy (data->source,
4793 (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
4797 /* Ensure all progress callbacks are done running in main thread */
4798 if (data->progress_cb != NULL)
4799 g_io_scheduler_job_send_to_mainloop (job,
4805 g_simple_async_result_set_from_error (res, error);
4806 g_error_free (error);
4809 g_simple_async_result_complete_in_idle (res);
4815 g_file_real_copy_async (GFile *source,
4817 GFileCopyFlags flags,
4819 GCancellable *cancellable,
4820 GFileProgressCallback progress_callback,
4821 gpointer progress_callback_data,
4822 GAsyncReadyCallback callback,
4825 GSimpleAsyncResult *res;
4826 CopyAsyncData *data;
4828 data = g_new0 (CopyAsyncData, 1);
4829 data->source = g_object_ref (source);
4830 data->destination = g_object_ref (destination);
4831 data->flags = flags;
4832 data->progress_cb = progress_callback;
4833 data->progress_cb_data = progress_callback_data;
4835 res = g_simple_async_result_new (G_OBJECT (source), callback, user_data, g_file_real_copy_async);
4836 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)copy_async_data_free);
4838 g_io_scheduler_push_job (copy_async_thread, res, g_object_unref, io_priority, cancellable);
4842 g_file_real_copy_finish (GFile *file,
4846 /* Error handled in g_file_copy_finish() */
4851 /********************************************
4852 * Default VFS operations *
4853 ********************************************/
4856 * g_file_new_for_path:
4857 * @path: a string containing a relative or absolute path.
4859 * Constructs a #GFile for a given path. This operation never
4860 * fails, but the returned object might not support any I/O
4861 * operation if @path is malformed.
4863 * Returns: a new #GFile for the given @path.
4866 g_file_new_for_path (const char *path)
4868 g_return_val_if_fail (path != NULL, NULL);
4870 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
4874 * g_file_new_for_uri:
4875 * @uri: a string containing a URI.
4877 * Constructs a #GFile for a given URI. This operation never
4878 * fails, but the returned object might not support any I/O
4879 * operation if @uri is malformed or if the uri type is
4882 * Returns: a #GFile for the given @uri.
4885 g_file_new_for_uri (const char *uri)
4887 g_return_val_if_fail (uri != NULL, NULL);
4889 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
4893 * g_file_parse_name:
4894 * @parse_name: a file name or path to be parsed.
4896 * Constructs a #GFile with the given @parse_name (i.e. something given by g_file_get_parse_name()).
4897 * This operation never fails, but the returned object might not support any I/O
4898 * operation if the @parse_name cannot be parsed.
4900 * Returns: a new #GFile.
4903 g_file_parse_name (const char *parse_name)
4905 g_return_val_if_fail (parse_name != NULL, NULL);
4907 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
4911 is_valid_scheme_character (char c)
4913 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
4916 /* Following RFC 2396, valid schemes are built like:
4917 * scheme = alpha *( alpha | digit | "+" | "-" | "." )
4920 has_valid_scheme (const char *uri)
4926 if (!g_ascii_isalpha (*p))
4931 } while (is_valid_scheme_character (*p));
4937 * g_file_new_for_commandline_arg:
4938 * @arg: a command line string.
4940 * Creates a #GFile with the given argument from the command line. The value of
4941 * @arg can be either a URI, an absolute path or a relative path resolved
4942 * relative to the current working directory.
4943 * This operation never fails, but the returned object might not support any
4944 * I/O operation if @arg points to a malformed path.
4946 * Returns: a new #GFile.
4949 g_file_new_for_commandline_arg (const char *arg)
4955 g_return_val_if_fail (arg != NULL, NULL);
4957 if (g_path_is_absolute (arg))
4958 return g_file_new_for_path (arg);
4960 if (has_valid_scheme (arg))
4961 return g_file_new_for_uri (arg);
4963 current_dir = g_get_current_dir ();
4964 filename = g_build_filename (current_dir, arg, NULL);
4965 g_free (current_dir);
4967 file = g_file_new_for_path (filename);
4974 * g_file_mount_enclosing_volume:
4975 * @location: input #GFile.
4976 * @flags: flags affecting the operation
4977 * @mount_operation: a #GMountOperation or %NULL to avoid user interaction.
4978 * @cancellable: optional #GCancellable object, %NULL to ignore.
4979 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4980 * @user_data: the data to pass to callback function
4982 * Starts a @mount_operation, mounting the volume that contains the file @location.
4984 * When this operation has completed, @callback will be called with
4985 * @user_user data, and the operation can be finalized with
4986 * g_file_mount_enclosing_volume_finish().
4988 * If @cancellable is not %NULL, then the operation can be cancelled by
4989 * triggering the cancellable object from another thread. If the operation
4990 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4993 g_file_mount_enclosing_volume (GFile *location,
4994 GMountMountFlags flags,
4995 GMountOperation *mount_operation,
4996 GCancellable *cancellable,
4997 GAsyncReadyCallback callback,
5002 g_return_if_fail (G_IS_FILE (location));
5004 iface = G_FILE_GET_IFACE (location);
5006 if (iface->mount_enclosing_volume == NULL)
5008 g_simple_async_report_error_in_idle (G_OBJECT (location),
5009 callback, user_data,
5010 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5011 _("volume doesn't implement mount"));
5016 (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
5021 * g_file_mount_enclosing_volume_finish:
5022 * @location: input #GFile.
5023 * @result: a #GAsyncResult.
5024 * @error: a #GError, or %NULL
5026 * Finishes a mount operation started by g_file_mount_enclosing_volume().
5028 * Returns: %TRUE if successful. If an error
5029 * has occurred, this function will return %FALSE and set @error
5030 * appropriately if present.
5033 g_file_mount_enclosing_volume_finish (GFile *location,
5034 GAsyncResult *result,
5039 g_return_val_if_fail (G_IS_FILE (location), FALSE);
5040 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5042 if (G_IS_SIMPLE_ASYNC_RESULT (result))
5044 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
5045 if (g_simple_async_result_propagate_error (simple, error))
5049 iface = G_FILE_GET_IFACE (location);
5051 return (* iface->mount_enclosing_volume_finish) (location, result, error);
5054 /********************************************
5055 * Utility functions *
5056 ********************************************/
5059 * g_file_query_default_handler:
5060 * @file: a #GFile to open.
5061 * @cancellable: optional #GCancellable object, %NULL to ignore.
5062 * @error: a #GError, or %NULL
5064 * Returns the #GAppInfo that is registered as the default
5065 * application to handle the file specified by @file.
5067 * If @cancellable is not %NULL, then the operation can be cancelled by
5068 * triggering the cancellable object from another thread. If the operation
5069 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5071 * Returns: a #GAppInfo if the handle was found, %NULL if there were errors.
5072 * When you are done with it, release it with g_object_unref()
5075 g_file_query_default_handler (GFile *file,
5076 GCancellable *cancellable,
5080 const char *content_type;
5085 uri_scheme = g_file_get_uri_scheme (file);
5086 appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
5087 g_free (uri_scheme);
5089 if (appinfo != NULL)
5092 info = g_file_query_info (file,
5093 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
5102 content_type = g_file_info_get_content_type (info);
5105 /* Don't use is_native(), as we want to support fuse paths if availible */
5106 path = g_file_get_path (file);
5107 appinfo = g_app_info_get_default_for_type (content_type,
5112 g_object_unref (info);
5114 if (appinfo != NULL)
5117 g_set_error_literal (error, G_IO_ERROR,
5118 G_IO_ERROR_NOT_SUPPORTED,
5119 _("No application is registered as handling this file"));
5125 #define GET_CONTENT_BLOCK_SIZE 8192
5128 * g_file_load_contents:
5129 * @file: input #GFile.
5130 * @cancellable: optional #GCancellable object, %NULL to ignore.
5131 * @contents: a location to place the contents of the file.
5132 * @length: a location to place the length of the contents of the file,
5133 * or %NULL if the length is not needed
5134 * @etag_out: a location to place the current entity tag for the file,
5135 * or %NULL if the entity tag is not needed
5136 * @error: a #GError, or %NULL
5138 * Loads the content of the file into memory. The data is always
5139 * zero-terminated, but this is not included in the resultant @length.
5140 * The returned @content should be freed with g_free() when no longer
5143 * If @cancellable is not %NULL, then the operation can be cancelled by
5144 * triggering the cancellable object from another thread. If the operation
5145 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5147 * Returns: %TRUE if the @file's contents were successfully loaded.
5148 * %FALSE if there were errors.
5151 g_file_load_contents (GFile *file,
5152 GCancellable *cancellable,
5158 GFileInputStream *in;
5159 GByteArray *content;
5164 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5165 g_return_val_if_fail (contents != NULL, FALSE);
5167 in = g_file_read (file, cancellable, error);
5171 content = g_byte_array_new ();
5174 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
5175 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
5176 content->data + pos,
5177 GET_CONTENT_BLOCK_SIZE,
5178 cancellable, error)) > 0)
5181 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
5188 info = g_file_input_stream_query_info (in,
5189 G_FILE_ATTRIBUTE_ETAG_VALUE,
5194 *etag_out = g_strdup (g_file_info_get_etag (info));
5195 g_object_unref (info);
5199 /* Ignore errors on close */
5200 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
5201 g_object_unref (in);
5205 /* error is set already */
5206 g_byte_array_free (content, TRUE);
5213 /* Zero terminate (we got an extra byte allocated for this */
5214 content->data[pos] = 0;
5216 *contents = (char *)g_byte_array_free (content, FALSE);
5224 GCancellable *cancellable;
5225 GFileReadMoreCallback read_more_callback;
5226 GAsyncReadyCallback callback;
5228 GByteArray *content;
5235 load_contents_data_free (LoadContentsData *data)
5238 g_error_free (data->error);
5239 if (data->cancellable)
5240 g_object_unref (data->cancellable);
5242 g_byte_array_free (data->content, TRUE);
5243 g_free (data->etag);
5244 g_object_unref (data->file);
5249 load_contents_close_callback (GObject *obj,
5250 GAsyncResult *close_res,
5253 GInputStream *stream = G_INPUT_STREAM (obj);
5254 LoadContentsData *data = user_data;
5255 GSimpleAsyncResult *res;
5257 /* Ignore errors here, we're only reading anyway */
5258 g_input_stream_close_finish (stream, close_res, NULL);
5259 g_object_unref (stream);
5261 res = g_simple_async_result_new (G_OBJECT (data->file),
5264 g_file_load_contents_async);
5265 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)load_contents_data_free);
5266 g_simple_async_result_complete (res);
5267 g_object_unref (res);
5271 load_contents_fstat_callback (GObject *obj,
5272 GAsyncResult *stat_res,
5275 GInputStream *stream = G_INPUT_STREAM (obj);
5276 LoadContentsData *data = user_data;
5279 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
5283 data->etag = g_strdup (g_file_info_get_etag (info));
5284 g_object_unref (info);
5287 g_input_stream_close_async (stream, 0,
5289 load_contents_close_callback, data);
5293 load_contents_read_callback (GObject *obj,
5294 GAsyncResult *read_res,
5297 GInputStream *stream = G_INPUT_STREAM (obj);
5298 LoadContentsData *data = user_data;
5299 GError *error = NULL;
5302 read_size = g_input_stream_read_finish (stream, read_res, &error);
5306 /* Error or EOF, close the file */
5307 data->error = error;
5308 g_input_stream_close_async (stream, 0,
5310 load_contents_close_callback, data);
5312 else if (read_size == 0)
5314 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
5315 G_FILE_ATTRIBUTE_ETAG_VALUE,
5318 load_contents_fstat_callback,
5321 else if (read_size > 0)
5323 data->pos += read_size;
5325 g_byte_array_set_size (data->content,
5326 data->pos + GET_CONTENT_BLOCK_SIZE);
5329 if (data->read_more_callback &&
5330 !data->read_more_callback ((char *)data->content->data, data->pos, data->user_data))
5331 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
5332 G_FILE_ATTRIBUTE_ETAG_VALUE,
5335 load_contents_fstat_callback,
5338 g_input_stream_read_async (stream,
5339 data->content->data + data->pos,
5340 GET_CONTENT_BLOCK_SIZE,
5343 load_contents_read_callback,
5349 load_contents_open_callback (GObject *obj,
5350 GAsyncResult *open_res,
5353 GFile *file = G_FILE (obj);
5354 GFileInputStream *stream;
5355 LoadContentsData *data = user_data;
5356 GError *error = NULL;
5357 GSimpleAsyncResult *res;
5359 stream = g_file_read_finish (file, open_res, &error);
5363 g_byte_array_set_size (data->content,
5364 data->pos + GET_CONTENT_BLOCK_SIZE);
5365 g_input_stream_read_async (G_INPUT_STREAM (stream),
5366 data->content->data + data->pos,
5367 GET_CONTENT_BLOCK_SIZE,
5370 load_contents_read_callback,
5376 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
5380 g_simple_async_result_complete (res);
5381 g_error_free (error);
5382 load_contents_data_free (data);
5383 g_object_unref (res);
5388 * g_file_load_partial_contents_async:
5389 * @file: input #GFile.
5390 * @cancellable: optional #GCancellable object, %NULL to ignore.
5391 * @read_more_callback: a #GFileReadMoreCallback to receive partial data and to specify whether further data should be read.
5392 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5393 * @user_data: the data to pass to the callback functions.
5395 * Reads the partial contents of a file. A #GFileReadMoreCallback should be
5396 * used to stop reading from the file when appropriate, else this function
5397 * will behave exactly as g_file_load_contents_async(). This operation
5398 * can be finished by g_file_load_partial_contents_finish().
5400 * Users of this function should be aware that @user_data is passed to
5401 * both the @read_more_callback and the @callback.
5403 * If @cancellable is not %NULL, then the operation can be cancelled by
5404 * triggering the cancellable object from another thread. If the operation
5405 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5408 g_file_load_partial_contents_async (GFile *file,
5409 GCancellable *cancellable,
5410 GFileReadMoreCallback read_more_callback,
5411 GAsyncReadyCallback callback,
5414 LoadContentsData *data;
5416 g_return_if_fail (G_IS_FILE (file));
5418 data = g_new0 (LoadContentsData, 1);
5421 data->cancellable = g_object_ref (cancellable);
5422 data->read_more_callback = read_more_callback;
5423 data->callback = callback;
5424 data->user_data = user_data;
5425 data->content = g_byte_array_new ();
5426 data->file = g_object_ref (file);
5428 g_file_read_async (file,
5431 load_contents_open_callback,
5436 * g_file_load_partial_contents_finish:
5437 * @file: input #GFile.
5438 * @res: a #GAsyncResult.
5439 * @contents: a location to place the contents of the file.
5440 * @length: a location to place the length of the contents of the file,
5441 * or %NULL if the length is not needed
5442 * @etag_out: a location to place the current entity tag for the file,
5443 * or %NULL if the entity tag is not needed
5444 * @error: a #GError, or %NULL
5446 * Finishes an asynchronous partial load operation that was started
5447 * with g_file_load_partial_contents_async(). The data is always
5448 * zero-terminated, but this is not included in the resultant @length.
5449 * The returned @content should be freed with g_free() when no longer
5452 * Returns: %TRUE if the load was successful. If %FALSE and @error is
5453 * present, it will be set appropriately.
5456 g_file_load_partial_contents_finish (GFile *file,
5463 GSimpleAsyncResult *simple;
5464 LoadContentsData *data;
5466 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5467 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
5468 g_return_val_if_fail (contents != NULL, FALSE);
5470 simple = G_SIMPLE_ASYNC_RESULT (res);
5472 if (g_simple_async_result_propagate_error (simple, error))
5475 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_load_contents_async);
5477 data = g_simple_async_result_get_op_res_gpointer (simple);
5481 g_propagate_error (error, data->error);
5490 *length = data->pos;
5494 *etag_out = data->etag;
5498 /* Zero terminate */
5499 g_byte_array_set_size (data->content, data->pos + 1);
5500 data->content->data[data->pos] = 0;
5502 *contents = (char *)g_byte_array_free (data->content, FALSE);
5503 data->content = NULL;
5509 * g_file_load_contents_async:
5510 * @file: input #GFile.
5511 * @cancellable: optional #GCancellable object, %NULL to ignore.
5512 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5513 * @user_data: the data to pass to callback function
5515 * Starts an asynchronous load of the @file's contents.
5517 * For more details, see g_file_load_contents() which is
5518 * the synchronous version of this call.
5520 * When the load operation has completed, @callback will be called
5521 * with @user data. To finish the operation, call
5522 * g_file_load_contents_finish() with the #GAsyncResult returned by
5525 * If @cancellable is not %NULL, then the operation can be cancelled by
5526 * triggering the cancellable object from another thread. If the operation
5527 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5530 g_file_load_contents_async (GFile *file,
5531 GCancellable *cancellable,
5532 GAsyncReadyCallback callback,
5535 g_file_load_partial_contents_async (file,
5538 callback, user_data);
5542 * g_file_load_contents_finish:
5543 * @file: input #GFile.
5544 * @res: a #GAsyncResult.
5545 * @contents: a location to place the contents of the file.
5546 * @length: a location to place the length of the contents of the file,
5547 * or %NULL if the length is not needed
5548 * @etag_out: a location to place the current entity tag for the file,
5549 * or %NULL if the entity tag is not needed
5550 * @error: a #GError, or %NULL
5552 * Finishes an asynchronous load of the @file's contents.
5553 * The contents are placed in @contents, and @length is set to the
5554 * size of the @contents string. The @content should be freed with
5555 * g_free() when no longer needed. If @etag_out is present, it will be
5556 * set to the new entity tag for the @file.
5558 * Returns: %TRUE if the load was successful. If %FALSE and @error is
5559 * present, it will be set appropriately.
5562 g_file_load_contents_finish (GFile *file,
5569 return g_file_load_partial_contents_finish (file,
5578 * g_file_replace_contents:
5579 * @file: input #GFile.
5580 * @contents: a string containing the new contents for @file.
5581 * @length: the length of @contents in bytes.
5582 * @etag: the old <link linkend="gfile-etag">entity tag</link>
5583 * for the document, or %NULL
5584 * @make_backup: %TRUE if a backup should be created.
5585 * @flags: a set of #GFileCreateFlags.
5586 * @new_etag: a location to a new <link linkend="gfile-etag">entity tag</link>
5587 * for the document. This should be freed with g_free() when no longer
5589 * @cancellable: optional #GCancellable object, %NULL to ignore.
5590 * @error: a #GError, or %NULL
5592 * Replaces the contents of @file with @contents of @length bytes.
5594 * If @etag is specified (not %NULL) any existing file must have that etag, or
5595 * the error %G_IO_ERROR_WRONG_ETAG will be returned.
5597 * If @make_backup is %TRUE, this function will attempt to make a backup of @file.
5599 * If @cancellable is not %NULL, then the operation can be cancelled by
5600 * triggering the cancellable object from another thread. If the operation
5601 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5603 * The returned @new_etag can be used to verify that the file hasn't changed the
5604 * next time it is saved over.
5606 * Returns: %TRUE if successful. If an error
5607 * has occurred, this function will return %FALSE and set @error
5608 * appropriately if present.
5611 g_file_replace_contents (GFile *file,
5612 const char *contents,
5615 gboolean make_backup,
5616 GFileCreateFlags flags,
5618 GCancellable *cancellable,
5621 GFileOutputStream *out;
5622 gsize pos, remainder;
5626 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5627 g_return_val_if_fail (contents != NULL, FALSE);
5629 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
5635 while (remainder > 0 &&
5636 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
5638 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
5646 if (remainder > 0 && res < 0)
5648 /* Ignore errors on close */
5649 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
5650 g_object_unref (out);
5652 /* error is set already */
5656 ret = g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error);
5659 *new_etag = g_file_output_stream_get_etag (out);
5661 g_object_unref (out);
5669 GCancellable *cancellable;
5670 GAsyncReadyCallback callback;
5672 const char *content;
5676 } ReplaceContentsData;
5679 replace_contents_data_free (ReplaceContentsData *data)
5682 g_error_free (data->error);
5683 if (data->cancellable)
5684 g_object_unref (data->cancellable);
5685 g_object_unref (data->file);
5686 g_free (data->etag);
5691 replace_contents_close_callback (GObject *obj,
5692 GAsyncResult *close_res,
5695 GOutputStream *stream = G_OUTPUT_STREAM (obj);
5696 ReplaceContentsData *data = user_data;
5697 GSimpleAsyncResult *res;
5699 /* Ignore errors here, we're only reading anyway */
5700 g_output_stream_close_finish (stream, close_res, NULL);
5701 g_object_unref (stream);
5703 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
5705 res = g_simple_async_result_new (G_OBJECT (data->file),
5708 g_file_replace_contents_async);
5709 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_contents_data_free);
5710 g_simple_async_result_complete (res);
5711 g_object_unref (res);
5715 replace_contents_write_callback (GObject *obj,
5716 GAsyncResult *read_res,
5719 GOutputStream *stream = G_OUTPUT_STREAM (obj);
5720 ReplaceContentsData *data = user_data;
5721 GError *error = NULL;
5724 write_size = g_output_stream_write_finish (stream, read_res, &error);
5726 if (write_size <= 0)
5728 /* Error or EOF, close the file */
5730 data->error = error;
5731 g_output_stream_close_async (stream, 0,
5733 replace_contents_close_callback, data);
5735 else if (write_size > 0)
5737 data->pos += write_size;
5739 if (data->pos >= data->length)
5740 g_output_stream_close_async (stream, 0,
5742 replace_contents_close_callback, data);
5744 g_output_stream_write_async (stream,
5745 data->content + data->pos,
5746 data->length - data->pos,
5749 replace_contents_write_callback,
5755 replace_contents_open_callback (GObject *obj,
5756 GAsyncResult *open_res,
5759 GFile *file = G_FILE (obj);
5760 GFileOutputStream *stream;
5761 ReplaceContentsData *data = user_data;
5762 GError *error = NULL;
5763 GSimpleAsyncResult *res;
5765 stream = g_file_replace_finish (file, open_res, &error);
5769 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
5770 data->content + data->pos,
5771 data->length - data->pos,
5774 replace_contents_write_callback,
5780 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
5784 g_simple_async_result_complete (res);
5785 g_error_free (error);
5786 replace_contents_data_free (data);
5787 g_object_unref (res);
5792 * g_file_replace_contents_async:
5793 * @file: input #GFile.
5794 * @contents: string of contents to replace the file with.
5795 * @length: the length of @contents in bytes.
5796 * @etag: a new <link linkend="gfile-etag">entity tag</link> for the @file, or %NULL
5797 * @make_backup: %TRUE if a backup should be created.
5798 * @flags: a set of #GFileCreateFlags.
5799 * @cancellable: optional #GCancellable object, %NULL to ignore.
5800 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5801 * @user_data: the data to pass to callback function
5803 * Starts an asynchronous replacement of @file with the given
5804 * @contents of @length bytes. @etag will replace the document's
5805 * current entity tag.
5807 * When this operation has completed, @callback will be called with
5808 * @user_user data, and the operation can be finalized with
5809 * g_file_replace_contents_finish().
5811 * If @cancellable is not %NULL, then the operation can be cancelled by
5812 * triggering the cancellable object from another thread. If the operation
5813 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5815 * If @make_backup is %TRUE, this function will attempt to
5816 * make a backup of @file.
5819 g_file_replace_contents_async (GFile *file,
5820 const char *contents,
5823 gboolean make_backup,
5824 GFileCreateFlags flags,
5825 GCancellable *cancellable,
5826 GAsyncReadyCallback callback,
5829 ReplaceContentsData *data;
5831 g_return_if_fail (G_IS_FILE (file));
5832 g_return_if_fail (contents != NULL);
5834 data = g_new0 (ReplaceContentsData, 1);
5837 data->cancellable = g_object_ref (cancellable);
5838 data->callback = callback;
5839 data->user_data = user_data;
5840 data->content = contents;
5841 data->length = length;
5843 data->file = g_object_ref (file);
5845 g_file_replace_async (file,
5851 replace_contents_open_callback,
5856 * g_file_replace_contents_finish:
5857 * @file: input #GFile.
5858 * @res: a #GAsyncResult.
5859 * @new_etag: a location of a new <link linkend="gfile-etag">entity tag</link>
5860 * for the document. This should be freed with g_free() when it is no
5861 * longer needed, or %NULL
5862 * @error: a #GError, or %NULL
5864 * Finishes an asynchronous replace of the given @file. See
5865 * g_file_replace_contents_async(). Sets @new_etag to the new entity
5866 * tag for the document, if present.
5868 * Returns: %TRUE on success, %FALSE on failure.
5871 g_file_replace_contents_finish (GFile *file,
5876 GSimpleAsyncResult *simple;
5877 ReplaceContentsData *data;
5879 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5880 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
5882 simple = G_SIMPLE_ASYNC_RESULT (res);
5884 if (g_simple_async_result_propagate_error (simple, error))
5887 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_replace_contents_async);
5889 data = g_simple_async_result_get_op_res_gpointer (simple);
5893 g_propagate_error (error, data->error);
5901 *new_etag = data->etag;
5902 data->etag = NULL; /* Take ownership */
5908 #define __G_FILE_C__
5909 #include "gioaliasdef.c"