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);
816 * g_file_query_exists:
817 * @file: input #GFile.
818 * @cancellable: optional #GCancellable object, %NULL to ignore.
820 * Utility function to check if a particular file exists. This is
821 * implemented using g_file_query_info() and as such does blocking I/O.
823 * Note that in many cases it is racy to first check for file existance
824 * and then execute something based on the outcome of that, because the
825 * file might have been created or removed inbetween the operations. The
826 * general approach to handling that is to not check, but just do the
827 * operation and handle the errors as they come.
829 * As an example of race-free checking. Take the case of reading a file, and
830 * if it doesn't exist, create it. There are two racy versions: read it, and
831 * on error create it; and: check if it exists, if not create it. These
832 * can both result in two processes creating the file (with perhaps a partially
833 * written file as the result). The correct approach is to always try to create
834 * the file with g_file_create() which will either atomically create the file
835 * or fail with an G_IO_ERROR_EXISTS error.
837 * However, in many cases an existance check is useful in a user
838 * interface, for instance to make a menu item sensitive/insensitive, so that
839 * you don't have to fool users that something is possible and then just show
840 * and error dialog. If you do this, you should make sure to also handle the
841 * errors that can happen due to races when you execute the operation.
843 * Returns: %TRUE if the file exists (and can be detected without error), %FALSE otherwise (or if cancelled).
846 g_file_query_exists (GFile *file,
847 GCancellable *cancellable)
851 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE,
852 G_FILE_QUERY_INFO_NONE,
856 g_object_unref (info);
865 * @file: input #GFile.
866 * @attributes: an attribute query string.
867 * @flags: a set of #GFileQueryInfoFlags.
868 * @cancellable: optional #GCancellable object, %NULL to ignore.
871 * Gets the requested information about specified @file. The result
872 * is a #GFileInfo objects that contains key-value attributes (like type or size
875 * The @attribute value is a string that specifies the file attributes that
876 * should be gathered. It is not an error if its not possible to read a particular
877 * requested attribute from a file, it just won't be set. @attribute should
878 * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
879 * means all attributes, and a wildcard like "standard::*" means all attributes in the standard
880 * namespace. An example attribute query be "standard::*,owner::user".
881 * The standard attributes are available as defines, like #G_FILE_ATTRIBUTE_STANDARD_NAME.
883 * If @cancellable is not %NULL, then the operation can be cancelled by
884 * triggering the cancellable object from another thread. If the operation
885 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
887 * For symlinks, normally the information about the target of the
888 * symlink is returned, rather than information about the symlink itself.
889 * However if you pass #G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS in @flags the
890 * information about the symlink itself will be returned. Also, for symlinks
891 * that points to non-existing files the information about the symlink itself
894 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
895 * Other errors are possible too, and depend on what kind of filesystem the file is on.
897 * Returns: a #GFileInfo for the given @file, or %NULL on error.
900 g_file_query_info (GFile *file,
901 const char *attributes,
902 GFileQueryInfoFlags flags,
903 GCancellable *cancellable,
908 g_return_val_if_fail (G_IS_FILE (file), NULL);
910 if (g_cancellable_set_error_if_cancelled (cancellable, error))
913 iface = G_FILE_GET_IFACE (file);
915 if (iface->query_info == NULL)
917 g_set_error (error, G_IO_ERROR,
918 G_IO_ERROR_NOT_SUPPORTED,
919 _("Operation not supported"));
923 return (* iface->query_info) (file, attributes, flags, cancellable, error);
927 * g_file_query_info_async:
928 * @file: input #GFile.
929 * @attributes: an attribute query string.
930 * @flags: a set of #GFileQueryInfoFlags.
931 * @io_priority: the <link linkend="io-priority">I/O priority</link>
933 * @cancellable: optional #GCancellable object, %NULL to ignore.
934 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
935 * @user_data: the data to pass to callback function
937 * Asynchronously gets the requested information about specified @file. The result
938 * is a #GFileInfo objects that contains key-value attributes (such as type or size
941 * For more details, see g_file_query_info() which is
942 * the synchronous version of this call.
944 * When the operation is finished, @callback will be called. You can then call
945 * g_file_query_info_finish() to get the result of the operation.
948 g_file_query_info_async (GFile *file,
949 const char *attributes,
950 GFileQueryInfoFlags flags,
952 GCancellable *cancellable,
953 GAsyncReadyCallback callback,
958 g_return_if_fail (G_IS_FILE (file));
960 iface = G_FILE_GET_IFACE (file);
961 (* iface->query_info_async) (file,
971 * g_file_query_info_finish:
972 * @file: input #GFile.
973 * @res: a #GAsyncResult.
976 * Finishes an asynchronous file info query.
977 * See g_file_query_info_async().
979 * Returns: #GFileInfo for given @file or %NULL on error.
982 g_file_query_info_finish (GFile *file,
988 g_return_val_if_fail (G_IS_FILE (file), NULL);
989 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
991 if (G_IS_SIMPLE_ASYNC_RESULT (res))
993 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
994 if (g_simple_async_result_propagate_error (simple, error))
998 iface = G_FILE_GET_IFACE (file);
999 return (* iface->query_info_finish) (file, res, error);
1003 * g_file_query_filesystem_info:
1004 * @file: input #GFile.
1005 * @attributes: an attribute query string.
1006 * @cancellable: optional #GCancellable object, %NULL to ignore.
1007 * @error: a #GError.
1009 * Similar to g_file_query_info(), but obtains information
1010 * about the filesystem the @file is on, rather than the file itself.
1011 * For instance the amount of space available and the type of
1014 * The @attribute value is a string that specifies the file attributes that
1015 * should be gathered. It is not an error if its not possible to read a particular
1016 * requested attribute from a file, it just won't be set. @attribute should
1017 * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
1018 * means all attributes, and a wildcard like "fs:*" means all attributes in the fs
1019 * namespace. The standard namespace for filesystem attributes is "fs".
1020 * Common attributes of interest are #G_FILE_ATTRIBUTE_FILESYSTEM_SIZE
1021 * (the total size of the filesystem in bytes), #G_FILE_ATTRIBUTE_FILESYSTEM_FREE (number of
1022 * bytes available), and #G_FILE_ATTRIBUTE_FILESYSTEM_TYPE (type of the filesystem).
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 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1029 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1031 * Returns: a #GFileInfo or %NULL if there was an error.
1034 g_file_query_filesystem_info (GFile *file,
1035 const char *attributes,
1036 GCancellable *cancellable,
1041 g_return_val_if_fail (G_IS_FILE (file), NULL);
1043 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1046 iface = G_FILE_GET_IFACE (file);
1048 if (iface->query_filesystem_info == NULL)
1050 g_set_error (error, G_IO_ERROR,
1051 G_IO_ERROR_NOT_SUPPORTED,
1052 _("Operation not supported"));
1056 return (* iface->query_filesystem_info) (file, attributes, cancellable, error);
1060 * g_file_find_enclosing_mount:
1061 * @file: input #GFile.
1062 * @cancellable: optional #GCancellable object, %NULL to ignore.
1063 * @error: a #GError.
1065 * Gets a #GMount for the #GFile.
1067 * If the #GFileIface for @file does not have a mount (e.g. possibly a
1068 * remote share), @error will be set to %G_IO_ERROR_NOT_FOUND and %NULL
1071 * If @cancellable is not %NULL, then the operation can be cancelled by
1072 * triggering the cancellable object from another thread. If the operation
1073 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1075 * Returns: a #GMount where the @file is located or %NULL on error.
1078 g_file_find_enclosing_mount (GFile *file,
1079 GCancellable *cancellable,
1084 g_return_val_if_fail (G_IS_FILE (file), NULL);
1086 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1089 iface = G_FILE_GET_IFACE (file);
1090 if (iface->find_enclosing_mount == NULL)
1092 g_set_error (error, G_IO_ERROR,
1093 G_IO_ERROR_NOT_FOUND,
1094 _("Containing mount does not exist"));
1098 return (* iface->find_enclosing_mount) (file, cancellable, error);
1103 * @file: #GFile to read.
1104 * @cancellable: a #GCancellable
1105 * @error: a #GError, or %NULL
1107 * Opens a file for reading. The result is a #GFileInputStream that
1108 * can be used to read the contents of the file.
1110 * If @cancellable is not %NULL, then the operation can be cancelled by
1111 * triggering the cancellable object from another thread. If the operation
1112 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1114 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1115 * If the file is a directory, the G_IO_ERROR_IS_DIRECTORY error will be returned.
1116 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1118 * Returns: #GFileInputStream or %NULL on error.
1121 g_file_read (GFile *file,
1122 GCancellable *cancellable,
1127 g_return_val_if_fail (G_IS_FILE (file), NULL);
1129 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1132 iface = G_FILE_GET_IFACE (file);
1134 if (iface->read_fn == NULL)
1136 g_set_error (error, G_IO_ERROR,
1137 G_IO_ERROR_NOT_SUPPORTED,
1138 _("Operation not supported"));
1142 return (* iface->read_fn) (file, cancellable, error);
1147 * @file: input #GFile.
1148 * @flags: a set of #GFileCreateFlags.
1149 * @cancellable: optional #GCancellable object, %NULL to ignore.
1150 * @error: a #GError, or %NULL
1152 * Gets an output stream for appending data to the file. If
1153 * the file doesn't already exist it is created.
1155 * By default files created are generally readable by everyone,
1156 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1157 * will be made readable only to the current user, to the level that
1158 * is supported on the target filesystem.
1160 * If @cancellable is not %NULL, then the operation can be cancelled by
1161 * triggering the cancellable object from another thread. If the operation
1162 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1164 * Some file systems don't allow all file names, and may
1165 * return an G_IO_ERROR_INVALID_FILENAME error.
1166 * If the file is a directory the G_IO_ERROR_IS_DIRECTORY error will be
1167 * returned. Other errors are possible too, and depend on what kind of
1168 * filesystem the file is on.
1170 * Returns: a #GFileOutputStream.
1173 g_file_append_to (GFile *file,
1174 GFileCreateFlags flags,
1175 GCancellable *cancellable,
1180 g_return_val_if_fail (G_IS_FILE (file), NULL);
1182 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1185 iface = G_FILE_GET_IFACE (file);
1187 if (iface->append_to == NULL)
1189 g_set_error (error, G_IO_ERROR,
1190 G_IO_ERROR_NOT_SUPPORTED,
1191 _("Operation not supported"));
1195 return (* iface->append_to) (file, flags, cancellable, error);
1200 * @file: input #GFile.
1201 * @flags: a set of #GFileCreateFlags.
1202 * @cancellable: optional #GCancellable object, %NULL to ignore.
1203 * @error: a #GError, or %NULL
1205 * Creates a new file and returns an output stream for writing to it.
1206 * The file must not already exists.
1208 * By default files created are generally readable by everyone,
1209 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1210 * will be made readable only to the current user, to the level that
1211 * is supported on the target filesystem.
1213 * If @cancellable is not %NULL, then the operation can be cancelled by
1214 * triggering the cancellable object from another thread. If the operation
1215 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1217 * If a file with this name already exists the G_IO_ERROR_EXISTS error
1218 * will be returned. If the file is a directory the G_IO_ERROR_IS_DIRECTORY
1219 * error will be returned.
1220 * Some file systems don't allow all file names, and may
1221 * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1222 * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1223 * Other errors are possible too, and depend on what kind of
1224 * filesystem the file is on.
1226 * Returns: a #GFileOutputStream for the newly created file, or
1230 g_file_create (GFile *file,
1231 GFileCreateFlags flags,
1232 GCancellable *cancellable,
1237 g_return_val_if_fail (G_IS_FILE (file), NULL);
1239 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1242 iface = G_FILE_GET_IFACE (file);
1244 if (iface->create == NULL)
1246 g_set_error (error, G_IO_ERROR,
1247 G_IO_ERROR_NOT_SUPPORTED,
1248 _("Operation not supported"));
1252 return (* iface->create) (file, flags, cancellable, error);
1257 * @file: input #GFile.
1258 * @etag: an optional <link linkend="gfile-etag">entity tag</link> for the
1259 * current #GFile, or #NULL to ignore.
1260 * @make_backup: %TRUE if a backup should be created.
1261 * @flags: a set of #GFileCreateFlags.
1262 * @cancellable: optional #GCancellable object, %NULL to ignore.
1263 * @error: a #GError, or %NULL
1265 * Returns an output stream for overwriting the file, possibly
1266 * creating a backup copy of the file first.
1268 * This will try to replace the file in the safest way possible so
1269 * that any errors during the writing will not affect an already
1270 * existing copy of the file. For instance, for local files it
1271 * may write to a temporary file and then atomically rename over
1272 * the destination when the stream is closed.
1274 * By default files created are generally readable by everyone,
1275 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1276 * will be made readable only to the current user, to the level that
1277 * is supported on the target filesystem.
1279 * If @cancellable is not %NULL, then the operation can be cancelled by
1280 * triggering the cancellable object from another thread. If the operation
1281 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1283 * If you pass in a non-#NULL @etag value, then this value is
1284 * compared to the current entity tag of the file, and if they differ
1285 * an G_IO_ERROR_WRONG_ETAG error is returned. This generally means
1286 * that the file has been changed since you last read it. You can get
1287 * the new etag from g_file_output_stream_get_etag() after you've
1288 * finished writing and closed the #GFileOutputStream. When you load
1289 * a new file you can use g_file_input_stream_query_info() to get
1290 * the etag of the file.
1292 * If @make_backup is %TRUE, this function will attempt to make a backup
1293 * of the current file before overwriting it. If this fails a G_IO_ERROR_CANT_CREATE_BACKUP
1294 * error will be returned. If you want to replace anyway, try again with
1295 * @make_backup set to %FALSE.
1297 * If the file is a directory the G_IO_ERROR_IS_DIRECTORY error will be returned,
1298 * and if the file is some other form of non-regular file then a
1299 * G_IO_ERROR_NOT_REGULAR_FILE error will be returned.
1300 * Some file systems don't allow all file names, and may
1301 * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1302 * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1303 * Other errors are possible too, and depend on what kind of
1304 * filesystem the file is on.
1306 * Returns: a #GFileOutputStream or %NULL on error.
1309 g_file_replace (GFile *file,
1311 gboolean make_backup,
1312 GFileCreateFlags flags,
1313 GCancellable *cancellable,
1318 g_return_val_if_fail (G_IS_FILE (file), NULL);
1320 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1323 iface = G_FILE_GET_IFACE (file);
1325 if (iface->replace == NULL)
1327 g_set_error (error, G_IO_ERROR,
1328 G_IO_ERROR_NOT_SUPPORTED,
1329 _("Operation not supported"));
1334 /* Handle empty tag string as NULL in consistent way. */
1335 if (etag && *etag == 0)
1338 return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1342 * g_file_read_async:
1343 * @file: input #GFile.
1344 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1346 * @cancellable: optional #GCancellable object, %NULL to ignore.
1347 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1348 * @user_data: the data to pass to callback function
1350 * Asynchronously opens @file for reading.
1352 * For more details, see g_file_read() which is
1353 * the synchronous version of this call.
1355 * When the operation is finished, @callback will be called. You can then call
1356 * g_file_read_finish() to get the result of the operation.
1359 g_file_read_async (GFile *file,
1361 GCancellable *cancellable,
1362 GAsyncReadyCallback callback,
1367 g_return_if_fail (G_IS_FILE (file));
1369 iface = G_FILE_GET_IFACE (file);
1370 (* iface->read_async) (file,
1378 * g_file_read_finish:
1379 * @file: input #GFile.
1380 * @res: a #GAsyncResult.
1381 * @error: a #GError, or %NULL
1383 * Finishes an asynchronous file read operation started with
1384 * g_file_read_async().
1386 * Returns: a #GFileInputStream or %NULL on error.
1389 g_file_read_finish (GFile *file,
1395 g_return_val_if_fail (G_IS_FILE (file), NULL);
1396 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1398 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1400 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1401 if (g_simple_async_result_propagate_error (simple, error))
1405 iface = G_FILE_GET_IFACE (file);
1406 return (* iface->read_finish) (file, res, error);
1410 * g_file_append_to_async:
1411 * @file: input #GFile.
1412 * @flags: a set of #GFileCreateFlags.
1413 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1415 * @cancellable: optional #GCancellable object, %NULL to ignore.
1416 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1417 * @user_data: the data to pass to callback function
1419 * Asynchronously opens @file for appending.
1421 * For more details, see g_file_append_to() which is
1422 * the synchronous version of this call.
1424 * When the operation is finished, @callback will be called. You can then call
1425 * g_file_append_to_finish() to get the result of the operation.
1428 g_file_append_to_async (GFile *file,
1429 GFileCreateFlags flags,
1431 GCancellable *cancellable,
1432 GAsyncReadyCallback callback,
1437 g_return_if_fail (G_IS_FILE (file));
1439 iface = G_FILE_GET_IFACE (file);
1440 (* iface->append_to_async) (file,
1449 * g_file_append_to_finish:
1450 * @file: input #GFile.
1451 * @res: #GAsyncResult
1452 * @error: a #GError, or %NULL
1454 * Finishes an asynchronous file append operation started with
1455 * g_file_append_to_async().
1457 * Returns: a valid #GFileOutputStream or %NULL on error.
1460 g_file_append_to_finish (GFile *file,
1466 g_return_val_if_fail (G_IS_FILE (file), NULL);
1467 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1469 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1471 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1472 if (g_simple_async_result_propagate_error (simple, error))
1476 iface = G_FILE_GET_IFACE (file);
1477 return (* iface->append_to_finish) (file, res, error);
1481 * g_file_create_async:
1482 * @file: input #GFile.
1483 * @flags: a set of #GFileCreateFlags.
1484 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1486 * @cancellable: optional #GCancellable object, %NULL to ignore.
1487 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1488 * @user_data: the data to pass to callback function
1490 * Asynchronously creates a new file and returns an output stream for writing to it.
1491 * The file must not already exists.
1493 * For more details, see g_file_create() which is
1494 * the synchronous version of this call.
1496 * When the operation is finished, @callback will be called. You can then call
1497 * g_file_create_finish() to get the result of the operation.
1500 g_file_create_async (GFile *file,
1501 GFileCreateFlags flags,
1503 GCancellable *cancellable,
1504 GAsyncReadyCallback callback,
1509 g_return_if_fail (G_IS_FILE (file));
1511 iface = G_FILE_GET_IFACE (file);
1512 (* iface->create_async) (file,
1521 * g_file_create_finish:
1522 * @file: input #GFile.
1523 * @res: a #GAsyncResult.
1524 * @error: a #GError, or %NULL
1526 * Finishes an asynchronous file create operation started with
1527 * g_file_create_async().
1529 * Returns: a #GFileOutputStream or %NULL on error.
1532 g_file_create_finish (GFile *file,
1538 g_return_val_if_fail (G_IS_FILE (file), NULL);
1539 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1541 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1543 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1544 if (g_simple_async_result_propagate_error (simple, error))
1548 iface = G_FILE_GET_IFACE (file);
1549 return (* iface->create_finish) (file, res, error);
1553 * g_file_replace_async:
1554 * @file: input #GFile.
1555 * @etag: an <link linkend="gfile-etag">entity tag</link> for the
1556 * current #GFile, or NULL to ignore.
1557 * @make_backup: %TRUE if a backup should be created.
1558 * @flags: a set of #GFileCreateFlags.
1559 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1561 * @cancellable: optional #GCancellable object, %NULL to ignore.
1562 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1563 * @user_data: the data to pass to callback function
1565 * Asynchronously overwrites the file, replacing the contents, possibly
1566 * creating a backup copy of the file first.
1568 * For more details, see g_file_replace() which is
1569 * the synchronous version of this call.
1571 * When the operation is finished, @callback will be called. You can then call
1572 * g_file_replace_finish() to get the result of the operation.
1575 g_file_replace_async (GFile *file,
1577 gboolean make_backup,
1578 GFileCreateFlags flags,
1580 GCancellable *cancellable,
1581 GAsyncReadyCallback callback,
1586 g_return_if_fail (G_IS_FILE (file));
1588 iface = G_FILE_GET_IFACE (file);
1589 (* iface->replace_async) (file,
1600 * g_file_replace_finish:
1601 * @file: input #GFile.
1602 * @res: a #GAsyncResult.
1603 * @error: a #GError, or %NULL
1605 * Finishes an asynchronous file replace operation started with
1606 * g_file_replace_async().
1608 * Returns: a #GFileOutputStream, or %NULL on error.
1611 g_file_replace_finish (GFile *file,
1617 g_return_val_if_fail (G_IS_FILE (file), NULL);
1618 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1620 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1622 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1623 if (g_simple_async_result_propagate_error (simple, error))
1627 iface = G_FILE_GET_IFACE (file);
1628 return (* iface->replace_finish) (file, res, error);
1632 copy_symlink (GFile *destination,
1633 GFileCopyFlags flags,
1634 GCancellable *cancellable,
1639 gboolean tried_delete;
1641 GFileType file_type;
1643 tried_delete = FALSE;
1647 if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
1649 /* Maybe it already existed, and we want to overwrite? */
1650 if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) &&
1651 my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
1653 g_error_free (my_error);
1656 /* Don't overwrite if the destination is a directory */
1657 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1658 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1659 cancellable, &my_error);
1662 file_type = g_file_info_get_file_type (info);
1663 g_object_unref (info);
1665 if (file_type == G_FILE_TYPE_DIRECTORY)
1667 g_set_error (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
1668 _("Can't copy over directory"));
1673 if (!g_file_delete (destination, cancellable, error))
1676 tried_delete = TRUE;
1680 g_propagate_error (error, my_error);
1687 static GInputStream *
1688 open_source_for_copy (GFile *source,
1690 GFileCopyFlags flags,
1691 GCancellable *cancellable,
1697 GFileType file_type;
1700 in = (GInputStream *)g_file_read (source, cancellable, &my_error);
1704 /* There was an error opening the source, try to set a good error for it: */
1706 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
1708 /* The source is a directory, don't fail with WOULD_RECURSE immediately,
1709 * as that is less useful to the app. Better check for errors on the
1712 g_error_free (my_error);
1715 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1716 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1717 cancellable, &my_error);
1720 file_type = g_file_info_get_file_type (info);
1721 g_object_unref (info);
1723 if (flags & G_FILE_COPY_OVERWRITE)
1725 if (file_type == G_FILE_TYPE_DIRECTORY)
1727 g_set_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
1728 _("Can't copy directory over directory"));
1731 /* continue to would_recurse error */
1735 g_set_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
1736 _("Target file exists"));
1742 /* Error getting info from target, return that error
1743 * (except for NOT_FOUND, which is no error here)
1745 if (my_error->domain != G_IO_ERROR && my_error->code != G_IO_ERROR_NOT_FOUND)
1747 g_propagate_error (error, my_error);
1750 g_error_free (my_error);
1753 g_set_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
1754 _("Can't recursively copy directory"));
1758 g_propagate_error (error, my_error);
1763 should_copy (GFileAttributeInfo *info,
1767 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
1768 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
1772 build_attribute_list_for_copy (GFileAttributeInfoList *attributes,
1773 GFileAttributeInfoList *namespaces,
1781 s = g_string_new ("");
1785 for (i = 0; i < attributes->n_infos; i++)
1787 if (should_copy (&attributes->infos[i], as_move))
1792 g_string_append_c (s, ',');
1794 g_string_append (s, attributes->infos[i].name);
1801 for (i = 0; i < namespaces->n_infos; i++)
1803 if (should_copy (&namespaces->infos[i], as_move))
1808 g_string_append_c (s, ',');
1810 g_string_append (s, namespaces->infos[i].name);
1811 g_string_append (s, ":*");
1816 return g_string_free (s, FALSE);
1820 * g_file_copy_attributes:
1821 * @source: a #GFile with attributes.
1822 * @destination: a #GFile to copy attributes to.
1823 * @flags: a set of #GFileCopyFlags.
1824 * @cancellable: optional #GCancellable object, %NULL to ignore.
1825 * @error: a #GError, %NULL to ignore.
1827 * Copies the file attributes from @source to @destination.
1829 * Normally only a subset of the file attributes are copied,
1830 * those that are copies in a normal file copy operation
1831 * (which for instance does not include e.g. mtime). However
1832 * if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
1833 * all the metadata that is possible to copy is copied.
1835 * Returns: %TRUE if the attributes were copied successfully, %FALSE otherwise.
1838 g_file_copy_attributes (GFile *source,
1840 GFileCopyFlags flags,
1841 GCancellable *cancellable,
1844 GFileAttributeInfoList *attributes, *namespaces;
1845 char *attrs_to_read;
1849 gboolean source_nofollow_symlinks;
1851 as_move = flags & G_FILE_COPY_ALL_METADATA;
1852 source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
1854 /* Ignore errors here, if the target supports no attributes there is nothing to copy */
1855 attributes = g_file_query_settable_attributes (destination, cancellable, NULL);
1856 namespaces = g_file_query_writable_namespaces (destination, cancellable, NULL);
1858 if (attributes == NULL && namespaces == NULL)
1861 attrs_to_read = build_attribute_list_for_copy (attributes, namespaces, as_move);
1863 /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
1864 * we just don't copy it.
1866 info = g_file_query_info (source, attrs_to_read,
1867 source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
1871 g_free (attrs_to_read);
1876 res = g_file_set_attributes_from_info (destination,
1880 g_object_unref (info);
1883 g_file_attribute_info_list_unref (attributes);
1884 g_file_attribute_info_list_unref (namespaces);
1889 /* Closes the streams */
1891 copy_stream_with_progress (GInputStream *in,
1893 GCancellable *cancellable,
1894 GFileProgressCallback progress_callback,
1895 gpointer progress_callback_data,
1898 gssize n_read, n_written;
1899 goffset current_size;
1900 char buffer[8192], *p;
1906 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
1907 G_FILE_ATTRIBUTE_STANDARD_SIZE,
1911 total_size = g_file_info_get_size (info);
1912 g_object_unref (info);
1919 n_read = g_input_stream_read (in, buffer, sizeof (buffer), cancellable, error);
1929 current_size += n_read;
1934 n_written = g_output_stream_write (out, p, n_read, cancellable, error);
1935 if (n_written == -1)
1942 n_read -= n_written;
1948 if (progress_callback)
1949 progress_callback (current_size, total_size, progress_callback_data);
1953 error = NULL; /* Ignore further errors */
1955 /* Make sure we send full copied size */
1956 if (progress_callback)
1957 progress_callback (current_size, total_size, progress_callback_data);
1960 /* Don't care about errors in source here */
1961 g_input_stream_close (in, cancellable, NULL);
1963 /* But write errors on close are bad! */
1964 if (!g_output_stream_close (out, cancellable, error))
1967 g_object_unref (in);
1968 g_object_unref (out);
1974 file_copy_fallback (GFile *source,
1976 GFileCopyFlags flags,
1977 GCancellable *cancellable,
1978 GFileProgressCallback progress_callback,
1979 gpointer progress_callback_data,
1987 /* Maybe copy the symlink? */
1988 if (flags & G_FILE_COPY_NOFOLLOW_SYMLINKS)
1990 info = g_file_query_info (source,
1991 G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
1992 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1998 if (g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK &&
1999 (target = g_file_info_get_symlink_target (info)) != NULL)
2001 if (!copy_symlink (destination, flags, cancellable, target, error))
2003 g_object_unref (info);
2007 g_object_unref (info);
2011 g_object_unref (info);
2014 in = open_source_for_copy (source, destination, flags, cancellable, error);
2018 if (flags & G_FILE_COPY_OVERWRITE)
2020 out = (GOutputStream *)g_file_replace (destination,
2022 flags & G_FILE_COPY_BACKUP,
2023 cancellable, error);
2027 out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
2032 g_object_unref (in);
2036 if (!copy_stream_with_progress (in, out, cancellable,
2037 progress_callback, progress_callback_data,
2043 /* Ignore errors here. Failure to copy metadata is not a hard error */
2044 g_file_copy_attributes (source, destination,
2045 flags, cancellable, NULL);
2052 * @source: input #GFile.
2053 * @destination: destination #GFile
2054 * @flags: set of #GFileCopyFlags
2055 * @cancellable: optional #GCancellable object, %NULL to ignore.
2056 * @progress_callback: function to callback with progress information
2057 * @progress_callback_data: user data to pass to @progress_callback
2058 * @error: #GError to set on error, or %NULL
2060 * Copies the file @source to the location specified by @destination.
2061 * Can not handle recursive copies of directories.
2063 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2064 * existing @destination file is overwritten.
2066 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2067 * will be copied as symlinks, otherwise the target of the
2068 * @source symlink will be copied.
2070 * If @cancellable is not %NULL, then the operation can be cancelled by
2071 * triggering the cancellable object from another thread. If the operation
2072 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2074 * If @progress_callback is not %NULL, then the operation can be monitored by
2075 * setting this to a #GFileProgressCallback function. @progress_callback_data
2076 * will be passed to this function. It is guaranteed that this callback will
2077 * be called after all data has been transferred with the total number of bytes
2078 * copied during the operation.
2080 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2081 * error is returned, independent on the status of the @destination.
2083 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2084 * error G_IO_ERROR_EXISTS is returned.
2086 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2087 * error is returned. If trying to overwrite a directory with a directory the
2088 * G_IO_ERROR_WOULD_MERGE error is returned.
2090 * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2091 * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2094 * If you are interested in copying the #GFile object itself (not the on-disk
2095 * file), see g_file_dup().
2097 * Returns: %TRUE on success, %FALSE otherwise.
2100 g_file_copy (GFile *source,
2102 GFileCopyFlags flags,
2103 GCancellable *cancellable,
2104 GFileProgressCallback progress_callback,
2105 gpointer progress_callback_data,
2112 g_return_val_if_fail (G_IS_FILE (source), FALSE);
2113 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2115 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2118 if (G_OBJECT_TYPE (source) == G_OBJECT_TYPE (destination))
2120 iface = G_FILE_GET_IFACE (source);
2125 res = (* iface->copy) (source, destination, flags, cancellable, progress_callback, progress_callback_data, &my_error);
2130 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2132 g_propagate_error (error, my_error);
2138 return file_copy_fallback (source, destination, flags, cancellable,
2139 progress_callback, progress_callback_data,
2146 * @source: #GFile pointing to the source location.
2147 * @destination: #GFile pointing to the destination location.
2148 * @flags: set of #GFileCopyFlags.
2149 * @cancellable: optional #GCancellable object, %NULL to ignore.
2150 * @progress_callback: #GFileProgressCallback function for updates.
2151 * @progress_callback_data: gpointer to user data for the callback function.
2152 * @error: #GError for returning error conditions, or %NULL
2155 * Tries to move the file or directory @source to the location specified by @destination.
2156 * If native move operations is supported then this is used, otherwise a copy + delete
2157 * fallback is used. The native implementation may support moving directories (for instance
2158 * on moves inside the same filesystem), but the fallback code does not.
2160 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2161 * existing @destination file is overwritten.
2163 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2164 * will be copied as symlinks, otherwise the target of the
2165 * @source symlink will be copied.
2167 * If @cancellable is not %NULL, then the operation can be cancelled by
2168 * triggering the cancellable object from another thread. If the operation
2169 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2171 * If @progress_callback is not %NULL, then the operation can be monitored by
2172 * setting this to a #GFileProgressCallback function. @progress_callback_data
2173 * will be passed to this function. It is guaranteed that this callback will
2174 * be called after all data has been transferred with the total number of bytes
2175 * copied during the operation.
2177 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2178 * error is returned, independent on the status of the @destination.
2180 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2181 * error G_IO_ERROR_EXISTS is returned.
2183 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2184 * error is returned. If trying to overwrite a directory with a directory the
2185 * G_IO_ERROR_WOULD_MERGE error is returned.
2187 * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2188 * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2189 * may be returned (if the native move operation isn't available).
2191 * Returns: %TRUE on successful move, %FALSE otherwise.
2194 g_file_move (GFile *source,
2196 GFileCopyFlags flags,
2197 GCancellable *cancellable,
2198 GFileProgressCallback progress_callback,
2199 gpointer progress_callback_data,
2206 g_return_val_if_fail (G_IS_FILE (source), FALSE);
2207 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2209 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2212 if (G_OBJECT_TYPE (source) == G_OBJECT_TYPE (destination))
2214 iface = G_FILE_GET_IFACE (source);
2219 res = (* iface->move) (source, destination, flags, cancellable, progress_callback, progress_callback_data, &my_error);
2224 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2226 g_propagate_error (error, my_error);
2232 if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
2234 g_set_error (error, G_IO_ERROR,
2235 G_IO_ERROR_NOT_SUPPORTED,
2236 _("Operation not supported"));
2240 flags |= G_FILE_COPY_ALL_METADATA;
2241 if (!g_file_copy (source, destination, flags, cancellable,
2242 progress_callback, progress_callback_data,
2246 return g_file_delete (source, cancellable, error);
2250 * g_file_make_directory
2251 * @file: input #GFile.
2252 * @cancellable: optional #GCancellable object, %NULL to ignore.
2253 * @error: a #GError, or %NULL
2255 * Creates a directory.
2257 * If @cancellable is not %NULL, then the operation can be cancelled by
2258 * triggering the cancellable object from another thread. If the operation
2259 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2261 * Returns: %TRUE on successful creation, %FALSE otherwise.
2264 g_file_make_directory (GFile *file,
2265 GCancellable *cancellable,
2270 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2272 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2275 iface = G_FILE_GET_IFACE (file);
2277 if (iface->make_directory == NULL)
2279 g_set_error (error, G_IO_ERROR,
2280 G_IO_ERROR_NOT_SUPPORTED,
2281 _("Operation not supported"));
2285 return (* iface->make_directory) (file, cancellable, error);
2289 * g_file_make_symbolic_link:
2290 * @file: input #GFile.
2291 * @symlink_value: a string with the value of the new symlink.
2292 * @cancellable: optional #GCancellable object, %NULL to ignore.
2293 * @error: a #GError.
2295 * Creates a symbolic link.
2297 * If @cancellable is not %NULL, then the operation can be cancelled by
2298 * triggering the cancellable object from another thread. If the operation
2299 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2301 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
2304 g_file_make_symbolic_link (GFile *file,
2305 const char *symlink_value,
2306 GCancellable *cancellable,
2311 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2312 g_return_val_if_fail (symlink_value != NULL, FALSE);
2314 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2317 if (*symlink_value == '\0')
2319 g_set_error (error, G_IO_ERROR,
2320 G_IO_ERROR_INVALID_ARGUMENT,
2321 _("Invalid symlink value given"));
2325 iface = G_FILE_GET_IFACE (file);
2327 if (iface->make_symbolic_link == NULL)
2329 g_set_error (error, G_IO_ERROR,
2330 G_IO_ERROR_NOT_SUPPORTED,
2331 _("Operation not supported"));
2335 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
2340 * @file: input #GFile.
2341 * @cancellable: optional #GCancellable object, %NULL to ignore.
2342 * @error: a #GError, or %NULL
2346 * If @cancellable is not %NULL, then the operation can be cancelled by
2347 * triggering the cancellable object from another thread. If the operation
2348 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2350 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
2353 g_file_delete (GFile *file,
2354 GCancellable *cancellable,
2359 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2361 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2364 iface = G_FILE_GET_IFACE (file);
2366 if (iface->delete_file == NULL)
2368 g_set_error (error, G_IO_ERROR,
2369 G_IO_ERROR_NOT_SUPPORTED,
2370 _("Operation not supported"));
2374 return (* iface->delete_file) (file, cancellable, error);
2379 * @file: #GFile to send to trash.
2380 * @cancellable: optional #GCancellable object, %NULL to ignore.
2381 * @error: a #GError, or %NULL
2383 * Sends @file to the "Trashcan", if possible. This is similar to
2384 * deleting it, but the user can recover it before emptying the trashcan.
2385 * Not all file systems support trashing, so this call can return the
2386 * %G_IO_ERROR_NOT_SUPPORTED error.
2389 * If @cancellable is not %NULL, then the operation can be cancelled by
2390 * triggering the cancellable object from another thread. If the operation
2391 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2393 * Returns: %TRUE on successful trash, %FALSE otherwise.
2396 g_file_trash (GFile *file,
2397 GCancellable *cancellable,
2402 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2404 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2407 iface = G_FILE_GET_IFACE (file);
2409 if (iface->trash == NULL)
2412 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2413 _("Trash not supported"));
2417 return (* iface->trash) (file, cancellable, error);
2421 * g_file_set_display_name:
2422 * @file: input #GFile.
2423 * @display_name: a string.
2424 * @cancellable: optional #GCancellable object, %NULL to ignore.
2425 * @error: a #GError, or %NULL
2427 * Renames @file to the specified display name.
2429 * The display name is converted from UTF8 to the correct encoding for the target
2430 * filesystem if possible and the @file is renamed to this.
2432 * If you want to implement a rename operation in the user interface the edit name
2433 * (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the initial value in the rename
2434 * widget, and then the result after editing should be passed to g_file_set_display_name().
2436 * On success the resulting converted filename is returned.
2438 * If @cancellable is not %NULL, then the operation can be cancelled by
2439 * triggering the cancellable object from another thread. If the operation
2440 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2442 * Returns: a #GFile specifying what @file was renamed to, or %NULL if there was an error.
2445 g_file_set_display_name (GFile *file,
2446 const char *display_name,
2447 GCancellable *cancellable,
2452 g_return_val_if_fail (G_IS_FILE (file), NULL);
2453 g_return_val_if_fail (display_name != NULL, NULL);
2455 if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
2459 G_IO_ERROR_INVALID_ARGUMENT,
2460 _("File names cannot contain '%c'"), G_DIR_SEPARATOR);
2464 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2467 iface = G_FILE_GET_IFACE (file);
2469 return (* iface->set_display_name) (file, display_name, cancellable, error);
2473 * g_file_set_display_name_async:
2474 * @file: input #GFile.
2475 * @display_name: a string.
2476 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2478 * @cancellable: optional #GCancellable object, %NULL to ignore.
2479 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2480 * @user_data: the data to pass to callback function
2482 * Asynchronously sets the display name for a given #GFile.
2484 * For more details, see g_set_display_name() which is
2485 * the synchronous version of this call.
2487 * When the operation is finished, @callback will be called. You can then call
2488 * g_file_set_display_name_finish() to get the result of the operation.
2491 g_file_set_display_name_async (GFile *file,
2492 const char *display_name,
2494 GCancellable *cancellable,
2495 GAsyncReadyCallback callback,
2500 g_return_if_fail (G_IS_FILE (file));
2501 g_return_if_fail (display_name != NULL);
2503 iface = G_FILE_GET_IFACE (file);
2504 (* iface->set_display_name_async) (file,
2513 * g_file_set_display_name_finish:
2514 * @file: input #GFile.
2515 * @res: a #GAsyncResult.
2516 * @error: a #GError, or %NULL
2518 * Finishes setting a display name started with
2519 * g_file_set_display_name_async().
2521 * Returns: a #GFile or %NULL on error.
2524 g_file_set_display_name_finish (GFile *file,
2530 g_return_val_if_fail (G_IS_FILE (file), NULL);
2531 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2533 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2535 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2536 if (g_simple_async_result_propagate_error (simple, error))
2540 iface = G_FILE_GET_IFACE (file);
2541 return (* iface->set_display_name_finish) (file, res, error);
2545 * g_file_query_settable_attributes:
2546 * @file: input #GFile.
2547 * @cancellable: optional #GCancellable object, %NULL to ignore.
2548 * @error: a #GError, or %NULL
2550 * Obtain the list of settable attributes for the file.
2552 * Returns the type and full attribute name of all the attributes
2553 * that can be set on this file. This doesn't mean setting it will always
2554 * succeed though, you might get an access failure, or some specific
2555 * file may not support a specific attribute.
2557 * If @cancellable is not %NULL, then the operation can be cancelled by
2558 * triggering the cancellable object from another thread. If the operation
2559 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2561 * Returns: a #GFileAttributeInfoList describing the settable attributes.
2562 * When you are done with it, release it with g_file_attribute_info_list_unref()
2564 GFileAttributeInfoList *
2565 g_file_query_settable_attributes (GFile *file,
2566 GCancellable *cancellable,
2571 GFileAttributeInfoList *list;
2573 g_return_val_if_fail (G_IS_FILE (file), NULL);
2575 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2578 iface = G_FILE_GET_IFACE (file);
2580 if (iface->query_settable_attributes == NULL)
2581 return g_file_attribute_info_list_new ();
2584 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
2588 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
2590 list = g_file_attribute_info_list_new ();
2591 g_error_free (my_error);
2594 g_propagate_error (error, my_error);
2601 * g_file_query_writable_namespaces:
2602 * @file: input #GFile.
2603 * @cancellable: optional #GCancellable object, %NULL to ignore.
2604 * @error: a #GError, or %NULL
2606 * Obtain the list of attribute namespaces where new attributes
2607 * can be created by a user. An example of this is extended
2608 * attributes (in the "xattr" namespace).
2610 * If @cancellable is not %NULL, then the operation can be cancelled by
2611 * triggering the cancellable object from another thread. If the operation
2612 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2614 * Returns: a #GFileAttributeInfoList describing the writable namespaces.
2615 * When you are done with it, release it with g_file_attribute_info_list_unref()
2617 GFileAttributeInfoList *
2618 g_file_query_writable_namespaces (GFile *file,
2619 GCancellable *cancellable,
2624 GFileAttributeInfoList *list;
2626 g_return_val_if_fail (G_IS_FILE (file), NULL);
2628 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2631 iface = G_FILE_GET_IFACE (file);
2633 if (iface->query_writable_namespaces == NULL)
2634 return g_file_attribute_info_list_new ();
2637 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
2641 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
2643 list = g_file_attribute_info_list_new ();
2644 g_error_free (my_error);
2647 g_propagate_error (error, my_error);
2654 * g_file_set_attribute:
2655 * @file: input #GFile.
2656 * @attribute: a string containing the attribute's name.
2657 * @type: The type of the attribute
2658 * @value_p: a pointer to the value (or the pointer itself if the type is a pointer type)
2659 * @flags: a set of #GFileQueryInfoFlags.
2660 * @cancellable: optional #GCancellable object, %NULL to ignore.
2661 * @error: a #GError, or %NULL
2663 * Sets an attribute in the file with attribute name @attribute to @value.
2665 * If @cancellable is not %NULL, then the operation can be cancelled by
2666 * triggering the cancellable object from another thread. If the operation
2667 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2669 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
2672 g_file_set_attribute (GFile *file,
2673 const char *attribute,
2674 GFileAttributeType type,
2676 GFileQueryInfoFlags flags,
2677 GCancellable *cancellable,
2682 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2683 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
2685 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2688 iface = G_FILE_GET_IFACE (file);
2690 if (iface->set_attribute == NULL)
2692 g_set_error (error, G_IO_ERROR,
2693 G_IO_ERROR_NOT_SUPPORTED,
2694 _("Operation not supported"));
2698 return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
2702 * g_file_set_attributes_from_info:
2703 * @file: input #GFile.
2704 * @info: a #GFileInfo.
2705 * @flags: #GFileQueryInfoFlags
2706 * @cancellable: optional #GCancellable object, %NULL to ignore.
2707 * @error: a #GError, or %NULL
2709 * Tries to set all attributes in the #GFileInfo on the target values,
2710 * not stopping on the first error.
2712 * If there is any error during this operation then @error will be set to
2713 * the first error. Error on particular fields are flagged by setting
2714 * the "status" field in the attribute value to
2715 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can also detect
2718 * If @cancellable is not %NULL, then the operation can be cancelled by
2719 * triggering the cancellable object from another thread. If the operation
2720 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2722 * Returns: %TRUE if there was any error, %FALSE otherwise.
2725 g_file_set_attributes_from_info (GFile *file,
2727 GFileQueryInfoFlags flags,
2728 GCancellable *cancellable,
2733 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2734 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
2736 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2739 g_file_info_clear_status (info);
2741 iface = G_FILE_GET_IFACE (file);
2743 return (* iface->set_attributes_from_info) (file,
2752 g_file_real_set_attributes_from_info (GFile *file,
2754 GFileQueryInfoFlags flags,
2755 GCancellable *cancellable,
2761 GFileAttributeValue *value;
2765 attributes = g_file_info_list_attributes (info, NULL);
2767 for (i = 0; attributes[i] != NULL; i++)
2769 value = _g_file_info_get_attribute_value (info, attributes[i]);
2771 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
2774 if (!g_file_set_attribute (file, attributes[i],
2775 value->type, _g_file_attribute_value_peek_as_pointer (value),
2776 flags, cancellable, error))
2778 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2780 /* Don't set error multiple times */
2784 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2787 g_strfreev (attributes);
2793 * g_file_set_attributes_async:
2794 * @file: input #GFile.
2795 * @info: a #GFileInfo.
2796 * @flags: a #GFileQueryInfoFlags.
2797 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2799 * @cancellable: optional #GCancellable object, %NULL to ignore.
2800 * @callback: a #GAsyncReadyCallback.
2801 * @user_data: a #gpointer.
2803 * Asynchronously sets the attributes of @file with @info.
2805 * For more details, see g_file_set_attributes_from_info() which is
2806 * the synchronous version of this call.
2808 * When the operation is finished, @callback will be called. You can then call
2809 * g_file_set_attributes_finish() to get the result of the operation.
2812 g_file_set_attributes_async (GFile *file,
2814 GFileQueryInfoFlags flags,
2816 GCancellable *cancellable,
2817 GAsyncReadyCallback callback,
2822 g_return_if_fail (G_IS_FILE (file));
2823 g_return_if_fail (G_IS_FILE_INFO (info));
2825 iface = G_FILE_GET_IFACE (file);
2826 (* iface->set_attributes_async) (file,
2836 * g_file_set_attributes_finish:
2837 * @file: input #GFile.
2838 * @result: a #GAsyncResult.
2839 * @info: a #GFileInfo.
2840 * @error: a #GError, or %NULL
2842 * Finishes setting an attribute started in g_file_set_attributes_async().
2844 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
2847 g_file_set_attributes_finish (GFile *file,
2848 GAsyncResult *result,
2854 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2855 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
2857 /* No standard handling of errors here, as we must set info even
2860 iface = G_FILE_GET_IFACE (file);
2861 return (* iface->set_attributes_finish) (file, result, info, error);
2865 * g_file_set_attribute_string:
2866 * @file: input #GFile.
2867 * @attribute: a string containing the attribute's name.
2868 * @value: a string containing the attribute's value.
2869 * @flags: #GFileQueryInfoFlags.
2870 * @cancellable: optional #GCancellable object, %NULL to ignore.
2871 * @error: a #GError, or %NULL
2873 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
2874 * If @attribute is of a different type, this operation will fail.
2876 * If @cancellable is not %NULL, then the operation can be cancelled by
2877 * triggering the cancellable object from another thread. If the operation
2878 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2880 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
2883 g_file_set_attribute_string (GFile *file,
2884 const char *attribute,
2886 GFileQueryInfoFlags flags,
2887 GCancellable *cancellable,
2890 return g_file_set_attribute (file, attribute,
2891 G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
2892 flags, cancellable, error);
2896 * g_file_set_attribute_byte_string:
2897 * @file: input #GFile.
2898 * @attribute: a string containing the attribute's name.
2899 * @value: a string containing the attribute's new value.
2900 * @flags: a #GFileQueryInfoFlags.
2901 * @cancellable: optional #GCancellable object, %NULL to ignore.
2902 * @error: a #GError, or %NULL
2904 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
2905 * If @attribute is of a different type, this operation will fail,
2908 * If @cancellable is not %NULL, then the operation can be cancelled by
2909 * triggering the cancellable object from another thread. If the operation
2910 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2912 * Returns: %TRUE if the @attribute was successfully set to @value
2913 * in the @file, %FALSE otherwise.
2916 g_file_set_attribute_byte_string (GFile *file,
2917 const char *attribute,
2919 GFileQueryInfoFlags flags,
2920 GCancellable *cancellable,
2923 return g_file_set_attribute (file, attribute,
2924 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
2925 flags, cancellable, error);
2929 * g_file_set_attribute_uint32:
2930 * @file: input #GFile.
2931 * @attribute: a string containing the attribute's name.
2932 * @value: a #guint32 containing the attribute's new value.
2933 * @flags: a #GFileQueryInfoFlags.
2934 * @cancellable: optional #GCancellable object, %NULL to ignore.
2935 * @error: a #GError, or %NULL
2937 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
2938 * If @attribute is of a different type, this operation will fail.
2940 * If @cancellable is not %NULL, then the operation can be cancelled by
2941 * triggering the cancellable object from another thread. If the operation
2942 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2944 * Returns: %TRUE if the @attribute was successfully set to @value
2945 * in the @file, %FALSE otherwise.
2948 g_file_set_attribute_uint32 (GFile *file,
2949 const char *attribute,
2951 GFileQueryInfoFlags flags,
2952 GCancellable *cancellable,
2955 return g_file_set_attribute (file, attribute,
2956 G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
2957 flags, cancellable, error);
2961 * g_file_set_attribute_int32:
2962 * @file: input #GFile.
2963 * @attribute: a string containing the attribute's name.
2964 * @value: a #gint32 containing the attribute's new value.
2965 * @flags: a #GFileQueryInfoFlags.
2966 * @cancellable: optional #GCancellable object, %NULL to ignore.
2967 * @error: a #GError, or %NULL
2969 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
2970 * If @attribute is of a different type, this operation will fail.
2972 * If @cancellable is not %NULL, then the operation can be cancelled by
2973 * triggering the cancellable object from another thread. If the operation
2974 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2976 * Returns: %TRUE if the @attribute was successfully set to @value
2977 * in the @file, %FALSE otherwise.
2980 g_file_set_attribute_int32 (GFile *file,
2981 const char *attribute,
2983 GFileQueryInfoFlags flags,
2984 GCancellable *cancellable,
2987 return g_file_set_attribute (file, attribute,
2988 G_FILE_ATTRIBUTE_TYPE_INT32, &value,
2989 flags, cancellable, error);
2993 * g_file_set_attribute_uint64:
2994 * @file: input #GFile.
2995 * @attribute: a string containing the attribute's name.
2996 * @value: a #guint64 containing the attribute's new value.
2997 * @flags: a #GFileQueryInfoFlags.
2998 * @cancellable: optional #GCancellable object, %NULL to ignore.
2999 * @error: a #GError, or %NULL
3001 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
3002 * If @attribute is of a different type, this operation will fail.
3004 * If @cancellable is not %NULL, then the operation can be cancelled by
3005 * triggering the cancellable object from another thread. If the operation
3006 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3008 * Returns: %TRUE if the @attribute was successfully set to @value
3009 * in the @file, %FALSE otherwise.
3012 g_file_set_attribute_uint64 (GFile *file,
3013 const char *attribute,
3015 GFileQueryInfoFlags flags,
3016 GCancellable *cancellable,
3019 return g_file_set_attribute (file, attribute,
3020 G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
3021 flags, cancellable, error);
3025 * g_file_set_attribute_int64:
3026 * @file: input #GFile.
3027 * @attribute: a string containing the attribute's name.
3028 * @value: a #guint64 containing the attribute's new value.
3029 * @flags: a #GFileQueryInfoFlags.
3030 * @cancellable: optional #GCancellable object, %NULL to ignore.
3031 * @error: a #GError, or %NULL
3033 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
3034 * If @attribute is of a different type, this operation will fail.
3036 * If @cancellable is not %NULL, then the operation can be cancelled by
3037 * triggering the cancellable object from another thread. If the operation
3038 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3040 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
3043 g_file_set_attribute_int64 (GFile *file,
3044 const char *attribute,
3046 GFileQueryInfoFlags flags,
3047 GCancellable *cancellable,
3050 return g_file_set_attribute (file, attribute,
3051 G_FILE_ATTRIBUTE_TYPE_INT64, &value,
3052 flags, cancellable, error);
3056 * g_file_mount_mountable:
3057 * @file: input #GFile.
3058 * @mount_operation: a #GMountOperation, or %NULL to avoid user interaction.
3059 * @cancellable: optional #GCancellable object, %NULL to ignore.
3060 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3061 * @user_data: the data to pass to callback function
3063 * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
3064 * Using @mount_operation, you can request callbacks when, for instance,
3065 * passwords are needed during authentication.
3067 * If @cancellable is not %NULL, then the operation can be cancelled by
3068 * triggering the cancellable object from another thread. If the operation
3069 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3071 * When the operation is finished, @callback will be called. You can then call
3072 * g_file_mount_mountable_finish() to get the result of the operation.
3075 g_file_mount_mountable (GFile *file,
3076 GMountOperation *mount_operation,
3077 GCancellable *cancellable,
3078 GAsyncReadyCallback callback,
3083 g_return_if_fail (G_IS_FILE (file));
3085 iface = G_FILE_GET_IFACE (file);
3087 if (iface->mount_mountable == NULL)
3088 g_simple_async_report_error_in_idle (G_OBJECT (file),
3092 G_IO_ERROR_NOT_SUPPORTED,
3093 _("Operation not supported"));
3095 (* iface->mount_mountable) (file,
3103 * g_file_mount_mountable_finish:
3104 * @file: input #GFile.
3105 * @result: a #GAsyncResult.
3106 * @error: a #GError, or %NULL
3108 * Finishes a mount operation. See g_file_mount_mountable() for details.
3110 * Finish an asynchronous mount operation that was started
3111 * with g_file_mount_mountable().
3113 * Returns: a #GFile or %NULL on error.
3116 g_file_mount_mountable_finish (GFile *file,
3117 GAsyncResult *result,
3122 g_return_val_if_fail (G_IS_FILE (file), NULL);
3123 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
3125 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3127 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3128 if (g_simple_async_result_propagate_error (simple, error))
3132 iface = G_FILE_GET_IFACE (file);
3133 return (* iface->mount_mountable_finish) (file, result, error);
3137 * g_file_unmount_mountable:
3138 * @file: input #GFile.
3139 * @flags: flags affecting the operation
3140 * @cancellable: optional #GCancellable object, %NULL to ignore.
3141 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3142 * @user_data: the data to pass to callback function
3144 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
3146 * If @cancellable is not %NULL, then the operation can be cancelled by
3147 * triggering the cancellable object from another thread. If the operation
3148 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3150 * When the operation is finished, @callback will be called. You can then call
3151 * g_file_unmount_mountable_finish() to get the result of the operation.
3154 g_file_unmount_mountable (GFile *file,
3155 GMountUnmountFlags flags,
3156 GCancellable *cancellable,
3157 GAsyncReadyCallback callback,
3162 g_return_if_fail (G_IS_FILE (file));
3164 iface = G_FILE_GET_IFACE (file);
3166 if (iface->unmount_mountable == NULL)
3167 g_simple_async_report_error_in_idle (G_OBJECT (file),
3171 G_IO_ERROR_NOT_SUPPORTED,
3172 _("Operation not supported"));
3174 (* iface->unmount_mountable) (file,
3182 * g_file_unmount_mountable_finish:
3183 * @file: input #GFile.
3184 * @result: a #GAsyncResult.
3185 * @error: a #GError, or %NULL
3187 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
3189 * Finish an asynchronous unmount operation that was started
3190 * with g_file_unmount_mountable().
3192 * Returns: %TRUE if the operation finished successfully. %FALSE
3196 g_file_unmount_mountable_finish (GFile *file,
3197 GAsyncResult *result,
3202 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3203 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3205 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3207 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3208 if (g_simple_async_result_propagate_error (simple, error))
3212 iface = G_FILE_GET_IFACE (file);
3213 return (* iface->unmount_mountable_finish) (file, result, error);
3217 * g_file_eject_mountable:
3218 * @file: input #GFile.
3219 * @flags: flags affecting the operation
3220 * @cancellable: optional #GCancellable object, %NULL to ignore.
3221 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3222 * @user_data: the data to pass to callback function
3224 * Starts an asynchronous eject on a mountable.
3225 * When this operation has completed, @callback will be called with
3226 * @user_user data, and the operation can be finalized with
3227 * g_file_eject_mountable_finish().
3229 * If @cancellable is not %NULL, then the operation can be cancelled by
3230 * triggering the cancellable object from another thread. If the operation
3231 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3234 g_file_eject_mountable (GFile *file,
3235 GMountUnmountFlags flags,
3236 GCancellable *cancellable,
3237 GAsyncReadyCallback callback,
3242 g_return_if_fail (G_IS_FILE (file));
3244 iface = G_FILE_GET_IFACE (file);
3246 if (iface->eject_mountable == NULL)
3247 g_simple_async_report_error_in_idle (G_OBJECT (file),
3251 G_IO_ERROR_NOT_SUPPORTED,
3252 _("Operation not supported"));
3254 (* iface->eject_mountable) (file,
3262 * g_file_eject_mountable_finish:
3263 * @file: input #GFile.
3264 * @result: a #GAsyncResult.
3265 * @error: a #GError, or %NULL
3267 * Finishes an asynchronous eject operation started by
3268 * g_file_eject_mountable().
3270 * Returns: %TRUE if the @file was ejected successfully. %FALSE
3274 g_file_eject_mountable_finish (GFile *file,
3275 GAsyncResult *result,
3280 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3281 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3283 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3285 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3286 if (g_simple_async_result_propagate_error (simple, error))
3290 iface = G_FILE_GET_IFACE (file);
3291 return (* iface->eject_mountable_finish) (file, result, error);
3295 * g_file_monitor_directory:
3296 * @file: input #GFile.
3297 * @flags: a set of #GFileMonitorFlags.
3298 * @cancellable: optional #GCancellable object, %NULL to ignore.
3299 * @error: a #GError, or %NULL.
3301 * Obtains a directory monitor for the given file.
3302 * This may fail if directory monitoring is not supported.
3304 * If @cancellable is not %NULL, then the operation can be cancelled by
3305 * triggering the cancellable object from another thread. If the operation
3306 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3308 * Returns: a #GFileMonitor for the given @file,
3309 * or %NULL on error.
3312 g_file_monitor_directory (GFile *file,
3313 GFileMonitorFlags flags,
3314 GCancellable *cancellable,
3319 g_return_val_if_fail (G_IS_FILE (file), NULL);
3321 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3324 iface = G_FILE_GET_IFACE (file);
3326 if (iface->monitor_dir == NULL)
3328 g_set_error (error, G_IO_ERROR,
3329 G_IO_ERROR_NOT_SUPPORTED,
3330 _("Operation not supported"));
3334 return (* iface->monitor_dir) (file, flags, cancellable, error);
3338 * g_file_monitor_file:
3339 * @file: input #GFile.
3340 * @flags: a set of #GFileMonitorFlags.
3341 * @cancellable: optional #GCancellable object, %NULL to ignore.
3342 * @error: a #GError, or %NULL.
3344 * Obtains a file monitor for the given file. If no file notification
3345 * mechanism exists, then regular polling of the file is used.
3347 * If @cancellable is not %NULL, then the operation can be cancelled by
3348 * triggering the cancellable object from another thread. If the operation
3349 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3351 * Returns: a #GFileMonitor for the given @file.
3354 g_file_monitor_file (GFile *file,
3355 GFileMonitorFlags flags,
3356 GCancellable *cancellable,
3360 GFileMonitor *monitor;
3362 g_return_val_if_fail (G_IS_FILE (file), NULL);
3364 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3367 iface = G_FILE_GET_IFACE (file);
3371 if (iface->monitor_file)
3372 monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
3374 /* Fallback to polling */
3375 if (monitor == NULL)
3376 monitor = _g_poll_file_monitor_new (file);
3381 /********************************************
3382 * Default implementation of async ops *
3383 ********************************************/
3387 GFileQueryInfoFlags flags;
3389 } QueryInfoAsyncData;
3392 query_info_data_free (QueryInfoAsyncData *data)
3395 g_object_unref (data->info);
3396 g_free (data->attributes);
3401 query_info_async_thread (GSimpleAsyncResult *res,
3403 GCancellable *cancellable)
3405 GError *error = NULL;
3406 QueryInfoAsyncData *data;
3409 data = g_simple_async_result_get_op_res_gpointer (res);
3411 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3415 g_simple_async_result_set_from_error (res, error);
3416 g_error_free (error);
3423 g_file_real_query_info_async (GFile *file,
3424 const char *attributes,
3425 GFileQueryInfoFlags flags,
3427 GCancellable *cancellable,
3428 GAsyncReadyCallback callback,
3431 GSimpleAsyncResult *res;
3432 QueryInfoAsyncData *data;
3434 data = g_new0 (QueryInfoAsyncData, 1);
3435 data->attributes = g_strdup (attributes);
3436 data->flags = flags;
3438 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_info_async);
3439 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
3441 g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
3442 g_object_unref (res);
3446 g_file_real_query_info_finish (GFile *file,
3450 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3451 QueryInfoAsyncData *data;
3453 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_info_async);
3455 data = g_simple_async_result_get_op_res_gpointer (simple);
3457 return g_object_ref (data->info);
3464 GFileQueryInfoFlags flags;
3465 GFileEnumerator *enumerator;
3466 } EnumerateChildrenAsyncData;
3469 enumerate_children_data_free (EnumerateChildrenAsyncData *data)
3471 if (data->enumerator)
3472 g_object_unref (data->enumerator);
3473 g_free (data->attributes);
3478 enumerate_children_async_thread (GSimpleAsyncResult *res,
3480 GCancellable *cancellable)
3482 GError *error = NULL;
3483 EnumerateChildrenAsyncData *data;
3484 GFileEnumerator *enumerator;
3486 data = g_simple_async_result_get_op_res_gpointer (res);
3488 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3490 if (enumerator == NULL)
3492 g_simple_async_result_set_from_error (res, error);
3493 g_error_free (error);
3496 data->enumerator = enumerator;
3500 g_file_real_enumerate_children_async (GFile *file,
3501 const char *attributes,
3502 GFileQueryInfoFlags flags,
3504 GCancellable *cancellable,
3505 GAsyncReadyCallback callback,
3508 GSimpleAsyncResult *res;
3509 EnumerateChildrenAsyncData *data;
3511 data = g_new0 (EnumerateChildrenAsyncData, 1);
3512 data->attributes = g_strdup (attributes);
3513 data->flags = flags;
3515 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_enumerate_children_async);
3516 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)enumerate_children_data_free);
3518 g_simple_async_result_run_in_thread (res, enumerate_children_async_thread, io_priority, cancellable);
3519 g_object_unref (res);
3522 static GFileEnumerator *
3523 g_file_real_enumerate_children_finish (GFile *file,
3527 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3528 EnumerateChildrenAsyncData *data;
3530 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_enumerate_children_async);
3532 data = g_simple_async_result_get_op_res_gpointer (simple);
3533 if (data->enumerator)
3534 return g_object_ref (data->enumerator);
3540 open_read_async_thread (GSimpleAsyncResult *res,
3542 GCancellable *cancellable)
3545 GFileInputStream *stream;
3546 GError *error = NULL;
3548 iface = G_FILE_GET_IFACE (object);
3550 stream = iface->read_fn (G_FILE (object), cancellable, &error);
3554 g_simple_async_result_set_from_error (res, error);
3555 g_error_free (error);
3558 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3562 g_file_real_read_async (GFile *file,
3564 GCancellable *cancellable,
3565 GAsyncReadyCallback callback,
3568 GSimpleAsyncResult *res;
3570 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_read_async);
3572 g_simple_async_result_run_in_thread (res, open_read_async_thread, io_priority, cancellable);
3573 g_object_unref (res);
3576 static GFileInputStream *
3577 g_file_real_read_finish (GFile *file,
3581 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3584 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_read_async);
3586 op = g_simple_async_result_get_op_res_gpointer (simple);
3588 return g_object_ref (op);
3594 append_to_async_thread (GSimpleAsyncResult *res,
3596 GCancellable *cancellable)
3599 GFileCreateFlags *data;
3600 GFileOutputStream *stream;
3601 GError *error = NULL;
3603 iface = G_FILE_GET_IFACE (object);
3605 data = g_simple_async_result_get_op_res_gpointer (res);
3607 stream = iface->append_to (G_FILE (object), *data, cancellable, &error);
3611 g_simple_async_result_set_from_error (res, error);
3612 g_error_free (error);
3615 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3619 g_file_real_append_to_async (GFile *file,
3620 GFileCreateFlags flags,
3622 GCancellable *cancellable,
3623 GAsyncReadyCallback callback,
3626 GFileCreateFlags *data;
3627 GSimpleAsyncResult *res;
3629 data = g_new0 (GFileCreateFlags, 1);
3632 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_append_to_async);
3633 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
3635 g_simple_async_result_run_in_thread (res, append_to_async_thread, io_priority, cancellable);
3636 g_object_unref (res);
3639 static GFileOutputStream *
3640 g_file_real_append_to_finish (GFile *file,
3644 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3647 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_append_to_async);
3649 op = g_simple_async_result_get_op_res_gpointer (simple);
3651 return g_object_ref (op);
3657 create_async_thread (GSimpleAsyncResult *res,
3659 GCancellable *cancellable)
3662 GFileCreateFlags *data;
3663 GFileOutputStream *stream;
3664 GError *error = NULL;
3666 iface = G_FILE_GET_IFACE (object);
3668 data = g_simple_async_result_get_op_res_gpointer (res);
3670 stream = iface->create (G_FILE (object), *data, cancellable, &error);
3674 g_simple_async_result_set_from_error (res, error);
3675 g_error_free (error);
3678 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3682 g_file_real_create_async (GFile *file,
3683 GFileCreateFlags flags,
3685 GCancellable *cancellable,
3686 GAsyncReadyCallback callback,
3689 GFileCreateFlags *data;
3690 GSimpleAsyncResult *res;
3692 data = g_new0 (GFileCreateFlags, 1);
3695 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_create_async);
3696 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
3698 g_simple_async_result_run_in_thread (res, create_async_thread, io_priority, cancellable);
3699 g_object_unref (res);
3702 static GFileOutputStream *
3703 g_file_real_create_finish (GFile *file,
3707 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3710 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_create_async);
3712 op = g_simple_async_result_get_op_res_gpointer (simple);
3714 return g_object_ref (op);
3720 GFileOutputStream *stream;
3722 gboolean make_backup;
3723 GFileCreateFlags flags;
3727 replace_async_data_free (ReplaceAsyncData *data)
3730 g_object_unref (data->stream);
3731 g_free (data->etag);
3736 replace_async_thread (GSimpleAsyncResult *res,
3738 GCancellable *cancellable)
3741 GFileOutputStream *stream;
3742 GError *error = NULL;
3743 ReplaceAsyncData *data;
3745 iface = G_FILE_GET_IFACE (object);
3747 data = g_simple_async_result_get_op_res_gpointer (res);
3749 stream = iface->replace (G_FILE (object),
3758 g_simple_async_result_set_from_error (res, error);
3759 g_error_free (error);
3762 data->stream = stream;
3766 g_file_real_replace_async (GFile *file,
3768 gboolean make_backup,
3769 GFileCreateFlags flags,
3771 GCancellable *cancellable,
3772 GAsyncReadyCallback callback,
3775 GSimpleAsyncResult *res;
3776 ReplaceAsyncData *data;
3778 data = g_new0 (ReplaceAsyncData, 1);
3779 data->etag = g_strdup (etag);
3780 data->make_backup = make_backup;
3781 data->flags = flags;
3783 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_replace_async);
3784 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_async_data_free);
3786 g_simple_async_result_run_in_thread (res, replace_async_thread, io_priority, cancellable);
3787 g_object_unref (res);
3790 static GFileOutputStream *
3791 g_file_real_replace_finish (GFile *file,
3795 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3796 ReplaceAsyncData *data;
3798 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_replace_async);
3800 data = g_simple_async_result_get_op_res_gpointer (simple);
3802 return g_object_ref (data->stream);
3810 } SetDisplayNameAsyncData;
3813 set_display_name_data_free (SetDisplayNameAsyncData *data)
3815 g_free (data->name);
3817 g_object_unref (data->file);
3822 set_display_name_async_thread (GSimpleAsyncResult *res,
3824 GCancellable *cancellable)
3826 GError *error = NULL;
3827 SetDisplayNameAsyncData *data;
3830 data = g_simple_async_result_get_op_res_gpointer (res);
3832 file = g_file_set_display_name (G_FILE (object), data->name, cancellable, &error);
3836 g_simple_async_result_set_from_error (res, error);
3837 g_error_free (error);
3844 g_file_real_set_display_name_async (GFile *file,
3845 const char *display_name,
3847 GCancellable *cancellable,
3848 GAsyncReadyCallback callback,
3851 GSimpleAsyncResult *res;
3852 SetDisplayNameAsyncData *data;
3854 data = g_new0 (SetDisplayNameAsyncData, 1);
3855 data->name = g_strdup (display_name);
3857 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_display_name_async);
3858 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_display_name_data_free);
3860 g_simple_async_result_run_in_thread (res, set_display_name_async_thread, io_priority, cancellable);
3861 g_object_unref (res);
3865 g_file_real_set_display_name_finish (GFile *file,
3869 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3870 SetDisplayNameAsyncData *data;
3872 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_display_name_async);
3874 data = g_simple_async_result_get_op_res_gpointer (simple);
3876 return g_object_ref (data->file);
3882 GFileQueryInfoFlags flags;
3889 set_info_data_free (SetInfoAsyncData *data)
3892 g_object_unref (data->info);
3894 g_error_free (data->error);
3899 set_info_async_thread (GSimpleAsyncResult *res,
3901 GCancellable *cancellable)
3903 SetInfoAsyncData *data;
3905 data = g_simple_async_result_get_op_res_gpointer (res);
3908 data->res = g_file_set_attributes_from_info (G_FILE (object),
3916 g_file_real_set_attributes_async (GFile *file,
3918 GFileQueryInfoFlags flags,
3920 GCancellable *cancellable,
3921 GAsyncReadyCallback callback,
3924 GSimpleAsyncResult *res;
3925 SetInfoAsyncData *data;
3927 data = g_new0 (SetInfoAsyncData, 1);
3928 data->info = g_file_info_dup (info);
3929 data->flags = flags;
3931 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_attributes_async);
3932 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_info_data_free);
3934 g_simple_async_result_run_in_thread (res, set_info_async_thread, io_priority, cancellable);
3935 g_object_unref (res);
3939 g_file_real_set_attributes_finish (GFile *file,
3944 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3945 SetInfoAsyncData *data;
3947 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_attributes_async);
3949 data = g_simple_async_result_get_op_res_gpointer (simple);
3952 *info = g_object_ref (data->info);
3954 if (error != NULL && data->error)
3955 *error = g_error_copy (data->error);
3960 /********************************************
3961 * Default VFS operations *
3962 ********************************************/
3965 * g_file_new_for_path:
3966 * @path: a string containing a relative or absolute path.
3968 * Constructs a #GFile for a given path. This operation never
3969 * fails, but the returned object might not support any I/O
3970 * operation if @path is malformed.
3972 * Returns: a new #GFile for the given @path.
3975 g_file_new_for_path (const char *path)
3977 g_return_val_if_fail (path != NULL, NULL);
3979 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
3983 * g_file_new_for_uri:
3984 * @uri: a string containing a URI.
3986 * Constructs a #GFile for a given URI. This operation never
3987 * fails, but the returned object might not support any I/O
3988 * operation if @uri is malformed or if the uri type is
3991 * Returns: a #GFile for the given @uri.
3994 g_file_new_for_uri (const char *uri)
3996 g_return_val_if_fail (uri != NULL, NULL);
3998 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
4002 * g_file_parse_name:
4003 * @parse_name: a file name or path to be parsed.
4005 * Constructs a #GFile with the given @parse_name (i.e. something given by g_file_get_parse_name()).
4006 * This operation never fails, but the returned object might not support any I/O
4007 * operation if the @parse_name cannot be parsed.
4009 * Returns: a new #GFile.
4012 g_file_parse_name (const char *parse_name)
4014 g_return_val_if_fail (parse_name != NULL, NULL);
4016 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
4020 is_valid_scheme_character (char c)
4022 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
4026 has_valid_scheme (const char *uri)
4032 if (!is_valid_scheme_character (*p))
4037 } while (is_valid_scheme_character (*p));
4043 * g_file_new_for_commandline_arg:
4044 * @arg: a command line string.
4046 * Creates a #GFile with the given argument from the command line. The value of
4047 * @arg can be either a URI, an absolute path or a relative path resolved
4048 * relative to the current working directory.
4049 * This operation never fails, but the returned object might not support any
4050 * I/O operation if @arg points to a malformed path.
4052 * Returns: a new #GFile.
4055 g_file_new_for_commandline_arg (const char *arg)
4061 g_return_val_if_fail (arg != NULL, NULL);
4063 if (g_path_is_absolute (arg))
4064 return g_file_new_for_path (arg);
4066 if (has_valid_scheme (arg))
4067 return g_file_new_for_uri (arg);
4069 current_dir = g_get_current_dir ();
4070 filename = g_build_filename (current_dir, arg, NULL);
4071 g_free (current_dir);
4073 file = g_file_new_for_path (filename);
4080 * g_file_mount_enclosing_volume:
4081 * @location: input #GFile.
4082 * @mount_operation: a #GMountOperation or %NULL to avoid user interaction.
4083 * @cancellable: optional #GCancellable object, %NULL to ignore.
4084 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4085 * @user_data: the data to pass to callback function
4087 * Starts a @mount_operation, mounting the volume that contains the file @location.
4089 * When this operation has completed, @callback will be called with
4090 * @user_user data, and the operation can be finalized with
4091 * g_file_mount_enclosing_volume_finish().
4093 * If @cancellable is not %NULL, then the operation can be cancelled by
4094 * triggering the cancellable object from another thread. If the operation
4095 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4098 g_file_mount_enclosing_volume (GFile *location,
4099 GMountOperation *mount_operation,
4100 GCancellable *cancellable,
4101 GAsyncReadyCallback callback,
4106 g_return_if_fail (G_IS_FILE (location));
4108 iface = G_FILE_GET_IFACE (location);
4110 if (iface->mount_enclosing_volume == NULL)
4112 g_simple_async_report_error_in_idle (G_OBJECT (location),
4113 callback, user_data,
4114 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4115 _("volume doesn't implement mount"));
4120 (* iface->mount_enclosing_volume) (location, mount_operation, cancellable, callback, user_data);
4125 * g_file_mount_enclosing_volume_finish:
4126 * @location: input #GFile.
4127 * @result: a #GAsyncResult.
4128 * @error: a #GError, or %NULL
4130 * Finishes a mount operation started by g_file_mount_enclosing_volume().
4132 * Returns: %TRUE if successful. If an error
4133 * has occurred, this function will return %FALSE and set @error
4134 * appropriately if present.
4137 g_file_mount_enclosing_volume_finish (GFile *location,
4138 GAsyncResult *result,
4143 g_return_val_if_fail (G_IS_FILE (location), FALSE);
4144 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4146 if (G_IS_SIMPLE_ASYNC_RESULT (result))
4148 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
4149 if (g_simple_async_result_propagate_error (simple, error))
4153 iface = G_FILE_GET_IFACE (location);
4155 return (* iface->mount_enclosing_volume_finish) (location, result, error);
4158 /********************************************
4159 * Utility functions *
4160 ********************************************/
4162 #define GET_CONTENT_BLOCK_SIZE 8192
4165 * g_file_load_contents:
4166 * @file: input #GFile.
4167 * @cancellable: optional #GCancellable object, %NULL to ignore.
4168 * @contents: a location to place the contents of the file.
4169 * @length: a location to place the length of the contents of the file.
4170 * @etag_out: a location to place the current entity tag for the file.
4171 * @error: a #GError, or %NULL
4173 * Loads the content of the file into memory, returning the size of
4174 * the data. The data is always zero terminated, but this is not
4175 * included in the resultant @length.
4177 * If @cancellable is not %NULL, then the operation can be cancelled by
4178 * triggering the cancellable object from another thread. If the operation
4179 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4181 * Returns: %TRUE if the @file's contents were successfully loaded.
4182 * %FALSE if there were errors..
4185 g_file_load_contents (GFile *file,
4186 GCancellable *cancellable,
4192 GFileInputStream *in;
4193 GByteArray *content;
4198 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4199 g_return_val_if_fail (contents != NULL, FALSE);
4201 in = g_file_read (file, cancellable, error);
4205 content = g_byte_array_new ();
4208 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
4209 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
4210 content->data + pos,
4211 GET_CONTENT_BLOCK_SIZE,
4212 cancellable, error)) > 0)
4215 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
4222 info = g_file_input_stream_query_info (in,
4223 G_FILE_ATTRIBUTE_ETAG_VALUE,
4228 *etag_out = g_strdup (g_file_info_get_etag (info));
4229 g_object_unref (info);
4233 /* Ignore errors on close */
4234 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
4235 g_object_unref (in);
4239 /* error is set already */
4240 g_byte_array_free (content, TRUE);
4247 /* Zero terminate (we got an extra byte allocated for this */
4248 content->data[pos] = 0;
4250 *contents = (char *)g_byte_array_free (content, FALSE);
4258 GCancellable *cancellable;
4259 GFileReadMoreCallback read_more_callback;
4260 GAsyncReadyCallback callback;
4262 GByteArray *content;
4269 load_contents_data_free (LoadContentsData *data)
4272 g_error_free (data->error);
4273 if (data->cancellable)
4274 g_object_unref (data->cancellable);
4276 g_byte_array_free (data->content, TRUE);
4277 g_free (data->etag);
4278 g_object_unref (data->file);
4283 load_contents_close_callback (GObject *obj,
4284 GAsyncResult *close_res,
4287 GInputStream *stream = G_INPUT_STREAM (obj);
4288 LoadContentsData *data = user_data;
4289 GSimpleAsyncResult *res;
4291 /* Ignore errors here, we're only reading anyway */
4292 g_input_stream_close_finish (stream, close_res, NULL);
4293 g_object_unref (stream);
4295 res = g_simple_async_result_new (G_OBJECT (data->file),
4298 g_file_load_contents_async);
4299 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)load_contents_data_free);
4300 g_simple_async_result_complete (res);
4301 g_object_unref (res);
4305 load_contents_fstat_callback (GObject *obj,
4306 GAsyncResult *stat_res,
4309 GInputStream *stream = G_INPUT_STREAM (obj);
4310 LoadContentsData *data = user_data;
4313 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
4317 data->etag = g_strdup (g_file_info_get_etag (info));
4318 g_object_unref (info);
4321 g_input_stream_close_async (stream, 0,
4323 load_contents_close_callback, data);
4327 load_contents_read_callback (GObject *obj,
4328 GAsyncResult *read_res,
4331 GInputStream *stream = G_INPUT_STREAM (obj);
4332 LoadContentsData *data = user_data;
4333 GError *error = NULL;
4336 read_size = g_input_stream_read_finish (stream, read_res, &error);
4340 /* Error or EOF, close the file */
4341 data->error = error;
4342 g_input_stream_close_async (stream, 0,
4344 load_contents_close_callback, data);
4346 else if (read_size == 0)
4348 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
4349 G_FILE_ATTRIBUTE_ETAG_VALUE,
4352 load_contents_fstat_callback,
4355 else if (read_size > 0)
4357 data->pos += read_size;
4359 g_byte_array_set_size (data->content,
4360 data->pos + GET_CONTENT_BLOCK_SIZE);
4363 if (data->read_more_callback &&
4364 !data->read_more_callback ((char *)data->content->data, data->pos, data->user_data))
4365 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
4366 G_FILE_ATTRIBUTE_ETAG_VALUE,
4369 load_contents_fstat_callback,
4372 g_input_stream_read_async (stream,
4373 data->content->data + data->pos,
4374 GET_CONTENT_BLOCK_SIZE,
4377 load_contents_read_callback,
4383 load_contents_open_callback (GObject *obj,
4384 GAsyncResult *open_res,
4387 GFile *file = G_FILE (obj);
4388 GFileInputStream *stream;
4389 LoadContentsData *data = user_data;
4390 GError *error = NULL;
4391 GSimpleAsyncResult *res;
4393 stream = g_file_read_finish (file, open_res, &error);
4397 g_byte_array_set_size (data->content,
4398 data->pos + GET_CONTENT_BLOCK_SIZE);
4399 g_input_stream_read_async (G_INPUT_STREAM (stream),
4400 data->content->data + data->pos,
4401 GET_CONTENT_BLOCK_SIZE,
4404 load_contents_read_callback,
4410 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
4414 g_simple_async_result_complete (res);
4415 g_error_free (error);
4416 load_contents_data_free (data);
4417 g_object_unref (res);
4422 * g_file_load_partial_contents_async:
4423 * @file: input #GFile.
4424 * @cancellable: optional #GCancellable object, %NULL to ignore.
4425 * @read_more_callback: a #GFileReadMoreCallback to receive partial data and to specify whether further data should be read.
4426 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
4427 * @user_data: the data to pass to the callback functions.
4429 * Reads the partial contents of a file. A #GFileReadMoreCallback should be
4430 * used to stop reading from the file when appropriate, else this function
4431 * will behave exactly as g_file_load_contents_async(). This operation
4432 * can be finished by g_file_load_partial_contents_finish().
4434 * Users of this function should be aware that @user_data is passed to
4435 * both the @read_more_callback and the @callback.
4437 * If @cancellable is not %NULL, then the operation can be cancelled by
4438 * triggering the cancellable object from another thread. If the operation
4439 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4442 g_file_load_partial_contents_async (GFile *file,
4443 GCancellable *cancellable,
4444 GFileReadMoreCallback read_more_callback,
4445 GAsyncReadyCallback callback,
4448 LoadContentsData *data;
4450 g_return_if_fail (G_IS_FILE (file));
4452 data = g_new0 (LoadContentsData, 1);
4455 data->cancellable = g_object_ref (cancellable);
4456 data->read_more_callback = read_more_callback;
4457 data->callback = callback;
4458 data->user_data = user_data;
4459 data->content = g_byte_array_new ();
4460 data->file = g_object_ref (file);
4462 g_file_read_async (file,
4465 load_contents_open_callback,
4470 * g_file_load_partial_contents_finish:
4471 * @file: input #GFile.
4472 * @res: a #GAsyncResult.
4473 * @contents: a location to place the contents of the file.
4474 * @length: a location to place the length of the contents of the file.
4475 * @etag_out: a location to place the current entity tag for the file.
4476 * @error: a #GError, or %NULL
4478 * Finishes an asynchronous partial load operation that was started
4479 * with g_file_load_partial_contents_async().
4481 * Returns: %TRUE if the load was successful. If %FALSE and @error is
4482 * present, it will be set appropriately.
4485 g_file_load_partial_contents_finish (GFile *file,
4492 GSimpleAsyncResult *simple;
4493 LoadContentsData *data;
4495 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4496 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
4497 g_return_val_if_fail (contents != NULL, FALSE);
4499 simple = G_SIMPLE_ASYNC_RESULT (res);
4501 if (g_simple_async_result_propagate_error (simple, error))
4504 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_load_contents_async);
4506 data = g_simple_async_result_get_op_res_gpointer (simple);
4510 g_propagate_error (error, data->error);
4519 *length = data->pos;
4523 *etag_out = data->etag;
4527 /* Zero terminate */
4528 g_byte_array_set_size (data->content, data->pos + 1);
4529 data->content->data[data->pos] = 0;
4531 *contents = (char *)g_byte_array_free (data->content, FALSE);
4532 data->content = NULL;
4538 * g_file_load_contents_async:
4539 * @file: input #GFile.
4540 * @cancellable: optional #GCancellable object, %NULL to ignore.
4541 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
4542 * @user_data: the data to pass to callback function
4544 * Starts an asynchronous load of the @file's contents.
4546 * For more details, see g_file_load_contents() which is
4547 * the synchronous version of this call.
4549 * When the load operation has completed, @callback will be called
4550 * with @user data. To finish the operation, call
4551 * g_file_load_contents_finish() with the #GAsyncResult returned by
4554 * If @cancellable is not %NULL, then the operation can be cancelled by
4555 * triggering the cancellable object from another thread. If the operation
4556 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4559 g_file_load_contents_async (GFile *file,
4560 GCancellable *cancellable,
4561 GAsyncReadyCallback callback,
4564 g_file_load_partial_contents_async (file,
4567 callback, user_data);
4571 * g_file_load_contents_finish:
4572 * @file: input #GFile.
4573 * @res: a #GAsyncResult.
4574 * @contents: a location to place the contents of the file.
4575 * @length: a location to place the length of the contents of the file.
4576 * @etag_out: a location to place the current entity tag for the file.
4577 * @error: a #GError, or %NULL
4579 * Finishes an asynchronous load of the @file's contents.
4580 * The contents are placed in @contents, and @length is set to the
4581 * size of the @contents string. If @etag_out is present, it will be
4582 * set to the new entity tag for the @file.
4584 * Returns: %TRUE if the load was successful. If %FALSE and @error is
4585 * present, it will be set appropriately.
4588 g_file_load_contents_finish (GFile *file,
4595 return g_file_load_partial_contents_finish (file,
4604 * g_file_replace_contents:
4605 * @file: input #GFile.
4606 * @contents: a string containing the new contents for @file.
4607 * @length: the length of @contents in bytes.
4608 * @etag: the old <link linkend="gfile-etag">entity tag</link>
4610 * @make_backup: %TRUE if a backup should be created.
4611 * @flags: a set of #GFileCreateFlags.
4612 * @new_etag: a location to a new <link linkend="gfile-etag">entity tag</link>
4614 * @cancellable: optional #GCancellable object, %NULL to ignore.
4615 * @error: a #GError, or %NULL
4617 * Replaces the contents of @file with @contents of @length bytes.
4619 * If @etag is specified (not %NULL) any existing file must have that etag, or
4620 * the error %G_IO_ERROR_WRONG_ETAG will be returned.
4622 * If @make_backup is %TRUE, this function will attempt to make a backup of @file.
4624 * If @cancellable is not %NULL, then the operation can be cancelled by
4625 * triggering the cancellable object from another thread. If the operation
4626 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4628 * The returned @new_etag can be used to verify that the file hasn't changed the
4629 * next time it is saved over.
4631 * Returns: %TRUE if successful. If an error
4632 * has occurred, this function will return %FALSE and set @error
4633 * appropriately if present.
4636 g_file_replace_contents (GFile *file,
4637 const char *contents,
4640 gboolean make_backup,
4641 GFileCreateFlags flags,
4643 GCancellable *cancellable,
4646 GFileOutputStream *out;
4647 gsize pos, remainder;
4650 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4651 g_return_val_if_fail (contents != NULL, FALSE);
4653 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
4659 while (remainder > 0 &&
4660 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
4662 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
4670 if (remainder > 0 && res < 0)
4672 /* Ignore errors on close */
4673 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
4675 /* error is set already */
4679 if (!g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error))
4683 *new_etag = g_file_output_stream_get_etag (out);
4691 GCancellable *cancellable;
4692 GAsyncReadyCallback callback;
4694 const char *content;
4698 } ReplaceContentsData;
4701 replace_contents_data_free (ReplaceContentsData *data)
4704 g_error_free (data->error);
4705 if (data->cancellable)
4706 g_object_unref (data->cancellable);
4707 g_object_unref (data->file);
4708 g_free (data->etag);
4713 replace_contents_close_callback (GObject *obj,
4714 GAsyncResult *close_res,
4717 GOutputStream *stream = G_OUTPUT_STREAM (obj);
4718 ReplaceContentsData *data = user_data;
4719 GSimpleAsyncResult *res;
4721 /* Ignore errors here, we're only reading anyway */
4722 g_output_stream_close_finish (stream, close_res, NULL);
4723 g_object_unref (stream);
4725 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
4727 res = g_simple_async_result_new (G_OBJECT (data->file),
4730 g_file_replace_contents_async);
4731 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_contents_data_free);
4732 g_simple_async_result_complete (res);
4733 g_object_unref (res);
4737 replace_contents_write_callback (GObject *obj,
4738 GAsyncResult *read_res,
4741 GOutputStream *stream = G_OUTPUT_STREAM (obj);
4742 ReplaceContentsData *data = user_data;
4743 GError *error = NULL;
4746 write_size = g_output_stream_write_finish (stream, read_res, &error);
4748 if (write_size <= 0)
4750 /* Error or EOF, close the file */
4752 data->error = error;
4753 g_output_stream_close_async (stream, 0,
4755 replace_contents_close_callback, data);
4757 else if (write_size > 0)
4759 data->pos += write_size;
4761 if (data->pos >= data->length)
4762 g_output_stream_close_async (stream, 0,
4764 replace_contents_close_callback, data);
4766 g_output_stream_write_async (stream,
4767 data->content + data->pos,
4768 data->length - data->pos,
4771 replace_contents_write_callback,
4777 replace_contents_open_callback (GObject *obj,
4778 GAsyncResult *open_res,
4781 GFile *file = G_FILE (obj);
4782 GFileOutputStream *stream;
4783 ReplaceContentsData *data = user_data;
4784 GError *error = NULL;
4785 GSimpleAsyncResult *res;
4787 stream = g_file_replace_finish (file, open_res, &error);
4791 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
4792 data->content + data->pos,
4793 data->length - data->pos,
4796 replace_contents_write_callback,
4802 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
4806 g_simple_async_result_complete (res);
4807 g_error_free (error);
4808 replace_contents_data_free (data);
4809 g_object_unref (res);
4814 * g_file_replace_contents_async:
4815 * @file: input #GFile.
4816 * @contents: string of contents to replace the file with.
4817 * @length: the length of @contents in bytes.
4818 * @etag: a new <link linkend="gfile-etag">entity tag</link> for the @file.
4819 * @make_backup: %TRUE if a backup should be created.
4820 * @flags: a set of #GFileCreateFlags.
4821 * @cancellable: optional #GCancellable object, %NULL to ignore.
4822 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
4823 * @user_data: the data to pass to callback function
4825 * Starts an asynchronous replacement of @file with the given
4826 * @contents of @length bytes. @etag will replace the document's
4827 * current entity tag.
4829 * When this operation has completed, @callback will be called with
4830 * @user_user data, and the operation can be finalized with
4831 * g_file_replace_contents_finish().
4833 * If @cancellable is not %NULL, then the operation can be cancelled by
4834 * triggering the cancellable object from another thread. If the operation
4835 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4837 * If @make_backup is %TRUE, this function will attempt to
4838 * make a backup of @file.
4841 g_file_replace_contents_async (GFile *file,
4842 const char *contents,
4845 gboolean make_backup,
4846 GFileCreateFlags flags,
4847 GCancellable *cancellable,
4848 GAsyncReadyCallback callback,
4851 ReplaceContentsData *data;
4853 g_return_if_fail (G_IS_FILE (file));
4854 g_return_if_fail (contents != NULL);
4856 data = g_new0 (ReplaceContentsData, 1);
4859 data->cancellable = g_object_ref (cancellable);
4860 data->callback = callback;
4861 data->user_data = user_data;
4862 data->content = contents;
4863 data->length = length;
4865 data->file = g_object_ref (file);
4867 g_file_replace_async (file,
4873 replace_contents_open_callback,
4878 * g_file_replace_contents_finish:
4879 * @file: input #GFile.
4880 * @res: a #GAsyncResult.
4881 * @new_etag: a location of a new <link linkend="gfile-etag">entity tag</link>
4883 * @error: a #GError, or %NULL
4885 * Finishes an asynchronous replace of the given @file. See
4886 * g_file_replace_contents_async(). Sets @new_etag to the new entity
4887 * tag for the document, if present.
4889 * Returns: %TRUE on success, %FALSE on failure.
4892 g_file_replace_contents_finish (GFile *file,
4897 GSimpleAsyncResult *simple;
4898 ReplaceContentsData *data;
4900 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4901 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
4903 simple = G_SIMPLE_ASYNC_RESULT (res);
4905 if (g_simple_async_result_propagate_error (simple, error))
4908 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_replace_contents_async);
4910 data = g_simple_async_result_get_op_res_gpointer (simple);
4914 g_propagate_error (error, data->error);
4922 *new_etag = data->etag;
4923 data->etag = NULL; /* Take ownership */
4929 #define __G_FILE_C__
4930 #include "gioaliasdef.c"