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 file system 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 it's 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 it's 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 it's 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 iface = G_FILE_GET_IFACE (destination);
2122 res = (* iface->copy) (source, destination,
2124 progress_callback, progress_callback_data,
2130 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2132 g_propagate_error (error, my_error);
2137 /* If the types are different, and the destination method failed
2138 also try the source method */
2139 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
2141 iface = G_FILE_GET_IFACE (source);
2146 res = (* iface->copy) (source, destination,
2148 progress_callback, progress_callback_data,
2154 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2156 g_propagate_error (error, my_error);
2162 return file_copy_fallback (source, destination, flags, cancellable,
2163 progress_callback, progress_callback_data,
2170 * @source: #GFile pointing to the source location.
2171 * @destination: #GFile pointing to the destination location.
2172 * @flags: set of #GFileCopyFlags.
2173 * @cancellable: optional #GCancellable object, %NULL to ignore.
2174 * @progress_callback: #GFileProgressCallback function for updates.
2175 * @progress_callback_data: gpointer to user data for the callback function.
2176 * @error: #GError for returning error conditions, or %NULL
2179 * Tries to move the file or directory @source to the location specified by @destination.
2180 * If native move operations is supported then this is used, otherwise a copy + delete
2181 * fallback is used. The native implementation may support moving directories (for instance
2182 * on moves inside the same filesystem), but the fallback code does not.
2184 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2185 * existing @destination file is overwritten.
2187 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2188 * will be copied as symlinks, otherwise the target of the
2189 * @source symlink will be copied.
2191 * If @cancellable is not %NULL, then the operation can be cancelled by
2192 * triggering the cancellable object from another thread. If the operation
2193 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2195 * If @progress_callback is not %NULL, then the operation can be monitored by
2196 * setting this to a #GFileProgressCallback function. @progress_callback_data
2197 * will be passed to this function. It is guaranteed that this callback will
2198 * be called after all data has been transferred with the total number of bytes
2199 * copied during the operation.
2201 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2202 * error is returned, independent on the status of the @destination.
2204 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2205 * error G_IO_ERROR_EXISTS is returned.
2207 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2208 * error is returned. If trying to overwrite a directory with a directory the
2209 * G_IO_ERROR_WOULD_MERGE error is returned.
2211 * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2212 * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2213 * may be returned (if the native move operation isn't available).
2215 * Returns: %TRUE on successful move, %FALSE otherwise.
2218 g_file_move (GFile *source,
2220 GFileCopyFlags flags,
2221 GCancellable *cancellable,
2222 GFileProgressCallback progress_callback,
2223 gpointer progress_callback_data,
2230 g_return_val_if_fail (G_IS_FILE (source), FALSE);
2231 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2233 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2236 iface = G_FILE_GET_IFACE (destination);
2240 res = (* iface->move) (source, destination,
2242 progress_callback, progress_callback_data,
2248 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2250 g_propagate_error (error, my_error);
2255 /* If the types are different, and the destination method failed
2256 also try the source method */
2257 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
2259 iface = G_FILE_GET_IFACE (source);
2264 res = (* iface->move) (source, destination,
2266 progress_callback, progress_callback_data,
2272 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2274 g_propagate_error (error, my_error);
2280 if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
2282 g_set_error (error, G_IO_ERROR,
2283 G_IO_ERROR_NOT_SUPPORTED,
2284 _("Operation not supported"));
2288 flags |= G_FILE_COPY_ALL_METADATA;
2289 if (!g_file_copy (source, destination, flags, cancellable,
2290 progress_callback, progress_callback_data,
2294 return g_file_delete (source, cancellable, error);
2298 * g_file_make_directory
2299 * @file: input #GFile.
2300 * @cancellable: optional #GCancellable object, %NULL to ignore.
2301 * @error: a #GError, or %NULL
2303 * Creates a directory.
2305 * If @cancellable is not %NULL, then the operation can be cancelled by
2306 * triggering the cancellable object from another thread. If the operation
2307 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2309 * Returns: %TRUE on successful creation, %FALSE otherwise.
2312 g_file_make_directory (GFile *file,
2313 GCancellable *cancellable,
2318 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2320 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2323 iface = G_FILE_GET_IFACE (file);
2325 if (iface->make_directory == NULL)
2327 g_set_error (error, G_IO_ERROR,
2328 G_IO_ERROR_NOT_SUPPORTED,
2329 _("Operation not supported"));
2333 return (* iface->make_directory) (file, cancellable, error);
2337 * g_file_make_symbolic_link:
2338 * @file: input #GFile.
2339 * @symlink_value: a string with the value of the new symlink.
2340 * @cancellable: optional #GCancellable object, %NULL to ignore.
2341 * @error: a #GError.
2343 * Creates a symbolic link.
2345 * If @cancellable is not %NULL, then the operation can be cancelled by
2346 * triggering the cancellable object from another thread. If the operation
2347 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2349 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
2352 g_file_make_symbolic_link (GFile *file,
2353 const char *symlink_value,
2354 GCancellable *cancellable,
2359 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2360 g_return_val_if_fail (symlink_value != NULL, FALSE);
2362 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2365 if (*symlink_value == '\0')
2367 g_set_error (error, G_IO_ERROR,
2368 G_IO_ERROR_INVALID_ARGUMENT,
2369 _("Invalid symlink value given"));
2373 iface = G_FILE_GET_IFACE (file);
2375 if (iface->make_symbolic_link == NULL)
2377 g_set_error (error, G_IO_ERROR,
2378 G_IO_ERROR_NOT_SUPPORTED,
2379 _("Operation not supported"));
2383 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
2388 * @file: input #GFile.
2389 * @cancellable: optional #GCancellable object, %NULL to ignore.
2390 * @error: a #GError, or %NULL
2394 * If @cancellable is not %NULL, then the operation can be cancelled by
2395 * triggering the cancellable object from another thread. If the operation
2396 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2398 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
2401 g_file_delete (GFile *file,
2402 GCancellable *cancellable,
2407 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2409 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2412 iface = G_FILE_GET_IFACE (file);
2414 if (iface->delete_file == NULL)
2416 g_set_error (error, G_IO_ERROR,
2417 G_IO_ERROR_NOT_SUPPORTED,
2418 _("Operation not supported"));
2422 return (* iface->delete_file) (file, cancellable, error);
2427 * @file: #GFile to send to trash.
2428 * @cancellable: optional #GCancellable object, %NULL to ignore.
2429 * @error: a #GError, or %NULL
2431 * Sends @file to the "Trashcan", if possible. This is similar to
2432 * deleting it, but the user can recover it before emptying the trashcan.
2433 * Not all file systems support trashing, so this call can return the
2434 * %G_IO_ERROR_NOT_SUPPORTED error.
2437 * If @cancellable is not %NULL, then the operation can be cancelled by
2438 * triggering the cancellable object from another thread. If the operation
2439 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2441 * Returns: %TRUE on successful trash, %FALSE otherwise.
2444 g_file_trash (GFile *file,
2445 GCancellable *cancellable,
2450 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2452 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2455 iface = G_FILE_GET_IFACE (file);
2457 if (iface->trash == NULL)
2460 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2461 _("Trash not supported"));
2465 return (* iface->trash) (file, cancellable, error);
2469 * g_file_set_display_name:
2470 * @file: input #GFile.
2471 * @display_name: a string.
2472 * @cancellable: optional #GCancellable object, %NULL to ignore.
2473 * @error: a #GError, or %NULL
2475 * Renames @file to the specified display name.
2477 * The display name is converted from UTF8 to the correct encoding for the target
2478 * filesystem if possible and the @file is renamed to this.
2480 * If you want to implement a rename operation in the user interface the edit name
2481 * (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the initial value in the rename
2482 * widget, and then the result after editing should be passed to g_file_set_display_name().
2484 * On success the resulting converted filename is returned.
2486 * If @cancellable is not %NULL, then the operation can be cancelled by
2487 * triggering the cancellable object from another thread. If the operation
2488 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2490 * Returns: a #GFile specifying what @file was renamed to, or %NULL if there was an error.
2493 g_file_set_display_name (GFile *file,
2494 const char *display_name,
2495 GCancellable *cancellable,
2500 g_return_val_if_fail (G_IS_FILE (file), NULL);
2501 g_return_val_if_fail (display_name != NULL, NULL);
2503 if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
2507 G_IO_ERROR_INVALID_ARGUMENT,
2508 _("File names cannot contain '%c'"), G_DIR_SEPARATOR);
2512 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2515 iface = G_FILE_GET_IFACE (file);
2517 return (* iface->set_display_name) (file, display_name, cancellable, error);
2521 * g_file_set_display_name_async:
2522 * @file: input #GFile.
2523 * @display_name: a string.
2524 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2526 * @cancellable: optional #GCancellable object, %NULL to ignore.
2527 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2528 * @user_data: the data to pass to callback function
2530 * Asynchronously sets the display name for a given #GFile.
2532 * For more details, see g_set_display_name() which is
2533 * the synchronous version of this call.
2535 * When the operation is finished, @callback will be called. You can then call
2536 * g_file_set_display_name_finish() to get the result of the operation.
2539 g_file_set_display_name_async (GFile *file,
2540 const char *display_name,
2542 GCancellable *cancellable,
2543 GAsyncReadyCallback callback,
2548 g_return_if_fail (G_IS_FILE (file));
2549 g_return_if_fail (display_name != NULL);
2551 iface = G_FILE_GET_IFACE (file);
2552 (* iface->set_display_name_async) (file,
2561 * g_file_set_display_name_finish:
2562 * @file: input #GFile.
2563 * @res: a #GAsyncResult.
2564 * @error: a #GError, or %NULL
2566 * Finishes setting a display name started with
2567 * g_file_set_display_name_async().
2569 * Returns: a #GFile or %NULL on error.
2572 g_file_set_display_name_finish (GFile *file,
2578 g_return_val_if_fail (G_IS_FILE (file), NULL);
2579 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2581 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2583 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2584 if (g_simple_async_result_propagate_error (simple, error))
2588 iface = G_FILE_GET_IFACE (file);
2589 return (* iface->set_display_name_finish) (file, res, error);
2593 * g_file_query_settable_attributes:
2594 * @file: input #GFile.
2595 * @cancellable: optional #GCancellable object, %NULL to ignore.
2596 * @error: a #GError, or %NULL
2598 * Obtain the list of settable attributes for the file.
2600 * Returns the type and full attribute name of all the attributes
2601 * that can be set on this file. This doesn't mean setting it will always
2602 * succeed though, you might get an access failure, or some specific
2603 * file may not support a specific attribute.
2605 * If @cancellable is not %NULL, then the operation can be cancelled by
2606 * triggering the cancellable object from another thread. If the operation
2607 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2609 * Returns: a #GFileAttributeInfoList describing the settable attributes.
2610 * When you are done with it, release it with g_file_attribute_info_list_unref()
2612 GFileAttributeInfoList *
2613 g_file_query_settable_attributes (GFile *file,
2614 GCancellable *cancellable,
2619 GFileAttributeInfoList *list;
2621 g_return_val_if_fail (G_IS_FILE (file), NULL);
2623 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2626 iface = G_FILE_GET_IFACE (file);
2628 if (iface->query_settable_attributes == NULL)
2629 return g_file_attribute_info_list_new ();
2632 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
2636 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
2638 list = g_file_attribute_info_list_new ();
2639 g_error_free (my_error);
2642 g_propagate_error (error, my_error);
2649 * g_file_query_writable_namespaces:
2650 * @file: input #GFile.
2651 * @cancellable: optional #GCancellable object, %NULL to ignore.
2652 * @error: a #GError, or %NULL
2654 * Obtain the list of attribute namespaces where new attributes
2655 * can be created by a user. An example of this is extended
2656 * attributes (in the "xattr" namespace).
2658 * If @cancellable is not %NULL, then the operation can be cancelled by
2659 * triggering the cancellable object from another thread. If the operation
2660 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2662 * Returns: a #GFileAttributeInfoList describing the writable namespaces.
2663 * When you are done with it, release it with g_file_attribute_info_list_unref()
2665 GFileAttributeInfoList *
2666 g_file_query_writable_namespaces (GFile *file,
2667 GCancellable *cancellable,
2672 GFileAttributeInfoList *list;
2674 g_return_val_if_fail (G_IS_FILE (file), NULL);
2676 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2679 iface = G_FILE_GET_IFACE (file);
2681 if (iface->query_writable_namespaces == NULL)
2682 return g_file_attribute_info_list_new ();
2685 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
2689 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
2691 list = g_file_attribute_info_list_new ();
2692 g_error_free (my_error);
2695 g_propagate_error (error, my_error);
2702 * g_file_set_attribute:
2703 * @file: input #GFile.
2704 * @attribute: a string containing the attribute's name.
2705 * @type: The type of the attribute
2706 * @value_p: a pointer to the value (or the pointer itself if the type is a pointer type)
2707 * @flags: a set of #GFileQueryInfoFlags.
2708 * @cancellable: optional #GCancellable object, %NULL to ignore.
2709 * @error: a #GError, or %NULL
2711 * Sets an attribute in the file with attribute name @attribute to @value.
2713 * If @cancellable is not %NULL, then the operation can be cancelled by
2714 * triggering the cancellable object from another thread. If the operation
2715 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2717 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
2720 g_file_set_attribute (GFile *file,
2721 const char *attribute,
2722 GFileAttributeType type,
2724 GFileQueryInfoFlags flags,
2725 GCancellable *cancellable,
2730 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2731 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
2733 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2736 iface = G_FILE_GET_IFACE (file);
2738 if (iface->set_attribute == NULL)
2740 g_set_error (error, G_IO_ERROR,
2741 G_IO_ERROR_NOT_SUPPORTED,
2742 _("Operation not supported"));
2746 return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
2750 * g_file_set_attributes_from_info:
2751 * @file: input #GFile.
2752 * @info: a #GFileInfo.
2753 * @flags: #GFileQueryInfoFlags
2754 * @cancellable: optional #GCancellable object, %NULL to ignore.
2755 * @error: a #GError, or %NULL
2757 * Tries to set all attributes in the #GFileInfo on the target values,
2758 * not stopping on the first error.
2760 * If there is any error during this operation then @error will be set to
2761 * the first error. Error on particular fields are flagged by setting
2762 * the "status" field in the attribute value to
2763 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can also detect
2766 * If @cancellable is not %NULL, then the operation can be cancelled by
2767 * triggering the cancellable object from another thread. If the operation
2768 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2770 * Returns: %TRUE if there was any error, %FALSE otherwise.
2773 g_file_set_attributes_from_info (GFile *file,
2775 GFileQueryInfoFlags flags,
2776 GCancellable *cancellable,
2781 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2782 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
2784 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2787 g_file_info_clear_status (info);
2789 iface = G_FILE_GET_IFACE (file);
2791 return (* iface->set_attributes_from_info) (file,
2800 g_file_real_set_attributes_from_info (GFile *file,
2802 GFileQueryInfoFlags flags,
2803 GCancellable *cancellable,
2809 GFileAttributeValue *value;
2813 attributes = g_file_info_list_attributes (info, NULL);
2815 for (i = 0; attributes[i] != NULL; i++)
2817 value = _g_file_info_get_attribute_value (info, attributes[i]);
2819 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
2822 if (!g_file_set_attribute (file, attributes[i],
2823 value->type, _g_file_attribute_value_peek_as_pointer (value),
2824 flags, cancellable, error))
2826 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2828 /* Don't set error multiple times */
2832 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2835 g_strfreev (attributes);
2841 * g_file_set_attributes_async:
2842 * @file: input #GFile.
2843 * @info: a #GFileInfo.
2844 * @flags: a #GFileQueryInfoFlags.
2845 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2847 * @cancellable: optional #GCancellable object, %NULL to ignore.
2848 * @callback: a #GAsyncReadyCallback.
2849 * @user_data: a #gpointer.
2851 * Asynchronously sets the attributes of @file with @info.
2853 * For more details, see g_file_set_attributes_from_info() which is
2854 * the synchronous version of this call.
2856 * When the operation is finished, @callback will be called. You can then call
2857 * g_file_set_attributes_finish() to get the result of the operation.
2860 g_file_set_attributes_async (GFile *file,
2862 GFileQueryInfoFlags flags,
2864 GCancellable *cancellable,
2865 GAsyncReadyCallback callback,
2870 g_return_if_fail (G_IS_FILE (file));
2871 g_return_if_fail (G_IS_FILE_INFO (info));
2873 iface = G_FILE_GET_IFACE (file);
2874 (* iface->set_attributes_async) (file,
2884 * g_file_set_attributes_finish:
2885 * @file: input #GFile.
2886 * @result: a #GAsyncResult.
2887 * @info: a #GFileInfo.
2888 * @error: a #GError, or %NULL
2890 * Finishes setting an attribute started in g_file_set_attributes_async().
2892 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
2895 g_file_set_attributes_finish (GFile *file,
2896 GAsyncResult *result,
2902 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2903 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
2905 /* No standard handling of errors here, as we must set info even
2908 iface = G_FILE_GET_IFACE (file);
2909 return (* iface->set_attributes_finish) (file, result, info, error);
2913 * g_file_set_attribute_string:
2914 * @file: input #GFile.
2915 * @attribute: a string containing the attribute's name.
2916 * @value: a string containing the attribute's value.
2917 * @flags: #GFileQueryInfoFlags.
2918 * @cancellable: optional #GCancellable object, %NULL to ignore.
2919 * @error: a #GError, or %NULL
2921 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
2922 * If @attribute is of a different type, this operation will fail.
2924 * If @cancellable is not %NULL, then the operation can be cancelled by
2925 * triggering the cancellable object from another thread. If the operation
2926 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2928 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
2931 g_file_set_attribute_string (GFile *file,
2932 const char *attribute,
2934 GFileQueryInfoFlags flags,
2935 GCancellable *cancellable,
2938 return g_file_set_attribute (file, attribute,
2939 G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
2940 flags, cancellable, error);
2944 * g_file_set_attribute_byte_string:
2945 * @file: input #GFile.
2946 * @attribute: a string containing the attribute's name.
2947 * @value: a string containing the attribute's new value.
2948 * @flags: a #GFileQueryInfoFlags.
2949 * @cancellable: optional #GCancellable object, %NULL to ignore.
2950 * @error: a #GError, or %NULL
2952 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
2953 * If @attribute is of a different type, this operation will fail,
2956 * If @cancellable is not %NULL, then the operation can be cancelled by
2957 * triggering the cancellable object from another thread. If the operation
2958 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2960 * Returns: %TRUE if the @attribute was successfully set to @value
2961 * in the @file, %FALSE otherwise.
2964 g_file_set_attribute_byte_string (GFile *file,
2965 const char *attribute,
2967 GFileQueryInfoFlags flags,
2968 GCancellable *cancellable,
2971 return g_file_set_attribute (file, attribute,
2972 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
2973 flags, cancellable, error);
2977 * g_file_set_attribute_uint32:
2978 * @file: input #GFile.
2979 * @attribute: a string containing the attribute's name.
2980 * @value: a #guint32 containing the attribute's new value.
2981 * @flags: a #GFileQueryInfoFlags.
2982 * @cancellable: optional #GCancellable object, %NULL to ignore.
2983 * @error: a #GError, or %NULL
2985 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
2986 * If @attribute is of a different type, this operation will fail.
2988 * If @cancellable is not %NULL, then the operation can be cancelled by
2989 * triggering the cancellable object from another thread. If the operation
2990 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2992 * Returns: %TRUE if the @attribute was successfully set to @value
2993 * in the @file, %FALSE otherwise.
2996 g_file_set_attribute_uint32 (GFile *file,
2997 const char *attribute,
2999 GFileQueryInfoFlags flags,
3000 GCancellable *cancellable,
3003 return g_file_set_attribute (file, attribute,
3004 G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
3005 flags, cancellable, error);
3009 * g_file_set_attribute_int32:
3010 * @file: input #GFile.
3011 * @attribute: a string containing the attribute's name.
3012 * @value: a #gint32 containing the attribute's new value.
3013 * @flags: a #GFileQueryInfoFlags.
3014 * @cancellable: optional #GCancellable object, %NULL to ignore.
3015 * @error: a #GError, or %NULL
3017 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
3018 * If @attribute is of a different type, this operation will fail.
3020 * If @cancellable is not %NULL, then the operation can be cancelled by
3021 * triggering the cancellable object from another thread. If the operation
3022 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3024 * Returns: %TRUE if the @attribute was successfully set to @value
3025 * in the @file, %FALSE otherwise.
3028 g_file_set_attribute_int32 (GFile *file,
3029 const char *attribute,
3031 GFileQueryInfoFlags flags,
3032 GCancellable *cancellable,
3035 return g_file_set_attribute (file, attribute,
3036 G_FILE_ATTRIBUTE_TYPE_INT32, &value,
3037 flags, cancellable, error);
3041 * g_file_set_attribute_uint64:
3042 * @file: input #GFile.
3043 * @attribute: a string containing the attribute's name.
3044 * @value: a #guint64 containing the attribute's new value.
3045 * @flags: a #GFileQueryInfoFlags.
3046 * @cancellable: optional #GCancellable object, %NULL to ignore.
3047 * @error: a #GError, or %NULL
3049 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
3050 * If @attribute is of a different type, this operation will fail.
3052 * If @cancellable is not %NULL, then the operation can be cancelled by
3053 * triggering the cancellable object from another thread. If the operation
3054 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3056 * Returns: %TRUE if the @attribute was successfully set to @value
3057 * in the @file, %FALSE otherwise.
3060 g_file_set_attribute_uint64 (GFile *file,
3061 const char *attribute,
3063 GFileQueryInfoFlags flags,
3064 GCancellable *cancellable,
3067 return g_file_set_attribute (file, attribute,
3068 G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
3069 flags, cancellable, error);
3073 * g_file_set_attribute_int64:
3074 * @file: input #GFile.
3075 * @attribute: a string containing the attribute's name.
3076 * @value: a #guint64 containing the attribute's new value.
3077 * @flags: a #GFileQueryInfoFlags.
3078 * @cancellable: optional #GCancellable object, %NULL to ignore.
3079 * @error: a #GError, or %NULL
3081 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
3082 * If @attribute is of a different type, this operation will fail.
3084 * If @cancellable is not %NULL, then the operation can be cancelled by
3085 * triggering the cancellable object from another thread. If the operation
3086 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3088 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
3091 g_file_set_attribute_int64 (GFile *file,
3092 const char *attribute,
3094 GFileQueryInfoFlags flags,
3095 GCancellable *cancellable,
3098 return g_file_set_attribute (file, attribute,
3099 G_FILE_ATTRIBUTE_TYPE_INT64, &value,
3100 flags, cancellable, error);
3104 * g_file_mount_mountable:
3105 * @file: input #GFile.
3106 * @mount_operation: a #GMountOperation, or %NULL to avoid user interaction.
3107 * @cancellable: optional #GCancellable object, %NULL to ignore.
3108 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3109 * @user_data: the data to pass to callback function
3111 * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
3112 * Using @mount_operation, you can request callbacks when, for instance,
3113 * passwords are needed during authentication.
3115 * If @cancellable is not %NULL, then the operation can be cancelled by
3116 * triggering the cancellable object from another thread. If the operation
3117 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3119 * When the operation is finished, @callback will be called. You can then call
3120 * g_file_mount_mountable_finish() to get the result of the operation.
3123 g_file_mount_mountable (GFile *file,
3124 GMountOperation *mount_operation,
3125 GCancellable *cancellable,
3126 GAsyncReadyCallback callback,
3131 g_return_if_fail (G_IS_FILE (file));
3133 iface = G_FILE_GET_IFACE (file);
3135 if (iface->mount_mountable == NULL)
3136 g_simple_async_report_error_in_idle (G_OBJECT (file),
3140 G_IO_ERROR_NOT_SUPPORTED,
3141 _("Operation not supported"));
3143 (* iface->mount_mountable) (file,
3151 * g_file_mount_mountable_finish:
3152 * @file: input #GFile.
3153 * @result: a #GAsyncResult.
3154 * @error: a #GError, or %NULL
3156 * Finishes a mount operation. See g_file_mount_mountable() for details.
3158 * Finish an asynchronous mount operation that was started
3159 * with g_file_mount_mountable().
3161 * Returns: a #GFile or %NULL on error.
3164 g_file_mount_mountable_finish (GFile *file,
3165 GAsyncResult *result,
3170 g_return_val_if_fail (G_IS_FILE (file), NULL);
3171 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
3173 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3175 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3176 if (g_simple_async_result_propagate_error (simple, error))
3180 iface = G_FILE_GET_IFACE (file);
3181 return (* iface->mount_mountable_finish) (file, result, error);
3185 * g_file_unmount_mountable:
3186 * @file: input #GFile.
3187 * @flags: flags affecting the operation
3188 * @cancellable: optional #GCancellable object, %NULL to ignore.
3189 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3190 * @user_data: the data to pass to callback function
3192 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
3194 * If @cancellable is not %NULL, then the operation can be cancelled by
3195 * triggering the cancellable object from another thread. If the operation
3196 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3198 * When the operation is finished, @callback will be called. You can then call
3199 * g_file_unmount_mountable_finish() to get the result of the operation.
3202 g_file_unmount_mountable (GFile *file,
3203 GMountUnmountFlags flags,
3204 GCancellable *cancellable,
3205 GAsyncReadyCallback callback,
3210 g_return_if_fail (G_IS_FILE (file));
3212 iface = G_FILE_GET_IFACE (file);
3214 if (iface->unmount_mountable == NULL)
3215 g_simple_async_report_error_in_idle (G_OBJECT (file),
3219 G_IO_ERROR_NOT_SUPPORTED,
3220 _("Operation not supported"));
3222 (* iface->unmount_mountable) (file,
3230 * g_file_unmount_mountable_finish:
3231 * @file: input #GFile.
3232 * @result: a #GAsyncResult.
3233 * @error: a #GError, or %NULL
3235 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
3237 * Finish an asynchronous unmount operation that was started
3238 * with g_file_unmount_mountable().
3240 * Returns: %TRUE if the operation finished successfully. %FALSE
3244 g_file_unmount_mountable_finish (GFile *file,
3245 GAsyncResult *result,
3250 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3251 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3253 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3255 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3256 if (g_simple_async_result_propagate_error (simple, error))
3260 iface = G_FILE_GET_IFACE (file);
3261 return (* iface->unmount_mountable_finish) (file, result, error);
3265 * g_file_eject_mountable:
3266 * @file: input #GFile.
3267 * @flags: flags affecting the operation
3268 * @cancellable: optional #GCancellable object, %NULL to ignore.
3269 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3270 * @user_data: the data to pass to callback function
3272 * Starts an asynchronous eject on a mountable.
3273 * When this operation has completed, @callback will be called with
3274 * @user_user data, and the operation can be finalized with
3275 * g_file_eject_mountable_finish().
3277 * If @cancellable is not %NULL, then the operation can be cancelled by
3278 * triggering the cancellable object from another thread. If the operation
3279 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3282 g_file_eject_mountable (GFile *file,
3283 GMountUnmountFlags flags,
3284 GCancellable *cancellable,
3285 GAsyncReadyCallback callback,
3290 g_return_if_fail (G_IS_FILE (file));
3292 iface = G_FILE_GET_IFACE (file);
3294 if (iface->eject_mountable == NULL)
3295 g_simple_async_report_error_in_idle (G_OBJECT (file),
3299 G_IO_ERROR_NOT_SUPPORTED,
3300 _("Operation not supported"));
3302 (* iface->eject_mountable) (file,
3310 * g_file_eject_mountable_finish:
3311 * @file: input #GFile.
3312 * @result: a #GAsyncResult.
3313 * @error: a #GError, or %NULL
3315 * Finishes an asynchronous eject operation started by
3316 * g_file_eject_mountable().
3318 * Returns: %TRUE if the @file was ejected successfully. %FALSE
3322 g_file_eject_mountable_finish (GFile *file,
3323 GAsyncResult *result,
3328 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3329 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3331 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3333 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3334 if (g_simple_async_result_propagate_error (simple, error))
3338 iface = G_FILE_GET_IFACE (file);
3339 return (* iface->eject_mountable_finish) (file, result, error);
3343 * g_file_monitor_directory:
3344 * @file: input #GFile.
3345 * @flags: a set of #GFileMonitorFlags.
3346 * @cancellable: optional #GCancellable object, %NULL to ignore.
3347 * @error: a #GError, or %NULL.
3349 * Obtains a directory monitor for the given file.
3350 * This may fail if directory monitoring is not supported.
3352 * If @cancellable is not %NULL, then the operation can be cancelled by
3353 * triggering the cancellable object from another thread. If the operation
3354 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3356 * Returns: a #GFileMonitor for the given @file,
3357 * or %NULL on error.
3360 g_file_monitor_directory (GFile *file,
3361 GFileMonitorFlags flags,
3362 GCancellable *cancellable,
3367 g_return_val_if_fail (G_IS_FILE (file), NULL);
3369 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3372 iface = G_FILE_GET_IFACE (file);
3374 if (iface->monitor_dir == NULL)
3376 g_set_error (error, G_IO_ERROR,
3377 G_IO_ERROR_NOT_SUPPORTED,
3378 _("Operation not supported"));
3382 return (* iface->monitor_dir) (file, flags, cancellable, error);
3386 * g_file_monitor_file:
3387 * @file: input #GFile.
3388 * @flags: a set of #GFileMonitorFlags.
3389 * @cancellable: optional #GCancellable object, %NULL to ignore.
3390 * @error: a #GError, or %NULL.
3392 * Obtains a file monitor for the given file. If no file notification
3393 * mechanism exists, then regular polling of the file is used.
3395 * If @cancellable is not %NULL, then the operation can be cancelled by
3396 * triggering the cancellable object from another thread. If the operation
3397 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3399 * Returns: a #GFileMonitor for the given @file.
3402 g_file_monitor_file (GFile *file,
3403 GFileMonitorFlags flags,
3404 GCancellable *cancellable,
3408 GFileMonitor *monitor;
3410 g_return_val_if_fail (G_IS_FILE (file), NULL);
3412 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3415 iface = G_FILE_GET_IFACE (file);
3419 if (iface->monitor_file)
3420 monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
3422 /* Fallback to polling */
3423 if (monitor == NULL)
3424 monitor = _g_poll_file_monitor_new (file);
3429 /********************************************
3430 * Default implementation of async ops *
3431 ********************************************/
3435 GFileQueryInfoFlags flags;
3437 } QueryInfoAsyncData;
3440 query_info_data_free (QueryInfoAsyncData *data)
3443 g_object_unref (data->info);
3444 g_free (data->attributes);
3449 query_info_async_thread (GSimpleAsyncResult *res,
3451 GCancellable *cancellable)
3453 GError *error = NULL;
3454 QueryInfoAsyncData *data;
3457 data = g_simple_async_result_get_op_res_gpointer (res);
3459 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3463 g_simple_async_result_set_from_error (res, error);
3464 g_error_free (error);
3471 g_file_real_query_info_async (GFile *file,
3472 const char *attributes,
3473 GFileQueryInfoFlags flags,
3475 GCancellable *cancellable,
3476 GAsyncReadyCallback callback,
3479 GSimpleAsyncResult *res;
3480 QueryInfoAsyncData *data;
3482 data = g_new0 (QueryInfoAsyncData, 1);
3483 data->attributes = g_strdup (attributes);
3484 data->flags = flags;
3486 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_info_async);
3487 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
3489 g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
3490 g_object_unref (res);
3494 g_file_real_query_info_finish (GFile *file,
3498 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3499 QueryInfoAsyncData *data;
3501 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_info_async);
3503 data = g_simple_async_result_get_op_res_gpointer (simple);
3505 return g_object_ref (data->info);
3512 GFileQueryInfoFlags flags;
3513 GFileEnumerator *enumerator;
3514 } EnumerateChildrenAsyncData;
3517 enumerate_children_data_free (EnumerateChildrenAsyncData *data)
3519 if (data->enumerator)
3520 g_object_unref (data->enumerator);
3521 g_free (data->attributes);
3526 enumerate_children_async_thread (GSimpleAsyncResult *res,
3528 GCancellable *cancellable)
3530 GError *error = NULL;
3531 EnumerateChildrenAsyncData *data;
3532 GFileEnumerator *enumerator;
3534 data = g_simple_async_result_get_op_res_gpointer (res);
3536 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3538 if (enumerator == NULL)
3540 g_simple_async_result_set_from_error (res, error);
3541 g_error_free (error);
3544 data->enumerator = enumerator;
3548 g_file_real_enumerate_children_async (GFile *file,
3549 const char *attributes,
3550 GFileQueryInfoFlags flags,
3552 GCancellable *cancellable,
3553 GAsyncReadyCallback callback,
3556 GSimpleAsyncResult *res;
3557 EnumerateChildrenAsyncData *data;
3559 data = g_new0 (EnumerateChildrenAsyncData, 1);
3560 data->attributes = g_strdup (attributes);
3561 data->flags = flags;
3563 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_enumerate_children_async);
3564 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)enumerate_children_data_free);
3566 g_simple_async_result_run_in_thread (res, enumerate_children_async_thread, io_priority, cancellable);
3567 g_object_unref (res);
3570 static GFileEnumerator *
3571 g_file_real_enumerate_children_finish (GFile *file,
3575 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3576 EnumerateChildrenAsyncData *data;
3578 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_enumerate_children_async);
3580 data = g_simple_async_result_get_op_res_gpointer (simple);
3581 if (data->enumerator)
3582 return g_object_ref (data->enumerator);
3588 open_read_async_thread (GSimpleAsyncResult *res,
3590 GCancellable *cancellable)
3593 GFileInputStream *stream;
3594 GError *error = NULL;
3596 iface = G_FILE_GET_IFACE (object);
3598 stream = iface->read_fn (G_FILE (object), cancellable, &error);
3602 g_simple_async_result_set_from_error (res, error);
3603 g_error_free (error);
3606 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3610 g_file_real_read_async (GFile *file,
3612 GCancellable *cancellable,
3613 GAsyncReadyCallback callback,
3616 GSimpleAsyncResult *res;
3618 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_read_async);
3620 g_simple_async_result_run_in_thread (res, open_read_async_thread, io_priority, cancellable);
3621 g_object_unref (res);
3624 static GFileInputStream *
3625 g_file_real_read_finish (GFile *file,
3629 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3632 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_read_async);
3634 op = g_simple_async_result_get_op_res_gpointer (simple);
3636 return g_object_ref (op);
3642 append_to_async_thread (GSimpleAsyncResult *res,
3644 GCancellable *cancellable)
3647 GFileCreateFlags *data;
3648 GFileOutputStream *stream;
3649 GError *error = NULL;
3651 iface = G_FILE_GET_IFACE (object);
3653 data = g_simple_async_result_get_op_res_gpointer (res);
3655 stream = iface->append_to (G_FILE (object), *data, cancellable, &error);
3659 g_simple_async_result_set_from_error (res, error);
3660 g_error_free (error);
3663 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3667 g_file_real_append_to_async (GFile *file,
3668 GFileCreateFlags flags,
3670 GCancellable *cancellable,
3671 GAsyncReadyCallback callback,
3674 GFileCreateFlags *data;
3675 GSimpleAsyncResult *res;
3677 data = g_new0 (GFileCreateFlags, 1);
3680 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_append_to_async);
3681 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
3683 g_simple_async_result_run_in_thread (res, append_to_async_thread, io_priority, cancellable);
3684 g_object_unref (res);
3687 static GFileOutputStream *
3688 g_file_real_append_to_finish (GFile *file,
3692 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3695 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_append_to_async);
3697 op = g_simple_async_result_get_op_res_gpointer (simple);
3699 return g_object_ref (op);
3705 create_async_thread (GSimpleAsyncResult *res,
3707 GCancellable *cancellable)
3710 GFileCreateFlags *data;
3711 GFileOutputStream *stream;
3712 GError *error = NULL;
3714 iface = G_FILE_GET_IFACE (object);
3716 data = g_simple_async_result_get_op_res_gpointer (res);
3718 stream = iface->create (G_FILE (object), *data, cancellable, &error);
3722 g_simple_async_result_set_from_error (res, error);
3723 g_error_free (error);
3726 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3730 g_file_real_create_async (GFile *file,
3731 GFileCreateFlags flags,
3733 GCancellable *cancellable,
3734 GAsyncReadyCallback callback,
3737 GFileCreateFlags *data;
3738 GSimpleAsyncResult *res;
3740 data = g_new0 (GFileCreateFlags, 1);
3743 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_create_async);
3744 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
3746 g_simple_async_result_run_in_thread (res, create_async_thread, io_priority, cancellable);
3747 g_object_unref (res);
3750 static GFileOutputStream *
3751 g_file_real_create_finish (GFile *file,
3755 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3758 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_create_async);
3760 op = g_simple_async_result_get_op_res_gpointer (simple);
3762 return g_object_ref (op);
3768 GFileOutputStream *stream;
3770 gboolean make_backup;
3771 GFileCreateFlags flags;
3775 replace_async_data_free (ReplaceAsyncData *data)
3778 g_object_unref (data->stream);
3779 g_free (data->etag);
3784 replace_async_thread (GSimpleAsyncResult *res,
3786 GCancellable *cancellable)
3789 GFileOutputStream *stream;
3790 GError *error = NULL;
3791 ReplaceAsyncData *data;
3793 iface = G_FILE_GET_IFACE (object);
3795 data = g_simple_async_result_get_op_res_gpointer (res);
3797 stream = iface->replace (G_FILE (object),
3806 g_simple_async_result_set_from_error (res, error);
3807 g_error_free (error);
3810 data->stream = stream;
3814 g_file_real_replace_async (GFile *file,
3816 gboolean make_backup,
3817 GFileCreateFlags flags,
3819 GCancellable *cancellable,
3820 GAsyncReadyCallback callback,
3823 GSimpleAsyncResult *res;
3824 ReplaceAsyncData *data;
3826 data = g_new0 (ReplaceAsyncData, 1);
3827 data->etag = g_strdup (etag);
3828 data->make_backup = make_backup;
3829 data->flags = flags;
3831 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_replace_async);
3832 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_async_data_free);
3834 g_simple_async_result_run_in_thread (res, replace_async_thread, io_priority, cancellable);
3835 g_object_unref (res);
3838 static GFileOutputStream *
3839 g_file_real_replace_finish (GFile *file,
3843 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3844 ReplaceAsyncData *data;
3846 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_replace_async);
3848 data = g_simple_async_result_get_op_res_gpointer (simple);
3850 return g_object_ref (data->stream);
3858 } SetDisplayNameAsyncData;
3861 set_display_name_data_free (SetDisplayNameAsyncData *data)
3863 g_free (data->name);
3865 g_object_unref (data->file);
3870 set_display_name_async_thread (GSimpleAsyncResult *res,
3872 GCancellable *cancellable)
3874 GError *error = NULL;
3875 SetDisplayNameAsyncData *data;
3878 data = g_simple_async_result_get_op_res_gpointer (res);
3880 file = g_file_set_display_name (G_FILE (object), data->name, cancellable, &error);
3884 g_simple_async_result_set_from_error (res, error);
3885 g_error_free (error);
3892 g_file_real_set_display_name_async (GFile *file,
3893 const char *display_name,
3895 GCancellable *cancellable,
3896 GAsyncReadyCallback callback,
3899 GSimpleAsyncResult *res;
3900 SetDisplayNameAsyncData *data;
3902 data = g_new0 (SetDisplayNameAsyncData, 1);
3903 data->name = g_strdup (display_name);
3905 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_display_name_async);
3906 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_display_name_data_free);
3908 g_simple_async_result_run_in_thread (res, set_display_name_async_thread, io_priority, cancellable);
3909 g_object_unref (res);
3913 g_file_real_set_display_name_finish (GFile *file,
3917 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3918 SetDisplayNameAsyncData *data;
3920 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_display_name_async);
3922 data = g_simple_async_result_get_op_res_gpointer (simple);
3924 return g_object_ref (data->file);
3930 GFileQueryInfoFlags flags;
3937 set_info_data_free (SetInfoAsyncData *data)
3940 g_object_unref (data->info);
3942 g_error_free (data->error);
3947 set_info_async_thread (GSimpleAsyncResult *res,
3949 GCancellable *cancellable)
3951 SetInfoAsyncData *data;
3953 data = g_simple_async_result_get_op_res_gpointer (res);
3956 data->res = g_file_set_attributes_from_info (G_FILE (object),
3964 g_file_real_set_attributes_async (GFile *file,
3966 GFileQueryInfoFlags flags,
3968 GCancellable *cancellable,
3969 GAsyncReadyCallback callback,
3972 GSimpleAsyncResult *res;
3973 SetInfoAsyncData *data;
3975 data = g_new0 (SetInfoAsyncData, 1);
3976 data->info = g_file_info_dup (info);
3977 data->flags = flags;
3979 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_attributes_async);
3980 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_info_data_free);
3982 g_simple_async_result_run_in_thread (res, set_info_async_thread, io_priority, cancellable);
3983 g_object_unref (res);
3987 g_file_real_set_attributes_finish (GFile *file,
3992 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3993 SetInfoAsyncData *data;
3995 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_attributes_async);
3997 data = g_simple_async_result_get_op_res_gpointer (simple);
4000 *info = g_object_ref (data->info);
4002 if (error != NULL && data->error)
4003 *error = g_error_copy (data->error);
4008 /********************************************
4009 * Default VFS operations *
4010 ********************************************/
4013 * g_file_new_for_path:
4014 * @path: a string containing a relative or absolute path.
4016 * Constructs a #GFile for a given path. This operation never
4017 * fails, but the returned object might not support any I/O
4018 * operation if @path is malformed.
4020 * Returns: a new #GFile for the given @path.
4023 g_file_new_for_path (const char *path)
4025 g_return_val_if_fail (path != NULL, NULL);
4027 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
4031 * g_file_new_for_uri:
4032 * @uri: a string containing a URI.
4034 * Constructs a #GFile for a given URI. This operation never
4035 * fails, but the returned object might not support any I/O
4036 * operation if @uri is malformed or if the uri type is
4039 * Returns: a #GFile for the given @uri.
4042 g_file_new_for_uri (const char *uri)
4044 g_return_val_if_fail (uri != NULL, NULL);
4046 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
4050 * g_file_parse_name:
4051 * @parse_name: a file name or path to be parsed.
4053 * Constructs a #GFile with the given @parse_name (i.e. something given by g_file_get_parse_name()).
4054 * This operation never fails, but the returned object might not support any I/O
4055 * operation if the @parse_name cannot be parsed.
4057 * Returns: a new #GFile.
4060 g_file_parse_name (const char *parse_name)
4062 g_return_val_if_fail (parse_name != NULL, NULL);
4064 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
4068 is_valid_scheme_character (char c)
4070 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
4074 has_valid_scheme (const char *uri)
4080 if (!is_valid_scheme_character (*p))
4085 } while (is_valid_scheme_character (*p));
4091 * g_file_new_for_commandline_arg:
4092 * @arg: a command line string.
4094 * Creates a #GFile with the given argument from the command line. The value of
4095 * @arg can be either a URI, an absolute path or a relative path resolved
4096 * relative to the current working directory.
4097 * This operation never fails, but the returned object might not support any
4098 * I/O operation if @arg points to a malformed path.
4100 * Returns: a new #GFile.
4103 g_file_new_for_commandline_arg (const char *arg)
4109 g_return_val_if_fail (arg != NULL, NULL);
4111 if (g_path_is_absolute (arg))
4112 return g_file_new_for_path (arg);
4114 if (has_valid_scheme (arg))
4115 return g_file_new_for_uri (arg);
4117 current_dir = g_get_current_dir ();
4118 filename = g_build_filename (current_dir, arg, NULL);
4119 g_free (current_dir);
4121 file = g_file_new_for_path (filename);
4128 * g_file_mount_enclosing_volume:
4129 * @location: input #GFile.
4130 * @mount_operation: a #GMountOperation or %NULL to avoid user interaction.
4131 * @cancellable: optional #GCancellable object, %NULL to ignore.
4132 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4133 * @user_data: the data to pass to callback function
4135 * Starts a @mount_operation, mounting the volume that contains the file @location.
4137 * When this operation has completed, @callback will be called with
4138 * @user_user data, and the operation can be finalized with
4139 * g_file_mount_enclosing_volume_finish().
4141 * If @cancellable is not %NULL, then the operation can be cancelled by
4142 * triggering the cancellable object from another thread. If the operation
4143 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4146 g_file_mount_enclosing_volume (GFile *location,
4147 GMountOperation *mount_operation,
4148 GCancellable *cancellable,
4149 GAsyncReadyCallback callback,
4154 g_return_if_fail (G_IS_FILE (location));
4156 iface = G_FILE_GET_IFACE (location);
4158 if (iface->mount_enclosing_volume == NULL)
4160 g_simple_async_report_error_in_idle (G_OBJECT (location),
4161 callback, user_data,
4162 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4163 _("volume doesn't implement mount"));
4168 (* iface->mount_enclosing_volume) (location, mount_operation, cancellable, callback, user_data);
4173 * g_file_mount_enclosing_volume_finish:
4174 * @location: input #GFile.
4175 * @result: a #GAsyncResult.
4176 * @error: a #GError, or %NULL
4178 * Finishes a mount operation started by g_file_mount_enclosing_volume().
4180 * Returns: %TRUE if successful. If an error
4181 * has occurred, this function will return %FALSE and set @error
4182 * appropriately if present.
4185 g_file_mount_enclosing_volume_finish (GFile *location,
4186 GAsyncResult *result,
4191 g_return_val_if_fail (G_IS_FILE (location), FALSE);
4192 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4194 if (G_IS_SIMPLE_ASYNC_RESULT (result))
4196 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
4197 if (g_simple_async_result_propagate_error (simple, error))
4201 iface = G_FILE_GET_IFACE (location);
4203 return (* iface->mount_enclosing_volume_finish) (location, result, error);
4206 /********************************************
4207 * Utility functions *
4208 ********************************************/
4210 #define GET_CONTENT_BLOCK_SIZE 8192
4213 * g_file_load_contents:
4214 * @file: input #GFile.
4215 * @cancellable: optional #GCancellable object, %NULL to ignore.
4216 * @contents: a location to place the contents of the file.
4217 * @length: a location to place the length of the contents of the file.
4218 * @etag_out: a location to place the current entity tag for the file.
4219 * @error: a #GError, or %NULL
4221 * Loads the content of the file into memory, returning the size of
4222 * the data. The data is always zero terminated, but this is not
4223 * included in the resultant @length.
4225 * If @cancellable is not %NULL, then the operation can be cancelled by
4226 * triggering the cancellable object from another thread. If the operation
4227 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4229 * Returns: %TRUE if the @file's contents were successfully loaded.
4230 * %FALSE if there were errors..
4233 g_file_load_contents (GFile *file,
4234 GCancellable *cancellable,
4240 GFileInputStream *in;
4241 GByteArray *content;
4246 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4247 g_return_val_if_fail (contents != NULL, FALSE);
4249 in = g_file_read (file, cancellable, error);
4253 content = g_byte_array_new ();
4256 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
4257 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
4258 content->data + pos,
4259 GET_CONTENT_BLOCK_SIZE,
4260 cancellable, error)) > 0)
4263 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
4270 info = g_file_input_stream_query_info (in,
4271 G_FILE_ATTRIBUTE_ETAG_VALUE,
4276 *etag_out = g_strdup (g_file_info_get_etag (info));
4277 g_object_unref (info);
4281 /* Ignore errors on close */
4282 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
4283 g_object_unref (in);
4287 /* error is set already */
4288 g_byte_array_free (content, TRUE);
4295 /* Zero terminate (we got an extra byte allocated for this */
4296 content->data[pos] = 0;
4298 *contents = (char *)g_byte_array_free (content, FALSE);
4306 GCancellable *cancellable;
4307 GFileReadMoreCallback read_more_callback;
4308 GAsyncReadyCallback callback;
4310 GByteArray *content;
4317 load_contents_data_free (LoadContentsData *data)
4320 g_error_free (data->error);
4321 if (data->cancellable)
4322 g_object_unref (data->cancellable);
4324 g_byte_array_free (data->content, TRUE);
4325 g_free (data->etag);
4326 g_object_unref (data->file);
4331 load_contents_close_callback (GObject *obj,
4332 GAsyncResult *close_res,
4335 GInputStream *stream = G_INPUT_STREAM (obj);
4336 LoadContentsData *data = user_data;
4337 GSimpleAsyncResult *res;
4339 /* Ignore errors here, we're only reading anyway */
4340 g_input_stream_close_finish (stream, close_res, NULL);
4341 g_object_unref (stream);
4343 res = g_simple_async_result_new (G_OBJECT (data->file),
4346 g_file_load_contents_async);
4347 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)load_contents_data_free);
4348 g_simple_async_result_complete (res);
4349 g_object_unref (res);
4353 load_contents_fstat_callback (GObject *obj,
4354 GAsyncResult *stat_res,
4357 GInputStream *stream = G_INPUT_STREAM (obj);
4358 LoadContentsData *data = user_data;
4361 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
4365 data->etag = g_strdup (g_file_info_get_etag (info));
4366 g_object_unref (info);
4369 g_input_stream_close_async (stream, 0,
4371 load_contents_close_callback, data);
4375 load_contents_read_callback (GObject *obj,
4376 GAsyncResult *read_res,
4379 GInputStream *stream = G_INPUT_STREAM (obj);
4380 LoadContentsData *data = user_data;
4381 GError *error = NULL;
4384 read_size = g_input_stream_read_finish (stream, read_res, &error);
4388 /* Error or EOF, close the file */
4389 data->error = error;
4390 g_input_stream_close_async (stream, 0,
4392 load_contents_close_callback, data);
4394 else if (read_size == 0)
4396 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
4397 G_FILE_ATTRIBUTE_ETAG_VALUE,
4400 load_contents_fstat_callback,
4403 else if (read_size > 0)
4405 data->pos += read_size;
4407 g_byte_array_set_size (data->content,
4408 data->pos + GET_CONTENT_BLOCK_SIZE);
4411 if (data->read_more_callback &&
4412 !data->read_more_callback ((char *)data->content->data, data->pos, data->user_data))
4413 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
4414 G_FILE_ATTRIBUTE_ETAG_VALUE,
4417 load_contents_fstat_callback,
4420 g_input_stream_read_async (stream,
4421 data->content->data + data->pos,
4422 GET_CONTENT_BLOCK_SIZE,
4425 load_contents_read_callback,
4431 load_contents_open_callback (GObject *obj,
4432 GAsyncResult *open_res,
4435 GFile *file = G_FILE (obj);
4436 GFileInputStream *stream;
4437 LoadContentsData *data = user_data;
4438 GError *error = NULL;
4439 GSimpleAsyncResult *res;
4441 stream = g_file_read_finish (file, open_res, &error);
4445 g_byte_array_set_size (data->content,
4446 data->pos + GET_CONTENT_BLOCK_SIZE);
4447 g_input_stream_read_async (G_INPUT_STREAM (stream),
4448 data->content->data + data->pos,
4449 GET_CONTENT_BLOCK_SIZE,
4452 load_contents_read_callback,
4458 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
4462 g_simple_async_result_complete (res);
4463 g_error_free (error);
4464 load_contents_data_free (data);
4465 g_object_unref (res);
4470 * g_file_load_partial_contents_async:
4471 * @file: input #GFile.
4472 * @cancellable: optional #GCancellable object, %NULL to ignore.
4473 * @read_more_callback: a #GFileReadMoreCallback to receive partial data and to specify whether further data should be read.
4474 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
4475 * @user_data: the data to pass to the callback functions.
4477 * Reads the partial contents of a file. A #GFileReadMoreCallback should be
4478 * used to stop reading from the file when appropriate, else this function
4479 * will behave exactly as g_file_load_contents_async(). This operation
4480 * can be finished by g_file_load_partial_contents_finish().
4482 * Users of this function should be aware that @user_data is passed to
4483 * both the @read_more_callback and the @callback.
4485 * If @cancellable is not %NULL, then the operation can be cancelled by
4486 * triggering the cancellable object from another thread. If the operation
4487 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4490 g_file_load_partial_contents_async (GFile *file,
4491 GCancellable *cancellable,
4492 GFileReadMoreCallback read_more_callback,
4493 GAsyncReadyCallback callback,
4496 LoadContentsData *data;
4498 g_return_if_fail (G_IS_FILE (file));
4500 data = g_new0 (LoadContentsData, 1);
4503 data->cancellable = g_object_ref (cancellable);
4504 data->read_more_callback = read_more_callback;
4505 data->callback = callback;
4506 data->user_data = user_data;
4507 data->content = g_byte_array_new ();
4508 data->file = g_object_ref (file);
4510 g_file_read_async (file,
4513 load_contents_open_callback,
4518 * g_file_load_partial_contents_finish:
4519 * @file: input #GFile.
4520 * @res: a #GAsyncResult.
4521 * @contents: a location to place the contents of the file.
4522 * @length: a location to place the length of the contents of the file.
4523 * @etag_out: a location to place the current entity tag for the file.
4524 * @error: a #GError, or %NULL
4526 * Finishes an asynchronous partial load operation that was started
4527 * with g_file_load_partial_contents_async().
4529 * Returns: %TRUE if the load was successful. If %FALSE and @error is
4530 * present, it will be set appropriately.
4533 g_file_load_partial_contents_finish (GFile *file,
4540 GSimpleAsyncResult *simple;
4541 LoadContentsData *data;
4543 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4544 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
4545 g_return_val_if_fail (contents != NULL, FALSE);
4547 simple = G_SIMPLE_ASYNC_RESULT (res);
4549 if (g_simple_async_result_propagate_error (simple, error))
4552 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_load_contents_async);
4554 data = g_simple_async_result_get_op_res_gpointer (simple);
4558 g_propagate_error (error, data->error);
4567 *length = data->pos;
4571 *etag_out = data->etag;
4575 /* Zero terminate */
4576 g_byte_array_set_size (data->content, data->pos + 1);
4577 data->content->data[data->pos] = 0;
4579 *contents = (char *)g_byte_array_free (data->content, FALSE);
4580 data->content = NULL;
4586 * g_file_load_contents_async:
4587 * @file: input #GFile.
4588 * @cancellable: optional #GCancellable object, %NULL to ignore.
4589 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
4590 * @user_data: the data to pass to callback function
4592 * Starts an asynchronous load of the @file's contents.
4594 * For more details, see g_file_load_contents() which is
4595 * the synchronous version of this call.
4597 * When the load operation has completed, @callback will be called
4598 * with @user data. To finish the operation, call
4599 * g_file_load_contents_finish() with the #GAsyncResult returned by
4602 * If @cancellable is not %NULL, then the operation can be cancelled by
4603 * triggering the cancellable object from another thread. If the operation
4604 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4607 g_file_load_contents_async (GFile *file,
4608 GCancellable *cancellable,
4609 GAsyncReadyCallback callback,
4612 g_file_load_partial_contents_async (file,
4615 callback, user_data);
4619 * g_file_load_contents_finish:
4620 * @file: input #GFile.
4621 * @res: a #GAsyncResult.
4622 * @contents: a location to place the contents of the file.
4623 * @length: a location to place the length of the contents of the file.
4624 * @etag_out: a location to place the current entity tag for the file.
4625 * @error: a #GError, or %NULL
4627 * Finishes an asynchronous load of the @file's contents.
4628 * The contents are placed in @contents, and @length is set to the
4629 * size of the @contents string. If @etag_out is present, it will be
4630 * set to the new entity tag for the @file.
4632 * Returns: %TRUE if the load was successful. If %FALSE and @error is
4633 * present, it will be set appropriately.
4636 g_file_load_contents_finish (GFile *file,
4643 return g_file_load_partial_contents_finish (file,
4652 * g_file_replace_contents:
4653 * @file: input #GFile.
4654 * @contents: a string containing the new contents for @file.
4655 * @length: the length of @contents in bytes.
4656 * @etag: the old <link linkend="gfile-etag">entity tag</link>
4658 * @make_backup: %TRUE if a backup should be created.
4659 * @flags: a set of #GFileCreateFlags.
4660 * @new_etag: a location to a new <link linkend="gfile-etag">entity tag</link>
4662 * @cancellable: optional #GCancellable object, %NULL to ignore.
4663 * @error: a #GError, or %NULL
4665 * Replaces the contents of @file with @contents of @length bytes.
4667 * If @etag is specified (not %NULL) any existing file must have that etag, or
4668 * the error %G_IO_ERROR_WRONG_ETAG will be returned.
4670 * If @make_backup is %TRUE, this function will attempt to make a backup of @file.
4672 * If @cancellable is not %NULL, then the operation can be cancelled by
4673 * triggering the cancellable object from another thread. If the operation
4674 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4676 * The returned @new_etag can be used to verify that the file hasn't changed the
4677 * next time it is saved over.
4679 * Returns: %TRUE if successful. If an error
4680 * has occurred, this function will return %FALSE and set @error
4681 * appropriately if present.
4684 g_file_replace_contents (GFile *file,
4685 const char *contents,
4688 gboolean make_backup,
4689 GFileCreateFlags flags,
4691 GCancellable *cancellable,
4694 GFileOutputStream *out;
4695 gsize pos, remainder;
4698 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4699 g_return_val_if_fail (contents != NULL, FALSE);
4701 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
4707 while (remainder > 0 &&
4708 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
4710 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
4718 if (remainder > 0 && res < 0)
4720 /* Ignore errors on close */
4721 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
4723 /* error is set already */
4727 if (!g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error))
4731 *new_etag = g_file_output_stream_get_etag (out);
4739 GCancellable *cancellable;
4740 GAsyncReadyCallback callback;
4742 const char *content;
4746 } ReplaceContentsData;
4749 replace_contents_data_free (ReplaceContentsData *data)
4752 g_error_free (data->error);
4753 if (data->cancellable)
4754 g_object_unref (data->cancellable);
4755 g_object_unref (data->file);
4756 g_free (data->etag);
4761 replace_contents_close_callback (GObject *obj,
4762 GAsyncResult *close_res,
4765 GOutputStream *stream = G_OUTPUT_STREAM (obj);
4766 ReplaceContentsData *data = user_data;
4767 GSimpleAsyncResult *res;
4769 /* Ignore errors here, we're only reading anyway */
4770 g_output_stream_close_finish (stream, close_res, NULL);
4771 g_object_unref (stream);
4773 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
4775 res = g_simple_async_result_new (G_OBJECT (data->file),
4778 g_file_replace_contents_async);
4779 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_contents_data_free);
4780 g_simple_async_result_complete (res);
4781 g_object_unref (res);
4785 replace_contents_write_callback (GObject *obj,
4786 GAsyncResult *read_res,
4789 GOutputStream *stream = G_OUTPUT_STREAM (obj);
4790 ReplaceContentsData *data = user_data;
4791 GError *error = NULL;
4794 write_size = g_output_stream_write_finish (stream, read_res, &error);
4796 if (write_size <= 0)
4798 /* Error or EOF, close the file */
4800 data->error = error;
4801 g_output_stream_close_async (stream, 0,
4803 replace_contents_close_callback, data);
4805 else if (write_size > 0)
4807 data->pos += write_size;
4809 if (data->pos >= data->length)
4810 g_output_stream_close_async (stream, 0,
4812 replace_contents_close_callback, data);
4814 g_output_stream_write_async (stream,
4815 data->content + data->pos,
4816 data->length - data->pos,
4819 replace_contents_write_callback,
4825 replace_contents_open_callback (GObject *obj,
4826 GAsyncResult *open_res,
4829 GFile *file = G_FILE (obj);
4830 GFileOutputStream *stream;
4831 ReplaceContentsData *data = user_data;
4832 GError *error = NULL;
4833 GSimpleAsyncResult *res;
4835 stream = g_file_replace_finish (file, open_res, &error);
4839 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
4840 data->content + data->pos,
4841 data->length - data->pos,
4844 replace_contents_write_callback,
4850 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
4854 g_simple_async_result_complete (res);
4855 g_error_free (error);
4856 replace_contents_data_free (data);
4857 g_object_unref (res);
4862 * g_file_replace_contents_async:
4863 * @file: input #GFile.
4864 * @contents: string of contents to replace the file with.
4865 * @length: the length of @contents in bytes.
4866 * @etag: a new <link linkend="gfile-etag">entity tag</link> for the @file.
4867 * @make_backup: %TRUE if a backup should be created.
4868 * @flags: a set of #GFileCreateFlags.
4869 * @cancellable: optional #GCancellable object, %NULL to ignore.
4870 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
4871 * @user_data: the data to pass to callback function
4873 * Starts an asynchronous replacement of @file with the given
4874 * @contents of @length bytes. @etag will replace the document's
4875 * current entity tag.
4877 * When this operation has completed, @callback will be called with
4878 * @user_user data, and the operation can be finalized with
4879 * g_file_replace_contents_finish().
4881 * If @cancellable is not %NULL, then the operation can be cancelled by
4882 * triggering the cancellable object from another thread. If the operation
4883 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4885 * If @make_backup is %TRUE, this function will attempt to
4886 * make a backup of @file.
4889 g_file_replace_contents_async (GFile *file,
4890 const char *contents,
4893 gboolean make_backup,
4894 GFileCreateFlags flags,
4895 GCancellable *cancellable,
4896 GAsyncReadyCallback callback,
4899 ReplaceContentsData *data;
4901 g_return_if_fail (G_IS_FILE (file));
4902 g_return_if_fail (contents != NULL);
4904 data = g_new0 (ReplaceContentsData, 1);
4907 data->cancellable = g_object_ref (cancellable);
4908 data->callback = callback;
4909 data->user_data = user_data;
4910 data->content = contents;
4911 data->length = length;
4913 data->file = g_object_ref (file);
4915 g_file_replace_async (file,
4921 replace_contents_open_callback,
4926 * g_file_replace_contents_finish:
4927 * @file: input #GFile.
4928 * @res: a #GAsyncResult.
4929 * @new_etag: a location of a new <link linkend="gfile-etag">entity tag</link>
4931 * @error: a #GError, or %NULL
4933 * Finishes an asynchronous replace of the given @file. See
4934 * g_file_replace_contents_async(). Sets @new_etag to the new entity
4935 * tag for the document, if present.
4937 * Returns: %TRUE on success, %FALSE on failure.
4940 g_file_replace_contents_finish (GFile *file,
4945 GSimpleAsyncResult *simple;
4946 ReplaceContentsData *data;
4948 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4949 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
4951 simple = G_SIMPLE_ASYNC_RESULT (res);
4953 if (g_simple_async_result_propagate_error (simple, error))
4956 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_replace_contents_async);
4958 data = g_simple_async_result_get_op_res_gpointer (simple);
4962 g_propagate_error (error, data->error);
4970 *new_etag = data->etag;
4971 data->etag = NULL; /* Take ownership */
4977 #define __G_FILE_C__
4978 #include "gioaliasdef.c"