1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Alexander Larsson <alexl@redhat.com>
25 #include <sys/types.h>
31 #include "gioscheduler.h"
32 #include <glocalfile.h>
33 #include "gsimpleasyncresult.h"
34 #include "gfileattribute-priv.h"
35 #include "gpollfilemonitor.h"
42 * @short_description: File and Directory Handling
44 * @see_also: #GFileInfo, #GFileEnumerator
46 * #GFile is a high level abstraction for manipulating files on a
47 * virtual file system. #GFile<!-- -->s are lightweight, immutable
48 * objects that do no I/O upon creation. It is necessary to understand that
49 * #GFile objects do not represent files, merely a handle to a file. All
50 * file I/O is implemented as streaming operations (see #GInputStream and
53 * To construct a #GFile, you can use:
54 * g_file_new_for_path() if you have a path.
55 * g_file_new_for_uri() if you have a URI.
56 * g_file_new_for_commandline_arg() for a command line argument.
58 * You can move through the filesystem with #GFile handles with
59 * g_file_get_parent() to get a handle to the parent directory.
60 * g_file_get_child() to get a handle to a child within a directory.
61 * g_file_resolve_relative_path() to resolve a relative path between
62 * two #GFile<!-- -->s.
64 * Many #GFile operations have both synchronous and asynchronous versions
65 * to suit your application. Asynchronous versions of synchronous functions
66 * simply have _async() appended to their function names. The asynchronous
67 * I/O functions call a #GAsyncReadyCallback which is then used to finalize
68 * the operation, producing a GAsyncResult which is then passed to the
69 * function's matching _finish()
72 * Some #GFile operations do not have synchronous analogs, as they may
73 * take a very long time to finish, and blocking may leave an application
74 * unusable. Notable cases include:
75 * g_file_mount_mountable() to mount a mountable file.
76 * g_file_unmount_mountable() to unmount a mountable file.
77 * g_file_eject_mountable() to eject a mountable file.
79 * <para id="gfile-etag"><indexterm><primary>entity tag</primary></indexterm>
80 * One notable feature of #GFile<!-- -->s are entity tags, or "etags" for
81 * short. Entity tags are somewhat like a more abstract version of the
82 * traditional mtime, and can be used to quickly determine if the file has
83 * been modified from the version on the file system. See the HTTP 1.1
84 * <ulink url="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html">specification</ulink>
85 * for HTTP Etag headers, which are a very similar concept.
89 static void g_file_base_init (gpointer g_class);
90 static void g_file_class_init (gpointer g_class,
93 static void g_file_real_query_info_async (GFile *file,
94 const char *attributes,
95 GFileQueryInfoFlags flags,
97 GCancellable *cancellable,
98 GAsyncReadyCallback callback,
100 static GFileInfo * g_file_real_query_info_finish (GFile *file,
103 static void g_file_real_enumerate_children_async (GFile *file,
104 const char *attributes,
105 GFileQueryInfoFlags flags,
107 GCancellable *cancellable,
108 GAsyncReadyCallback callback,
110 static GFileEnumerator * g_file_real_enumerate_children_finish (GFile *file,
113 static void g_file_real_read_async (GFile *file,
115 GCancellable *cancellable,
116 GAsyncReadyCallback callback,
118 static GFileInputStream * g_file_real_read_finish (GFile *file,
121 static void g_file_real_append_to_async (GFile *file,
122 GFileCreateFlags flags,
124 GCancellable *cancellable,
125 GAsyncReadyCallback callback,
127 static GFileOutputStream *g_file_real_append_to_finish (GFile *file,
130 static void g_file_real_create_async (GFile *file,
131 GFileCreateFlags flags,
133 GCancellable *cancellable,
134 GAsyncReadyCallback callback,
136 static GFileOutputStream *g_file_real_create_finish (GFile *file,
139 static void g_file_real_replace_async (GFile *file,
141 gboolean make_backup,
142 GFileCreateFlags flags,
144 GCancellable *cancellable,
145 GAsyncReadyCallback callback,
147 static GFileOutputStream *g_file_real_replace_finish (GFile *file,
150 static gboolean g_file_real_set_attributes_from_info (GFile *file,
152 GFileQueryInfoFlags flags,
153 GCancellable *cancellable,
155 static void g_file_real_set_display_name_async (GFile *file,
156 const char *display_name,
158 GCancellable *cancellable,
159 GAsyncReadyCallback callback,
161 static GFile * g_file_real_set_display_name_finish (GFile *file,
164 static void g_file_real_set_attributes_async (GFile *file,
166 GFileQueryInfoFlags flags,
168 GCancellable *cancellable,
169 GAsyncReadyCallback callback,
171 static gboolean g_file_real_set_attributes_finish (GFile *file,
177 g_file_get_type (void)
179 static GType file_type = 0;
183 static const GTypeInfo file_info =
185 sizeof (GFileIface), /* class_size */
186 g_file_base_init, /* base_init */
187 NULL, /* base_finalize */
189 NULL, /* class_finalize */
190 NULL, /* class_data */
197 g_type_register_static (G_TYPE_INTERFACE, I_("GFile"),
200 g_type_interface_add_prerequisite (file_type, G_TYPE_OBJECT);
207 g_file_class_init (gpointer g_class,
210 GFileIface *iface = g_class;
212 iface->enumerate_children_async = g_file_real_enumerate_children_async;
213 iface->enumerate_children_finish = g_file_real_enumerate_children_finish;
214 iface->set_display_name_async = g_file_real_set_display_name_async;
215 iface->set_display_name_finish = g_file_real_set_display_name_finish;
216 iface->query_info_async = g_file_real_query_info_async;
217 iface->query_info_finish = g_file_real_query_info_finish;
218 iface->set_attributes_async = g_file_real_set_attributes_async;
219 iface->set_attributes_finish = g_file_real_set_attributes_finish;
220 iface->read_async = g_file_real_read_async;
221 iface->read_finish = g_file_real_read_finish;
222 iface->append_to_async = g_file_real_append_to_async;
223 iface->append_to_finish = g_file_real_append_to_finish;
224 iface->create_async = g_file_real_create_async;
225 iface->create_finish = g_file_real_create_finish;
226 iface->replace_async = g_file_real_replace_async;
227 iface->replace_finish = g_file_real_replace_finish;
228 iface->set_attributes_from_info = g_file_real_set_attributes_from_info;
232 g_file_base_init (gpointer g_class)
239 * @file: input #GFile.
241 * Checks to see if a file is native to the platform.
243 * A native file s one expressed in the platform-native filename format,
244 * e.g. "C:\Windows" or "/usr/bin/". This does not mean the file is local,
245 * as it might be on a locally mounted remote filesystem.
247 * On some systems non-native files may be available using
248 * the native filesystem via a userspace filesystem (FUSE), in
249 * these cases this call will return %FALSE, but g_file_get_path()
250 * will still return a native path.
252 * This call does no blocking i/o.
254 * Returns: %TRUE if file is native.
257 g_file_is_native (GFile *file)
261 g_return_val_if_fail (G_IS_FILE (file), FALSE);
263 iface = G_FILE_GET_IFACE (file);
265 return (* iface->is_native) (file);
270 * g_file_has_uri_scheme:
271 * @file: input #GFile.
272 * @uri_scheme: a string containing a URI scheme.
274 * Checks to see if a #GFile has a given URI scheme.
276 * This call does no blocking i/o.
278 * Returns: %TRUE if #GFile's backend supports the
279 * given URI scheme, %FALSE if URI scheme is %NULL,
280 * not supported, or #GFile is invalid.
283 g_file_has_uri_scheme (GFile *file,
284 const char *uri_scheme)
288 g_return_val_if_fail (G_IS_FILE (file), FALSE);
289 g_return_val_if_fail (uri_scheme != NULL, FALSE);
291 iface = G_FILE_GET_IFACE (file);
293 return (* iface->has_uri_scheme) (file, uri_scheme);
298 * g_file_get_uri_scheme:
299 * @file: input #GFile.
301 * Gets the URI scheme for a #GFile.
302 * RFC 3986 decodes the scheme as:
304 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
306 * Common schemes include "file", "http", "ftp", etc.
308 * This call does no blocking i/o.
310 * Returns: a string containing the URI scheme for the given
311 * #GFile. The returned string should be freed with g_free()
312 * when no longer needed.
315 g_file_get_uri_scheme (GFile *file)
319 g_return_val_if_fail (G_IS_FILE (file), NULL);
321 iface = G_FILE_GET_IFACE (file);
323 return (* iface->get_uri_scheme) (file);
328 * g_file_get_basename:
329 * @file: input #GFile.
331 * Gets the base name (the last component of the path) for a given #GFile.
333 * If called for the top level of a system (such as the filesystem root
334 * or a uri like sftp://host/ it will return a single directory separator
335 * (and on Windows, possibly a drive letter).
337 * This call does no blocking i/o.
339 * Returns: string containing the #GFile's base name, or %NULL
340 * if given #GFile is invalid. The returned string should be
341 * freed with g_free() when no longer needed.
344 g_file_get_basename (GFile *file)
348 g_return_val_if_fail (G_IS_FILE (file), NULL);
350 iface = G_FILE_GET_IFACE (file);
352 return (* iface->get_basename) (file);
357 * @file: input #GFile.
359 * Gets the local pathname for #GFile, if one exists.
361 * This call does no blocking i/o.
363 * Returns: string containing the #GFile's path, or %NULL if
364 * no such path exists. The returned string should be
365 * freed with g_free() when no longer needed.
368 g_file_get_path (GFile *file)
372 g_return_val_if_fail (G_IS_FILE (file), NULL);
374 iface = G_FILE_GET_IFACE (file);
376 return (* iface->get_path) (file);
381 * @file: input #GFile.
383 * Gets the URI for the @file.
385 * This call does no blocking i/o.
387 * Returns: a string containing the #GFile's URI.
388 * The returned string should be freed with g_free() when no longer needed.
391 g_file_get_uri (GFile *file)
395 g_return_val_if_fail (G_IS_FILE (file), NULL);
397 iface = G_FILE_GET_IFACE (file);
399 return (* iface->get_uri) (file);
403 * g_file_get_parse_name:
404 * @file: input #GFile.
406 * Gets the parse name of the @file.
407 * A parse name is a UTF-8 string that describes the
408 * file such that one can get the #GFile back using
409 * g_file_parse_name().
411 * This is generally used to show the #GFile as a nice
412 * string in a user interface, like in a location entry.
414 * For local files with names that can safely be converted
415 * to UTF8 the pathname is used, otherwise the IRI is used
416 * (a form of URI that allows UTF8 characters unescaped).
418 * This call does no blocking i/o.
420 * Returns: a string containing the #GFile's parse name. The returned
421 * string should be freed with g_free() when no longer needed.
424 g_file_get_parse_name (GFile *file)
428 g_return_val_if_fail (G_IS_FILE (file), NULL);
430 iface = G_FILE_GET_IFACE (file);
432 return (* iface->get_parse_name) (file);
437 * @file: input #GFile.
439 * Duplicates a #GFile handle. This operation does not duplicate
440 * the actual file or directory represented by the #GFile; see
441 * g_file_copy() if attempting to copy a file.
443 * This call does no blocking i/o.
445 * Returns: #GFile that is a duplicate of the given #GFile.
448 g_file_dup (GFile *file)
452 g_return_val_if_fail (G_IS_FILE (file), NULL);
454 iface = G_FILE_GET_IFACE (file);
456 return (* iface->dup) (file);
461 * @file: #gconstpointer to a #GFile.
463 * Creates a hash value for a #GFile.
465 * This call does no blocking i/o.
467 * Returns: 0 if @file is not a valid #GFile, otherwise an
468 * integer that can be used as hash value for the #GFile.
469 * This function is intended for easily hashing a #GFile to
470 * add to a #GHashTable or similar data structure.
473 g_file_hash (gconstpointer file)
477 g_return_val_if_fail (G_IS_FILE (file), 0);
479 iface = G_FILE_GET_IFACE (file);
481 return (* iface->hash) ((GFile *)file);
486 * @file1: the first #GFile.
487 * @file2: the second #GFile.
489 * Checks equality of two given #GFile<!-- -->s
491 * This call does no blocking i/o.
493 * Returns: %TRUE if @file1 and @file2 are equal.
494 * %FALSE if either is not a #GFile.
497 g_file_equal (GFile *file1,
502 g_return_val_if_fail (G_IS_FILE (file1), FALSE);
503 g_return_val_if_fail (G_IS_FILE (file2), FALSE);
505 if (G_TYPE_FROM_INSTANCE (file1) != G_TYPE_FROM_INSTANCE (file2))
508 iface = G_FILE_GET_IFACE (file1);
510 return (* iface->equal) (file1, file2);
516 * @file: input #GFile.
518 * Gets the parent directory for the @file.
519 * If the @file represents the root directory of the
520 * file system, then %NULL will be returned.
522 * This call does no blocking i/o.
524 * Returns: a #GFile structure to the parent of the given
525 * #GFile or %NULL if there is no parent.
528 g_file_get_parent (GFile *file)
532 g_return_val_if_fail (G_IS_FILE (file), NULL);
534 iface = G_FILE_GET_IFACE (file);
536 return (* iface->get_parent) (file);
541 * @file: input #GFile.
542 * @name: string containing the child's name.
544 * Gets a specific child of @file with name equal to @name.
546 * Note that the file with that specific name might not exist, but
547 * you can still have a #GFile that points to it. You can use this
548 * for instance to create that file.
550 * This call does no blocking i/o.
552 * Returns: a #GFile to a child specified by @name.
555 g_file_get_child (GFile *file,
558 g_return_val_if_fail (G_IS_FILE (file), NULL);
559 g_return_val_if_fail (name != NULL, NULL);
561 return g_file_resolve_relative_path (file, name);
565 * g_file_get_child_for_display_name:
566 * @file: input #GFile.
567 * @display_name: string to a possible child.
570 * Gets the child of @file for a given @display_name (i.e. a UTF8
571 * version of the name). If this function fails, it returns %NULL and @error will be
572 * set. This is very useful when constructing a GFile for a new file
573 * and the user entered the filename in the user interface, for instance
574 * when you select a directory and type a filename in the file selector.
576 * This call does no blocking i/o.
578 * Returns: a #GFile to the specified child, or
579 * %NULL if the display name couldn't be converted.
582 g_file_get_child_for_display_name (GFile *file,
583 const char *display_name,
588 g_return_val_if_fail (G_IS_FILE (file), NULL);
589 g_return_val_if_fail (display_name != NULL, NULL);
591 iface = G_FILE_GET_IFACE (file);
593 return (* iface->get_child_for_display_name) (file, display_name, error);
597 * g_file_contains_file:
598 * @parent: input #GFile.
599 * @descendant: input #GFile.
601 * Checks whether @parent (recursively) contains the specified @descendant.
603 * This call does no blocking i/o.
605 * Returns: %TRUE if the @descendant's parent, grandparent, etc is @parent. %FALSE otherwise.
608 g_file_contains_file (GFile *parent,
613 g_return_val_if_fail (G_IS_FILE (parent), FALSE);
614 g_return_val_if_fail (G_IS_FILE (descendant), FALSE);
616 if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
619 iface = G_FILE_GET_IFACE (parent);
621 return (* iface->contains_file) (parent, descendant);
625 * g_file_get_relative_path:
626 * @parent: input #GFile.
627 * @descendant: input #GFile.
629 * Gets the path for @descendant relative to @parent.
631 * This call does no blocking i/o.
633 * Returns: string with the relative path from @descendant
634 * to @parent, or %NULL if @descendant is not a descendant of @parent. The returned string should be freed with
635 * g_free() when no longer needed.
638 g_file_get_relative_path (GFile *parent,
643 g_return_val_if_fail (G_IS_FILE (parent), NULL);
644 g_return_val_if_fail (G_IS_FILE (descendant), NULL);
646 if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
649 iface = G_FILE_GET_IFACE (parent);
651 return (* iface->get_relative_path) (parent, descendant);
655 * g_file_resolve_relative_path:
656 * @file: input #GFile.
657 * @relative_path: a given relative path string.
659 * Resolves a relative path for @file to an absolute path.
661 * This call does no blocking i/o.
663 * Returns: #GFile to the resolved path. %NULL if @relative_path
664 * is %NULL or if @file is invalid.
667 g_file_resolve_relative_path (GFile *file,
668 const char *relative_path)
672 g_return_val_if_fail (G_IS_FILE (file), NULL);
673 g_return_val_if_fail (relative_path != NULL, NULL);
675 iface = G_FILE_GET_IFACE (file);
677 return (* iface->resolve_relative_path) (file, relative_path);
681 * g_file_enumerate_children:
682 * @file: input #GFile.
683 * @attributes: an attribute query string.
684 * @flags: a set of #GFileQueryInfoFlags.
685 * @cancellable: optional #GCancellable object, %NULL to ignore.
686 * @error: #GError for error reporting.
688 * Gets the requested information about the files in a directory. The result
689 * is a #GFileEnumerator object that will give out #GFileInfo objects for
690 * all the files in the directory.
692 * The @attribute value is a string that specifies the file attributes that
693 * should be gathered. It is not an error if its not possible to read a particular
694 * requested attribute from a file, it just won't be set. @attribute should
695 * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
696 * means all attributes, and a wildcard like "standard::*" means all attributes in the standard
697 * namespace. An example attribute query be "standard::*,owner::user".
698 * The standard attributes are available as defines, like #G_FILE_ATTRIBUTE_STANDARD_NAME.
700 * If @cancellable is not %NULL, then the operation can be cancelled by
701 * triggering the cancellable object from another thread. If the operation
702 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
704 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
705 * If the file is not a directory, the G_FILE_ERROR_NOTDIR error will be returned.
706 * Other errors are possible too.
708 * Returns: A #GFileEnumerator if successful, %NULL on error.
711 g_file_enumerate_children (GFile *file,
712 const char *attributes,
713 GFileQueryInfoFlags flags,
714 GCancellable *cancellable,
720 g_return_val_if_fail (G_IS_FILE (file), NULL);
722 if (g_cancellable_set_error_if_cancelled (cancellable, error))
725 iface = G_FILE_GET_IFACE (file);
727 if (iface->enumerate_children == NULL)
729 g_set_error (error, G_IO_ERROR,
730 G_IO_ERROR_NOT_SUPPORTED,
731 _("Operation not supported"));
735 return (* iface->enumerate_children) (file, attributes, flags,
740 * g_file_enumerate_children_async:
741 * @file: input #GFile.
742 * @attributes: an attribute query string.
743 * @flags: a set of #GFileQueryInfoFlags.
744 * @io_priority: the <link linkend="io-priority">I/O priority</link>
746 * @cancellable: optional #GCancellable object, %NULL to ignore.
747 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
748 * @user_data: the data to pass to callback function
750 * Asynchronously gets the requested information about the files in a directory. The result
751 * is a #GFileEnumerator object that will give out #GFileInfo objects for
752 * all the files in the directory.
754 * For more details, see g_file_enumerate_children() which is
755 * the synchronous version of this call.
757 * When the operation is finished, @callback will be called. You can then call
758 * g_file_enumerate_children_finish() to get the result of the operation.
761 g_file_enumerate_children_async (GFile *file,
762 const char *attributes,
763 GFileQueryInfoFlags flags,
765 GCancellable *cancellable,
766 GAsyncReadyCallback callback,
771 g_return_if_fail (G_IS_FILE (file));
773 iface = G_FILE_GET_IFACE (file);
774 (* iface->enumerate_children_async) (file,
784 * g_file_enumerate_children_finish:
785 * @file: input #GFile.
786 * @res: a #GAsyncResult.
789 * Finishes an async enumerate children operation.
790 * See g_file_enumerate_children_async().
792 * Returns: a #GFileEnumerator or %NULL if an error occurred.
795 g_file_enumerate_children_finish (GFile *file,
801 g_return_val_if_fail (G_IS_FILE (file), NULL);
802 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
804 if (G_IS_SIMPLE_ASYNC_RESULT (res))
806 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
807 if (g_simple_async_result_propagate_error (simple, error))
811 iface = G_FILE_GET_IFACE (file);
812 return (* iface->enumerate_children_finish) (file, res, error);
818 * @file: input #GFile.
819 * @attributes: an attribute query string.
820 * @flags: a set of #GFileQueryInfoFlags.
821 * @cancellable: optional #GCancellable object, %NULL to ignore.
824 * Gets the requested information about specified @file. The result
825 * is a #GFileInfo objects that contains key-value attributes (like type or size
828 * The @attribute value is a string that specifies the file attributes that
829 * should be gathered. It is not an error if its not possible to read a particular
830 * requested attribute from a file, it just won't be set. @attribute should
831 * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
832 * means all attributes, and a wildcard like "standard::*" means all attributes in the standard
833 * namespace. An example attribute query be "standard::*,owner::user".
834 * The standard attributes are available as defines, like #G_FILE_ATTRIBUTE_STANDARD_NAME.
836 * If @cancellable is not %NULL, then the operation can be cancelled by
837 * triggering the cancellable object from another thread. If the operation
838 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
840 * For symlinks, normally the information about the target of the
841 * symlink is returned, rather than information about the symlink itself.
842 * However if you pass #G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS in @flags the
843 * information about the symlink itself will be returned. Also, for symlinks
844 * that points to non-existing files the information about the symlink itself
847 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
848 * Other errors are possible too, and depend on what kind of filesystem the file is on.
850 * Returns: a #GFileInfo for the given @file, or %NULL on error.
853 g_file_query_info (GFile *file,
854 const char *attributes,
855 GFileQueryInfoFlags flags,
856 GCancellable *cancellable,
861 g_return_val_if_fail (G_IS_FILE (file), NULL);
863 if (g_cancellable_set_error_if_cancelled (cancellable, error))
866 iface = G_FILE_GET_IFACE (file);
868 if (iface->query_info == NULL)
870 g_set_error (error, G_IO_ERROR,
871 G_IO_ERROR_NOT_SUPPORTED,
872 _("Operation not supported"));
876 return (* iface->query_info) (file, attributes, flags, cancellable, error);
880 * g_file_query_info_async:
881 * @file: input #GFile.
882 * @attributes: an attribute query string.
883 * @flags: a set of #GFileQueryInfoFlags.
884 * @io_priority: the <link linkend="io-priority">I/O priority</link>
886 * @cancellable: optional #GCancellable object, %NULL to ignore.
887 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
888 * @user_data: the data to pass to callback function
890 * Asynchronously gets the requested information about specified @file. The result
891 * is a #GFileInfo objects that contains key-value attributes (such as type or size
894 * For more details, see g_file_query_info() which is
895 * the synchronous version of this call.
897 * When the operation is finished, @callback will be called. You can then call
898 * g_file_query_info_finish() to get the result of the operation.
901 g_file_query_info_async (GFile *file,
902 const char *attributes,
903 GFileQueryInfoFlags flags,
905 GCancellable *cancellable,
906 GAsyncReadyCallback callback,
911 g_return_if_fail (G_IS_FILE (file));
913 iface = G_FILE_GET_IFACE (file);
914 (* iface->query_info_async) (file,
924 * g_file_query_info_finish:
925 * @file: input #GFile.
926 * @res: a #GAsyncResult.
929 * Finishes an asynchronous file info query.
930 * See g_file_query_info_async().
932 * Returns: #GFileInfo for given @file or %NULL on error.
935 g_file_query_info_finish (GFile *file,
941 g_return_val_if_fail (G_IS_FILE (file), NULL);
942 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
944 if (G_IS_SIMPLE_ASYNC_RESULT (res))
946 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
947 if (g_simple_async_result_propagate_error (simple, error))
951 iface = G_FILE_GET_IFACE (file);
952 return (* iface->query_info_finish) (file, res, error);
956 * g_file_query_filesystem_info:
957 * @file: input #GFile.
958 * @attributes: an attribute query string.
959 * @cancellable: optional #GCancellable object, %NULL to ignore.
962 * Similar to g_file_query_info(), but obtains information
963 * about the filesystem the @file is on, rather than the file itself.
964 * For instance the amount of space available and the type of
967 * The @attribute value is a string that specifies the file attributes that
968 * should be gathered. It is not an error if its not possible to read a particular
969 * requested attribute from a file, it just won't be set. @attribute should
970 * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
971 * means all attributes, and a wildcard like "fs:*" means all attributes in the fs
972 * namespace. The standard namespace for filesystem attributes is "fs".
973 * Common attributes of interest are #G_FILE_ATTRIBUTE_FILESYSTEM_SIZE
974 * (the total size of the filesystem in bytes), #G_FILE_ATTRIBUTE_FILESYSTEM_FREE (number of
975 * bytes available), and #G_FILE_ATTRIBUTE_FILESYSTEM_TYPE (type of the filesystem).
977 * If @cancellable is not %NULL, then the operation can be cancelled by
978 * triggering the cancellable object from another thread. If the operation
979 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
981 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
982 * Other errors are possible too, and depend on what kind of filesystem the file is on.
984 * Returns: a #GFileInfo or %NULL if there was an error.
987 g_file_query_filesystem_info (GFile *file,
988 const char *attributes,
989 GCancellable *cancellable,
994 g_return_val_if_fail (G_IS_FILE (file), NULL);
996 if (g_cancellable_set_error_if_cancelled (cancellable, error))
999 iface = G_FILE_GET_IFACE (file);
1001 if (iface->query_filesystem_info == NULL)
1003 g_set_error (error, G_IO_ERROR,
1004 G_IO_ERROR_NOT_SUPPORTED,
1005 _("Operation not supported"));
1009 return (* iface->query_filesystem_info) (file, attributes, cancellable, error);
1013 * g_file_find_enclosing_mount:
1014 * @file: input #GFile.
1015 * @cancellable: optional #GCancellable object, %NULL to ignore.
1016 * @error: a #GError.
1018 * Gets a #GMount for the #GFile.
1020 * If the #GFileIface for @file does not have a mount (e.g. possibly a
1021 * remote share), @error will be set to %G_IO_ERROR_NOT_FOUND and %NULL
1024 * If @cancellable is not %NULL, then the operation can be cancelled by
1025 * triggering the cancellable object from another thread. If the operation
1026 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1028 * Returns: a #GMount where the @file is located or %NULL on error.
1031 g_file_find_enclosing_mount (GFile *file,
1032 GCancellable *cancellable,
1037 g_return_val_if_fail (G_IS_FILE (file), NULL);
1039 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1042 iface = G_FILE_GET_IFACE (file);
1043 if (iface->find_enclosing_mount == NULL)
1045 g_set_error (error, G_IO_ERROR,
1046 G_IO_ERROR_NOT_FOUND,
1047 _("Containing mount does not exist"));
1051 return (* iface->find_enclosing_mount) (file, cancellable, error);
1056 * @file: #GFile to read.
1057 * @cancellable: a #GCancellable
1058 * @error: a #GError, or %NULL
1060 * Opens a file for reading. The result is a #GFileInputStream that
1061 * can be used to read the contents of the file.
1063 * If @cancellable is not %NULL, then the operation can be cancelled by
1064 * triggering the cancellable object from another thread. If the operation
1065 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1067 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1068 * If the file is a directory, the G_IO_ERROR_IS_DIRECTORY error will be returned.
1069 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1071 * Returns: #GFileInputStream or %NULL on error.
1074 g_file_read (GFile *file,
1075 GCancellable *cancellable,
1080 g_return_val_if_fail (G_IS_FILE (file), NULL);
1082 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1085 iface = G_FILE_GET_IFACE (file);
1087 if (iface->read_fn == NULL)
1089 g_set_error (error, G_IO_ERROR,
1090 G_IO_ERROR_NOT_SUPPORTED,
1091 _("Operation not supported"));
1095 return (* iface->read_fn) (file, cancellable, error);
1100 * @file: input #GFile.
1101 * @flags: a set of #GFileCreateFlags.
1102 * @cancellable: optional #GCancellable object, %NULL to ignore.
1103 * @error: a #GError, or %NULL
1105 * Gets an output stream for appending data to the file. If
1106 * the file doesn't already exist it is created.
1108 * By default files created are generally readable by everyone,
1109 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1110 * will be made readable only to the current user, to the level that
1111 * is supported on the target filesystem.
1113 * If @cancellable is not %NULL, then the operation can be cancelled by
1114 * triggering the cancellable object from another thread. If the operation
1115 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1117 * Some file systems don't allow all file names, and may
1118 * return an G_IO_ERROR_INVALID_FILENAME error.
1119 * If the file is a directory the G_IO_ERROR_IS_DIRECTORY error will be
1120 * returned. Other errors are possible too, and depend on what kind of
1121 * filesystem the file is on.
1123 * Returns: a #GFileOutputStream.
1126 g_file_append_to (GFile *file,
1127 GFileCreateFlags flags,
1128 GCancellable *cancellable,
1133 g_return_val_if_fail (G_IS_FILE (file), NULL);
1135 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1138 iface = G_FILE_GET_IFACE (file);
1140 if (iface->append_to == NULL)
1142 g_set_error (error, G_IO_ERROR,
1143 G_IO_ERROR_NOT_SUPPORTED,
1144 _("Operation not supported"));
1148 return (* iface->append_to) (file, flags, cancellable, error);
1153 * @file: input #GFile.
1154 * @flags: a set of #GFileCreateFlags.
1155 * @cancellable: optional #GCancellable object, %NULL to ignore.
1156 * @error: a #GError, or %NULL
1158 * Creates a new file and returns an output stream for writing to it.
1159 * The file must not already exists.
1161 * By default files created are generally readable by everyone,
1162 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1163 * will be made readable only to the current user, to the level that
1164 * is supported on the target filesystem.
1166 * If @cancellable is not %NULL, then the operation can be cancelled by
1167 * triggering the cancellable object from another thread. If the operation
1168 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1170 * If a file with this name already exists the G_IO_ERROR_EXISTS error
1171 * will be returned. If the file is a directory the G_IO_ERROR_IS_DIRECTORY
1172 * error will be returned.
1173 * Some file systems don't allow all file names, and may
1174 * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1175 * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1176 * Other errors are possible too, and depend on what kind of
1177 * filesystem the file is on.
1179 * Returns: a #GFileOutputStream for the newly created file, or
1183 g_file_create (GFile *file,
1184 GFileCreateFlags flags,
1185 GCancellable *cancellable,
1190 g_return_val_if_fail (G_IS_FILE (file), NULL);
1192 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1195 iface = G_FILE_GET_IFACE (file);
1197 if (iface->create == NULL)
1199 g_set_error (error, G_IO_ERROR,
1200 G_IO_ERROR_NOT_SUPPORTED,
1201 _("Operation not supported"));
1205 return (* iface->create) (file, flags, cancellable, error);
1210 * @file: input #GFile.
1211 * @etag: an optional <link linkend="gfile-etag">entity tag</link> for the
1212 * current #GFile, or #NULL to ignore.
1213 * @make_backup: %TRUE if a backup should be created.
1214 * @flags: a set of #GFileCreateFlags.
1215 * @cancellable: optional #GCancellable object, %NULL to ignore.
1216 * @error: a #GError, or %NULL
1218 * Returns an output stream for overwriting the file, possibly
1219 * creating a backup copy of the file first.
1221 * This will try to replace the file in the safest way possible so
1222 * that any errors during the writing will not affect an already
1223 * existing copy of the file. For instance, for local files it
1224 * may write to a temporary file and then atomically rename over
1225 * the destination when the stream is closed.
1227 * By default files created are generally readable by everyone,
1228 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1229 * will be made readable only to the current user, to the level that
1230 * is supported on the target filesystem.
1232 * If @cancellable is not %NULL, then the operation can be cancelled by
1233 * triggering the cancellable object from another thread. If the operation
1234 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1236 * If you pass in a non-#NULL @etag value, then this value is
1237 * compared to the current entity tag of the file, and if they differ
1238 * an G_IO_ERROR_WRONG_ETAG error is returned. This generally means
1239 * that the file has been changed since you last read it. You can get
1240 * the new etag from g_file_output_stream_get_etag() after you've
1241 * finished writing and closed the #GFileOutputStream. When you load
1242 * a new file you can use g_file_input_stream_query_info() to get
1243 * the etag of the file.
1245 * If @make_backup is %TRUE, this function will attempt to make a backup
1246 * of the current file before overwriting it. If this fails a G_IO_ERROR_CANT_CREATE_BACKUP
1247 * error will be returned. If you want to replace anyway, try again with
1248 * @make_backup set to %FALSE.
1250 * If the file is a directory the G_IO_ERROR_IS_DIRECTORY error will be returned,
1251 * and if the file is some other form of non-regular file then a
1252 * G_IO_ERROR_NOT_REGULAR_FILE error will be returned.
1253 * Some file systems don't allow all file names, and may
1254 * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1255 * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1256 * Other errors are possible too, and depend on what kind of
1257 * filesystem the file is on.
1259 * Returns: a #GFileOutputStream or %NULL on error.
1262 g_file_replace (GFile *file,
1264 gboolean make_backup,
1265 GFileCreateFlags flags,
1266 GCancellable *cancellable,
1271 g_return_val_if_fail (G_IS_FILE (file), NULL);
1273 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1276 iface = G_FILE_GET_IFACE (file);
1278 if (iface->replace == NULL)
1280 g_set_error (error, G_IO_ERROR,
1281 G_IO_ERROR_NOT_SUPPORTED,
1282 _("Operation not supported"));
1287 /* Handle empty tag string as NULL in consistent way. */
1288 if (etag && *etag == 0)
1291 return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1295 * g_file_read_async:
1296 * @file: input #GFile.
1297 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1299 * @cancellable: optional #GCancellable object, %NULL to ignore.
1300 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1301 * @user_data: the data to pass to callback function
1303 * Asynchronously opens @file for reading.
1305 * For more details, see g_file_read() which is
1306 * the synchronous version of this call.
1308 * When the operation is finished, @callback will be called. You can then call
1309 * g_file_read_finish() to get the result of the operation.
1312 g_file_read_async (GFile *file,
1314 GCancellable *cancellable,
1315 GAsyncReadyCallback callback,
1320 g_return_if_fail (G_IS_FILE (file));
1322 iface = G_FILE_GET_IFACE (file);
1323 (* iface->read_async) (file,
1331 * g_file_read_finish:
1332 * @file: input #GFile.
1333 * @res: a #GAsyncResult.
1334 * @error: a #GError, or %NULL
1336 * Finishes an asynchronous file read operation started with
1337 * g_file_read_async().
1339 * Returns: a #GFileInputStream or %NULL on error.
1342 g_file_read_finish (GFile *file,
1348 g_return_val_if_fail (G_IS_FILE (file), NULL);
1349 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1351 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1353 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1354 if (g_simple_async_result_propagate_error (simple, error))
1358 iface = G_FILE_GET_IFACE (file);
1359 return (* iface->read_finish) (file, res, error);
1363 * g_file_append_to_async:
1364 * @file: input #GFile.
1365 * @flags: a set of #GFileCreateFlags.
1366 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1368 * @cancellable: optional #GCancellable object, %NULL to ignore.
1369 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1370 * @user_data: the data to pass to callback function
1372 * Asynchronously opens @file for appending.
1374 * For more details, see g_file_append_to() which is
1375 * the synchronous version of this call.
1377 * When the operation is finished, @callback will be called. You can then call
1378 * g_file_append_to_finish() to get the result of the operation.
1381 g_file_append_to_async (GFile *file,
1382 GFileCreateFlags flags,
1384 GCancellable *cancellable,
1385 GAsyncReadyCallback callback,
1390 g_return_if_fail (G_IS_FILE (file));
1392 iface = G_FILE_GET_IFACE (file);
1393 (* iface->append_to_async) (file,
1402 * g_file_append_to_finish:
1403 * @file: input #GFile.
1404 * @res: #GAsyncResult
1405 * @error: a #GError, or %NULL
1407 * Finishes an asynchronous file append operation started with
1408 * g_file_append_to_async().
1410 * Returns: a valid #GFileOutputStream or %NULL on error.
1413 g_file_append_to_finish (GFile *file,
1419 g_return_val_if_fail (G_IS_FILE (file), NULL);
1420 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1422 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1424 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1425 if (g_simple_async_result_propagate_error (simple, error))
1429 iface = G_FILE_GET_IFACE (file);
1430 return (* iface->append_to_finish) (file, res, error);
1434 * g_file_create_async:
1435 * @file: input #GFile.
1436 * @flags: a set of #GFileCreateFlags.
1437 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1439 * @cancellable: optional #GCancellable object, %NULL to ignore.
1440 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1441 * @user_data: the data to pass to callback function
1443 * Asynchronously creates a new file and returns an output stream for writing to it.
1444 * The file must not already exists.
1446 * For more details, see g_file_create() which is
1447 * the synchronous version of this call.
1449 * When the operation is finished, @callback will be called. You can then call
1450 * g_file_create_finish() to get the result of the operation.
1453 g_file_create_async (GFile *file,
1454 GFileCreateFlags flags,
1456 GCancellable *cancellable,
1457 GAsyncReadyCallback callback,
1462 g_return_if_fail (G_IS_FILE (file));
1464 iface = G_FILE_GET_IFACE (file);
1465 (* iface->create_async) (file,
1474 * g_file_create_finish:
1475 * @file: input #GFile.
1476 * @res: a #GAsyncResult.
1477 * @error: a #GError, or %NULL
1479 * Finishes an asynchronous file create operation started with
1480 * g_file_create_async().
1482 * Returns: a #GFileOutputStream or %NULL on error.
1485 g_file_create_finish (GFile *file,
1491 g_return_val_if_fail (G_IS_FILE (file), NULL);
1492 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1494 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1496 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1497 if (g_simple_async_result_propagate_error (simple, error))
1501 iface = G_FILE_GET_IFACE (file);
1502 return (* iface->create_finish) (file, res, error);
1506 * g_file_replace_async:
1507 * @file: input #GFile.
1508 * @etag: an <link linkend="gfile-etag">entity tag</link> for the
1509 * current #GFile, or NULL to ignore.
1510 * @make_backup: %TRUE if a backup should be created.
1511 * @flags: a set of #GFileCreateFlags.
1512 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1514 * @cancellable: optional #GCancellable object, %NULL to ignore.
1515 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1516 * @user_data: the data to pass to callback function
1518 * Asynchronously overwrites the file, replacing the contents, possibly
1519 * creating a backup copy of the file first.
1521 * For more details, see g_file_replace() which is
1522 * the synchronous version of this call.
1524 * When the operation is finished, @callback will be called. You can then call
1525 * g_file_replace_finish() to get the result of the operation.
1528 g_file_replace_async (GFile *file,
1530 gboolean make_backup,
1531 GFileCreateFlags flags,
1533 GCancellable *cancellable,
1534 GAsyncReadyCallback callback,
1539 g_return_if_fail (G_IS_FILE (file));
1541 iface = G_FILE_GET_IFACE (file);
1542 (* iface->replace_async) (file,
1553 * g_file_replace_finish:
1554 * @file: input #GFile.
1555 * @res: a #GAsyncResult.
1556 * @error: a #GError, or %NULL
1558 * Finishes an asynchronous file replace operation started with
1559 * g_file_replace_async().
1561 * Returns: a #GFileOutputStream, or %NULL on error.
1564 g_file_replace_finish (GFile *file,
1570 g_return_val_if_fail (G_IS_FILE (file), NULL);
1571 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1573 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1575 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1576 if (g_simple_async_result_propagate_error (simple, error))
1580 iface = G_FILE_GET_IFACE (file);
1581 return (* iface->replace_finish) (file, res, error);
1585 copy_symlink (GFile *destination,
1586 GFileCopyFlags flags,
1587 GCancellable *cancellable,
1592 gboolean tried_delete;
1594 GFileType file_type;
1596 tried_delete = FALSE;
1600 if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
1602 /* Maybe it already existed, and we want to overwrite? */
1603 if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) &&
1604 my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
1606 g_error_free (my_error);
1609 /* Don't overwrite if the destination is a directory */
1610 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1611 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1612 cancellable, &my_error);
1615 file_type = g_file_info_get_file_type (info);
1616 g_object_unref (info);
1618 if (file_type == G_FILE_TYPE_DIRECTORY)
1620 g_set_error (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
1621 _("Can't copy over directory"));
1626 if (!g_file_delete (destination, cancellable, error))
1629 tried_delete = TRUE;
1633 g_propagate_error (error, my_error);
1640 static GInputStream *
1641 open_source_for_copy (GFile *source,
1643 GFileCopyFlags flags,
1644 GCancellable *cancellable,
1650 GFileType file_type;
1653 in = (GInputStream *)g_file_read (source, cancellable, &my_error);
1657 /* There was an error opening the source, try to set a good error for it: */
1659 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
1661 /* The source is a directory, don't fail with WOULD_RECURSE immediately,
1662 * as that is less useful to the app. Better check for errors on the
1665 g_error_free (my_error);
1668 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1669 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1670 cancellable, &my_error);
1673 file_type = g_file_info_get_file_type (info);
1674 g_object_unref (info);
1676 if (flags & G_FILE_COPY_OVERWRITE)
1678 if (file_type == G_FILE_TYPE_DIRECTORY)
1680 g_set_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
1681 _("Can't copy directory over directory"));
1684 /* continue to would_recurse error */
1688 g_set_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
1689 _("Target file exists"));
1695 /* Error getting info from target, return that error
1696 * (except for NOT_FOUND, which is no error here)
1698 if (my_error->domain != G_IO_ERROR && my_error->code != G_IO_ERROR_NOT_FOUND)
1700 g_propagate_error (error, my_error);
1703 g_error_free (my_error);
1706 g_set_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
1707 _("Can't recursively copy directory"));
1711 g_propagate_error (error, my_error);
1716 should_copy (GFileAttributeInfo *info,
1720 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
1721 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
1725 build_attribute_list_for_copy (GFileAttributeInfoList *attributes,
1726 GFileAttributeInfoList *namespaces,
1734 s = g_string_new ("");
1738 for (i = 0; i < attributes->n_infos; i++)
1740 if (should_copy (&attributes->infos[i], as_move))
1745 g_string_append_c (s, ',');
1747 g_string_append (s, attributes->infos[i].name);
1754 for (i = 0; i < namespaces->n_infos; i++)
1756 if (should_copy (&namespaces->infos[i], as_move))
1761 g_string_append_c (s, ',');
1763 g_string_append (s, namespaces->infos[i].name);
1764 g_string_append (s, ":*");
1769 return g_string_free (s, FALSE);
1773 * g_file_copy_attributes:
1774 * @source: a #GFile with attributes.
1775 * @destination: a #GFile to copy attributes to.
1776 * @flags: a set of #GFileCopyFlags.
1777 * @cancellable: optional #GCancellable object, %NULL to ignore.
1778 * @error: a #GError, %NULL to ignore.
1780 * Copies the file attributes from @source to @destination.
1782 * Normally only a subset of the file attributes are copied,
1783 * those that are copies in a normal file copy operation
1784 * (which for instance does not include e.g. mtime). However
1785 * if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
1786 * all the metadata that is possible to copy is copied.
1788 * Returns: %TRUE if the attributes were copied successfully, %FALSE otherwise.
1791 g_file_copy_attributes (GFile *source,
1793 GFileCopyFlags flags,
1794 GCancellable *cancellable,
1797 GFileAttributeInfoList *attributes, *namespaces;
1798 char *attrs_to_read;
1802 gboolean source_nofollow_symlinks;
1804 as_move = flags & G_FILE_COPY_ALL_METADATA;
1805 source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
1807 /* Ignore errors here, if the target supports no attributes there is nothing to copy */
1808 attributes = g_file_query_settable_attributes (destination, cancellable, NULL);
1809 namespaces = g_file_query_writable_namespaces (destination, cancellable, NULL);
1811 if (attributes == NULL && namespaces == NULL)
1814 attrs_to_read = build_attribute_list_for_copy (attributes, namespaces, as_move);
1816 /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
1817 * we just don't copy it.
1819 info = g_file_query_info (source, attrs_to_read,
1820 source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
1824 g_free (attrs_to_read);
1829 res = g_file_set_attributes_from_info (destination,
1833 g_object_unref (info);
1836 g_file_attribute_info_list_unref (attributes);
1837 g_file_attribute_info_list_unref (namespaces);
1842 /* Closes the streams */
1844 copy_stream_with_progress (GInputStream *in,
1846 GCancellable *cancellable,
1847 GFileProgressCallback progress_callback,
1848 gpointer progress_callback_data,
1851 gssize n_read, n_written;
1852 goffset current_size;
1853 char buffer[8192], *p;
1859 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
1860 G_FILE_ATTRIBUTE_STANDARD_SIZE,
1864 total_size = g_file_info_get_size (info);
1865 g_object_unref (info);
1872 n_read = g_input_stream_read (in, buffer, sizeof (buffer), cancellable, error);
1882 current_size += n_read;
1887 n_written = g_output_stream_write (out, p, n_read, cancellable, error);
1888 if (n_written == -1)
1895 n_read -= n_written;
1901 if (progress_callback)
1902 progress_callback (current_size, total_size, progress_callback_data);
1906 error = NULL; /* Ignore further errors */
1908 /* Make sure we send full copied size */
1909 if (progress_callback)
1910 progress_callback (current_size, total_size, progress_callback_data);
1913 /* Don't care about errors in source here */
1914 g_input_stream_close (in, cancellable, NULL);
1916 /* But write errors on close are bad! */
1917 if (!g_output_stream_close (out, cancellable, error))
1920 g_object_unref (in);
1921 g_object_unref (out);
1927 file_copy_fallback (GFile *source,
1929 GFileCopyFlags flags,
1930 GCancellable *cancellable,
1931 GFileProgressCallback progress_callback,
1932 gpointer progress_callback_data,
1940 /* Maybe copy the symlink? */
1941 if (flags & G_FILE_COPY_NOFOLLOW_SYMLINKS)
1943 info = g_file_query_info (source,
1944 G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
1945 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1951 if (g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK &&
1952 (target = g_file_info_get_symlink_target (info)) != NULL)
1954 if (!copy_symlink (destination, flags, cancellable, target, error))
1956 g_object_unref (info);
1960 g_object_unref (info);
1964 g_object_unref (info);
1967 in = open_source_for_copy (source, destination, flags, cancellable, error);
1971 if (flags & G_FILE_COPY_OVERWRITE)
1973 out = (GOutputStream *)g_file_replace (destination,
1975 flags & G_FILE_COPY_BACKUP,
1976 cancellable, error);
1980 out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
1985 g_object_unref (in);
1989 if (!copy_stream_with_progress (in, out, cancellable,
1990 progress_callback, progress_callback_data,
1996 /* Ignore errors here. Failure to copy metadata is not a hard error */
1997 g_file_copy_attributes (source, destination,
1998 flags, cancellable, NULL);
2005 * @source: input #GFile.
2006 * @destination: destination #GFile
2007 * @flags: set of #GFileCopyFlags
2008 * @cancellable: optional #GCancellable object, %NULL to ignore.
2009 * @progress_callback: function to callback with progress information
2010 * @progress_callback_data: user data to pass to @progress_callback
2011 * @error: #GError to set on error, or %NULL
2013 * Copies the file @source to the location specified by @destination.
2014 * Can not handle recursive copies of directories.
2016 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2017 * existing @destination file is overwritten.
2019 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2020 * will be copied as symlinks, otherwise the target of the
2021 * @source symlink will be copied.
2023 * If @cancellable is not %NULL, then the operation can be cancelled by
2024 * triggering the cancellable object from another thread. If the operation
2025 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2027 * If @progress_callback is not %NULL, then the operation can be monitored by
2028 * setting this to a #GFileProgressCallback function. @progress_callback_data
2029 * will be passed to this function. It is guaranteed that this callback will
2030 * be called after all data has been transferred with the total number of bytes
2031 * copied during the operation.
2033 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2034 * error is returned, independent on the status of the @destination.
2036 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2037 * error G_IO_ERROR_EXISTS is returned.
2039 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2040 * error is returned. If trying to overwrite a directory with a directory the
2041 * G_IO_ERROR_WOULD_MERGE error is returned.
2043 * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2044 * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2047 * If you are interested in copying the #GFile object itself (not the on-disk
2048 * file), see g_file_dup().
2050 * Returns: %TRUE on success, %FALSE otherwise.
2053 g_file_copy (GFile *source,
2055 GFileCopyFlags flags,
2056 GCancellable *cancellable,
2057 GFileProgressCallback progress_callback,
2058 gpointer progress_callback_data,
2065 g_return_val_if_fail (G_IS_FILE (source), FALSE);
2066 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2068 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2071 if (G_OBJECT_TYPE (source) == G_OBJECT_TYPE (destination))
2073 iface = G_FILE_GET_IFACE (source);
2078 res = (* iface->copy) (source, destination, flags, cancellable, progress_callback, progress_callback_data, &my_error);
2083 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2085 g_propagate_error (error, my_error);
2091 return file_copy_fallback (source, destination, flags, cancellable,
2092 progress_callback, progress_callback_data,
2099 * @source: #GFile pointing to the source location.
2100 * @destination: #GFile pointing to the destination location.
2101 * @flags: set of #GFileCopyFlags.
2102 * @cancellable: optional #GCancellable object, %NULL to ignore.
2103 * @progress_callback: #GFileProgressCallback function for updates.
2104 * @progress_callback_data: gpointer to user data for the callback function.
2105 * @error: #GError for returning error conditions, or %NULL
2108 * Tries to move the file or directory @source to the location specified by @destination.
2109 * If native move operations is supported then this is used, otherwise a copy + delete
2110 * fallback is used. The native implementation may support moving directories (for instance
2111 * on moves inside the same filesystem), but the fallback code does not.
2113 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2114 * existing @destination file is overwritten.
2116 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2117 * will be copied as symlinks, otherwise the target of the
2118 * @source symlink will be copied.
2120 * If @cancellable is not %NULL, then the operation can be cancelled by
2121 * triggering the cancellable object from another thread. If the operation
2122 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2124 * If @progress_callback is not %NULL, then the operation can be monitored by
2125 * setting this to a #GFileProgressCallback function. @progress_callback_data
2126 * will be passed to this function. It is guaranteed that this callback will
2127 * be called after all data has been transferred with the total number of bytes
2128 * copied during the operation.
2130 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2131 * error is returned, independent on the status of the @destination.
2133 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2134 * error G_IO_ERROR_EXISTS is returned.
2136 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2137 * error is returned. If trying to overwrite a directory with a directory the
2138 * G_IO_ERROR_WOULD_MERGE error is returned.
2140 * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2141 * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2142 * may be returned (if the native move operation isn't available).
2144 * Returns: %TRUE on successful move, %FALSE otherwise.
2147 g_file_move (GFile *source,
2149 GFileCopyFlags flags,
2150 GCancellable *cancellable,
2151 GFileProgressCallback progress_callback,
2152 gpointer progress_callback_data,
2159 g_return_val_if_fail (G_IS_FILE (source), FALSE);
2160 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2162 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2165 if (G_OBJECT_TYPE (source) == G_OBJECT_TYPE (destination))
2167 iface = G_FILE_GET_IFACE (source);
2172 res = (* iface->move) (source, destination, flags, cancellable, progress_callback, progress_callback_data, &my_error);
2177 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2179 g_propagate_error (error, my_error);
2185 if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
2187 g_set_error (error, G_IO_ERROR,
2188 G_IO_ERROR_NOT_SUPPORTED,
2189 _("Operation not supported"));
2193 flags |= G_FILE_COPY_ALL_METADATA;
2194 if (!g_file_copy (source, destination, flags, cancellable,
2195 progress_callback, progress_callback_data,
2199 return g_file_delete (source, cancellable, error);
2203 * g_file_make_directory
2204 * @file: input #GFile.
2205 * @cancellable: optional #GCancellable object, %NULL to ignore.
2206 * @error: a #GError, or %NULL
2208 * Creates a directory.
2210 * If @cancellable is not %NULL, then the operation can be cancelled by
2211 * triggering the cancellable object from another thread. If the operation
2212 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2214 * Returns: %TRUE on successful creation, %FALSE otherwise.
2217 g_file_make_directory (GFile *file,
2218 GCancellable *cancellable,
2223 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2225 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2228 iface = G_FILE_GET_IFACE (file);
2230 if (iface->make_directory == NULL)
2232 g_set_error (error, G_IO_ERROR,
2233 G_IO_ERROR_NOT_SUPPORTED,
2234 _("Operation not supported"));
2238 return (* iface->make_directory) (file, cancellable, error);
2242 * g_file_make_symbolic_link:
2243 * @file: input #GFile.
2244 * @symlink_value: a string with the value of the new symlink.
2245 * @cancellable: optional #GCancellable object, %NULL to ignore.
2246 * @error: a #GError.
2248 * Creates a symbolic link.
2250 * If @cancellable is not %NULL, then the operation can be cancelled by
2251 * triggering the cancellable object from another thread. If the operation
2252 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2254 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
2257 g_file_make_symbolic_link (GFile *file,
2258 const char *symlink_value,
2259 GCancellable *cancellable,
2264 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2265 g_return_val_if_fail (symlink_value != NULL, FALSE);
2267 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2270 if (*symlink_value == '\0')
2272 g_set_error (error, G_IO_ERROR,
2273 G_IO_ERROR_INVALID_ARGUMENT,
2274 _("Invalid symlink value given"));
2278 iface = G_FILE_GET_IFACE (file);
2280 if (iface->make_symbolic_link == NULL)
2282 g_set_error (error, G_IO_ERROR,
2283 G_IO_ERROR_NOT_SUPPORTED,
2284 _("Operation not supported"));
2288 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
2293 * @file: input #GFile.
2294 * @cancellable: optional #GCancellable object, %NULL to ignore.
2295 * @error: a #GError, or %NULL
2299 * If @cancellable is not %NULL, then the operation can be cancelled by
2300 * triggering the cancellable object from another thread. If the operation
2301 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2303 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
2306 g_file_delete (GFile *file,
2307 GCancellable *cancellable,
2312 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2314 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2317 iface = G_FILE_GET_IFACE (file);
2319 if (iface->delete_file == NULL)
2321 g_set_error (error, G_IO_ERROR,
2322 G_IO_ERROR_NOT_SUPPORTED,
2323 _("Operation not supported"));
2327 return (* iface->delete_file) (file, cancellable, error);
2332 * @file: #GFile to send to trash.
2333 * @cancellable: optional #GCancellable object, %NULL to ignore.
2334 * @error: a #GError, or %NULL
2336 * Sends @file to the "Trashcan", if possible. This is similar to
2337 * deleting it, but the user can recover it before emptying the trashcan.
2338 * Not all file systems support trashing, so this call can return the
2339 * %G_IO_ERROR_NOT_SUPPORTED error.
2342 * If @cancellable is not %NULL, then the operation can be cancelled by
2343 * triggering the cancellable object from another thread. If the operation
2344 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2346 * Returns: %TRUE on successful trash, %FALSE otherwise.
2349 g_file_trash (GFile *file,
2350 GCancellable *cancellable,
2355 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2357 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2360 iface = G_FILE_GET_IFACE (file);
2362 if (iface->trash == NULL)
2365 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2366 _("Trash not supported"));
2370 return (* iface->trash) (file, cancellable, error);
2374 * g_file_set_display_name:
2375 * @file: input #GFile.
2376 * @display_name: a string.
2377 * @cancellable: optional #GCancellable object, %NULL to ignore.
2378 * @error: a #GError, or %NULL
2380 * Renames @file to the specified display name.
2382 * The display name is converted from UTF8 to the correct encoding for the target
2383 * filesystem if possible and the @file is renamed to this.
2385 * If you want to implement a rename operation in the user interface the edit name
2386 * (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the initial value in the rename
2387 * widget, and then the result after editing should be passed to g_file_set_display_name().
2389 * On success the resulting converted filename is returned.
2391 * If @cancellable is not %NULL, then the operation can be cancelled by
2392 * triggering the cancellable object from another thread. If the operation
2393 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2395 * Returns: a #GFile specifying what @file was renamed to, or %NULL if there was an error.
2398 g_file_set_display_name (GFile *file,
2399 const char *display_name,
2400 GCancellable *cancellable,
2405 g_return_val_if_fail (G_IS_FILE (file), NULL);
2406 g_return_val_if_fail (display_name != NULL, NULL);
2408 if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
2412 G_IO_ERROR_INVALID_ARGUMENT,
2413 _("File names cannot contain '%c'"), G_DIR_SEPARATOR);
2417 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2420 iface = G_FILE_GET_IFACE (file);
2422 return (* iface->set_display_name) (file, display_name, cancellable, error);
2426 * g_file_set_display_name_async:
2427 * @file: input #GFile.
2428 * @display_name: a string.
2429 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2431 * @cancellable: optional #GCancellable object, %NULL to ignore.
2432 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2433 * @user_data: the data to pass to callback function
2435 * Asynchronously sets the display name for a given #GFile.
2437 * For more details, see g_set_display_name() which is
2438 * the synchronous version of this call.
2440 * When the operation is finished, @callback will be called. You can then call
2441 * g_file_set_display_name_finish() to get the result of the operation.
2444 g_file_set_display_name_async (GFile *file,
2445 const char *display_name,
2447 GCancellable *cancellable,
2448 GAsyncReadyCallback callback,
2453 g_return_if_fail (G_IS_FILE (file));
2454 g_return_if_fail (display_name != NULL);
2456 iface = G_FILE_GET_IFACE (file);
2457 (* iface->set_display_name_async) (file,
2466 * g_file_set_display_name_finish:
2467 * @file: input #GFile.
2468 * @res: a #GAsyncResult.
2469 * @error: a #GError, or %NULL
2471 * Finishes setting a display name started with
2472 * g_file_set_display_name_async().
2474 * Returns: a #GFile or %NULL on error.
2477 g_file_set_display_name_finish (GFile *file,
2483 g_return_val_if_fail (G_IS_FILE (file), NULL);
2484 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2486 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2488 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2489 if (g_simple_async_result_propagate_error (simple, error))
2493 iface = G_FILE_GET_IFACE (file);
2494 return (* iface->set_display_name_finish) (file, res, error);
2498 * g_file_query_settable_attributes:
2499 * @file: input #GFile.
2500 * @cancellable: optional #GCancellable object, %NULL to ignore.
2501 * @error: a #GError, or %NULL
2503 * Obtain the list of settable attributes for the file.
2505 * Returns the type and full attribute name of all the attributes
2506 * that can be set on this file. This doesn't mean setting it will always
2507 * succeed though, you might get an access failure, or some specific
2508 * file may not support a specific attribute.
2510 * If @cancellable is not %NULL, then the operation can be cancelled by
2511 * triggering the cancellable object from another thread. If the operation
2512 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2514 * Returns: a #GFileAttributeInfoList describing the settable attributes.
2515 * When you are done with it, release it with g_file_attribute_info_list_unref()
2517 GFileAttributeInfoList *
2518 g_file_query_settable_attributes (GFile *file,
2519 GCancellable *cancellable,
2524 GFileAttributeInfoList *list;
2526 g_return_val_if_fail (G_IS_FILE (file), NULL);
2528 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2531 iface = G_FILE_GET_IFACE (file);
2533 if (iface->query_settable_attributes == NULL)
2534 return g_file_attribute_info_list_new ();
2537 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
2541 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
2543 list = g_file_attribute_info_list_new ();
2544 g_error_free (my_error);
2547 g_propagate_error (error, my_error);
2554 * g_file_query_writable_namespaces:
2555 * @file: input #GFile.
2556 * @cancellable: optional #GCancellable object, %NULL to ignore.
2557 * @error: a #GError, or %NULL
2559 * Obtain the list of attribute namespaces where new attributes
2560 * can be created by a user. An example of this is extended
2561 * attributes (in the "xattr" namespace).
2563 * If @cancellable is not %NULL, then the operation can be cancelled by
2564 * triggering the cancellable object from another thread. If the operation
2565 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2567 * Returns: a #GFileAttributeInfoList describing the writable namespaces.
2568 * When you are done with it, release it with g_file_attribute_info_list_unref()
2570 GFileAttributeInfoList *
2571 g_file_query_writable_namespaces (GFile *file,
2572 GCancellable *cancellable,
2577 GFileAttributeInfoList *list;
2579 g_return_val_if_fail (G_IS_FILE (file), NULL);
2581 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2584 iface = G_FILE_GET_IFACE (file);
2586 if (iface->query_writable_namespaces == NULL)
2587 return g_file_attribute_info_list_new ();
2590 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
2594 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
2596 list = g_file_attribute_info_list_new ();
2597 g_error_free (my_error);
2600 g_propagate_error (error, my_error);
2607 * g_file_set_attribute:
2608 * @file: input #GFile.
2609 * @attribute: a string containing the attribute's name.
2610 * @type: The type of the attribute
2611 * @value_p: a pointer to the value (or the pointer itself if the type is a pointer type)
2612 * @flags: a set of #GFileQueryInfoFlags.
2613 * @cancellable: optional #GCancellable object, %NULL to ignore.
2614 * @error: a #GError, or %NULL
2616 * Sets an attribute in the file with attribute name @attribute to @value.
2618 * If @cancellable is not %NULL, then the operation can be cancelled by
2619 * triggering the cancellable object from another thread. If the operation
2620 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2622 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
2625 g_file_set_attribute (GFile *file,
2626 const char *attribute,
2627 GFileAttributeType type,
2629 GFileQueryInfoFlags flags,
2630 GCancellable *cancellable,
2635 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2636 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
2638 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2641 iface = G_FILE_GET_IFACE (file);
2643 if (iface->set_attribute == NULL)
2645 g_set_error (error, G_IO_ERROR,
2646 G_IO_ERROR_NOT_SUPPORTED,
2647 _("Operation not supported"));
2651 return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
2655 * g_file_set_attributes_from_info:
2656 * @file: input #GFile.
2657 * @info: a #GFileInfo.
2658 * @flags: #GFileQueryInfoFlags
2659 * @cancellable: optional #GCancellable object, %NULL to ignore.
2660 * @error: a #GError, or %NULL
2662 * Tries to set all attributes in the #GFileInfo on the target values,
2663 * not stopping on the first error.
2665 * If there is any error during this operation then @error will be set to
2666 * the first error. Error on particular fields are flagged by setting
2667 * the "status" field in the attribute value to
2668 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can also detect
2671 * If @cancellable is not %NULL, then the operation can be cancelled by
2672 * triggering the cancellable object from another thread. If the operation
2673 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2675 * Returns: %TRUE if there was any error, %FALSE otherwise.
2678 g_file_set_attributes_from_info (GFile *file,
2680 GFileQueryInfoFlags flags,
2681 GCancellable *cancellable,
2686 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2687 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
2689 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2692 g_file_info_clear_status (info);
2694 iface = G_FILE_GET_IFACE (file);
2696 return (* iface->set_attributes_from_info) (file,
2705 g_file_real_set_attributes_from_info (GFile *file,
2707 GFileQueryInfoFlags flags,
2708 GCancellable *cancellable,
2714 GFileAttributeValue *value;
2718 attributes = g_file_info_list_attributes (info, NULL);
2720 for (i = 0; attributes[i] != NULL; i++)
2722 value = _g_file_info_get_attribute_value (info, attributes[i]);
2724 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
2727 if (!g_file_set_attribute (file, attributes[i],
2728 value->type, _g_file_attribute_value_peek_as_pointer (value),
2729 flags, cancellable, error))
2731 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2733 /* Don't set error multiple times */
2737 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2740 g_strfreev (attributes);
2746 * g_file_set_attributes_async:
2747 * @file: input #GFile.
2748 * @info: a #GFileInfo.
2749 * @flags: a #GFileQueryInfoFlags.
2750 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2752 * @cancellable: optional #GCancellable object, %NULL to ignore.
2753 * @callback: a #GAsyncReadyCallback.
2754 * @user_data: a #gpointer.
2756 * Asynchronously sets the attributes of @file with @info.
2758 * For more details, see g_file_set_attributes_from_info() which is
2759 * the synchronous version of this call.
2761 * When the operation is finished, @callback will be called. You can then call
2762 * g_file_set_attributes_finish() to get the result of the operation.
2765 g_file_set_attributes_async (GFile *file,
2767 GFileQueryInfoFlags flags,
2769 GCancellable *cancellable,
2770 GAsyncReadyCallback callback,
2775 g_return_if_fail (G_IS_FILE (file));
2776 g_return_if_fail (G_IS_FILE_INFO (info));
2778 iface = G_FILE_GET_IFACE (file);
2779 (* iface->set_attributes_async) (file,
2789 * g_file_set_attributes_finish:
2790 * @file: input #GFile.
2791 * @result: a #GAsyncResult.
2792 * @info: a #GFileInfo.
2793 * @error: a #GError, or %NULL
2795 * Finishes setting an attribute started in g_file_set_attributes_async().
2797 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
2800 g_file_set_attributes_finish (GFile *file,
2801 GAsyncResult *result,
2807 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2808 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
2810 /* No standard handling of errors here, as we must set info even
2813 iface = G_FILE_GET_IFACE (file);
2814 return (* iface->set_attributes_finish) (file, result, info, error);
2818 * g_file_set_attribute_string:
2819 * @file: input #GFile.
2820 * @attribute: a string containing the attribute's name.
2821 * @value: a string containing the attribute's value.
2822 * @flags: #GFileQueryInfoFlags.
2823 * @cancellable: optional #GCancellable object, %NULL to ignore.
2824 * @error: a #GError, or %NULL
2826 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
2827 * If @attribute is of a different type, this operation will fail.
2829 * If @cancellable is not %NULL, then the operation can be cancelled by
2830 * triggering the cancellable object from another thread. If the operation
2831 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2833 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
2836 g_file_set_attribute_string (GFile *file,
2837 const char *attribute,
2839 GFileQueryInfoFlags flags,
2840 GCancellable *cancellable,
2843 return g_file_set_attribute (file, attribute,
2844 G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
2845 flags, cancellable, error);
2849 * g_file_set_attribute_byte_string:
2850 * @file: input #GFile.
2851 * @attribute: a string containing the attribute's name.
2852 * @value: a string containing the attribute's new value.
2853 * @flags: a #GFileQueryInfoFlags.
2854 * @cancellable: optional #GCancellable object, %NULL to ignore.
2855 * @error: a #GError, or %NULL
2857 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
2858 * If @attribute is of a different type, this operation will fail,
2861 * If @cancellable is not %NULL, then the operation can be cancelled by
2862 * triggering the cancellable object from another thread. If the operation
2863 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2865 * Returns: %TRUE if the @attribute was successfully set to @value
2866 * in the @file, %FALSE otherwise.
2869 g_file_set_attribute_byte_string (GFile *file,
2870 const char *attribute,
2872 GFileQueryInfoFlags flags,
2873 GCancellable *cancellable,
2876 return g_file_set_attribute (file, attribute,
2877 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
2878 flags, cancellable, error);
2882 * g_file_set_attribute_uint32:
2883 * @file: input #GFile.
2884 * @attribute: a string containing the attribute's name.
2885 * @value: a #guint32 containing the attribute's new value.
2886 * @flags: a #GFileQueryInfoFlags.
2887 * @cancellable: optional #GCancellable object, %NULL to ignore.
2888 * @error: a #GError, or %NULL
2890 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
2891 * If @attribute is of a different type, this operation will fail.
2893 * If @cancellable is not %NULL, then the operation can be cancelled by
2894 * triggering the cancellable object from another thread. If the operation
2895 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2897 * Returns: %TRUE if the @attribute was successfully set to @value
2898 * in the @file, %FALSE otherwise.
2901 g_file_set_attribute_uint32 (GFile *file,
2902 const char *attribute,
2904 GFileQueryInfoFlags flags,
2905 GCancellable *cancellable,
2908 return g_file_set_attribute (file, attribute,
2909 G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
2910 flags, cancellable, error);
2914 * g_file_set_attribute_int32:
2915 * @file: input #GFile.
2916 * @attribute: a string containing the attribute's name.
2917 * @value: a #gint32 containing the attribute's new value.
2918 * @flags: a #GFileQueryInfoFlags.
2919 * @cancellable: optional #GCancellable object, %NULL to ignore.
2920 * @error: a #GError, or %NULL
2922 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
2923 * If @attribute is of a different type, this operation will fail.
2925 * If @cancellable is not %NULL, then the operation can be cancelled by
2926 * triggering the cancellable object from another thread. If the operation
2927 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2929 * Returns: %TRUE if the @attribute was successfully set to @value
2930 * in the @file, %FALSE otherwise.
2933 g_file_set_attribute_int32 (GFile *file,
2934 const char *attribute,
2936 GFileQueryInfoFlags flags,
2937 GCancellable *cancellable,
2940 return g_file_set_attribute (file, attribute,
2941 G_FILE_ATTRIBUTE_TYPE_INT32, &value,
2942 flags, cancellable, error);
2946 * g_file_set_attribute_uint64:
2947 * @file: input #GFile.
2948 * @attribute: a string containing the attribute's name.
2949 * @value: a #guint64 containing the attribute's new value.
2950 * @flags: a #GFileQueryInfoFlags.
2951 * @cancellable: optional #GCancellable object, %NULL to ignore.
2952 * @error: a #GError, or %NULL
2954 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
2955 * If @attribute is of a different type, this operation will fail.
2957 * If @cancellable is not %NULL, then the operation can be cancelled by
2958 * triggering the cancellable object from another thread. If the operation
2959 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2961 * Returns: %TRUE if the @attribute was successfully set to @value
2962 * in the @file, %FALSE otherwise.
2965 g_file_set_attribute_uint64 (GFile *file,
2966 const char *attribute,
2968 GFileQueryInfoFlags flags,
2969 GCancellable *cancellable,
2972 return g_file_set_attribute (file, attribute,
2973 G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
2974 flags, cancellable, error);
2978 * g_file_set_attribute_int64:
2979 * @file: input #GFile.
2980 * @attribute: a string containing the attribute's name.
2981 * @value: a #guint64 containing the attribute's new value.
2982 * @flags: a #GFileQueryInfoFlags.
2983 * @cancellable: optional #GCancellable object, %NULL to ignore.
2984 * @error: a #GError, or %NULL
2986 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
2987 * If @attribute is of a different type, this operation will fail.
2989 * If @cancellable is not %NULL, then the operation can be cancelled by
2990 * triggering the cancellable object from another thread. If the operation
2991 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2993 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
2996 g_file_set_attribute_int64 (GFile *file,
2997 const char *attribute,
2999 GFileQueryInfoFlags flags,
3000 GCancellable *cancellable,
3003 return g_file_set_attribute (file, attribute,
3004 G_FILE_ATTRIBUTE_TYPE_INT64, &value,
3005 flags, cancellable, error);
3009 * g_file_mount_mountable:
3010 * @file: input #GFile.
3011 * @mount_operation: a #GMountOperation, or %NULL to avoid user interaction.
3012 * @cancellable: optional #GCancellable object, %NULL to ignore.
3013 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3014 * @user_data: the data to pass to callback function
3016 * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
3017 * Using @mount_operation, you can request callbacks when, for instance,
3018 * passwords are needed during authentication.
3020 * If @cancellable is not %NULL, then the operation can be cancelled by
3021 * triggering the cancellable object from another thread. If the operation
3022 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3024 * When the operation is finished, @callback will be called. You can then call
3025 * g_file_mount_mountable_finish() to get the result of the operation.
3028 g_file_mount_mountable (GFile *file,
3029 GMountOperation *mount_operation,
3030 GCancellable *cancellable,
3031 GAsyncReadyCallback callback,
3036 g_return_if_fail (G_IS_FILE (file));
3038 iface = G_FILE_GET_IFACE (file);
3040 if (iface->mount_mountable == NULL)
3041 g_simple_async_report_error_in_idle (G_OBJECT (file),
3045 G_IO_ERROR_NOT_SUPPORTED,
3046 _("Operation not supported"));
3048 (* iface->mount_mountable) (file,
3056 * g_file_mount_mountable_finish:
3057 * @file: input #GFile.
3058 * @result: a #GAsyncResult.
3059 * @error: a #GError, or %NULL
3061 * Finishes a mount operation. See g_file_mount_mountable() for details.
3063 * Finish an asynchronous mount operation that was started
3064 * with g_file_mount_mountable().
3066 * Returns: a #GFile or %NULL on error.
3069 g_file_mount_mountable_finish (GFile *file,
3070 GAsyncResult *result,
3075 g_return_val_if_fail (G_IS_FILE (file), NULL);
3076 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
3078 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3080 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3081 if (g_simple_async_result_propagate_error (simple, error))
3085 iface = G_FILE_GET_IFACE (file);
3086 return (* iface->mount_mountable_finish) (file, result, error);
3090 * g_file_unmount_mountable:
3091 * @file: input #GFile.
3092 * @flags: flags affecting the operation
3093 * @cancellable: optional #GCancellable object, %NULL to ignore.
3094 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3095 * @user_data: the data to pass to callback function
3097 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
3099 * If @cancellable is not %NULL, then the operation can be cancelled by
3100 * triggering the cancellable object from another thread. If the operation
3101 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3103 * When the operation is finished, @callback will be called. You can then call
3104 * g_file_unmount_mountable_finish() to get the result of the operation.
3107 g_file_unmount_mountable (GFile *file,
3108 GMountUnmountFlags flags,
3109 GCancellable *cancellable,
3110 GAsyncReadyCallback callback,
3115 g_return_if_fail (G_IS_FILE (file));
3117 iface = G_FILE_GET_IFACE (file);
3119 if (iface->unmount_mountable == NULL)
3120 g_simple_async_report_error_in_idle (G_OBJECT (file),
3124 G_IO_ERROR_NOT_SUPPORTED,
3125 _("Operation not supported"));
3127 (* iface->unmount_mountable) (file,
3135 * g_file_unmount_mountable_finish:
3136 * @file: input #GFile.
3137 * @result: a #GAsyncResult.
3138 * @error: a #GError, or %NULL
3140 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
3142 * Finish an asynchronous unmount operation that was started
3143 * with g_file_unmount_mountable().
3145 * Returns: %TRUE if the operation finished successfully. %FALSE
3149 g_file_unmount_mountable_finish (GFile *file,
3150 GAsyncResult *result,
3155 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3156 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3158 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3160 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3161 if (g_simple_async_result_propagate_error (simple, error))
3165 iface = G_FILE_GET_IFACE (file);
3166 return (* iface->unmount_mountable_finish) (file, result, error);
3170 * g_file_eject_mountable:
3171 * @file: input #GFile.
3172 * @flags: flags affecting the operation
3173 * @cancellable: optional #GCancellable object, %NULL to ignore.
3174 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3175 * @user_data: the data to pass to callback function
3177 * Starts an asynchronous eject on a mountable.
3178 * When this operation has completed, @callback will be called with
3179 * @user_user data, and the operation can be finalized with
3180 * g_file_eject_mountable_finish().
3182 * If @cancellable is not %NULL, then the operation can be cancelled by
3183 * triggering the cancellable object from another thread. If the operation
3184 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3187 g_file_eject_mountable (GFile *file,
3188 GMountUnmountFlags flags,
3189 GCancellable *cancellable,
3190 GAsyncReadyCallback callback,
3195 g_return_if_fail (G_IS_FILE (file));
3197 iface = G_FILE_GET_IFACE (file);
3199 if (iface->eject_mountable == NULL)
3200 g_simple_async_report_error_in_idle (G_OBJECT (file),
3204 G_IO_ERROR_NOT_SUPPORTED,
3205 _("Operation not supported"));
3207 (* iface->eject_mountable) (file,
3215 * g_file_eject_mountable_finish:
3216 * @file: input #GFile.
3217 * @result: a #GAsyncResult.
3218 * @error: a #GError, or %NULL
3220 * Finishes an asynchronous eject operation started by
3221 * g_file_eject_mountable().
3223 * Returns: %TRUE if the @file was ejected successfully. %FALSE
3227 g_file_eject_mountable_finish (GFile *file,
3228 GAsyncResult *result,
3233 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3234 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3236 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3238 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3239 if (g_simple_async_result_propagate_error (simple, error))
3243 iface = G_FILE_GET_IFACE (file);
3244 return (* iface->eject_mountable_finish) (file, result, error);
3248 * g_file_monitor_directory:
3249 * @file: input #GFile.
3250 * @flags: a set of #GFileMonitorFlags.
3251 * @cancellable: optional #GCancellable object, %NULL to ignore.
3252 * @error: a #GError, or %NULL.
3254 * Obtains a directory monitor for the given file.
3255 * This may fail if directory monitoring is not supported.
3257 * If @cancellable is not %NULL, then the operation can be cancelled by
3258 * triggering the cancellable object from another thread. If the operation
3259 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3261 * Returns: a #GFileMonitor for the given @file,
3262 * or %NULL on error.
3265 g_file_monitor_directory (GFile *file,
3266 GFileMonitorFlags flags,
3267 GCancellable *cancellable,
3272 g_return_val_if_fail (G_IS_FILE (file), NULL);
3274 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3277 iface = G_FILE_GET_IFACE (file);
3279 if (iface->monitor_dir == NULL)
3281 g_set_error (error, G_IO_ERROR,
3282 G_IO_ERROR_NOT_SUPPORTED,
3283 _("Operation not supported"));
3287 return (* iface->monitor_dir) (file, flags, cancellable, error);
3291 * g_file_monitor_file:
3292 * @file: input #GFile.
3293 * @flags: a set of #GFileMonitorFlags.
3294 * @cancellable: optional #GCancellable object, %NULL to ignore.
3295 * @error: a #GError, or %NULL.
3297 * Obtains a file monitor for the given file. If no file notification
3298 * mechanism exists, then regular polling of the file is used.
3300 * If @cancellable is not %NULL, then the operation can be cancelled by
3301 * triggering the cancellable object from another thread. If the operation
3302 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3304 * Returns: a #GFileMonitor for the given @file.
3307 g_file_monitor_file (GFile *file,
3308 GFileMonitorFlags flags,
3309 GCancellable *cancellable,
3313 GFileMonitor *monitor;
3315 g_return_val_if_fail (G_IS_FILE (file), NULL);
3317 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3320 iface = G_FILE_GET_IFACE (file);
3324 if (iface->monitor_file)
3325 monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
3327 /* Fallback to polling */
3328 if (monitor == NULL)
3329 monitor = _g_poll_file_monitor_new (file);
3334 /********************************************
3335 * Default implementation of async ops *
3336 ********************************************/
3340 GFileQueryInfoFlags flags;
3342 } QueryInfoAsyncData;
3345 query_info_data_free (QueryInfoAsyncData *data)
3348 g_object_unref (data->info);
3349 g_free (data->attributes);
3354 query_info_async_thread (GSimpleAsyncResult *res,
3356 GCancellable *cancellable)
3358 GError *error = NULL;
3359 QueryInfoAsyncData *data;
3362 data = g_simple_async_result_get_op_res_gpointer (res);
3364 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3368 g_simple_async_result_set_from_error (res, error);
3369 g_error_free (error);
3376 g_file_real_query_info_async (GFile *file,
3377 const char *attributes,
3378 GFileQueryInfoFlags flags,
3380 GCancellable *cancellable,
3381 GAsyncReadyCallback callback,
3384 GSimpleAsyncResult *res;
3385 QueryInfoAsyncData *data;
3387 data = g_new0 (QueryInfoAsyncData, 1);
3388 data->attributes = g_strdup (attributes);
3389 data->flags = flags;
3391 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_info_async);
3392 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
3394 g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
3395 g_object_unref (res);
3399 g_file_real_query_info_finish (GFile *file,
3403 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3404 QueryInfoAsyncData *data;
3406 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_info_async);
3408 data = g_simple_async_result_get_op_res_gpointer (simple);
3410 return g_object_ref (data->info);
3417 GFileQueryInfoFlags flags;
3418 GFileEnumerator *enumerator;
3419 } EnumerateChildrenAsyncData;
3422 enumerate_children_data_free (EnumerateChildrenAsyncData *data)
3424 if (data->enumerator)
3425 g_object_unref (data->enumerator);
3426 g_free (data->attributes);
3431 enumerate_children_async_thread (GSimpleAsyncResult *res,
3433 GCancellable *cancellable)
3435 GError *error = NULL;
3436 EnumerateChildrenAsyncData *data;
3437 GFileEnumerator *enumerator;
3439 data = g_simple_async_result_get_op_res_gpointer (res);
3441 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3443 if (enumerator == NULL)
3445 g_simple_async_result_set_from_error (res, error);
3446 g_error_free (error);
3449 data->enumerator = enumerator;
3453 g_file_real_enumerate_children_async (GFile *file,
3454 const char *attributes,
3455 GFileQueryInfoFlags flags,
3457 GCancellable *cancellable,
3458 GAsyncReadyCallback callback,
3461 GSimpleAsyncResult *res;
3462 EnumerateChildrenAsyncData *data;
3464 data = g_new0 (EnumerateChildrenAsyncData, 1);
3465 data->attributes = g_strdup (attributes);
3466 data->flags = flags;
3468 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_enumerate_children_async);
3469 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)enumerate_children_data_free);
3471 g_simple_async_result_run_in_thread (res, enumerate_children_async_thread, io_priority, cancellable);
3472 g_object_unref (res);
3475 static GFileEnumerator *
3476 g_file_real_enumerate_children_finish (GFile *file,
3480 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3481 EnumerateChildrenAsyncData *data;
3483 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_enumerate_children_async);
3485 data = g_simple_async_result_get_op_res_gpointer (simple);
3486 if (data->enumerator)
3487 return g_object_ref (data->enumerator);
3493 open_read_async_thread (GSimpleAsyncResult *res,
3495 GCancellable *cancellable)
3498 GFileInputStream *stream;
3499 GError *error = NULL;
3501 iface = G_FILE_GET_IFACE (object);
3503 stream = iface->read_fn (G_FILE (object), cancellable, &error);
3507 g_simple_async_result_set_from_error (res, error);
3508 g_error_free (error);
3511 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3515 g_file_real_read_async (GFile *file,
3517 GCancellable *cancellable,
3518 GAsyncReadyCallback callback,
3521 GSimpleAsyncResult *res;
3523 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_read_async);
3525 g_simple_async_result_run_in_thread (res, open_read_async_thread, io_priority, cancellable);
3526 g_object_unref (res);
3529 static GFileInputStream *
3530 g_file_real_read_finish (GFile *file,
3534 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3537 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_read_async);
3539 op = g_simple_async_result_get_op_res_gpointer (simple);
3541 return g_object_ref (op);
3547 append_to_async_thread (GSimpleAsyncResult *res,
3549 GCancellable *cancellable)
3552 GFileCreateFlags *data;
3553 GFileOutputStream *stream;
3554 GError *error = NULL;
3556 iface = G_FILE_GET_IFACE (object);
3558 data = g_simple_async_result_get_op_res_gpointer (res);
3560 stream = iface->append_to (G_FILE (object), *data, cancellable, &error);
3564 g_simple_async_result_set_from_error (res, error);
3565 g_error_free (error);
3568 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3572 g_file_real_append_to_async (GFile *file,
3573 GFileCreateFlags flags,
3575 GCancellable *cancellable,
3576 GAsyncReadyCallback callback,
3579 GFileCreateFlags *data;
3580 GSimpleAsyncResult *res;
3582 data = g_new0 (GFileCreateFlags, 1);
3585 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_append_to_async);
3586 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
3588 g_simple_async_result_run_in_thread (res, append_to_async_thread, io_priority, cancellable);
3589 g_object_unref (res);
3592 static GFileOutputStream *
3593 g_file_real_append_to_finish (GFile *file,
3597 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3600 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_append_to_async);
3602 op = g_simple_async_result_get_op_res_gpointer (simple);
3604 return g_object_ref (op);
3610 create_async_thread (GSimpleAsyncResult *res,
3612 GCancellable *cancellable)
3615 GFileCreateFlags *data;
3616 GFileOutputStream *stream;
3617 GError *error = NULL;
3619 iface = G_FILE_GET_IFACE (object);
3621 data = g_simple_async_result_get_op_res_gpointer (res);
3623 stream = iface->create (G_FILE (object), *data, cancellable, &error);
3627 g_simple_async_result_set_from_error (res, error);
3628 g_error_free (error);
3631 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3635 g_file_real_create_async (GFile *file,
3636 GFileCreateFlags flags,
3638 GCancellable *cancellable,
3639 GAsyncReadyCallback callback,
3642 GFileCreateFlags *data;
3643 GSimpleAsyncResult *res;
3645 data = g_new0 (GFileCreateFlags, 1);
3648 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_create_async);
3649 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
3651 g_simple_async_result_run_in_thread (res, create_async_thread, io_priority, cancellable);
3652 g_object_unref (res);
3655 static GFileOutputStream *
3656 g_file_real_create_finish (GFile *file,
3660 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3663 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_create_async);
3665 op = g_simple_async_result_get_op_res_gpointer (simple);
3667 return g_object_ref (op);
3673 GFileOutputStream *stream;
3675 gboolean make_backup;
3676 GFileCreateFlags flags;
3680 replace_async_data_free (ReplaceAsyncData *data)
3683 g_object_unref (data->stream);
3684 g_free (data->etag);
3689 replace_async_thread (GSimpleAsyncResult *res,
3691 GCancellable *cancellable)
3694 GFileOutputStream *stream;
3695 GError *error = NULL;
3696 ReplaceAsyncData *data;
3698 iface = G_FILE_GET_IFACE (object);
3700 data = g_simple_async_result_get_op_res_gpointer (res);
3702 stream = iface->replace (G_FILE (object),
3711 g_simple_async_result_set_from_error (res, error);
3712 g_error_free (error);
3715 data->stream = stream;
3719 g_file_real_replace_async (GFile *file,
3721 gboolean make_backup,
3722 GFileCreateFlags flags,
3724 GCancellable *cancellable,
3725 GAsyncReadyCallback callback,
3728 GSimpleAsyncResult *res;
3729 ReplaceAsyncData *data;
3731 data = g_new0 (ReplaceAsyncData, 1);
3732 data->etag = g_strdup (etag);
3733 data->make_backup = make_backup;
3734 data->flags = flags;
3736 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_replace_async);
3737 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_async_data_free);
3739 g_simple_async_result_run_in_thread (res, replace_async_thread, io_priority, cancellable);
3740 g_object_unref (res);
3743 static GFileOutputStream *
3744 g_file_real_replace_finish (GFile *file,
3748 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3749 ReplaceAsyncData *data;
3751 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_replace_async);
3753 data = g_simple_async_result_get_op_res_gpointer (simple);
3755 return g_object_ref (data->stream);
3763 } SetDisplayNameAsyncData;
3766 set_display_name_data_free (SetDisplayNameAsyncData *data)
3768 g_free (data->name);
3770 g_object_unref (data->file);
3775 set_display_name_async_thread (GSimpleAsyncResult *res,
3777 GCancellable *cancellable)
3779 GError *error = NULL;
3780 SetDisplayNameAsyncData *data;
3783 data = g_simple_async_result_get_op_res_gpointer (res);
3785 file = g_file_set_display_name (G_FILE (object), data->name, cancellable, &error);
3789 g_simple_async_result_set_from_error (res, error);
3790 g_error_free (error);
3797 g_file_real_set_display_name_async (GFile *file,
3798 const char *display_name,
3800 GCancellable *cancellable,
3801 GAsyncReadyCallback callback,
3804 GSimpleAsyncResult *res;
3805 SetDisplayNameAsyncData *data;
3807 data = g_new0 (SetDisplayNameAsyncData, 1);
3808 data->name = g_strdup (display_name);
3810 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_display_name_async);
3811 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_display_name_data_free);
3813 g_simple_async_result_run_in_thread (res, set_display_name_async_thread, io_priority, cancellable);
3814 g_object_unref (res);
3818 g_file_real_set_display_name_finish (GFile *file,
3822 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3823 SetDisplayNameAsyncData *data;
3825 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_display_name_async);
3827 data = g_simple_async_result_get_op_res_gpointer (simple);
3829 return g_object_ref (data->file);
3835 GFileQueryInfoFlags flags;
3842 set_info_data_free (SetInfoAsyncData *data)
3845 g_object_unref (data->info);
3847 g_error_free (data->error);
3852 set_info_async_thread (GSimpleAsyncResult *res,
3854 GCancellable *cancellable)
3856 SetInfoAsyncData *data;
3858 data = g_simple_async_result_get_op_res_gpointer (res);
3861 data->res = g_file_set_attributes_from_info (G_FILE (object),
3869 g_file_real_set_attributes_async (GFile *file,
3871 GFileQueryInfoFlags flags,
3873 GCancellable *cancellable,
3874 GAsyncReadyCallback callback,
3877 GSimpleAsyncResult *res;
3878 SetInfoAsyncData *data;
3880 data = g_new0 (SetInfoAsyncData, 1);
3881 data->info = g_file_info_dup (info);
3882 data->flags = flags;
3884 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_attributes_async);
3885 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_info_data_free);
3887 g_simple_async_result_run_in_thread (res, set_info_async_thread, io_priority, cancellable);
3888 g_object_unref (res);
3892 g_file_real_set_attributes_finish (GFile *file,
3897 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3898 SetInfoAsyncData *data;
3900 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_attributes_async);
3902 data = g_simple_async_result_get_op_res_gpointer (simple);
3905 *info = g_object_ref (data->info);
3907 if (error != NULL && data->error)
3908 *error = g_error_copy (data->error);
3913 /********************************************
3914 * Default VFS operations *
3915 ********************************************/
3918 * g_file_new_for_path:
3919 * @path: a string containing a relative or absolute path.
3921 * Constructs a #GFile for a given path. This operation never
3922 * fails, but the returned object might not support any I/O
3923 * operation if @path is malformed.
3925 * Returns: a new #GFile for the given @path.
3928 g_file_new_for_path (const char *path)
3930 g_return_val_if_fail (path != NULL, NULL);
3932 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
3936 * g_file_new_for_uri:
3937 * @uri: a string containing a URI.
3939 * Constructs a #GFile for a given URI. This operation never
3940 * fails, but the returned object might not support any I/O
3941 * operation if @uri is malformed or if the uri type is
3944 * Returns: a #GFile for the given @uri.
3947 g_file_new_for_uri (const char *uri)
3949 g_return_val_if_fail (uri != NULL, NULL);
3951 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
3955 * g_file_parse_name:
3956 * @parse_name: a file name or path to be parsed.
3958 * Constructs a #GFile with the given @parse_name (i.e. something given by g_file_get_parse_name()).
3959 * This operation never fails, but the returned object might not support any I/O
3960 * operation if the @parse_name cannot be parsed.
3962 * Returns: a new #GFile.
3965 g_file_parse_name (const char *parse_name)
3967 g_return_val_if_fail (parse_name != NULL, NULL);
3969 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
3973 is_valid_scheme_character (char c)
3975 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
3979 has_valid_scheme (const char *uri)
3985 if (!is_valid_scheme_character (*p))
3990 } while (is_valid_scheme_character (*p));
3996 * g_file_new_for_commandline_arg:
3997 * @arg: a command line string.
3999 * Creates a #GFile with the given argument from the command line. The value of
4000 * @arg can be either a URI, an absolute path or a relative path resolved
4001 * relative to the current working directory.
4002 * This operation never fails, but the returned object might not support any
4003 * I/O operation if @arg points to a malformed path.
4005 * Returns: a new #GFile.
4008 g_file_new_for_commandline_arg (const char *arg)
4014 g_return_val_if_fail (arg != NULL, NULL);
4016 if (g_path_is_absolute (arg))
4017 return g_file_new_for_path (arg);
4019 if (has_valid_scheme (arg))
4020 return g_file_new_for_uri (arg);
4022 current_dir = g_get_current_dir ();
4023 filename = g_build_filename (current_dir, arg, NULL);
4024 g_free (current_dir);
4026 file = g_file_new_for_path (filename);
4033 * g_file_mount_enclosing_volume:
4034 * @location: input #GFile.
4035 * @mount_operation: a #GMountOperation or %NULL to avoid user interaction.
4036 * @cancellable: optional #GCancellable object, %NULL to ignore.
4037 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4038 * @user_data: the data to pass to callback function
4040 * Starts a @mount_operation, mounting the volume that contains the file @location.
4042 * When this operation has completed, @callback will be called with
4043 * @user_user data, and the operation can be finalized with
4044 * g_file_mount_enclosing_volume_finish().
4046 * If @cancellable is not %NULL, then the operation can be cancelled by
4047 * triggering the cancellable object from another thread. If the operation
4048 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4051 g_file_mount_enclosing_volume (GFile *location,
4052 GMountOperation *mount_operation,
4053 GCancellable *cancellable,
4054 GAsyncReadyCallback callback,
4059 g_return_if_fail (G_IS_FILE (location));
4061 iface = G_FILE_GET_IFACE (location);
4063 if (iface->mount_enclosing_volume == NULL)
4065 g_simple_async_report_error_in_idle (G_OBJECT (location),
4066 callback, user_data,
4067 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4068 _("volume doesn't implement mount"));
4073 (* iface->mount_enclosing_volume) (location, mount_operation, cancellable, callback, user_data);
4078 * g_file_mount_enclosing_volume_finish:
4079 * @location: input #GFile.
4080 * @result: a #GAsyncResult.
4081 * @error: a #GError, or %NULL
4083 * Finishes a mount operation started by g_file_mount_enclosing_volume().
4085 * Returns: %TRUE if successful. If an error
4086 * has occurred, this function will return %FALSE and set @error
4087 * appropriately if present.
4090 g_file_mount_enclosing_volume_finish (GFile *location,
4091 GAsyncResult *result,
4096 g_return_val_if_fail (G_IS_FILE (location), FALSE);
4097 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4099 if (G_IS_SIMPLE_ASYNC_RESULT (result))
4101 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
4102 if (g_simple_async_result_propagate_error (simple, error))
4106 iface = G_FILE_GET_IFACE (location);
4108 return (* iface->mount_enclosing_volume_finish) (location, result, error);
4111 /********************************************
4112 * Utility functions *
4113 ********************************************/
4115 #define GET_CONTENT_BLOCK_SIZE 8192
4118 * g_file_load_contents:
4119 * @file: input #GFile.
4120 * @cancellable: optional #GCancellable object, %NULL to ignore.
4121 * @contents: a location to place the contents of the file.
4122 * @length: a location to place the length of the contents of the file.
4123 * @etag_out: a location to place the current entity tag for the file.
4124 * @error: a #GError, or %NULL
4126 * Loads the content of the file into memory, returning the size of
4127 * the data. The data is always zero terminated, but this is not
4128 * included in the resultant @length.
4130 * If @cancellable is not %NULL, then the operation can be cancelled by
4131 * triggering the cancellable object from another thread. If the operation
4132 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4134 * Returns: %TRUE if the @file's contents were successfully loaded.
4135 * %FALSE if there were errors..
4138 g_file_load_contents (GFile *file,
4139 GCancellable *cancellable,
4145 GFileInputStream *in;
4146 GByteArray *content;
4151 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4152 g_return_val_if_fail (contents != NULL, FALSE);
4154 in = g_file_read (file, cancellable, error);
4158 content = g_byte_array_new ();
4161 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
4162 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
4163 content->data + pos,
4164 GET_CONTENT_BLOCK_SIZE,
4165 cancellable, error)) > 0)
4168 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
4175 info = g_file_input_stream_query_info (in,
4176 G_FILE_ATTRIBUTE_ETAG_VALUE,
4181 *etag_out = g_strdup (g_file_info_get_etag (info));
4182 g_object_unref (info);
4186 /* Ignore errors on close */
4187 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
4188 g_object_unref (in);
4192 /* error is set already */
4193 g_byte_array_free (content, TRUE);
4200 /* Zero terminate (we got an extra byte allocated for this */
4201 content->data[pos] = 0;
4203 *contents = (char *)g_byte_array_free (content, FALSE);
4211 GCancellable *cancellable;
4212 GFileReadMoreCallback read_more_callback;
4213 GAsyncReadyCallback callback;
4215 GByteArray *content;
4222 load_contents_data_free (LoadContentsData *data)
4225 g_error_free (data->error);
4226 if (data->cancellable)
4227 g_object_unref (data->cancellable);
4229 g_byte_array_free (data->content, TRUE);
4230 g_free (data->etag);
4231 g_object_unref (data->file);
4236 load_contents_close_callback (GObject *obj,
4237 GAsyncResult *close_res,
4240 GInputStream *stream = G_INPUT_STREAM (obj);
4241 LoadContentsData *data = user_data;
4242 GSimpleAsyncResult *res;
4244 /* Ignore errors here, we're only reading anyway */
4245 g_input_stream_close_finish (stream, close_res, NULL);
4246 g_object_unref (stream);
4248 res = g_simple_async_result_new (G_OBJECT (data->file),
4251 g_file_load_contents_async);
4252 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)load_contents_data_free);
4253 g_simple_async_result_complete (res);
4254 g_object_unref (res);
4258 load_contents_fstat_callback (GObject *obj,
4259 GAsyncResult *stat_res,
4262 GInputStream *stream = G_INPUT_STREAM (obj);
4263 LoadContentsData *data = user_data;
4266 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
4270 data->etag = g_strdup (g_file_info_get_etag (info));
4271 g_object_unref (info);
4274 g_input_stream_close_async (stream, 0,
4276 load_contents_close_callback, data);
4280 load_contents_read_callback (GObject *obj,
4281 GAsyncResult *read_res,
4284 GInputStream *stream = G_INPUT_STREAM (obj);
4285 LoadContentsData *data = user_data;
4286 GError *error = NULL;
4289 read_size = g_input_stream_read_finish (stream, read_res, &error);
4293 /* Error or EOF, close the file */
4294 data->error = error;
4295 g_input_stream_close_async (stream, 0,
4297 load_contents_close_callback, data);
4299 else if (read_size == 0)
4301 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
4302 G_FILE_ATTRIBUTE_ETAG_VALUE,
4305 load_contents_fstat_callback,
4308 else if (read_size > 0)
4310 data->pos += read_size;
4312 g_byte_array_set_size (data->content,
4313 data->pos + GET_CONTENT_BLOCK_SIZE);
4316 if (data->read_more_callback &&
4317 !data->read_more_callback ((char *)data->content->data, data->pos, data->user_data))
4318 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
4319 G_FILE_ATTRIBUTE_ETAG_VALUE,
4322 load_contents_fstat_callback,
4325 g_input_stream_read_async (stream,
4326 data->content->data + data->pos,
4327 GET_CONTENT_BLOCK_SIZE,
4330 load_contents_read_callback,
4336 load_contents_open_callback (GObject *obj,
4337 GAsyncResult *open_res,
4340 GFile *file = G_FILE (obj);
4341 GFileInputStream *stream;
4342 LoadContentsData *data = user_data;
4343 GError *error = NULL;
4344 GSimpleAsyncResult *res;
4346 stream = g_file_read_finish (file, open_res, &error);
4350 g_byte_array_set_size (data->content,
4351 data->pos + GET_CONTENT_BLOCK_SIZE);
4352 g_input_stream_read_async (G_INPUT_STREAM (stream),
4353 data->content->data + data->pos,
4354 GET_CONTENT_BLOCK_SIZE,
4357 load_contents_read_callback,
4363 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
4367 g_simple_async_result_complete (res);
4368 g_error_free (error);
4369 load_contents_data_free (data);
4370 g_object_unref (res);
4375 * g_file_load_partial_contents_async:
4376 * @file: input #GFile.
4377 * @cancellable: optional #GCancellable object, %NULL to ignore.
4378 * @read_more_callback: a #GFileReadMoreCallback to receive partial data and to specify whether further data should be read.
4379 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
4380 * @user_data: the data to pass to the callback functions.
4382 * Reads the partial contents of a file. A #GFileReadMoreCallback should be
4383 * used to stop reading from the file when appropriate, else this function
4384 * will behave exactly as g_file_load_contents_async(). This operation
4385 * can be finished by g_file_load_partial_contents_finish().
4387 * Users of this function should be aware that @user_data is passed to
4388 * both the @read_more_callback and the @callback.
4390 * If @cancellable is not %NULL, then the operation can be cancelled by
4391 * triggering the cancellable object from another thread. If the operation
4392 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4395 g_file_load_partial_contents_async (GFile *file,
4396 GCancellable *cancellable,
4397 GFileReadMoreCallback read_more_callback,
4398 GAsyncReadyCallback callback,
4401 LoadContentsData *data;
4403 g_return_if_fail (G_IS_FILE (file));
4405 data = g_new0 (LoadContentsData, 1);
4408 data->cancellable = g_object_ref (cancellable);
4409 data->read_more_callback = read_more_callback;
4410 data->callback = callback;
4411 data->user_data = user_data;
4412 data->content = g_byte_array_new ();
4413 data->file = g_object_ref (file);
4415 g_file_read_async (file,
4418 load_contents_open_callback,
4423 * g_file_load_partial_contents_finish:
4424 * @file: input #GFile.
4425 * @res: a #GAsyncResult.
4426 * @contents: a location to place the contents of the file.
4427 * @length: a location to place the length of the contents of the file.
4428 * @etag_out: a location to place the current entity tag for the file.
4429 * @error: a #GError, or %NULL
4431 * Finishes an asynchronous partial load operation that was started
4432 * with g_file_load_partial_contents_async().
4434 * Returns: %TRUE if the load was successful. If %FALSE and @error is
4435 * present, it will be set appropriately.
4438 g_file_load_partial_contents_finish (GFile *file,
4445 GSimpleAsyncResult *simple;
4446 LoadContentsData *data;
4448 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4449 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
4450 g_return_val_if_fail (contents != NULL, FALSE);
4452 simple = G_SIMPLE_ASYNC_RESULT (res);
4454 if (g_simple_async_result_propagate_error (simple, error))
4457 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_load_contents_async);
4459 data = g_simple_async_result_get_op_res_gpointer (simple);
4463 g_propagate_error (error, data->error);
4472 *length = data->pos;
4476 *etag_out = data->etag;
4480 /* Zero terminate */
4481 g_byte_array_set_size (data->content, data->pos + 1);
4482 data->content->data[data->pos] = 0;
4484 *contents = (char *)g_byte_array_free (data->content, FALSE);
4485 data->content = NULL;
4491 * g_file_load_contents_async:
4492 * @file: input #GFile.
4493 * @cancellable: optional #GCancellable object, %NULL to ignore.
4494 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
4495 * @user_data: the data to pass to callback function
4497 * Starts an asynchronous load of the @file's contents.
4499 * For more details, see g_file_load_contents() which is
4500 * the synchronous version of this call.
4502 * When the load operation has completed, @callback will be called
4503 * with @user data. To finish the operation, call
4504 * g_file_load_contents_finish() with the #GAsyncResult returned by
4507 * If @cancellable is not %NULL, then the operation can be cancelled by
4508 * triggering the cancellable object from another thread. If the operation
4509 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4512 g_file_load_contents_async (GFile *file,
4513 GCancellable *cancellable,
4514 GAsyncReadyCallback callback,
4517 g_file_load_partial_contents_async (file,
4520 callback, user_data);
4524 * g_file_load_contents_finish:
4525 * @file: input #GFile.
4526 * @res: a #GAsyncResult.
4527 * @contents: a location to place the contents of the file.
4528 * @length: a location to place the length of the contents of the file.
4529 * @etag_out: a location to place the current entity tag for the file.
4530 * @error: a #GError, or %NULL
4532 * Finishes an asynchronous load of the @file's contents.
4533 * The contents are placed in @contents, and @length is set to the
4534 * size of the @contents string. If @etag_out is present, it will be
4535 * set to the new entity tag for the @file.
4537 * Returns: %TRUE if the load was successful. If %FALSE and @error is
4538 * present, it will be set appropriately.
4541 g_file_load_contents_finish (GFile *file,
4548 return g_file_load_partial_contents_finish (file,
4557 * g_file_replace_contents:
4558 * @file: input #GFile.
4559 * @contents: a string containing the new contents for @file.
4560 * @length: the length of @contents in bytes.
4561 * @etag: the old <link linkend="gfile-etag">entity tag</link>
4563 * @make_backup: %TRUE if a backup should be created.
4564 * @flags: a set of #GFileCreateFlags.
4565 * @new_etag: a location to a new <link linkend="gfile-etag">entity tag</link>
4567 * @cancellable: optional #GCancellable object, %NULL to ignore.
4568 * @error: a #GError, or %NULL
4570 * Replaces the contents of @file with @contents of @length bytes.
4572 * If @etag is specified (not %NULL) any existing file must have that etag, or
4573 * the error %G_IO_ERROR_WRONG_ETAG will be returned.
4575 * If @make_backup is %TRUE, this function will attempt to make a backup of @file.
4577 * If @cancellable is not %NULL, then the operation can be cancelled by
4578 * triggering the cancellable object from another thread. If the operation
4579 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4581 * The returned @new_etag can be used to verify that the file hasn't changed the
4582 * next time it is saved over.
4584 * Returns: %TRUE if successful. If an error
4585 * has occurred, this function will return %FALSE and set @error
4586 * appropriately if present.
4589 g_file_replace_contents (GFile *file,
4590 const char *contents,
4593 gboolean make_backup,
4594 GFileCreateFlags flags,
4596 GCancellable *cancellable,
4599 GFileOutputStream *out;
4600 gsize pos, remainder;
4603 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4604 g_return_val_if_fail (contents != NULL, FALSE);
4606 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
4612 while (remainder > 0 &&
4613 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
4615 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
4623 if (remainder > 0 && res < 0)
4625 /* Ignore errors on close */
4626 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
4628 /* error is set already */
4632 if (!g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error))
4636 *new_etag = g_file_output_stream_get_etag (out);
4644 GCancellable *cancellable;
4645 GAsyncReadyCallback callback;
4647 const char *content;
4651 } ReplaceContentsData;
4654 replace_contents_data_free (ReplaceContentsData *data)
4657 g_error_free (data->error);
4658 if (data->cancellable)
4659 g_object_unref (data->cancellable);
4660 g_object_unref (data->file);
4661 g_free (data->etag);
4666 replace_contents_close_callback (GObject *obj,
4667 GAsyncResult *close_res,
4670 GOutputStream *stream = G_OUTPUT_STREAM (obj);
4671 ReplaceContentsData *data = user_data;
4672 GSimpleAsyncResult *res;
4674 /* Ignore errors here, we're only reading anyway */
4675 g_output_stream_close_finish (stream, close_res, NULL);
4676 g_object_unref (stream);
4678 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
4680 res = g_simple_async_result_new (G_OBJECT (data->file),
4683 g_file_replace_contents_async);
4684 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_contents_data_free);
4685 g_simple_async_result_complete (res);
4686 g_object_unref (res);
4690 replace_contents_write_callback (GObject *obj,
4691 GAsyncResult *read_res,
4694 GOutputStream *stream = G_OUTPUT_STREAM (obj);
4695 ReplaceContentsData *data = user_data;
4696 GError *error = NULL;
4699 write_size = g_output_stream_write_finish (stream, read_res, &error);
4701 if (write_size <= 0)
4703 /* Error or EOF, close the file */
4705 data->error = error;
4706 g_output_stream_close_async (stream, 0,
4708 replace_contents_close_callback, data);
4710 else if (write_size > 0)
4712 data->pos += write_size;
4714 if (data->pos >= data->length)
4715 g_output_stream_close_async (stream, 0,
4717 replace_contents_close_callback, data);
4719 g_output_stream_write_async (stream,
4720 data->content + data->pos,
4721 data->length - data->pos,
4724 replace_contents_write_callback,
4730 replace_contents_open_callback (GObject *obj,
4731 GAsyncResult *open_res,
4734 GFile *file = G_FILE (obj);
4735 GFileOutputStream *stream;
4736 ReplaceContentsData *data = user_data;
4737 GError *error = NULL;
4738 GSimpleAsyncResult *res;
4740 stream = g_file_replace_finish (file, open_res, &error);
4744 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
4745 data->content + data->pos,
4746 data->length - data->pos,
4749 replace_contents_write_callback,
4755 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
4759 g_simple_async_result_complete (res);
4760 g_error_free (error);
4761 replace_contents_data_free (data);
4762 g_object_unref (res);
4767 * g_file_replace_contents_async:
4768 * @file: input #GFile.
4769 * @contents: string of contents to replace the file with.
4770 * @length: the length of @contents in bytes.
4771 * @etag: a new <link linkend="gfile-etag">entity tag</link> for the @file.
4772 * @make_backup: %TRUE if a backup should be created.
4773 * @flags: a set of #GFileCreateFlags.
4774 * @cancellable: optional #GCancellable object, %NULL to ignore.
4775 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
4776 * @user_data: the data to pass to callback function
4778 * Starts an asynchronous replacement of @file with the given
4779 * @contents of @length bytes. @etag will replace the document's
4780 * current entity tag.
4782 * When this operation has completed, @callback will be called with
4783 * @user_user data, and the operation can be finalized with
4784 * g_file_replace_contents_finish().
4786 * If @cancellable is not %NULL, then the operation can be cancelled by
4787 * triggering the cancellable object from another thread. If the operation
4788 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4790 * If @make_backup is %TRUE, this function will attempt to
4791 * make a backup of @file.
4794 g_file_replace_contents_async (GFile *file,
4795 const char *contents,
4798 gboolean make_backup,
4799 GFileCreateFlags flags,
4800 GCancellable *cancellable,
4801 GAsyncReadyCallback callback,
4804 ReplaceContentsData *data;
4806 g_return_if_fail (G_IS_FILE (file));
4807 g_return_if_fail (contents != NULL);
4809 data = g_new0 (ReplaceContentsData, 1);
4812 data->cancellable = g_object_ref (cancellable);
4813 data->callback = callback;
4814 data->user_data = user_data;
4815 data->content = contents;
4816 data->length = length;
4818 data->file = g_object_ref (file);
4820 g_file_replace_async (file,
4826 replace_contents_open_callback,
4831 * g_file_replace_contents_finish:
4832 * @file: input #GFile.
4833 * @res: a #GAsyncResult.
4834 * @new_etag: a location of a new <link linkend="gfile-etag">entity tag</link>
4836 * @error: a #GError, or %NULL
4838 * Finishes an asynchronous replace of the given @file. See
4839 * g_file_replace_contents_async(). Sets @new_etag to the new entity
4840 * tag for the document, if present.
4842 * Returns: %TRUE on success, %FALSE on failure.
4845 g_file_replace_contents_finish (GFile *file,
4850 GSimpleAsyncResult *simple;
4851 ReplaceContentsData *data;
4853 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4854 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
4856 simple = G_SIMPLE_ASYNC_RESULT (res);
4858 if (g_simple_async_result_propagate_error (simple, error))
4861 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_replace_contents_async);
4863 data = g_simple_async_result_get_op_res_gpointer (simple);
4867 g_propagate_error (error, data->error);
4875 *new_etag = data->etag;
4876 data->etag = NULL; /* Take ownership */
4882 #define __G_FILE_C__
4883 #include "gioaliasdef.c"