1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
3 /* GIO - GLib Input, Output and Streaming Library
5 * Copyright (C) 2006-2007 Red Hat, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 * Boston, MA 02111-1307, USA.
22 * Author: Alexander Larsson <alexl@redhat.com>
27 #include <sys/types.h>
33 #include "gioscheduler.h"
34 #include "glocalfile.h"
35 #include "gsimpleasyncresult.h"
36 #include "gfileattribute-priv.h"
37 #include "gpollfilemonitor.h"
39 #include "gfileinputstream.h"
40 #include "gfileoutputstream.h"
41 #include "gcancellable.h"
42 #include "gasyncresult.h"
50 * @short_description: File and Directory Handling
52 * @see_also: #GFileInfo, #GFileEnumerator
54 * #GFile is a high level abstraction for manipulating files on a
55 * virtual file system. #GFile<!-- -->s are lightweight, immutable
56 * objects that do no I/O upon creation. It is necessary to understand that
57 * #GFile objects do not represent files, merely an identifier for a file. All
58 * file content I/O is implemented as streaming operations (see #GInputStream and
61 * To construct a #GFile, you can use:
62 * g_file_new_for_path() if you have a path.
63 * g_file_new_for_uri() if you have a URI.
64 * g_file_new_for_commandline_arg() for a command line argument.
65 * g_file_parse_name() from a utf8 string gotten from g_file_get_parse_name().
67 * One way to think of a #GFile is as an abstraction of a pathname. For normal
68 * files the system pathname is what is stored internally, but as #GFile<!-- -->s
69 * are extensible it could also be something else that corresponds to a pathname
70 * in a userspace implementation of a filesystem.
72 * #GFile<!-- -->s make up hierarchies of directories and files that correspond to the
73 * files on a filesystem. You can move through the file system with #GFile using
74 * g_file_get_parent() to get an identifier for the parent directory, g_file_get_child()
75 * to get a child within a directory, g_file_resolve_relative_path() to resolve a relative
76 * path between two #GFile<!-- -->s. There can be multiple hierarchies, so you may not
77 * end up at the same root if you repeatedly call g_file_get_parent() on two different
80 * All #GFile<!-- -->s have a basename (get with g_file_get_basename()). These names
81 * are byte strings that are used to identify the file on the filesystem (relative to
82 * its parent directory) and there is no guarantees that they have any particular charset
83 * encoding or even make any sense at all. If you want to use filenames in a user
84 * interface you should use the display name that you can get by requesting the
85 * %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME attribute with g_file_query_info().
86 * This is guaranteed to be in utf8 and can be used in a user interface. But always
87 * store the real basename or the #GFile to use to actually access the file, because
88 * there is no way to go from a display name to the actual name.
90 * Using #GFile as an identifier has the same weaknesses as using a path in that
91 * there may be multiple aliases for the same file. For instance, hard or
92 * soft links may cause two different #GFile<!-- -->s to refer to the same file.
93 * Other possible causes for aliases are: case insensitive filesystems, short
94 * and long names on Fat/NTFS, or bind mounts in Linux. If you want to check if
95 * two #GFile<!-- -->s point to the same file you can query for the
96 * %G_FILE_ATTRIBUTE_ID_FILE attribute. Note that #GFile does some trivial
97 * canonicalization of pathnames passed in, so that trivial differences in the
98 * path string used at creation (duplicated slashes, slash at end of path, "."
99 * or ".." path segments, etc) does not create different #GFile<!-- -->s.
101 * Many #GFile operations have both synchronous and asynchronous versions
102 * to suit your application. Asynchronous versions of synchronous functions
103 * simply have _async() appended to their function names. The asynchronous
104 * I/O functions call a #GAsyncReadyCallback which is then used to finalize
105 * the operation, producing a GAsyncResult which is then passed to the
106 * function's matching _finish() operation.
108 * Some #GFile operations do not have synchronous analogs, as they may
109 * take a very long time to finish, and blocking may leave an application
110 * unusable. Notable cases include:
111 * g_file_mount_mountable() to mount a mountable file.
112 * g_file_unmount_mountable() to unmount a mountable file.
113 * g_file_eject_mountable() to eject a mountable file.
115 * <para id="gfile-etag"><indexterm><primary>entity tag</primary></indexterm>
116 * One notable feature of #GFile<!-- -->s are entity tags, or "etags" for
117 * short. Entity tags are somewhat like a more abstract version of the
118 * traditional mtime, and can be used to quickly determine if the file has
119 * been modified from the version on the file system. See the HTTP 1.1
120 * <ulink url="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html">specification</ulink>
121 * for HTTP Etag headers, which are a very similar concept.
125 static void g_file_base_init (gpointer g_class);
126 static void g_file_class_init (gpointer g_class,
127 gpointer class_data);
129 static void g_file_real_query_info_async (GFile *file,
130 const char *attributes,
131 GFileQueryInfoFlags flags,
133 GCancellable *cancellable,
134 GAsyncReadyCallback callback,
136 static GFileInfo * g_file_real_query_info_finish (GFile *file,
139 static void g_file_real_query_filesystem_info_async (GFile *file,
140 const char *attributes,
142 GCancellable *cancellable,
143 GAsyncReadyCallback callback,
145 static GFileInfo * g_file_real_query_filesystem_info_finish (GFile *file,
148 static void g_file_real_enumerate_children_async (GFile *file,
149 const char *attributes,
150 GFileQueryInfoFlags flags,
152 GCancellable *cancellable,
153 GAsyncReadyCallback callback,
155 static GFileEnumerator * g_file_real_enumerate_children_finish (GFile *file,
158 static void g_file_real_read_async (GFile *file,
160 GCancellable *cancellable,
161 GAsyncReadyCallback callback,
163 static GFileInputStream * g_file_real_read_finish (GFile *file,
166 static void g_file_real_append_to_async (GFile *file,
167 GFileCreateFlags flags,
169 GCancellable *cancellable,
170 GAsyncReadyCallback callback,
172 static GFileOutputStream *g_file_real_append_to_finish (GFile *file,
175 static void g_file_real_create_async (GFile *file,
176 GFileCreateFlags flags,
178 GCancellable *cancellable,
179 GAsyncReadyCallback callback,
181 static GFileOutputStream *g_file_real_create_finish (GFile *file,
184 static void g_file_real_replace_async (GFile *file,
186 gboolean make_backup,
187 GFileCreateFlags flags,
189 GCancellable *cancellable,
190 GAsyncReadyCallback callback,
192 static GFileOutputStream *g_file_real_replace_finish (GFile *file,
195 static gboolean g_file_real_set_attributes_from_info (GFile *file,
197 GFileQueryInfoFlags flags,
198 GCancellable *cancellable,
200 static void g_file_real_set_display_name_async (GFile *file,
201 const char *display_name,
203 GCancellable *cancellable,
204 GAsyncReadyCallback callback,
206 static GFile * g_file_real_set_display_name_finish (GFile *file,
209 static void g_file_real_set_attributes_async (GFile *file,
211 GFileQueryInfoFlags flags,
213 GCancellable *cancellable,
214 GAsyncReadyCallback callback,
216 static gboolean g_file_real_set_attributes_finish (GFile *file,
220 static void g_file_real_find_enclosing_mount_async (GFile *file,
222 GCancellable *cancellable,
223 GAsyncReadyCallback callback,
225 static GMount * g_file_real_find_enclosing_mount_finish (GFile *file,
228 static void g_file_real_copy_async (GFile *source,
230 GFileCopyFlags flags,
232 GCancellable *cancellable,
233 GFileProgressCallback progress_callback,
234 gpointer progress_callback_data,
235 GAsyncReadyCallback callback,
237 static gboolean g_file_real_copy_finish (GFile *file,
242 g_file_get_type (void)
244 static GType file_type = 0;
248 static const GTypeInfo file_info =
250 sizeof (GFileIface), /* class_size */
251 g_file_base_init, /* base_init */
252 NULL, /* base_finalize */
254 NULL, /* class_finalize */
255 NULL, /* class_data */
262 g_type_register_static (G_TYPE_INTERFACE, I_("GFile"),
265 g_type_interface_add_prerequisite (file_type, G_TYPE_OBJECT);
272 g_file_class_init (gpointer g_class,
275 GFileIface *iface = g_class;
277 iface->enumerate_children_async = g_file_real_enumerate_children_async;
278 iface->enumerate_children_finish = g_file_real_enumerate_children_finish;
279 iface->set_display_name_async = g_file_real_set_display_name_async;
280 iface->set_display_name_finish = g_file_real_set_display_name_finish;
281 iface->query_info_async = g_file_real_query_info_async;
282 iface->query_info_finish = g_file_real_query_info_finish;
283 iface->query_filesystem_info_async = g_file_real_query_filesystem_info_async;
284 iface->query_filesystem_info_finish = g_file_real_query_filesystem_info_finish;
285 iface->set_attributes_async = g_file_real_set_attributes_async;
286 iface->set_attributes_finish = g_file_real_set_attributes_finish;
287 iface->read_async = g_file_real_read_async;
288 iface->read_finish = g_file_real_read_finish;
289 iface->append_to_async = g_file_real_append_to_async;
290 iface->append_to_finish = g_file_real_append_to_finish;
291 iface->create_async = g_file_real_create_async;
292 iface->create_finish = g_file_real_create_finish;
293 iface->replace_async = g_file_real_replace_async;
294 iface->replace_finish = g_file_real_replace_finish;
295 iface->find_enclosing_mount_async = g_file_real_find_enclosing_mount_async;
296 iface->find_enclosing_mount_finish = g_file_real_find_enclosing_mount_finish;
297 iface->set_attributes_from_info = g_file_real_set_attributes_from_info;
298 iface->copy_async = g_file_real_copy_async;
299 iface->copy_finish = g_file_real_copy_finish;
303 g_file_base_init (gpointer g_class)
310 * @file: input #GFile.
312 * Checks to see if a file is native to the platform.
314 * A native file s one expressed in the platform-native filename format,
315 * e.g. "C:\Windows" or "/usr/bin/". This does not mean the file is local,
316 * as it might be on a locally mounted remote filesystem.
318 * On some systems non-native files may be available using
319 * the native filesystem via a userspace filesystem (FUSE), in
320 * these cases this call will return %FALSE, but g_file_get_path()
321 * will still return a native path.
323 * This call does no blocking i/o.
325 * Returns: %TRUE if file is native.
328 g_file_is_native (GFile *file)
332 g_return_val_if_fail (G_IS_FILE (file), FALSE);
334 iface = G_FILE_GET_IFACE (file);
336 return (* iface->is_native) (file);
341 * g_file_has_uri_scheme:
342 * @file: input #GFile.
343 * @uri_scheme: a string containing a URI scheme.
345 * Checks to see if a #GFile has a given URI scheme.
347 * This call does no blocking i/o.
349 * Returns: %TRUE if #GFile's backend supports the
350 * given URI scheme, %FALSE if URI scheme is %NULL,
351 * not supported, or #GFile is invalid.
354 g_file_has_uri_scheme (GFile *file,
355 const char *uri_scheme)
359 g_return_val_if_fail (G_IS_FILE (file), FALSE);
360 g_return_val_if_fail (uri_scheme != NULL, FALSE);
362 iface = G_FILE_GET_IFACE (file);
364 return (* iface->has_uri_scheme) (file, uri_scheme);
369 * g_file_get_uri_scheme:
370 * @file: input #GFile.
372 * Gets the URI scheme for a #GFile.
373 * RFC 3986 decodes the scheme as:
375 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
377 * Common schemes include "file", "http", "ftp", etc.
379 * This call does no blocking i/o.
381 * Returns: a string containing the URI scheme for the given
382 * #GFile. The returned string should be freed with g_free()
383 * when no longer needed.
386 g_file_get_uri_scheme (GFile *file)
390 g_return_val_if_fail (G_IS_FILE (file), NULL);
392 iface = G_FILE_GET_IFACE (file);
394 return (* iface->get_uri_scheme) (file);
399 * g_file_get_basename:
400 * @file: input #GFile.
402 * Gets the base name (the last component of the path) for a given #GFile.
404 * If called for the top level of a system (such as the filesystem root
405 * or a uri like sftp://host/) it will return a single directory separator
406 * (and on Windows, possibly a drive letter).
408 * The base name is a byte string (*not* UTF-8). It has no defined encoding
409 * or rules other than it may not contain zero bytes. If you want to use
410 * filenames in a user interface you should use the display name that you
411 * can get by requesting the %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME
412 * attribute with g_file_query_info().
414 * This call does no blocking i/o.
416 * Returns: string containing the #GFile's base name, or %NULL
417 * if given #GFile is invalid. The returned string should be
418 * freed with g_free() when no longer needed.
421 g_file_get_basename (GFile *file)
425 g_return_val_if_fail (G_IS_FILE (file), NULL);
427 iface = G_FILE_GET_IFACE (file);
429 return (* iface->get_basename) (file);
434 * @file: input #GFile.
436 * Gets the local pathname for #GFile, if one exists.
438 * This call does no blocking i/o.
440 * Returns: string containing the #GFile's path, or %NULL if
441 * no such path exists. The returned string should be
442 * freed with g_free() when no longer needed.
445 g_file_get_path (GFile *file)
449 g_return_val_if_fail (G_IS_FILE (file), NULL);
451 iface = G_FILE_GET_IFACE (file);
453 return (* iface->get_path) (file);
458 * @file: input #GFile.
460 * Gets the URI for the @file.
462 * This call does no blocking i/o.
464 * Returns: a string containing the #GFile's URI.
465 * The returned string should be freed with g_free() when no longer needed.
468 g_file_get_uri (GFile *file)
472 g_return_val_if_fail (G_IS_FILE (file), NULL);
474 iface = G_FILE_GET_IFACE (file);
476 return (* iface->get_uri) (file);
480 * g_file_get_parse_name:
481 * @file: input #GFile.
483 * Gets the parse name of the @file.
484 * A parse name is a UTF-8 string that describes the
485 * file such that one can get the #GFile back using
486 * g_file_parse_name().
488 * This is generally used to show the #GFile as a nice
489 * full-pathname kind of string in a user interface,
490 * like in a location entry.
492 * For local files with names that can safely be converted
493 * to UTF8 the pathname is used, otherwise the IRI is used
494 * (a form of URI that allows UTF8 characters unescaped).
496 * This call does no blocking i/o.
498 * Returns: a string containing the #GFile's parse name. The returned
499 * string should be freed with g_free() when no longer needed.
502 g_file_get_parse_name (GFile *file)
506 g_return_val_if_fail (G_IS_FILE (file), NULL);
508 iface = G_FILE_GET_IFACE (file);
510 return (* iface->get_parse_name) (file);
515 * @file: input #GFile.
517 * Duplicates a #GFile handle. This operation does not duplicate
518 * the actual file or directory represented by the #GFile; see
519 * g_file_copy() if attempting to copy a file.
521 * This call does no blocking i/o.
523 * Returns: a new #GFile that is a duplicate of the given #GFile.
526 g_file_dup (GFile *file)
530 g_return_val_if_fail (G_IS_FILE (file), NULL);
532 iface = G_FILE_GET_IFACE (file);
534 return (* iface->dup) (file);
539 * @file: #gconstpointer to a #GFile.
541 * Creates a hash value for a #GFile.
543 * This call does no blocking i/o.
545 * Returns: 0 if @file is not a valid #GFile, otherwise an
546 * integer that can be used as hash value for the #GFile.
547 * This function is intended for easily hashing a #GFile to
548 * add to a #GHashTable or similar data structure.
551 g_file_hash (gconstpointer file)
555 g_return_val_if_fail (G_IS_FILE (file), 0);
557 iface = G_FILE_GET_IFACE (file);
559 return (* iface->hash) ((GFile *)file);
564 * @file1: the first #GFile.
565 * @file2: the second #GFile.
567 * Checks equality of two given #GFile<!-- -->s. Note that two
568 * #GFile<!-- -->s that differ can still refer to the same
569 * file on the filesystem due to various forms of filename
572 * This call does no blocking i/o.
574 * Returns: %TRUE if @file1 and @file2 are equal.
575 * %FALSE if either is not a #GFile.
578 g_file_equal (GFile *file1,
583 g_return_val_if_fail (G_IS_FILE (file1), FALSE);
584 g_return_val_if_fail (G_IS_FILE (file2), FALSE);
586 if (G_TYPE_FROM_INSTANCE (file1) != G_TYPE_FROM_INSTANCE (file2))
589 iface = G_FILE_GET_IFACE (file1);
591 return (* iface->equal) (file1, file2);
597 * @file: input #GFile.
599 * Gets the parent directory for the @file.
600 * If the @file represents the root directory of the
601 * file system, then %NULL will be returned.
603 * This call does no blocking i/o.
605 * Returns: a #GFile structure to the parent of the given
606 * #GFile or %NULL if there is no parent.
607 * Free the returned object with g_object_unref().
610 g_file_get_parent (GFile *file)
614 g_return_val_if_fail (G_IS_FILE (file), NULL);
616 iface = G_FILE_GET_IFACE (file);
618 return (* iface->get_parent) (file);
623 * @file: input #GFile.
624 * @name: string containing the child's basename.
626 * Gets a child of @file with basename equal to @name.
628 * Note that the file with that specific name might not exist, but
629 * you can still have a #GFile that points to it. You can use this
630 * for instance to create that file.
632 * This call does no blocking i/o.
634 * Returns: a #GFile to a child specified by @name.
635 * Free the returned object with g_object_unref().
638 g_file_get_child (GFile *file,
641 g_return_val_if_fail (G_IS_FILE (file), NULL);
642 g_return_val_if_fail (name != NULL, NULL);
644 return g_file_resolve_relative_path (file, name);
648 * g_file_get_child_for_display_name:
649 * @file: input #GFile.
650 * @display_name: string to a possible child.
653 * Gets the child of @file for a given @display_name (i.e. a UTF8
654 * version of the name). If this function fails, it returns %NULL and @error will be
655 * set. This is very useful when constructing a GFile for a new file
656 * and the user entered the filename in the user interface, for instance
657 * when you select a directory and type a filename in the file selector.
659 * This call does no blocking i/o.
661 * Returns: a #GFile to the specified child, or
662 * %NULL if the display name couldn't be converted.
663 * Free the returned object with g_object_unref().
666 g_file_get_child_for_display_name (GFile *file,
667 const char *display_name,
672 g_return_val_if_fail (G_IS_FILE (file), NULL);
673 g_return_val_if_fail (display_name != NULL, NULL);
675 iface = G_FILE_GET_IFACE (file);
677 return (* iface->get_child_for_display_name) (file, display_name, error);
682 * @file: input #GFile.
683 * @prefix: input #GFile.
685 * Checks whether @file has the prefix specified by @prefix. In other word,
686 * if the names of inital elements of @file<!-- -->s pathname match @prefix.
688 * This call does no i/o, as it works purely on names. As such it can
689 * sometimes return %FALSE even if @file is inside a @prefix (from a
690 * filesystem point of view), because the prefix of @file is an alias
693 * Returns: %TRUE if the @files's parent, grandparent, etc is @prefix.
697 g_file_has_prefix (GFile *file,
702 g_return_val_if_fail (G_IS_FILE (file), FALSE);
703 g_return_val_if_fail (G_IS_FILE (prefix), FALSE);
705 if (G_TYPE_FROM_INSTANCE (file) != G_TYPE_FROM_INSTANCE (prefix))
708 iface = G_FILE_GET_IFACE (file);
710 /* The vtable function differs in arg order since we're
711 using the old contains_file call */
712 return (* iface->prefix_matches) (prefix, file);
716 * g_file_get_relative_path:
717 * @parent: input #GFile.
718 * @descendant: input #GFile.
720 * Gets the path for @descendant relative to @parent.
722 * This call does no blocking i/o.
724 * Returns: string with the relative path from @descendant
725 * to @parent, or %NULL if @descendant doesn't have @parent as prefix.
726 * The returned string should be freed with g_free() when no longer needed.
729 g_file_get_relative_path (GFile *parent,
734 g_return_val_if_fail (G_IS_FILE (parent), NULL);
735 g_return_val_if_fail (G_IS_FILE (descendant), NULL);
737 if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
740 iface = G_FILE_GET_IFACE (parent);
742 return (* iface->get_relative_path) (parent, descendant);
746 * g_file_resolve_relative_path:
747 * @file: input #GFile.
748 * @relative_path: a given relative path string.
750 * Resolves a relative path for @file to an absolute path.
752 * This call does no blocking i/o.
754 * Returns: #GFile to the resolved path. %NULL if @relative_path
755 * is %NULL or if @file is invalid.
756 * Free the returned object with g_object_unref().
759 g_file_resolve_relative_path (GFile *file,
760 const char *relative_path)
764 g_return_val_if_fail (G_IS_FILE (file), NULL);
765 g_return_val_if_fail (relative_path != NULL, NULL);
767 iface = G_FILE_GET_IFACE (file);
769 return (* iface->resolve_relative_path) (file, relative_path);
773 * g_file_enumerate_children:
774 * @file: input #GFile.
775 * @attributes: an attribute query string.
776 * @flags: a set of #GFileQueryInfoFlags.
777 * @cancellable: optional #GCancellable object, %NULL to ignore.
778 * @error: #GError for error reporting.
780 * Gets the requested information about the files in a directory. The result
781 * is a #GFileEnumerator object that will give out #GFileInfo objects for
782 * all the files in the directory.
784 * The @attribute value is a string that specifies the file attributes that
785 * should be gathered. It is not an error if it's not possible to read a particular
786 * requested attribute from a file - it just won't be set. @attribute should
787 * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
788 * means all attributes, and a wildcard like "standard::*" means all attributes in the standard
789 * namespace. An example attribute query be "standard::*,owner::user".
790 * The standard attributes are available as defines, like #G_FILE_ATTRIBUTE_STANDARD_NAME.
792 * If @cancellable is not %NULL, then the operation can be cancelled by
793 * triggering the cancellable object from another thread. If the operation
794 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
796 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
797 * If the file is not a directory, the G_FILE_ERROR_NOTDIR error will be returned.
798 * Other errors are possible too.
800 * Returns: A #GFileEnumerator if successful, %NULL on error.
801 * Free the returned object with g_object_unref().
804 g_file_enumerate_children (GFile *file,
805 const char *attributes,
806 GFileQueryInfoFlags flags,
807 GCancellable *cancellable,
813 g_return_val_if_fail (G_IS_FILE (file), NULL);
815 if (g_cancellable_set_error_if_cancelled (cancellable, error))
818 iface = G_FILE_GET_IFACE (file);
820 if (iface->enumerate_children == NULL)
822 g_set_error_literal (error, G_IO_ERROR,
823 G_IO_ERROR_NOT_SUPPORTED,
824 _("Operation not supported"));
828 return (* iface->enumerate_children) (file, attributes, flags,
833 * g_file_enumerate_children_async:
834 * @file: input #GFile.
835 * @attributes: an attribute query string.
836 * @flags: a set of #GFileQueryInfoFlags.
837 * @io_priority: the <link linkend="io-priority">I/O priority</link>
839 * @cancellable: optional #GCancellable object, %NULL to ignore.
840 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
841 * @user_data: the data to pass to callback function
843 * Asynchronously gets the requested information about the files in a directory. The result
844 * is a #GFileEnumerator object that will give out #GFileInfo objects for
845 * all the files in the directory.
847 * For more details, see g_file_enumerate_children() which is
848 * the synchronous version of this call.
850 * When the operation is finished, @callback will be called. You can then call
851 * g_file_enumerate_children_finish() to get the result of the operation.
854 g_file_enumerate_children_async (GFile *file,
855 const char *attributes,
856 GFileQueryInfoFlags flags,
858 GCancellable *cancellable,
859 GAsyncReadyCallback callback,
864 g_return_if_fail (G_IS_FILE (file));
866 iface = G_FILE_GET_IFACE (file);
867 (* iface->enumerate_children_async) (file,
877 * g_file_enumerate_children_finish:
878 * @file: input #GFile.
879 * @res: a #GAsyncResult.
882 * Finishes an async enumerate children operation.
883 * See g_file_enumerate_children_async().
885 * Returns: a #GFileEnumerator or %NULL if an error occurred.
886 * Free the returned object with g_object_unref().
889 g_file_enumerate_children_finish (GFile *file,
895 g_return_val_if_fail (G_IS_FILE (file), NULL);
896 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
898 if (G_IS_SIMPLE_ASYNC_RESULT (res))
900 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
901 if (g_simple_async_result_propagate_error (simple, error))
905 iface = G_FILE_GET_IFACE (file);
906 return (* iface->enumerate_children_finish) (file, res, error);
910 * g_file_query_exists:
911 * @file: input #GFile.
912 * @cancellable: optional #GCancellable object, %NULL to ignore.
914 * Utility function to check if a particular file exists. This is
915 * implemented using g_file_query_info() and as such does blocking I/O.
917 * Note that in many cases it is racy to first check for file existence
918 * and then execute something based on the outcome of that, because the
919 * file might have been created or removed in between the operations. The
920 * general approach to handling that is to not check, but just do the
921 * operation and handle the errors as they come.
923 * As an example of race-free checking, take the case of reading a file, and
924 * if it doesn't exist, creating it. There are two racy versions: read it, and
925 * on error create it; and: check if it exists, if not create it. These
926 * can both result in two processes creating the file (with perhaps a partially
927 * written file as the result). The correct approach is to always try to create
928 * the file with g_file_create() which will either atomically create the file
929 * or fail with a G_IO_ERROR_EXISTS error.
931 * However, in many cases an existence check is useful in a user
932 * interface, for instance to make a menu item sensitive/insensitive, so that
933 * you don't have to fool users that something is possible and then just show
934 * and error dialog. If you do this, you should make sure to also handle the
935 * errors that can happen due to races when you execute the operation.
937 * Returns: %TRUE if the file exists (and can be detected without error), %FALSE otherwise (or if cancelled).
940 g_file_query_exists (GFile *file,
941 GCancellable *cancellable)
945 g_return_val_if_fail (G_IS_FILE(file), FALSE);
947 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE,
948 G_FILE_QUERY_INFO_NONE, cancellable, NULL);
951 g_object_unref (info);
959 * g_file_query_file_type:
960 * @file: input #GFile.
961 * @flags: a set of #GFileQueryInfoFlags passed to g_file_query_info().
962 * @cancellable: optional #GCancellable object, %NULL to ignore.
964 * Utility function to inspect the #GFileType of a file. This is
965 * implemented using g_file_query_info() and as such does blocking I/O.
967 * The primary use case of this method is to check if a file is a regular file,
968 * directory, or symlink.
970 * Returns: The #GFileType of the file and #G_FILE_TYPE_UNKNOWN if the file
976 g_file_query_file_type (GFile *file,
977 GFileQueryInfoFlags flags,
978 GCancellable *cancellable)
983 g_return_val_if_fail (G_IS_FILE(file), G_FILE_TYPE_UNKNOWN);
984 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE, flags,
988 file_type = g_file_info_get_file_type (info);
989 g_object_unref (info);
992 file_type = G_FILE_TYPE_UNKNOWN;
999 * @file: input #GFile.
1000 * @attributes: an attribute query string.
1001 * @flags: a set of #GFileQueryInfoFlags.
1002 * @cancellable: optional #GCancellable object, %NULL to ignore.
1003 * @error: a #GError.
1005 * Gets the requested information about specified @file. The result
1006 * is a #GFileInfo object that contains key-value attributes (such as
1007 * the type or size of the file).
1009 * The @attribute value is a string that specifies the file attributes that
1010 * should be gathered. It is not an error if it's not possible to read a particular
1011 * requested attribute from a file - it just won't be set. @attribute should
1012 * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
1013 * means all attributes, and a wildcard like "standard::*" means all attributes in the standard
1014 * namespace. An example attribute query be "standard::*,owner::user".
1015 * The standard attributes are available as defines, like #G_FILE_ATTRIBUTE_STANDARD_NAME.
1017 * If @cancellable is not %NULL, then the operation can be cancelled by
1018 * triggering the cancellable object from another thread. If the operation
1019 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1021 * For symlinks, normally the information about the target of the
1022 * symlink is returned, rather than information about the symlink itself.
1023 * However if you pass #G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS in @flags the
1024 * information about the symlink itself will be returned. Also, for symlinks
1025 * that point to non-existing files the information about the symlink itself
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 for the given @file, or %NULL on error.
1032 * Free the returned object with g_object_unref().
1035 g_file_query_info (GFile *file,
1036 const char *attributes,
1037 GFileQueryInfoFlags flags,
1038 GCancellable *cancellable,
1043 g_return_val_if_fail (G_IS_FILE (file), NULL);
1045 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1048 iface = G_FILE_GET_IFACE (file);
1050 if (iface->query_info == NULL)
1052 g_set_error_literal (error, G_IO_ERROR,
1053 G_IO_ERROR_NOT_SUPPORTED,
1054 _("Operation not supported"));
1058 return (* iface->query_info) (file, attributes, flags, cancellable, error);
1062 * g_file_query_info_async:
1063 * @file: input #GFile.
1064 * @attributes: an attribute query string.
1065 * @flags: a set of #GFileQueryInfoFlags.
1066 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1068 * @cancellable: optional #GCancellable object, %NULL to ignore.
1069 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1070 * @user_data: the data to pass to callback function
1072 * Asynchronously gets the requested information about specified @file. The result
1073 * is a #GFileInfo object that contains key-value attributes (such as type or size
1076 * For more details, see g_file_query_info() which is
1077 * the synchronous version of this call.
1079 * When the operation is finished, @callback will be called. You can then call
1080 * g_file_query_info_finish() to get the result of the operation.
1083 g_file_query_info_async (GFile *file,
1084 const char *attributes,
1085 GFileQueryInfoFlags flags,
1087 GCancellable *cancellable,
1088 GAsyncReadyCallback callback,
1093 g_return_if_fail (G_IS_FILE (file));
1095 iface = G_FILE_GET_IFACE (file);
1096 (* iface->query_info_async) (file,
1106 * g_file_query_info_finish:
1107 * @file: input #GFile.
1108 * @res: a #GAsyncResult.
1109 * @error: a #GError.
1111 * Finishes an asynchronous file info query.
1112 * See g_file_query_info_async().
1114 * Returns: #GFileInfo for given @file or %NULL on error.
1115 * Free the returned object with g_object_unref().
1118 g_file_query_info_finish (GFile *file,
1124 g_return_val_if_fail (G_IS_FILE (file), NULL);
1125 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1127 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1129 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1130 if (g_simple_async_result_propagate_error (simple, error))
1134 iface = G_FILE_GET_IFACE (file);
1135 return (* iface->query_info_finish) (file, res, error);
1139 * g_file_query_filesystem_info:
1140 * @file: input #GFile.
1141 * @attributes: an attribute query string.
1142 * @cancellable: optional #GCancellable object, %NULL to ignore.
1143 * @error: a #GError.
1145 * Similar to g_file_query_info(), but obtains information
1146 * about the filesystem the @file is on, rather than the file itself.
1147 * For instance the amount of space available and the type of
1150 * The @attribute value is a string that specifies the file attributes that
1151 * should be gathered. It is not an error if it's not possible to read a particular
1152 * requested attribute from a file - it just won't be set. @attribute should
1153 * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
1154 * means all attributes, and a wildcard like "fs:*" means all attributes in the fs
1155 * namespace. The standard namespace for filesystem attributes is "fs".
1156 * Common attributes of interest are #G_FILE_ATTRIBUTE_FILESYSTEM_SIZE
1157 * (the total size of the filesystem in bytes), #G_FILE_ATTRIBUTE_FILESYSTEM_FREE (number of
1158 * bytes available), and #G_FILE_ATTRIBUTE_FILESYSTEM_TYPE (type of the 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 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1165 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1167 * Returns: a #GFileInfo or %NULL if there was an error.
1168 * Free the returned object with g_object_unref().
1171 g_file_query_filesystem_info (GFile *file,
1172 const char *attributes,
1173 GCancellable *cancellable,
1178 g_return_val_if_fail (G_IS_FILE (file), NULL);
1180 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1183 iface = G_FILE_GET_IFACE (file);
1185 if (iface->query_filesystem_info == NULL)
1187 g_set_error_literal (error, G_IO_ERROR,
1188 G_IO_ERROR_NOT_SUPPORTED,
1189 _("Operation not supported"));
1193 return (* iface->query_filesystem_info) (file, attributes, cancellable, error);
1197 * g_file_query_filesystem_info_async:
1198 * @file: input #GFile.
1199 * @attributes: an attribute query string.
1200 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1202 * @cancellable: optional #GCancellable object, %NULL to ignore.
1203 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1204 * @user_data: the data to pass to callback function
1206 * Asynchronously gets the requested information about the filesystem
1207 * that the specified @file is on. The result is a #GFileInfo object
1208 * that contains key-value attributes (such as type or size for the
1211 * For more details, see g_file_query_filesystem_info() which is the
1212 * synchronous version of this call.
1214 * When the operation is finished, @callback will be called. You can
1215 * then call g_file_query_info_finish() to get the result of the
1219 g_file_query_filesystem_info_async (GFile *file,
1220 const char *attributes,
1222 GCancellable *cancellable,
1223 GAsyncReadyCallback callback,
1228 g_return_if_fail (G_IS_FILE (file));
1230 iface = G_FILE_GET_IFACE (file);
1231 (* iface->query_filesystem_info_async) (file,
1240 * g_file_query_filesystem_info_finish:
1241 * @file: input #GFile.
1242 * @res: a #GAsyncResult.
1243 * @error: a #GError.
1245 * Finishes an asynchronous filesystem info query. See
1246 * g_file_query_filesystem_info_async().
1248 * Returns: #GFileInfo for given @file or %NULL on error.
1249 * Free the returned object with g_object_unref().
1252 g_file_query_filesystem_info_finish (GFile *file,
1258 g_return_val_if_fail (G_IS_FILE (file), NULL);
1259 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1261 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1263 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1264 if (g_simple_async_result_propagate_error (simple, error))
1268 iface = G_FILE_GET_IFACE (file);
1269 return (* iface->query_filesystem_info_finish) (file, res, error);
1273 * g_file_find_enclosing_mount:
1274 * @file: input #GFile.
1275 * @cancellable: optional #GCancellable object, %NULL to ignore.
1276 * @error: a #GError.
1278 * Gets a #GMount for the #GFile.
1280 * If the #GFileIface for @file does not have a mount (e.g. possibly a
1281 * remote share), @error will be set to %G_IO_ERROR_NOT_FOUND and %NULL
1284 * If @cancellable is not %NULL, then the operation can be cancelled by
1285 * triggering the cancellable object from another thread. If the operation
1286 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1288 * Returns: a #GMount where the @file is located or %NULL on error.
1289 * Free the returned object with g_object_unref().
1292 g_file_find_enclosing_mount (GFile *file,
1293 GCancellable *cancellable,
1298 g_return_val_if_fail (G_IS_FILE (file), NULL);
1300 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1303 iface = G_FILE_GET_IFACE (file);
1304 if (iface->find_enclosing_mount == NULL)
1307 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1308 /* Translators: This is an error message when trying to find the
1309 * enclosing (user visible) mount of a file, but none exists. */
1310 _("Containing mount does not exist"));
1314 return (* iface->find_enclosing_mount) (file, cancellable, error);
1318 * g_file_find_enclosing_mount_async:
1320 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1322 * @cancellable: optional #GCancellable object, %NULL to ignore.
1323 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1324 * @user_data: the data to pass to callback function
1326 * Asynchronously gets the mount for the file.
1328 * For more details, see g_file_find_enclosing_mount() which is
1329 * the synchronous version of this call.
1331 * When the operation is finished, @callback will be called. You can then call
1332 * g_file_find_enclosing_mount_finish() to get the result of the operation.
1335 g_file_find_enclosing_mount_async (GFile *file,
1337 GCancellable *cancellable,
1338 GAsyncReadyCallback callback,
1343 g_return_if_fail (G_IS_FILE (file));
1345 iface = G_FILE_GET_IFACE (file);
1346 (* iface->find_enclosing_mount_async) (file,
1354 * g_file_find_enclosing_mount_finish:
1356 * @res: a #GAsyncResult
1359 * Finishes an asynchronous find mount request.
1360 * See g_file_find_enclosing_mount_async().
1362 * Returns: #GMount for given @file or %NULL on error.
1363 * Free the returned object with g_object_unref().
1366 g_file_find_enclosing_mount_finish (GFile *file,
1372 g_return_val_if_fail (G_IS_FILE (file), NULL);
1373 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1375 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1377 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1378 if (g_simple_async_result_propagate_error (simple, error))
1382 iface = G_FILE_GET_IFACE (file);
1383 return (* iface->find_enclosing_mount_finish) (file, res, error);
1389 * @file: #GFile to read.
1390 * @cancellable: a #GCancellable
1391 * @error: a #GError, or %NULL
1393 * Opens a file for reading. The result is a #GFileInputStream that
1394 * can be used to read the contents of the file.
1396 * If @cancellable is not %NULL, then the operation can be cancelled by
1397 * triggering the cancellable object from another thread. If the operation
1398 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1400 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1401 * If the file is a directory, the G_IO_ERROR_IS_DIRECTORY error will be returned.
1402 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1404 * Returns: #GFileInputStream or %NULL on error.
1405 * Free the returned object with g_object_unref().
1408 g_file_read (GFile *file,
1409 GCancellable *cancellable,
1414 g_return_val_if_fail (G_IS_FILE (file), NULL);
1416 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1419 iface = G_FILE_GET_IFACE (file);
1421 if (iface->read_fn == NULL)
1423 g_set_error_literal (error, G_IO_ERROR,
1424 G_IO_ERROR_NOT_SUPPORTED,
1425 _("Operation not supported"));
1429 return (* iface->read_fn) (file, cancellable, error);
1434 * @file: input #GFile.
1435 * @flags: a set of #GFileCreateFlags.
1436 * @cancellable: optional #GCancellable object, %NULL to ignore.
1437 * @error: a #GError, or %NULL
1439 * Gets an output stream for appending data to the file. If
1440 * the file doesn't already exist it is created.
1442 * By default files created are generally readable by everyone,
1443 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1444 * will be made readable only to the current user, to the level that
1445 * is supported on the target filesystem.
1447 * If @cancellable is not %NULL, then the operation can be cancelled by
1448 * triggering the cancellable object from another thread. If the operation
1449 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1451 * Some file systems don't allow all file names, and may
1452 * return an %G_IO_ERROR_INVALID_FILENAME error.
1453 * If the file is a directory the %G_IO_ERROR_IS_DIRECTORY error will be
1454 * returned. Other errors are possible too, and depend on what kind of
1455 * filesystem the file is on.
1457 * Returns: a #GFileOutputStream, or %NULL on error.
1458 * Free the returned object with g_object_unref().
1461 g_file_append_to (GFile *file,
1462 GFileCreateFlags flags,
1463 GCancellable *cancellable,
1468 g_return_val_if_fail (G_IS_FILE (file), NULL);
1470 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1473 iface = G_FILE_GET_IFACE (file);
1475 if (iface->append_to == NULL)
1477 g_set_error_literal (error, G_IO_ERROR,
1478 G_IO_ERROR_NOT_SUPPORTED,
1479 _("Operation not supported"));
1483 return (* iface->append_to) (file, flags, cancellable, error);
1488 * @file: input #GFile.
1489 * @flags: a set of #GFileCreateFlags.
1490 * @cancellable: optional #GCancellable object, %NULL to ignore.
1491 * @error: a #GError, or %NULL
1493 * Creates a new file and returns an output stream for writing to it.
1494 * The file must not already exist.
1496 * By default files created are generally readable by everyone,
1497 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1498 * will be made readable only to the current user, to the level that
1499 * is supported on the target filesystem.
1501 * If @cancellable is not %NULL, then the operation can be cancelled by
1502 * triggering the cancellable object from another thread. If the operation
1503 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1505 * If a file or directory with this name already exists the G_IO_ERROR_EXISTS
1506 * error will be returned.
1507 * Some file systems don't allow all file names, and may
1508 * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1509 * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1510 * Other errors are possible too, and depend on what kind of
1511 * filesystem the file is on.
1513 * Returns: a #GFileOutputStream for the newly created file, or
1515 * Free the returned object with g_object_unref().
1518 g_file_create (GFile *file,
1519 GFileCreateFlags flags,
1520 GCancellable *cancellable,
1525 g_return_val_if_fail (G_IS_FILE (file), NULL);
1527 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1530 iface = G_FILE_GET_IFACE (file);
1532 if (iface->create == NULL)
1534 g_set_error_literal (error, G_IO_ERROR,
1535 G_IO_ERROR_NOT_SUPPORTED,
1536 _("Operation not supported"));
1540 return (* iface->create) (file, flags, cancellable, error);
1545 * @file: input #GFile.
1546 * @etag: an optional <link linkend="gfile-etag">entity tag</link> for the
1547 * current #GFile, or #NULL to ignore.
1548 * @make_backup: %TRUE if a backup should be created.
1549 * @flags: a set of #GFileCreateFlags.
1550 * @cancellable: optional #GCancellable object, %NULL to ignore.
1551 * @error: a #GError, or %NULL
1553 * Returns an output stream for overwriting the file, possibly
1554 * creating a backup copy of the file first. If the file doesn't exist,
1555 * it will be created.
1557 * This will try to replace the file in the safest way possible so
1558 * that any errors during the writing will not affect an already
1559 * existing copy of the file. For instance, for local files it
1560 * may write to a temporary file and then atomically rename over
1561 * the destination when the stream is closed.
1563 * By default files created are generally readable by everyone,
1564 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1565 * will be made readable only to the current user, to the level that
1566 * is supported on the target filesystem.
1568 * If @cancellable is not %NULL, then the operation can be cancelled by
1569 * triggering the cancellable object from another thread. If the operation
1570 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1572 * If you pass in a non-#NULL @etag value, then this value is
1573 * compared to the current entity tag of the file, and if they differ
1574 * an G_IO_ERROR_WRONG_ETAG error is returned. This generally means
1575 * that the file has been changed since you last read it. You can get
1576 * the new etag from g_file_output_stream_get_etag() after you've
1577 * finished writing and closed the #GFileOutputStream. When you load
1578 * a new file you can use g_file_input_stream_query_info() to get
1579 * the etag of the file.
1581 * If @make_backup is %TRUE, this function will attempt to make a backup
1582 * of the current file before overwriting it. If this fails a G_IO_ERROR_CANT_CREATE_BACKUP
1583 * error will be returned. If you want to replace anyway, try again with
1584 * @make_backup set to %FALSE.
1586 * If the file is a directory the G_IO_ERROR_IS_DIRECTORY error will be returned,
1587 * and if the file is some other form of non-regular file then a
1588 * G_IO_ERROR_NOT_REGULAR_FILE error will be returned.
1589 * Some file systems don't allow all file names, and may
1590 * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1591 * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1592 * Other errors are possible too, and depend on what kind of
1593 * filesystem the file is on.
1595 * Returns: a #GFileOutputStream or %NULL on error.
1596 * Free the returned object with g_object_unref().
1599 g_file_replace (GFile *file,
1601 gboolean make_backup,
1602 GFileCreateFlags flags,
1603 GCancellable *cancellable,
1608 g_return_val_if_fail (G_IS_FILE (file), NULL);
1610 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1613 iface = G_FILE_GET_IFACE (file);
1615 if (iface->replace == NULL)
1617 g_set_error_literal (error, G_IO_ERROR,
1618 G_IO_ERROR_NOT_SUPPORTED,
1619 _("Operation not supported"));
1624 /* Handle empty tag string as NULL in consistent way. */
1625 if (etag && *etag == 0)
1628 return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1632 * g_file_read_async:
1633 * @file: input #GFile.
1634 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1636 * @cancellable: optional #GCancellable object, %NULL to ignore.
1637 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1638 * @user_data: the data to pass to callback function
1640 * Asynchronously opens @file for reading.
1642 * For more details, see g_file_read() which is
1643 * the synchronous version of this call.
1645 * When the operation is finished, @callback will be called. You can then call
1646 * g_file_read_finish() to get the result of the operation.
1649 g_file_read_async (GFile *file,
1651 GCancellable *cancellable,
1652 GAsyncReadyCallback callback,
1657 g_return_if_fail (G_IS_FILE (file));
1659 iface = G_FILE_GET_IFACE (file);
1660 (* iface->read_async) (file,
1668 * g_file_read_finish:
1669 * @file: input #GFile.
1670 * @res: a #GAsyncResult.
1671 * @error: a #GError, or %NULL
1673 * Finishes an asynchronous file read operation started with
1674 * g_file_read_async().
1676 * Returns: a #GFileInputStream or %NULL on error.
1677 * Free the returned object with g_object_unref().
1680 g_file_read_finish (GFile *file,
1686 g_return_val_if_fail (G_IS_FILE (file), NULL);
1687 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1689 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1691 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1692 if (g_simple_async_result_propagate_error (simple, error))
1696 iface = G_FILE_GET_IFACE (file);
1697 return (* iface->read_finish) (file, res, error);
1701 * g_file_append_to_async:
1702 * @file: input #GFile.
1703 * @flags: a set of #GFileCreateFlags.
1704 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1706 * @cancellable: optional #GCancellable object, %NULL to ignore.
1707 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1708 * @user_data: the data to pass to callback function
1710 * Asynchronously opens @file for appending.
1712 * For more details, see g_file_append_to() which is
1713 * the synchronous version of this call.
1715 * When the operation is finished, @callback will be called. You can then call
1716 * g_file_append_to_finish() to get the result of the operation.
1719 g_file_append_to_async (GFile *file,
1720 GFileCreateFlags flags,
1722 GCancellable *cancellable,
1723 GAsyncReadyCallback callback,
1728 g_return_if_fail (G_IS_FILE (file));
1730 iface = G_FILE_GET_IFACE (file);
1731 (* iface->append_to_async) (file,
1740 * g_file_append_to_finish:
1741 * @file: input #GFile.
1742 * @res: #GAsyncResult
1743 * @error: a #GError, or %NULL
1745 * Finishes an asynchronous file append operation started with
1746 * g_file_append_to_async().
1748 * Returns: a valid #GFileOutputStream or %NULL on error.
1749 * Free the returned object with g_object_unref().
1752 g_file_append_to_finish (GFile *file,
1758 g_return_val_if_fail (G_IS_FILE (file), NULL);
1759 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1761 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1763 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1764 if (g_simple_async_result_propagate_error (simple, error))
1768 iface = G_FILE_GET_IFACE (file);
1769 return (* iface->append_to_finish) (file, res, error);
1773 * g_file_create_async:
1774 * @file: input #GFile.
1775 * @flags: a set of #GFileCreateFlags.
1776 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1778 * @cancellable: optional #GCancellable object, %NULL to ignore.
1779 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1780 * @user_data: the data to pass to callback function
1782 * Asynchronously creates a new file and returns an output stream for writing to it.
1783 * The file must not already exist.
1785 * For more details, see g_file_create() which is
1786 * the synchronous version of this call.
1788 * When the operation is finished, @callback will be called. You can then call
1789 * g_file_create_finish() to get the result of the operation.
1792 g_file_create_async (GFile *file,
1793 GFileCreateFlags flags,
1795 GCancellable *cancellable,
1796 GAsyncReadyCallback callback,
1801 g_return_if_fail (G_IS_FILE (file));
1803 iface = G_FILE_GET_IFACE (file);
1804 (* iface->create_async) (file,
1813 * g_file_create_finish:
1814 * @file: input #GFile.
1815 * @res: a #GAsyncResult.
1816 * @error: a #GError, or %NULL
1818 * Finishes an asynchronous file create operation started with
1819 * g_file_create_async().
1821 * Returns: a #GFileOutputStream or %NULL on error.
1822 * Free the returned object with g_object_unref().
1825 g_file_create_finish (GFile *file,
1831 g_return_val_if_fail (G_IS_FILE (file), NULL);
1832 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1834 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1836 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1837 if (g_simple_async_result_propagate_error (simple, error))
1841 iface = G_FILE_GET_IFACE (file);
1842 return (* iface->create_finish) (file, res, error);
1846 * g_file_replace_async:
1847 * @file: input #GFile.
1848 * @etag: an <link linkend="gfile-etag">entity tag</link> for the
1849 * current #GFile, or NULL to ignore.
1850 * @make_backup: %TRUE if a backup should be created.
1851 * @flags: a set of #GFileCreateFlags.
1852 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1854 * @cancellable: optional #GCancellable object, %NULL to ignore.
1855 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1856 * @user_data: the data to pass to callback function
1858 * Asynchronously overwrites the file, replacing the contents, possibly
1859 * creating a backup copy of the file first.
1861 * For more details, see g_file_replace() which is
1862 * the synchronous version of this call.
1864 * When the operation is finished, @callback will be called. You can then call
1865 * g_file_replace_finish() to get the result of the operation.
1868 g_file_replace_async (GFile *file,
1870 gboolean make_backup,
1871 GFileCreateFlags flags,
1873 GCancellable *cancellable,
1874 GAsyncReadyCallback callback,
1879 g_return_if_fail (G_IS_FILE (file));
1881 iface = G_FILE_GET_IFACE (file);
1882 (* iface->replace_async) (file,
1893 * g_file_replace_finish:
1894 * @file: input #GFile.
1895 * @res: a #GAsyncResult.
1896 * @error: a #GError, or %NULL
1898 * Finishes an asynchronous file replace operation started with
1899 * g_file_replace_async().
1901 * Returns: a #GFileOutputStream, or %NULL on error.
1902 * Free the returned object with g_object_unref().
1905 g_file_replace_finish (GFile *file,
1911 g_return_val_if_fail (G_IS_FILE (file), NULL);
1912 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1914 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1916 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1917 if (g_simple_async_result_propagate_error (simple, error))
1921 iface = G_FILE_GET_IFACE (file);
1922 return (* iface->replace_finish) (file, res, error);
1926 copy_symlink (GFile *destination,
1927 GFileCopyFlags flags,
1928 GCancellable *cancellable,
1933 gboolean tried_delete;
1935 GFileType file_type;
1937 tried_delete = FALSE;
1941 if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
1943 /* Maybe it already existed, and we want to overwrite? */
1944 if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) &&
1945 my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
1947 g_error_free (my_error);
1950 /* Don't overwrite if the destination is a directory */
1951 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1952 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1953 cancellable, &my_error);
1956 file_type = g_file_info_get_file_type (info);
1957 g_object_unref (info);
1959 if (file_type == G_FILE_TYPE_DIRECTORY)
1961 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
1962 _("Can't copy over directory"));
1967 if (!g_file_delete (destination, cancellable, error))
1970 tried_delete = TRUE;
1974 g_propagate_error (error, my_error);
1981 static GInputStream *
1982 open_source_for_copy (GFile *source,
1984 GFileCopyFlags flags,
1985 GCancellable *cancellable,
1991 GFileType file_type;
1994 in = (GInputStream *)g_file_read (source, cancellable, &my_error);
1998 /* There was an error opening the source, try to set a good error for it: */
2000 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
2002 /* The source is a directory, don't fail with WOULD_RECURSE immediately,
2003 * as that is less useful to the app. Better check for errors on the
2006 g_error_free (my_error);
2009 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2010 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2011 cancellable, &my_error);
2014 file_type = g_file_info_get_file_type (info);
2015 g_object_unref (info);
2017 if (flags & G_FILE_COPY_OVERWRITE)
2019 if (file_type == G_FILE_TYPE_DIRECTORY)
2021 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
2022 _("Can't copy directory over directory"));
2025 /* continue to would_recurse error */
2029 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
2030 _("Target file exists"));
2036 /* Error getting info from target, return that error
2037 * (except for NOT_FOUND, which is no error here)
2039 if (my_error->domain != G_IO_ERROR && my_error->code != G_IO_ERROR_NOT_FOUND)
2041 g_propagate_error (error, my_error);
2044 g_error_free (my_error);
2047 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
2048 _("Can't recursively copy directory"));
2052 g_propagate_error (error, my_error);
2057 should_copy (GFileAttributeInfo *info,
2061 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
2062 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
2066 build_attribute_list_for_copy (GFileAttributeInfoList *attributes,
2067 GFileAttributeInfoList *namespaces,
2075 s = g_string_new ("");
2079 for (i = 0; i < attributes->n_infos; i++)
2081 if (should_copy (&attributes->infos[i], as_move))
2086 g_string_append_c (s, ',');
2088 g_string_append (s, attributes->infos[i].name);
2095 for (i = 0; i < namespaces->n_infos; i++)
2097 if (should_copy (&namespaces->infos[i], as_move))
2102 g_string_append_c (s, ',');
2104 g_string_append (s, namespaces->infos[i].name);
2105 g_string_append (s, ":*");
2110 return g_string_free (s, FALSE);
2114 * g_file_copy_attributes:
2115 * @source: a #GFile with attributes.
2116 * @destination: a #GFile to copy attributes to.
2117 * @flags: a set of #GFileCopyFlags.
2118 * @cancellable: optional #GCancellable object, %NULL to ignore.
2119 * @error: a #GError, %NULL to ignore.
2121 * Copies the file attributes from @source to @destination.
2123 * Normally only a subset of the file attributes are copied,
2124 * those that are copies in a normal file copy operation
2125 * (which for instance does not include e.g. mtime). However
2126 * if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
2127 * all the metadata that is possible to copy is copied.
2129 * Returns: %TRUE if the attributes were copied successfully, %FALSE otherwise.
2132 g_file_copy_attributes (GFile *source,
2134 GFileCopyFlags flags,
2135 GCancellable *cancellable,
2138 GFileAttributeInfoList *attributes, *namespaces;
2139 char *attrs_to_read;
2143 gboolean source_nofollow_symlinks;
2145 as_move = flags & G_FILE_COPY_ALL_METADATA;
2146 source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
2148 /* Ignore errors here, if the target supports no attributes there is nothing to copy */
2149 attributes = g_file_query_settable_attributes (destination, cancellable, NULL);
2150 namespaces = g_file_query_writable_namespaces (destination, cancellable, NULL);
2152 if (attributes == NULL && namespaces == NULL)
2155 attrs_to_read = build_attribute_list_for_copy (attributes, namespaces, as_move);
2157 /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
2158 * we just don't copy it.
2160 info = g_file_query_info (source, attrs_to_read,
2161 source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
2165 g_free (attrs_to_read);
2170 res = g_file_set_attributes_from_info (destination,
2174 g_object_unref (info);
2177 g_file_attribute_info_list_unref (attributes);
2178 g_file_attribute_info_list_unref (namespaces);
2183 /* Closes the streams */
2185 copy_stream_with_progress (GInputStream *in,
2188 GCancellable *cancellable,
2189 GFileProgressCallback progress_callback,
2190 gpointer progress_callback_data,
2193 gssize n_read, n_written;
2194 goffset current_size;
2195 char buffer[1024*64], *p;
2201 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
2202 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2206 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2207 total_size = g_file_info_get_size (info);
2208 g_object_unref (info);
2211 if (total_size == -1)
2213 info = g_file_query_info (source,
2214 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2215 G_FILE_QUERY_INFO_NONE,
2219 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2220 total_size = g_file_info_get_size (info);
2221 g_object_unref (info);
2225 if (total_size == -1)
2232 n_read = g_input_stream_read (in, buffer, sizeof (buffer), cancellable, error);
2242 current_size += n_read;
2247 n_written = g_output_stream_write (out, p, n_read, cancellable, error);
2248 if (n_written == -1)
2255 n_read -= n_written;
2261 if (progress_callback)
2262 progress_callback (current_size, total_size, progress_callback_data);
2266 error = NULL; /* Ignore further errors */
2268 /* Make sure we send full copied size */
2269 if (progress_callback)
2270 progress_callback (current_size, total_size, progress_callback_data);
2272 /* Don't care about errors in source here */
2273 g_input_stream_close (in, cancellable, NULL);
2275 /* But write errors on close are bad! */
2276 if (!g_output_stream_close (out, cancellable, error))
2279 g_object_unref (in);
2280 g_object_unref (out);
2286 file_copy_fallback (GFile *source,
2288 GFileCopyFlags flags,
2289 GCancellable *cancellable,
2290 GFileProgressCallback progress_callback,
2291 gpointer progress_callback_data,
2299 /* Maybe copy the symlink? */
2300 if (flags & G_FILE_COPY_NOFOLLOW_SYMLINKS)
2302 info = g_file_query_info (source,
2303 G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
2304 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2310 if (g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK &&
2311 (target = g_file_info_get_symlink_target (info)) != NULL)
2313 if (!copy_symlink (destination, flags, cancellable, target, error))
2315 g_object_unref (info);
2319 g_object_unref (info);
2323 g_object_unref (info);
2326 in = open_source_for_copy (source, destination, flags, cancellable, error);
2330 if (flags & G_FILE_COPY_OVERWRITE)
2332 out = (GOutputStream *)g_file_replace (destination,
2334 flags & G_FILE_COPY_BACKUP,
2336 cancellable, error);
2340 out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
2345 g_object_unref (in);
2349 if (!copy_stream_with_progress (in, out, source, cancellable,
2350 progress_callback, progress_callback_data,
2356 /* Ignore errors here. Failure to copy metadata is not a hard error */
2357 g_file_copy_attributes (source, destination,
2358 flags, cancellable, NULL);
2365 * @source: input #GFile.
2366 * @destination: destination #GFile
2367 * @flags: set of #GFileCopyFlags
2368 * @cancellable: optional #GCancellable object, %NULL to ignore.
2369 * @progress_callback: function to callback with progress information
2370 * @progress_callback_data: user data to pass to @progress_callback
2371 * @error: #GError to set on error, or %NULL
2373 * Copies the file @source to the location specified by @destination.
2374 * Can not handle recursive copies of directories.
2376 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2377 * existing @destination file is overwritten.
2379 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2380 * will be copied as symlinks, otherwise the target of the
2381 * @source symlink will be copied.
2383 * If @cancellable is not %NULL, then the operation can be cancelled by
2384 * triggering the cancellable object from another thread. If the operation
2385 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2387 * If @progress_callback is not %NULL, then the operation can be monitored by
2388 * setting this to a #GFileProgressCallback function. @progress_callback_data
2389 * will be passed to this function. It is guaranteed that this callback will
2390 * be called after all data has been transferred with the total number of bytes
2391 * copied during the operation.
2393 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2394 * error is returned, independent on the status of the @destination.
2396 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2397 * error G_IO_ERROR_EXISTS is returned.
2399 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2400 * error is returned. If trying to overwrite a directory with a directory the
2401 * G_IO_ERROR_WOULD_MERGE error is returned.
2403 * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2404 * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2407 * If you are interested in copying the #GFile object itself (not the on-disk
2408 * file), see g_file_dup().
2410 * Returns: %TRUE on success, %FALSE otherwise.
2413 g_file_copy (GFile *source,
2415 GFileCopyFlags flags,
2416 GCancellable *cancellable,
2417 GFileProgressCallback progress_callback,
2418 gpointer progress_callback_data,
2425 g_return_val_if_fail (G_IS_FILE (source), FALSE);
2426 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2428 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2431 iface = G_FILE_GET_IFACE (destination);
2435 res = (* iface->copy) (source, destination,
2437 progress_callback, progress_callback_data,
2443 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2445 g_propagate_error (error, my_error);
2450 /* If the types are different, and the destination method failed
2451 also try the source method */
2452 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
2454 iface = G_FILE_GET_IFACE (source);
2459 res = (* iface->copy) (source, destination,
2461 progress_callback, progress_callback_data,
2467 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2469 g_propagate_error (error, my_error);
2475 return file_copy_fallback (source, destination, flags, cancellable,
2476 progress_callback, progress_callback_data,
2481 * g_file_copy_async:
2482 * @source: input #GFile.
2483 * @destination: destination #GFile
2484 * @flags: set of #GFileCopyFlags
2485 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2487 * @cancellable: optional #GCancellable object, %NULL to ignore.
2488 * @progress_callback: function to callback with progress information
2489 * @progress_callback_data: user data to pass to @progress_callback
2490 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2491 * @user_data: the data to pass to callback function
2493 * Copies the file @source to the location specified by @destination
2494 * asynchronously. For details of the behaviour, see g_file_copy().
2496 * If @progress_callback is not %NULL, then that function that will be called
2497 * just like in g_file_copy(), however the callback will run in the main loop,
2498 * not in the thread that is doing the I/O operation.
2500 * When the operation is finished, @callback will be called. You can then call
2501 * g_file_copy_finish() to get the result of the operation.
2504 g_file_copy_async (GFile *source,
2506 GFileCopyFlags flags,
2508 GCancellable *cancellable,
2509 GFileProgressCallback progress_callback,
2510 gpointer progress_callback_data,
2511 GAsyncReadyCallback callback,
2516 g_return_if_fail (G_IS_FILE (source));
2517 g_return_if_fail (G_IS_FILE (destination));
2519 iface = G_FILE_GET_IFACE (source);
2520 (* iface->copy_async) (source,
2526 progress_callback_data,
2532 * g_file_copy_finish:
2533 * @file: input #GFile.
2534 * @res: a #GAsyncResult.
2535 * @error: a #GError, or %NULL
2537 * Finishes copying the file started with
2538 * g_file_copy_async().
2540 * Returns: a %TRUE on success, %FALSE on error.
2543 g_file_copy_finish (GFile *file,
2549 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2550 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
2552 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2554 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2556 if (g_simple_async_result_propagate_error (simple, error))
2560 iface = G_FILE_GET_IFACE (file);
2561 return (* iface->copy_finish) (file, res, error);
2566 * @source: #GFile pointing to the source location.
2567 * @destination: #GFile pointing to the destination location.
2568 * @flags: set of #GFileCopyFlags.
2569 * @cancellable: optional #GCancellable object, %NULL to ignore.
2570 * @progress_callback: #GFileProgressCallback function for updates.
2571 * @progress_callback_data: gpointer to user data for the callback function.
2572 * @error: #GError for returning error conditions, or %NULL
2575 * Tries to move the file or directory @source to the location specified by @destination.
2576 * If native move operations are supported then this is used, otherwise a copy + delete
2577 * fallback is used. The native implementation may support moving directories (for instance
2578 * on moves inside the same filesystem), but the fallback code does not.
2580 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2581 * existing @destination file is overwritten.
2583 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2584 * will be copied as symlinks, otherwise the target of the
2585 * @source symlink will be copied.
2587 * If @cancellable is not %NULL, then the operation can be cancelled by
2588 * triggering the cancellable object from another thread. If the operation
2589 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2591 * If @progress_callback is not %NULL, then the operation can be monitored by
2592 * setting this to a #GFileProgressCallback function. @progress_callback_data
2593 * will be passed to this function. It is guaranteed that this callback will
2594 * be called after all data has been transferred with the total number of bytes
2595 * copied during the operation.
2597 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2598 * error is returned, independent on the status of the @destination.
2600 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2601 * error G_IO_ERROR_EXISTS is returned.
2603 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2604 * error is returned. If trying to overwrite a directory with a directory the
2605 * G_IO_ERROR_WOULD_MERGE error is returned.
2607 * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2608 * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2609 * may be returned (if the native move operation isn't available).
2611 * Returns: %TRUE on successful move, %FALSE otherwise.
2614 g_file_move (GFile *source,
2616 GFileCopyFlags flags,
2617 GCancellable *cancellable,
2618 GFileProgressCallback progress_callback,
2619 gpointer progress_callback_data,
2626 g_return_val_if_fail (G_IS_FILE (source), FALSE);
2627 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2629 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2632 iface = G_FILE_GET_IFACE (destination);
2636 res = (* iface->move) (source, destination,
2638 progress_callback, progress_callback_data,
2644 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2646 g_propagate_error (error, my_error);
2651 /* If the types are different, and the destination method failed
2652 also try the source method */
2653 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
2655 iface = G_FILE_GET_IFACE (source);
2660 res = (* iface->move) (source, destination,
2662 progress_callback, progress_callback_data,
2668 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2670 g_propagate_error (error, my_error);
2676 if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
2678 g_set_error_literal (error, G_IO_ERROR,
2679 G_IO_ERROR_NOT_SUPPORTED,
2680 _("Operation not supported"));
2684 flags |= G_FILE_COPY_ALL_METADATA;
2685 if (!g_file_copy (source, destination, flags, cancellable,
2686 progress_callback, progress_callback_data,
2690 return g_file_delete (source, cancellable, error);
2694 * g_file_make_directory
2695 * @file: input #GFile.
2696 * @cancellable: optional #GCancellable object, %NULL to ignore.
2697 * @error: a #GError, or %NULL
2699 * Creates a directory. Note that this will only create a child directory of
2700 * the immediate parent directory of the path or URI given by the #GFile. To
2701 * recursively create directories, see g_file_make_directory_with_parents().
2702 * This function will fail if the parent directory does not exist, setting
2703 * @error to %G_IO_ERROR_NOT_FOUND. If the file system doesn't support creating
2704 * directories, this function will fail, setting @error to
2705 * %G_IO_ERROR_NOT_SUPPORTED.
2707 * If @cancellable is not %NULL, then the operation can be cancelled by
2708 * triggering the cancellable object from another thread. If the operation
2709 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2711 * Returns: %TRUE on successful creation, %FALSE otherwise.
2714 g_file_make_directory (GFile *file,
2715 GCancellable *cancellable,
2720 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2722 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2725 iface = G_FILE_GET_IFACE (file);
2727 if (iface->make_directory == NULL)
2729 g_set_error_literal (error, G_IO_ERROR,
2730 G_IO_ERROR_NOT_SUPPORTED,
2731 _("Operation not supported"));
2735 return (* iface->make_directory) (file, cancellable, error);
2739 * g_file_make_directory_with_parents:
2740 * @file: input #GFile.
2741 * @cancellable: optional #GCancellable object, %NULL to ignore.
2742 * @error: a #GError, or %NULL
2744 * Creates a directory and any parent directories that may not exist similar to
2745 * 'mkdir -p'. If the file system does not support creating directories, this
2746 * function will fail, setting @error to %G_IO_ERROR_NOT_SUPPORTED.
2748 * If @cancellable is not %NULL, then the operation can be cancelled by
2749 * triggering the cancellable object from another thread. If the operation
2750 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2752 * Returns: %TRUE if all directories have been successfully created, %FALSE
2758 g_file_make_directory_with_parents (GFile *file,
2759 GCancellable *cancellable,
2763 GFile *parent_file, *work_file;
2764 GList *list = NULL, *l;
2765 GError *my_error = NULL;
2767 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2770 result = g_file_make_directory (file, cancellable, &my_error);
2771 if (result || my_error->code != G_IO_ERROR_NOT_FOUND)
2774 g_propagate_error (error, my_error);
2780 while (!result && my_error->code == G_IO_ERROR_NOT_FOUND)
2782 g_clear_error (&my_error);
2784 parent_file = g_file_get_parent (work_file);
2785 if (parent_file == NULL)
2787 result = g_file_make_directory (parent_file, cancellable, &my_error);
2789 if (!result && my_error->code == G_IO_ERROR_NOT_FOUND)
2790 list = g_list_prepend (list, parent_file);
2792 work_file = parent_file;
2795 for (l = list; result && l; l = l->next)
2797 result = g_file_make_directory ((GFile *) l->data, cancellable, &my_error);
2801 while (list != NULL)
2803 g_object_unref ((GFile *) list->data);
2804 list = g_list_remove (list, list->data);
2809 g_propagate_error (error, my_error);
2813 return g_file_make_directory (file, cancellable, error);
2817 * g_file_make_symbolic_link:
2818 * @file: input #GFile.
2819 * @symlink_value: a string with the value of the new symlink.
2820 * @cancellable: optional #GCancellable object, %NULL to ignore.
2821 * @error: a #GError.
2823 * Creates a symbolic link.
2825 * If @cancellable is not %NULL, then the operation can be cancelled by
2826 * triggering the cancellable object from another thread. If the operation
2827 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2829 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
2832 g_file_make_symbolic_link (GFile *file,
2833 const char *symlink_value,
2834 GCancellable *cancellable,
2839 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2840 g_return_val_if_fail (symlink_value != NULL, FALSE);
2842 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2845 if (*symlink_value == '\0')
2847 g_set_error_literal (error, G_IO_ERROR,
2848 G_IO_ERROR_INVALID_ARGUMENT,
2849 _("Invalid symlink value given"));
2853 iface = G_FILE_GET_IFACE (file);
2855 if (iface->make_symbolic_link == NULL)
2857 g_set_error_literal (error, G_IO_ERROR,
2858 G_IO_ERROR_NOT_SUPPORTED,
2859 _("Operation not supported"));
2863 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
2868 * @file: input #GFile.
2869 * @cancellable: optional #GCancellable object, %NULL to ignore.
2870 * @error: a #GError, or %NULL
2872 * Deletes a file. If the @file is a directory, it will only be deleted if it
2875 * If @cancellable is not %NULL, then the operation can be cancelled by
2876 * triggering the cancellable object from another thread. If the operation
2877 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2879 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
2882 g_file_delete (GFile *file,
2883 GCancellable *cancellable,
2888 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2890 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2893 iface = G_FILE_GET_IFACE (file);
2895 if (iface->delete_file == NULL)
2897 g_set_error_literal (error, G_IO_ERROR,
2898 G_IO_ERROR_NOT_SUPPORTED,
2899 _("Operation not supported"));
2903 return (* iface->delete_file) (file, cancellable, error);
2908 * @file: #GFile to send to trash.
2909 * @cancellable: optional #GCancellable object, %NULL to ignore.
2910 * @error: a #GError, or %NULL
2912 * Sends @file to the "Trashcan", if possible. This is similar to
2913 * deleting it, but the user can recover it before emptying the trashcan.
2914 * Not all file systems support trashing, so this call can return the
2915 * %G_IO_ERROR_NOT_SUPPORTED error.
2918 * If @cancellable is not %NULL, then the operation can be cancelled by
2919 * triggering the cancellable object from another thread. If the operation
2920 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2922 * Returns: %TRUE on successful trash, %FALSE otherwise.
2925 g_file_trash (GFile *file,
2926 GCancellable *cancellable,
2931 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2933 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2936 iface = G_FILE_GET_IFACE (file);
2938 if (iface->trash == NULL)
2940 g_set_error_literal (error,
2941 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2942 _("Trash not supported"));
2946 return (* iface->trash) (file, cancellable, error);
2950 * g_file_set_display_name:
2951 * @file: input #GFile.
2952 * @display_name: a string.
2953 * @cancellable: optional #GCancellable object, %NULL to ignore.
2954 * @error: a #GError, or %NULL
2956 * Renames @file to the specified display name.
2958 * The display name is converted from UTF8 to the correct encoding for the target
2959 * filesystem if possible and the @file is renamed to this.
2961 * If you want to implement a rename operation in the user interface the edit name
2962 * (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the initial value in the rename
2963 * widget, and then the result after editing should be passed to g_file_set_display_name().
2965 * On success the resulting converted filename is returned.
2967 * If @cancellable is not %NULL, then the operation can be cancelled by
2968 * triggering the cancellable object from another thread. If the operation
2969 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2971 * Returns: a #GFile specifying what @file was renamed to, or %NULL
2972 * if there was an error.
2973 * Free the returned object with g_object_unref().
2976 g_file_set_display_name (GFile *file,
2977 const char *display_name,
2978 GCancellable *cancellable,
2983 g_return_val_if_fail (G_IS_FILE (file), NULL);
2984 g_return_val_if_fail (display_name != NULL, NULL);
2986 if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
2990 G_IO_ERROR_INVALID_ARGUMENT,
2991 _("File names cannot contain '%c'"), G_DIR_SEPARATOR);
2995 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2998 iface = G_FILE_GET_IFACE (file);
3000 return (* iface->set_display_name) (file, display_name, cancellable, error);
3004 * g_file_set_display_name_async:
3005 * @file: input #GFile.
3006 * @display_name: a string.
3007 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3009 * @cancellable: optional #GCancellable object, %NULL to ignore.
3010 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
3011 * @user_data: the data to pass to callback function
3013 * Asynchronously sets the display name for a given #GFile.
3015 * For more details, see g_set_display_name() which is
3016 * the synchronous version of this call.
3018 * When the operation is finished, @callback will be called. You can then call
3019 * g_file_set_display_name_finish() to get the result of the operation.
3022 g_file_set_display_name_async (GFile *file,
3023 const char *display_name,
3025 GCancellable *cancellable,
3026 GAsyncReadyCallback callback,
3031 g_return_if_fail (G_IS_FILE (file));
3032 g_return_if_fail (display_name != NULL);
3034 iface = G_FILE_GET_IFACE (file);
3035 (* iface->set_display_name_async) (file,
3044 * g_file_set_display_name_finish:
3045 * @file: input #GFile.
3046 * @res: a #GAsyncResult.
3047 * @error: a #GError, or %NULL
3049 * Finishes setting a display name started with
3050 * g_file_set_display_name_async().
3052 * Returns: a #GFile or %NULL on error.
3053 * Free the returned object with g_object_unref().
3056 g_file_set_display_name_finish (GFile *file,
3062 g_return_val_if_fail (G_IS_FILE (file), NULL);
3063 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
3065 if (G_IS_SIMPLE_ASYNC_RESULT (res))
3067 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3068 if (g_simple_async_result_propagate_error (simple, error))
3072 iface = G_FILE_GET_IFACE (file);
3073 return (* iface->set_display_name_finish) (file, res, error);
3077 * g_file_query_settable_attributes:
3078 * @file: input #GFile.
3079 * @cancellable: optional #GCancellable object, %NULL to ignore.
3080 * @error: a #GError, or %NULL
3082 * Obtain the list of settable attributes for the file.
3084 * Returns the type and full attribute name of all the attributes
3085 * that can be set on this file. This doesn't mean setting it will always
3086 * succeed though, you might get an access failure, or some specific
3087 * file may not support a specific attribute.
3089 * If @cancellable is not %NULL, then the operation can be cancelled by
3090 * triggering the cancellable object from another thread. If the operation
3091 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3093 * Returns: a #GFileAttributeInfoList describing the settable attributes.
3094 * When you are done with it, release it with g_file_attribute_info_list_unref()
3096 GFileAttributeInfoList *
3097 g_file_query_settable_attributes (GFile *file,
3098 GCancellable *cancellable,
3103 GFileAttributeInfoList *list;
3105 g_return_val_if_fail (G_IS_FILE (file), NULL);
3107 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3110 iface = G_FILE_GET_IFACE (file);
3112 if (iface->query_settable_attributes == NULL)
3113 return g_file_attribute_info_list_new ();
3116 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
3120 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
3122 list = g_file_attribute_info_list_new ();
3123 g_error_free (my_error);
3126 g_propagate_error (error, my_error);
3133 * g_file_query_writable_namespaces:
3134 * @file: input #GFile.
3135 * @cancellable: optional #GCancellable object, %NULL to ignore.
3136 * @error: a #GError, or %NULL
3138 * Obtain the list of attribute namespaces where new attributes
3139 * can be created by a user. An example of this is extended
3140 * attributes (in the "xattr" namespace).
3142 * If @cancellable is not %NULL, then the operation can be cancelled by
3143 * triggering the cancellable object from another thread. If the operation
3144 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3146 * Returns: a #GFileAttributeInfoList describing the writable namespaces.
3147 * When you are done with it, release it with g_file_attribute_info_list_unref()
3149 GFileAttributeInfoList *
3150 g_file_query_writable_namespaces (GFile *file,
3151 GCancellable *cancellable,
3156 GFileAttributeInfoList *list;
3158 g_return_val_if_fail (G_IS_FILE (file), NULL);
3160 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3163 iface = G_FILE_GET_IFACE (file);
3165 if (iface->query_writable_namespaces == NULL)
3166 return g_file_attribute_info_list_new ();
3169 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
3173 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
3175 list = g_file_attribute_info_list_new ();
3176 g_error_free (my_error);
3179 g_propagate_error (error, my_error);
3186 * g_file_set_attribute:
3187 * @file: input #GFile.
3188 * @attribute: a string containing the attribute's name.
3189 * @type: The type of the attribute
3190 * @value_p: a pointer to the value (or the pointer itself if the type is a pointer type)
3191 * @flags: a set of #GFileQueryInfoFlags.
3192 * @cancellable: optional #GCancellable object, %NULL to ignore.
3193 * @error: a #GError, or %NULL
3195 * Sets an attribute in the file with attribute name @attribute to @value.
3197 * If @cancellable is not %NULL, then the operation can be cancelled by
3198 * triggering the cancellable object from another thread. If the operation
3199 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3201 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
3204 g_file_set_attribute (GFile *file,
3205 const char *attribute,
3206 GFileAttributeType type,
3208 GFileQueryInfoFlags flags,
3209 GCancellable *cancellable,
3214 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3215 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
3217 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3220 iface = G_FILE_GET_IFACE (file);
3222 if (iface->set_attribute == NULL)
3224 g_set_error_literal (error, G_IO_ERROR,
3225 G_IO_ERROR_NOT_SUPPORTED,
3226 _("Operation not supported"));
3230 return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
3234 * g_file_set_attributes_from_info:
3235 * @file: input #GFile.
3236 * @info: a #GFileInfo.
3237 * @flags: #GFileQueryInfoFlags
3238 * @cancellable: optional #GCancellable object, %NULL to ignore.
3239 * @error: a #GError, or %NULL
3241 * Tries to set all attributes in the #GFileInfo on the target values,
3242 * not stopping on the first error.
3244 * If there is any error during this operation then @error will be set to
3245 * the first error. Error on particular fields are flagged by setting
3246 * the "status" field in the attribute value to
3247 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can also detect
3250 * If @cancellable is not %NULL, then the operation can be cancelled by
3251 * triggering the cancellable object from another thread. If the operation
3252 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3254 * Returns: %TRUE if there was any error, %FALSE otherwise.
3257 g_file_set_attributes_from_info (GFile *file,
3259 GFileQueryInfoFlags flags,
3260 GCancellable *cancellable,
3265 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3266 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
3268 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3271 g_file_info_clear_status (info);
3273 iface = G_FILE_GET_IFACE (file);
3275 return (* iface->set_attributes_from_info) (file,
3284 g_file_real_set_attributes_from_info (GFile *file,
3286 GFileQueryInfoFlags flags,
3287 GCancellable *cancellable,
3293 GFileAttributeValue *value;
3297 attributes = g_file_info_list_attributes (info, NULL);
3299 for (i = 0; attributes[i] != NULL; i++)
3301 value = _g_file_info_get_attribute_value (info, attributes[i]);
3303 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
3306 if (!g_file_set_attribute (file, attributes[i],
3307 value->type, _g_file_attribute_value_peek_as_pointer (value),
3308 flags, cancellable, error))
3310 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
3312 /* Don't set error multiple times */
3316 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
3319 g_strfreev (attributes);
3325 * g_file_set_attributes_async:
3326 * @file: input #GFile.
3327 * @info: a #GFileInfo.
3328 * @flags: a #GFileQueryInfoFlags.
3329 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3331 * @cancellable: optional #GCancellable object, %NULL to ignore.
3332 * @callback: a #GAsyncReadyCallback.
3333 * @user_data: a #gpointer.
3335 * Asynchronously sets the attributes of @file with @info.
3337 * For more details, see g_file_set_attributes_from_info() which is
3338 * the synchronous version of this call.
3340 * When the operation is finished, @callback will be called. You can then call
3341 * g_file_set_attributes_finish() to get the result of the operation.
3344 g_file_set_attributes_async (GFile *file,
3346 GFileQueryInfoFlags flags,
3348 GCancellable *cancellable,
3349 GAsyncReadyCallback callback,
3354 g_return_if_fail (G_IS_FILE (file));
3355 g_return_if_fail (G_IS_FILE_INFO (info));
3357 iface = G_FILE_GET_IFACE (file);
3358 (* iface->set_attributes_async) (file,
3368 * g_file_set_attributes_finish:
3369 * @file: input #GFile.
3370 * @result: a #GAsyncResult.
3371 * @info: a #GFileInfo.
3372 * @error: a #GError, or %NULL
3374 * Finishes setting an attribute started in g_file_set_attributes_async().
3376 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
3379 g_file_set_attributes_finish (GFile *file,
3380 GAsyncResult *result,
3386 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3387 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3389 /* No standard handling of errors here, as we must set info even
3392 iface = G_FILE_GET_IFACE (file);
3393 return (* iface->set_attributes_finish) (file, result, info, error);
3397 * g_file_set_attribute_string:
3398 * @file: input #GFile.
3399 * @attribute: a string containing the attribute's name.
3400 * @value: a string containing the attribute's value.
3401 * @flags: #GFileQueryInfoFlags.
3402 * @cancellable: optional #GCancellable object, %NULL to ignore.
3403 * @error: a #GError, or %NULL
3405 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
3406 * If @attribute is of a different type, this operation will fail.
3408 * If @cancellable is not %NULL, then the operation can be cancelled by
3409 * triggering the cancellable object from another thread. If the operation
3410 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3412 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
3415 g_file_set_attribute_string (GFile *file,
3416 const char *attribute,
3418 GFileQueryInfoFlags flags,
3419 GCancellable *cancellable,
3422 return g_file_set_attribute (file, attribute,
3423 G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
3424 flags, cancellable, error);
3428 * g_file_set_attribute_byte_string:
3429 * @file: input #GFile.
3430 * @attribute: a string containing the attribute's name.
3431 * @value: a string containing the attribute's new value.
3432 * @flags: a #GFileQueryInfoFlags.
3433 * @cancellable: optional #GCancellable object, %NULL to ignore.
3434 * @error: a #GError, or %NULL
3436 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
3437 * If @attribute is of a different type, this operation will fail,
3440 * If @cancellable is not %NULL, then the operation can be cancelled by
3441 * triggering the cancellable object from another thread. If the operation
3442 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3444 * Returns: %TRUE if the @attribute was successfully set to @value
3445 * in the @file, %FALSE otherwise.
3448 g_file_set_attribute_byte_string (GFile *file,
3449 const char *attribute,
3451 GFileQueryInfoFlags flags,
3452 GCancellable *cancellable,
3455 return g_file_set_attribute (file, attribute,
3456 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
3457 flags, cancellable, error);
3461 * g_file_set_attribute_uint32:
3462 * @file: input #GFile.
3463 * @attribute: a string containing the attribute's name.
3464 * @value: a #guint32 containing the attribute's new value.
3465 * @flags: a #GFileQueryInfoFlags.
3466 * @cancellable: optional #GCancellable object, %NULL to ignore.
3467 * @error: a #GError, or %NULL
3469 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
3470 * If @attribute is of a different type, this operation will fail.
3472 * If @cancellable is not %NULL, then the operation can be cancelled by
3473 * triggering the cancellable object from another thread. If the operation
3474 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3476 * Returns: %TRUE if the @attribute was successfully set to @value
3477 * in the @file, %FALSE otherwise.
3480 g_file_set_attribute_uint32 (GFile *file,
3481 const char *attribute,
3483 GFileQueryInfoFlags flags,
3484 GCancellable *cancellable,
3487 return g_file_set_attribute (file, attribute,
3488 G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
3489 flags, cancellable, error);
3493 * g_file_set_attribute_int32:
3494 * @file: input #GFile.
3495 * @attribute: a string containing the attribute's name.
3496 * @value: a #gint32 containing the attribute's new value.
3497 * @flags: a #GFileQueryInfoFlags.
3498 * @cancellable: optional #GCancellable object, %NULL to ignore.
3499 * @error: a #GError, or %NULL
3501 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
3502 * If @attribute is of a different type, this operation will fail.
3504 * If @cancellable is not %NULL, then the operation can be cancelled by
3505 * triggering the cancellable object from another thread. If the operation
3506 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3508 * Returns: %TRUE if the @attribute was successfully set to @value
3509 * in the @file, %FALSE otherwise.
3512 g_file_set_attribute_int32 (GFile *file,
3513 const char *attribute,
3515 GFileQueryInfoFlags flags,
3516 GCancellable *cancellable,
3519 return g_file_set_attribute (file, attribute,
3520 G_FILE_ATTRIBUTE_TYPE_INT32, &value,
3521 flags, cancellable, error);
3525 * g_file_set_attribute_uint64:
3526 * @file: input #GFile.
3527 * @attribute: a string containing the attribute's name.
3528 * @value: a #guint64 containing the attribute's new value.
3529 * @flags: a #GFileQueryInfoFlags.
3530 * @cancellable: optional #GCancellable object, %NULL to ignore.
3531 * @error: a #GError, or %NULL
3533 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
3534 * If @attribute is of a different type, this operation will fail.
3536 * If @cancellable is not %NULL, then the operation can be cancelled by
3537 * triggering the cancellable object from another thread. If the operation
3538 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3540 * Returns: %TRUE if the @attribute was successfully set to @value
3541 * in the @file, %FALSE otherwise.
3544 g_file_set_attribute_uint64 (GFile *file,
3545 const char *attribute,
3547 GFileQueryInfoFlags flags,
3548 GCancellable *cancellable,
3551 return g_file_set_attribute (file, attribute,
3552 G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
3553 flags, cancellable, error);
3557 * g_file_set_attribute_int64:
3558 * @file: input #GFile.
3559 * @attribute: a string containing the attribute's name.
3560 * @value: a #guint64 containing the attribute's new value.
3561 * @flags: a #GFileQueryInfoFlags.
3562 * @cancellable: optional #GCancellable object, %NULL to ignore.
3563 * @error: a #GError, or %NULL
3565 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
3566 * If @attribute is of a different type, this operation will fail.
3568 * If @cancellable is not %NULL, then the operation can be cancelled by
3569 * triggering the cancellable object from another thread. If the operation
3570 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3572 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
3575 g_file_set_attribute_int64 (GFile *file,
3576 const char *attribute,
3578 GFileQueryInfoFlags flags,
3579 GCancellable *cancellable,
3582 return g_file_set_attribute (file, attribute,
3583 G_FILE_ATTRIBUTE_TYPE_INT64, &value,
3584 flags, cancellable, error);
3588 * g_file_mount_mountable:
3589 * @file: input #GFile.
3590 * @flags: flags affecting the operation
3591 * @mount_operation: a #GMountOperation, or %NULL to avoid user interaction.
3592 * @cancellable: optional #GCancellable object, %NULL to ignore.
3593 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3594 * @user_data: the data to pass to callback function
3596 * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
3597 * Using @mount_operation, you can request callbacks when, for instance,
3598 * passwords are needed during authentication.
3600 * If @cancellable is not %NULL, then the operation can be cancelled by
3601 * triggering the cancellable object from another thread. If the operation
3602 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3604 * When the operation is finished, @callback will be called. You can then call
3605 * g_file_mount_mountable_finish() to get the result of the operation.
3608 g_file_mount_mountable (GFile *file,
3609 GMountMountFlags flags,
3610 GMountOperation *mount_operation,
3611 GCancellable *cancellable,
3612 GAsyncReadyCallback callback,
3617 g_return_if_fail (G_IS_FILE (file));
3619 iface = G_FILE_GET_IFACE (file);
3621 if (iface->mount_mountable == NULL)
3623 g_simple_async_report_error_in_idle (G_OBJECT (file),
3627 G_IO_ERROR_NOT_SUPPORTED,
3628 _("Operation not supported"));
3632 (* iface->mount_mountable) (file,
3641 * g_file_mount_mountable_finish:
3642 * @file: input #GFile.
3643 * @result: a #GAsyncResult.
3644 * @error: a #GError, or %NULL
3646 * Finishes a mount operation. See g_file_mount_mountable() for details.
3648 * Finish an asynchronous mount operation that was started
3649 * with g_file_mount_mountable().
3651 * Returns: a #GFile or %NULL on error.
3652 * Free the returned object with g_object_unref().
3655 g_file_mount_mountable_finish (GFile *file,
3656 GAsyncResult *result,
3661 g_return_val_if_fail (G_IS_FILE (file), NULL);
3662 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
3664 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3666 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3667 if (g_simple_async_result_propagate_error (simple, error))
3671 iface = G_FILE_GET_IFACE (file);
3672 return (* iface->mount_mountable_finish) (file, result, error);
3676 * g_file_unmount_mountable:
3677 * @file: input #GFile.
3678 * @flags: flags affecting the operation
3679 * @cancellable: optional #GCancellable object, %NULL to ignore.
3680 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3681 * @user_data: the data to pass to callback function
3683 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
3685 * If @cancellable is not %NULL, then the operation can be cancelled by
3686 * triggering the cancellable object from another thread. If the operation
3687 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3689 * When the operation is finished, @callback will be called. You can then call
3690 * g_file_unmount_mountable_finish() to get the result of the operation.
3693 g_file_unmount_mountable (GFile *file,
3694 GMountUnmountFlags flags,
3695 GCancellable *cancellable,
3696 GAsyncReadyCallback callback,
3701 g_return_if_fail (G_IS_FILE (file));
3703 iface = G_FILE_GET_IFACE (file);
3705 if (iface->unmount_mountable == NULL)
3707 g_simple_async_report_error_in_idle (G_OBJECT (file),
3711 G_IO_ERROR_NOT_SUPPORTED,
3712 _("Operation not supported"));
3716 (* iface->unmount_mountable) (file,
3724 * g_file_unmount_mountable_finish:
3725 * @file: input #GFile.
3726 * @result: a #GAsyncResult.
3727 * @error: a #GError, or %NULL
3729 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
3731 * Finish an asynchronous unmount operation that was started
3732 * with g_file_unmount_mountable().
3734 * Returns: %TRUE if the operation finished successfully. %FALSE
3738 g_file_unmount_mountable_finish (GFile *file,
3739 GAsyncResult *result,
3744 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3745 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3747 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3749 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3750 if (g_simple_async_result_propagate_error (simple, error))
3754 iface = G_FILE_GET_IFACE (file);
3755 return (* iface->unmount_mountable_finish) (file, result, error);
3759 * g_file_eject_mountable:
3760 * @file: input #GFile.
3761 * @flags: flags affecting the operation
3762 * @cancellable: optional #GCancellable object, %NULL to ignore.
3763 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3764 * @user_data: the data to pass to callback function
3766 * Starts an asynchronous eject on a mountable.
3767 * When this operation has completed, @callback will be called with
3768 * @user_user data, and the operation can be finalized with
3769 * g_file_eject_mountable_finish().
3771 * If @cancellable is not %NULL, then the operation can be cancelled by
3772 * triggering the cancellable object from another thread. If the operation
3773 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3776 g_file_eject_mountable (GFile *file,
3777 GMountUnmountFlags flags,
3778 GCancellable *cancellable,
3779 GAsyncReadyCallback callback,
3784 g_return_if_fail (G_IS_FILE (file));
3786 iface = G_FILE_GET_IFACE (file);
3788 if (iface->eject_mountable == NULL)
3790 g_simple_async_report_error_in_idle (G_OBJECT (file),
3794 G_IO_ERROR_NOT_SUPPORTED,
3795 _("Operation not supported"));
3799 (* iface->eject_mountable) (file,
3807 * g_file_eject_mountable_finish:
3808 * @file: input #GFile.
3809 * @result: a #GAsyncResult.
3810 * @error: a #GError, or %NULL
3812 * Finishes an asynchronous eject operation started by
3813 * g_file_eject_mountable().
3815 * Returns: %TRUE if the @file was ejected successfully. %FALSE
3819 g_file_eject_mountable_finish (GFile *file,
3820 GAsyncResult *result,
3825 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3826 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3828 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3830 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3831 if (g_simple_async_result_propagate_error (simple, error))
3835 iface = G_FILE_GET_IFACE (file);
3836 return (* iface->eject_mountable_finish) (file, result, error);
3840 * g_file_monitor_directory:
3841 * @file: input #GFile.
3842 * @flags: a set of #GFileMonitorFlags.
3843 * @cancellable: optional #GCancellable object, %NULL to ignore.
3844 * @error: a #GError, or %NULL.
3846 * Obtains a directory monitor for the given file.
3847 * This may fail if directory monitoring is not supported.
3849 * If @cancellable is not %NULL, then the operation can be cancelled by
3850 * triggering the cancellable object from another thread. If the operation
3851 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3853 * Returns: a #GFileMonitor for the given @file, or %NULL on error.
3854 * Free the returned object with g_object_unref().
3857 g_file_monitor_directory (GFile *file,
3858 GFileMonitorFlags flags,
3859 GCancellable *cancellable,
3864 g_return_val_if_fail (G_IS_FILE (file), NULL);
3866 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3869 iface = G_FILE_GET_IFACE (file);
3871 if (iface->monitor_dir == NULL)
3873 g_set_error_literal (error, G_IO_ERROR,
3874 G_IO_ERROR_NOT_SUPPORTED,
3875 _("Operation not supported"));
3879 return (* iface->monitor_dir) (file, flags, cancellable, error);
3883 * g_file_monitor_file:
3884 * @file: input #GFile.
3885 * @flags: a set of #GFileMonitorFlags.
3886 * @cancellable: optional #GCancellable object, %NULL to ignore.
3887 * @error: a #GError, or %NULL.
3889 * Obtains a file monitor for the given file. If no file notification
3890 * mechanism exists, then regular polling of the file is used.
3892 * If @cancellable is not %NULL, then the operation can be cancelled by
3893 * triggering the cancellable object from another thread. If the operation
3894 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3896 * Returns: a #GFileMonitor for the given @file, or %NULL on error.
3897 * Free the returned object with g_object_unref().
3900 g_file_monitor_file (GFile *file,
3901 GFileMonitorFlags flags,
3902 GCancellable *cancellable,
3906 GFileMonitor *monitor;
3908 g_return_val_if_fail (G_IS_FILE (file), NULL);
3910 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3913 iface = G_FILE_GET_IFACE (file);
3917 if (iface->monitor_file)
3918 monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
3920 /* Fallback to polling */
3921 if (monitor == NULL)
3922 monitor = _g_poll_file_monitor_new (file);
3929 * @file: input #GFile
3930 * @flags: a set of #GFileMonitorFlags
3931 * @cancellable: optional #GCancellable object, %NULL to ignore
3932 * @error: a #GError, or %NULL
3934 * Obtains a file or directory monitor for the given file, depending
3935 * on the type of the file.
3937 * If @cancellable is not %NULL, then the operation can be cancelled by
3938 * triggering the cancellable object from another thread. If the operation
3939 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3941 * Returns: a #GFileMonitor for the given @file, or %NULL on error.
3942 * Free the returned object with g_object_unref().
3947 g_file_monitor (GFile *file,
3948 GFileMonitorFlags flags,
3949 GCancellable *cancellable,
3952 if (g_file_query_file_type (file, 0, cancellable) == G_FILE_TYPE_DIRECTORY)
3953 return g_file_monitor_directory (file, flags, cancellable, error);
3955 return g_file_monitor_file (file, flags, cancellable, error);
3958 /********************************************
3959 * Default implementation of async ops *
3960 ********************************************/
3964 GFileQueryInfoFlags flags;
3966 } QueryInfoAsyncData;
3969 query_info_data_free (QueryInfoAsyncData *data)
3972 g_object_unref (data->info);
3973 g_free (data->attributes);
3978 query_info_async_thread (GSimpleAsyncResult *res,
3980 GCancellable *cancellable)
3982 GError *error = NULL;
3983 QueryInfoAsyncData *data;
3986 data = g_simple_async_result_get_op_res_gpointer (res);
3988 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3992 g_simple_async_result_set_from_error (res, error);
3993 g_error_free (error);
4000 g_file_real_query_info_async (GFile *file,
4001 const char *attributes,
4002 GFileQueryInfoFlags flags,
4004 GCancellable *cancellable,
4005 GAsyncReadyCallback callback,
4008 GSimpleAsyncResult *res;
4009 QueryInfoAsyncData *data;
4011 data = g_new0 (QueryInfoAsyncData, 1);
4012 data->attributes = g_strdup (attributes);
4013 data->flags = flags;
4015 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_info_async);
4016 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
4018 g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
4019 g_object_unref (res);
4023 g_file_real_query_info_finish (GFile *file,
4027 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4028 QueryInfoAsyncData *data;
4030 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_info_async);
4032 data = g_simple_async_result_get_op_res_gpointer (simple);
4034 return g_object_ref (data->info);
4042 } QueryFilesystemInfoAsyncData;
4045 query_filesystem_info_data_free (QueryFilesystemInfoAsyncData *data)
4048 g_object_unref (data->info);
4049 g_free (data->attributes);
4054 query_filesystem_info_async_thread (GSimpleAsyncResult *res,
4056 GCancellable *cancellable)
4058 GError *error = NULL;
4059 QueryFilesystemInfoAsyncData *data;
4062 data = g_simple_async_result_get_op_res_gpointer (res);
4064 info = g_file_query_filesystem_info (G_FILE (object), data->attributes, cancellable, &error);
4068 g_simple_async_result_set_from_error (res, error);
4069 g_error_free (error);
4076 g_file_real_query_filesystem_info_async (GFile *file,
4077 const char *attributes,
4079 GCancellable *cancellable,
4080 GAsyncReadyCallback callback,
4083 GSimpleAsyncResult *res;
4084 QueryFilesystemInfoAsyncData *data;
4086 data = g_new0 (QueryFilesystemInfoAsyncData, 1);
4087 data->attributes = g_strdup (attributes);
4089 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_filesystem_info_async);
4090 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_filesystem_info_data_free);
4092 g_simple_async_result_run_in_thread (res, query_filesystem_info_async_thread, io_priority, cancellable);
4093 g_object_unref (res);
4097 g_file_real_query_filesystem_info_finish (GFile *file,
4101 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4102 QueryFilesystemInfoAsyncData *data;
4104 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_filesystem_info_async);
4106 data = g_simple_async_result_get_op_res_gpointer (simple);
4108 return g_object_ref (data->info);
4115 GFileQueryInfoFlags flags;
4116 GFileEnumerator *enumerator;
4117 } EnumerateChildrenAsyncData;
4120 enumerate_children_data_free (EnumerateChildrenAsyncData *data)
4122 if (data->enumerator)
4123 g_object_unref (data->enumerator);
4124 g_free (data->attributes);
4129 enumerate_children_async_thread (GSimpleAsyncResult *res,
4131 GCancellable *cancellable)
4133 GError *error = NULL;
4134 EnumerateChildrenAsyncData *data;
4135 GFileEnumerator *enumerator;
4137 data = g_simple_async_result_get_op_res_gpointer (res);
4139 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
4141 if (enumerator == NULL)
4143 g_simple_async_result_set_from_error (res, error);
4144 g_error_free (error);
4147 data->enumerator = enumerator;
4151 g_file_real_enumerate_children_async (GFile *file,
4152 const char *attributes,
4153 GFileQueryInfoFlags flags,
4155 GCancellable *cancellable,
4156 GAsyncReadyCallback callback,
4159 GSimpleAsyncResult *res;
4160 EnumerateChildrenAsyncData *data;
4162 data = g_new0 (EnumerateChildrenAsyncData, 1);
4163 data->attributes = g_strdup (attributes);
4164 data->flags = flags;
4166 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_enumerate_children_async);
4167 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)enumerate_children_data_free);
4169 g_simple_async_result_run_in_thread (res, enumerate_children_async_thread, io_priority, cancellable);
4170 g_object_unref (res);
4173 static GFileEnumerator *
4174 g_file_real_enumerate_children_finish (GFile *file,
4178 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4179 EnumerateChildrenAsyncData *data;
4181 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_enumerate_children_async);
4183 data = g_simple_async_result_get_op_res_gpointer (simple);
4184 if (data->enumerator)
4185 return g_object_ref (data->enumerator);
4191 open_read_async_thread (GSimpleAsyncResult *res,
4193 GCancellable *cancellable)
4196 GFileInputStream *stream;
4197 GError *error = NULL;
4199 iface = G_FILE_GET_IFACE (object);
4201 stream = iface->read_fn (G_FILE (object), cancellable, &error);
4205 g_simple_async_result_set_from_error (res, error);
4206 g_error_free (error);
4209 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4213 g_file_real_read_async (GFile *file,
4215 GCancellable *cancellable,
4216 GAsyncReadyCallback callback,
4219 GSimpleAsyncResult *res;
4221 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_read_async);
4223 g_simple_async_result_run_in_thread (res, open_read_async_thread, io_priority, cancellable);
4224 g_object_unref (res);
4227 static GFileInputStream *
4228 g_file_real_read_finish (GFile *file,
4232 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4235 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_read_async);
4237 op = g_simple_async_result_get_op_res_gpointer (simple);
4239 return g_object_ref (op);
4245 append_to_async_thread (GSimpleAsyncResult *res,
4247 GCancellable *cancellable)
4250 GFileCreateFlags *data;
4251 GFileOutputStream *stream;
4252 GError *error = NULL;
4254 iface = G_FILE_GET_IFACE (object);
4256 data = g_simple_async_result_get_op_res_gpointer (res);
4258 stream = iface->append_to (G_FILE (object), *data, cancellable, &error);
4262 g_simple_async_result_set_from_error (res, error);
4263 g_error_free (error);
4266 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4270 g_file_real_append_to_async (GFile *file,
4271 GFileCreateFlags flags,
4273 GCancellable *cancellable,
4274 GAsyncReadyCallback callback,
4277 GFileCreateFlags *data;
4278 GSimpleAsyncResult *res;
4280 data = g_new0 (GFileCreateFlags, 1);
4283 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_append_to_async);
4284 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
4286 g_simple_async_result_run_in_thread (res, append_to_async_thread, io_priority, cancellable);
4287 g_object_unref (res);
4290 static GFileOutputStream *
4291 g_file_real_append_to_finish (GFile *file,
4295 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4298 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_append_to_async);
4300 op = g_simple_async_result_get_op_res_gpointer (simple);
4302 return g_object_ref (op);
4308 create_async_thread (GSimpleAsyncResult *res,
4310 GCancellable *cancellable)
4313 GFileCreateFlags *data;
4314 GFileOutputStream *stream;
4315 GError *error = NULL;
4317 iface = G_FILE_GET_IFACE (object);
4319 data = g_simple_async_result_get_op_res_gpointer (res);
4321 stream = iface->create (G_FILE (object), *data, cancellable, &error);
4325 g_simple_async_result_set_from_error (res, error);
4326 g_error_free (error);
4329 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4333 g_file_real_create_async (GFile *file,
4334 GFileCreateFlags flags,
4336 GCancellable *cancellable,
4337 GAsyncReadyCallback callback,
4340 GFileCreateFlags *data;
4341 GSimpleAsyncResult *res;
4343 data = g_new0 (GFileCreateFlags, 1);
4346 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_create_async);
4347 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
4349 g_simple_async_result_run_in_thread (res, create_async_thread, io_priority, cancellable);
4350 g_object_unref (res);
4353 static GFileOutputStream *
4354 g_file_real_create_finish (GFile *file,
4358 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4361 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_create_async);
4363 op = g_simple_async_result_get_op_res_gpointer (simple);
4365 return g_object_ref (op);
4371 GFileOutputStream *stream;
4373 gboolean make_backup;
4374 GFileCreateFlags flags;
4378 replace_async_data_free (ReplaceAsyncData *data)
4381 g_object_unref (data->stream);
4382 g_free (data->etag);
4387 replace_async_thread (GSimpleAsyncResult *res,
4389 GCancellable *cancellable)
4392 GFileOutputStream *stream;
4393 GError *error = NULL;
4394 ReplaceAsyncData *data;
4396 iface = G_FILE_GET_IFACE (object);
4398 data = g_simple_async_result_get_op_res_gpointer (res);
4400 stream = iface->replace (G_FILE (object),
4409 g_simple_async_result_set_from_error (res, error);
4410 g_error_free (error);
4413 data->stream = stream;
4417 g_file_real_replace_async (GFile *file,
4419 gboolean make_backup,
4420 GFileCreateFlags flags,
4422 GCancellable *cancellable,
4423 GAsyncReadyCallback callback,
4426 GSimpleAsyncResult *res;
4427 ReplaceAsyncData *data;
4429 data = g_new0 (ReplaceAsyncData, 1);
4430 data->etag = g_strdup (etag);
4431 data->make_backup = make_backup;
4432 data->flags = flags;
4434 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_replace_async);
4435 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_async_data_free);
4437 g_simple_async_result_run_in_thread (res, replace_async_thread, io_priority, cancellable);
4438 g_object_unref (res);
4441 static GFileOutputStream *
4442 g_file_real_replace_finish (GFile *file,
4446 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4447 ReplaceAsyncData *data;
4449 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_replace_async);
4451 data = g_simple_async_result_get_op_res_gpointer (simple);
4453 return g_object_ref (data->stream);
4461 } SetDisplayNameAsyncData;
4464 set_display_name_data_free (SetDisplayNameAsyncData *data)
4466 g_free (data->name);
4468 g_object_unref (data->file);
4473 set_display_name_async_thread (GSimpleAsyncResult *res,
4475 GCancellable *cancellable)
4477 GError *error = NULL;
4478 SetDisplayNameAsyncData *data;
4481 data = g_simple_async_result_get_op_res_gpointer (res);
4483 file = g_file_set_display_name (G_FILE (object), data->name, cancellable, &error);
4487 g_simple_async_result_set_from_error (res, error);
4488 g_error_free (error);
4495 g_file_real_set_display_name_async (GFile *file,
4496 const char *display_name,
4498 GCancellable *cancellable,
4499 GAsyncReadyCallback callback,
4502 GSimpleAsyncResult *res;
4503 SetDisplayNameAsyncData *data;
4505 data = g_new0 (SetDisplayNameAsyncData, 1);
4506 data->name = g_strdup (display_name);
4508 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_display_name_async);
4509 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_display_name_data_free);
4511 g_simple_async_result_run_in_thread (res, set_display_name_async_thread, io_priority, cancellable);
4512 g_object_unref (res);
4516 g_file_real_set_display_name_finish (GFile *file,
4520 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4521 SetDisplayNameAsyncData *data;
4523 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_display_name_async);
4525 data = g_simple_async_result_get_op_res_gpointer (simple);
4527 return g_object_ref (data->file);
4533 GFileQueryInfoFlags flags;
4540 set_info_data_free (SetInfoAsyncData *data)
4543 g_object_unref (data->info);
4545 g_error_free (data->error);
4550 set_info_async_thread (GSimpleAsyncResult *res,
4552 GCancellable *cancellable)
4554 SetInfoAsyncData *data;
4556 data = g_simple_async_result_get_op_res_gpointer (res);
4559 data->res = g_file_set_attributes_from_info (G_FILE (object),
4567 g_file_real_set_attributes_async (GFile *file,
4569 GFileQueryInfoFlags flags,
4571 GCancellable *cancellable,
4572 GAsyncReadyCallback callback,
4575 GSimpleAsyncResult *res;
4576 SetInfoAsyncData *data;
4578 data = g_new0 (SetInfoAsyncData, 1);
4579 data->info = g_file_info_dup (info);
4580 data->flags = flags;
4582 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_attributes_async);
4583 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_info_data_free);
4585 g_simple_async_result_run_in_thread (res, set_info_async_thread, io_priority, cancellable);
4586 g_object_unref (res);
4590 g_file_real_set_attributes_finish (GFile *file,
4595 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4596 SetInfoAsyncData *data;
4598 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_attributes_async);
4600 data = g_simple_async_result_get_op_res_gpointer (simple);
4603 *info = g_object_ref (data->info);
4605 if (error != NULL && data->error)
4606 *error = g_error_copy (data->error);
4612 find_enclosing_mount_async_thread (GSimpleAsyncResult *res,
4614 GCancellable *cancellable)
4616 GError *error = NULL;
4619 mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
4623 g_simple_async_result_set_from_error (res, error);
4624 g_error_free (error);
4627 g_simple_async_result_set_op_res_gpointer (res, mount, (GDestroyNotify)g_object_unref);
4631 g_file_real_find_enclosing_mount_async (GFile *file,
4633 GCancellable *cancellable,
4634 GAsyncReadyCallback callback,
4637 GSimpleAsyncResult *res;
4639 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_find_enclosing_mount_async);
4641 g_simple_async_result_run_in_thread (res, find_enclosing_mount_async_thread, io_priority, cancellable);
4642 g_object_unref (res);
4646 g_file_real_find_enclosing_mount_finish (GFile *file,
4650 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4653 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_find_enclosing_mount_async);
4655 mount = g_simple_async_result_get_op_res_gpointer (simple);
4656 return g_object_ref (mount);
4663 GFileCopyFlags flags;
4664 GFileProgressCallback progress_cb;
4665 gpointer progress_cb_data;
4666 GIOSchedulerJob *job;
4670 copy_async_data_free (CopyAsyncData *data)
4672 g_object_unref (data->source);
4673 g_object_unref (data->destination);
4678 CopyAsyncData *data;
4679 goffset current_num_bytes;
4680 goffset total_num_bytes;
4684 copy_async_progress_in_main (gpointer user_data)
4686 ProgressData *progress = user_data;
4687 CopyAsyncData *data = progress->data;
4689 data->progress_cb (progress->current_num_bytes,
4690 progress->total_num_bytes,
4691 data->progress_cb_data);
4697 mainloop_barrier (gpointer user_data)
4699 /* Does nothing, but ensures all queued idles before
4706 copy_async_progress_callback (goffset current_num_bytes,
4707 goffset total_num_bytes,
4710 CopyAsyncData *data = user_data;
4711 ProgressData *progress;
4713 progress = g_new (ProgressData, 1);
4714 progress->data = data;
4715 progress->current_num_bytes = current_num_bytes;
4716 progress->total_num_bytes = total_num_bytes;
4718 g_io_scheduler_job_send_to_mainloop_async (data->job,
4719 copy_async_progress_in_main,
4725 copy_async_thread (GIOSchedulerJob *job,
4726 GCancellable *cancellable,
4729 GSimpleAsyncResult *res;
4730 CopyAsyncData *data;
4735 data = g_simple_async_result_get_op_res_gpointer (res);
4739 result = g_file_copy (data->source,
4743 (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
4747 /* Ensure all progress callbacks are done running in main thread */
4748 if (data->progress_cb != NULL)
4749 g_io_scheduler_job_send_to_mainloop (job,
4755 g_simple_async_result_set_from_error (res, error);
4756 g_error_free (error);
4759 g_simple_async_result_complete_in_idle (res);
4765 g_file_real_copy_async (GFile *source,
4767 GFileCopyFlags flags,
4769 GCancellable *cancellable,
4770 GFileProgressCallback progress_callback,
4771 gpointer progress_callback_data,
4772 GAsyncReadyCallback callback,
4775 GSimpleAsyncResult *res;
4776 CopyAsyncData *data;
4778 data = g_new0 (CopyAsyncData, 1);
4779 data->source = g_object_ref (source);
4780 data->destination = g_object_ref (destination);
4781 data->flags = flags;
4782 data->progress_cb = progress_callback;
4783 data->progress_cb_data = progress_callback_data;
4785 res = g_simple_async_result_new (G_OBJECT (source), callback, user_data, g_file_real_copy_async);
4786 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)copy_async_data_free);
4788 g_io_scheduler_push_job (copy_async_thread, res, g_object_unref, io_priority, cancellable);
4792 g_file_real_copy_finish (GFile *file,
4796 /* Error handled in g_file_copy_finish() */
4801 /********************************************
4802 * Default VFS operations *
4803 ********************************************/
4806 * g_file_new_for_path:
4807 * @path: a string containing a relative or absolute path.
4809 * Constructs a #GFile for a given path. This operation never
4810 * fails, but the returned object might not support any I/O
4811 * operation if @path is malformed.
4813 * Returns: a new #GFile for the given @path.
4816 g_file_new_for_path (const char *path)
4818 g_return_val_if_fail (path != NULL, NULL);
4820 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
4824 * g_file_new_for_uri:
4825 * @uri: a string containing a URI.
4827 * Constructs a #GFile for a given URI. This operation never
4828 * fails, but the returned object might not support any I/O
4829 * operation if @uri is malformed or if the uri type is
4832 * Returns: a #GFile for the given @uri.
4835 g_file_new_for_uri (const char *uri)
4837 g_return_val_if_fail (uri != NULL, NULL);
4839 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
4843 * g_file_parse_name:
4844 * @parse_name: a file name or path to be parsed.
4846 * Constructs a #GFile with the given @parse_name (i.e. something given by g_file_get_parse_name()).
4847 * This operation never fails, but the returned object might not support any I/O
4848 * operation if the @parse_name cannot be parsed.
4850 * Returns: a new #GFile.
4853 g_file_parse_name (const char *parse_name)
4855 g_return_val_if_fail (parse_name != NULL, NULL);
4857 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
4861 is_valid_scheme_character (char c)
4863 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
4866 /* Following RFC 2396, valid schemes are built like:
4867 * scheme = alpha *( alpha | digit | "+" | "-" | "." )
4870 has_valid_scheme (const char *uri)
4876 if (!g_ascii_isalpha (*p))
4881 } while (is_valid_scheme_character (*p));
4887 * g_file_new_for_commandline_arg:
4888 * @arg: a command line string.
4890 * Creates a #GFile with the given argument from the command line. The value of
4891 * @arg can be either a URI, an absolute path or a relative path resolved
4892 * relative to the current working directory.
4893 * This operation never fails, but the returned object might not support any
4894 * I/O operation if @arg points to a malformed path.
4896 * Returns: a new #GFile.
4899 g_file_new_for_commandline_arg (const char *arg)
4905 g_return_val_if_fail (arg != NULL, NULL);
4907 if (g_path_is_absolute (arg))
4908 return g_file_new_for_path (arg);
4910 if (has_valid_scheme (arg))
4911 return g_file_new_for_uri (arg);
4913 current_dir = g_get_current_dir ();
4914 filename = g_build_filename (current_dir, arg, NULL);
4915 g_free (current_dir);
4917 file = g_file_new_for_path (filename);
4924 * g_file_mount_enclosing_volume:
4925 * @location: input #GFile.
4926 * @flags: flags affecting the operation
4927 * @mount_operation: a #GMountOperation or %NULL to avoid user interaction.
4928 * @cancellable: optional #GCancellable object, %NULL to ignore.
4929 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4930 * @user_data: the data to pass to callback function
4932 * Starts a @mount_operation, mounting the volume that contains the file @location.
4934 * When this operation has completed, @callback will be called with
4935 * @user_user data, and the operation can be finalized with
4936 * g_file_mount_enclosing_volume_finish().
4938 * If @cancellable is not %NULL, then the operation can be cancelled by
4939 * triggering the cancellable object from another thread. If the operation
4940 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4943 g_file_mount_enclosing_volume (GFile *location,
4944 GMountMountFlags flags,
4945 GMountOperation *mount_operation,
4946 GCancellable *cancellable,
4947 GAsyncReadyCallback callback,
4952 g_return_if_fail (G_IS_FILE (location));
4954 iface = G_FILE_GET_IFACE (location);
4956 if (iface->mount_enclosing_volume == NULL)
4958 g_simple_async_report_error_in_idle (G_OBJECT (location),
4959 callback, user_data,
4960 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4961 _("volume doesn't implement mount"));
4966 (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
4971 * g_file_mount_enclosing_volume_finish:
4972 * @location: input #GFile.
4973 * @result: a #GAsyncResult.
4974 * @error: a #GError, or %NULL
4976 * Finishes a mount operation started by g_file_mount_enclosing_volume().
4978 * Returns: %TRUE if successful. If an error
4979 * has occurred, this function will return %FALSE and set @error
4980 * appropriately if present.
4983 g_file_mount_enclosing_volume_finish (GFile *location,
4984 GAsyncResult *result,
4989 g_return_val_if_fail (G_IS_FILE (location), FALSE);
4990 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4992 if (G_IS_SIMPLE_ASYNC_RESULT (result))
4994 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
4995 if (g_simple_async_result_propagate_error (simple, error))
4999 iface = G_FILE_GET_IFACE (location);
5001 return (* iface->mount_enclosing_volume_finish) (location, result, error);
5004 /********************************************
5005 * Utility functions *
5006 ********************************************/
5009 * g_file_query_default_handler:
5010 * @file: a #GFile to open.
5011 * @cancellable: optional #GCancellable object, %NULL to ignore.
5012 * @error: a #GError, or %NULL
5014 * Returns the #GAppInfo that is registered as the default
5015 * application to handle the file specified by @file.
5017 * If @cancellable is not %NULL, then the operation can be cancelled by
5018 * triggering the cancellable object from another thread. If the operation
5019 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5021 * Returns: a #GAppInfo if the handle was found, %NULL if there were errors.
5022 * When you are done with it, release it with g_object_unref()
5025 g_file_query_default_handler (GFile *file,
5026 GCancellable *cancellable,
5030 const char *content_type;
5035 uri_scheme = g_file_get_uri_scheme (file);
5036 appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
5037 g_free (uri_scheme);
5039 if (appinfo != NULL)
5042 info = g_file_query_info (file,
5043 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
5052 content_type = g_file_info_get_content_type (info);
5055 /* Don't use is_native(), as we want to support fuse paths if availible */
5056 path = g_file_get_path (file);
5057 appinfo = g_app_info_get_default_for_type (content_type,
5062 g_object_unref (info);
5064 if (appinfo != NULL)
5067 g_set_error_literal (error, G_IO_ERROR,
5068 G_IO_ERROR_NOT_SUPPORTED,
5069 _("No application is registered as handling this file"));
5075 #define GET_CONTENT_BLOCK_SIZE 8192
5078 * g_file_load_contents:
5079 * @file: input #GFile.
5080 * @cancellable: optional #GCancellable object, %NULL to ignore.
5081 * @contents: a location to place the contents of the file.
5082 * @length: a location to place the length of the contents of the file,
5083 * or %NULL if the length is not needed
5084 * @etag_out: a location to place the current entity tag for the file,
5085 * or %NULL if the entity tag is not needed
5086 * @error: a #GError, or %NULL
5088 * Loads the content of the file into memory. The data is always
5089 * zero-terminated, but this is not included in the resultant @length.
5090 * The returned @content should be freed with g_free() when no longer
5093 * If @cancellable is not %NULL, then the operation can be cancelled by
5094 * triggering the cancellable object from another thread. If the operation
5095 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5097 * Returns: %TRUE if the @file's contents were successfully loaded.
5098 * %FALSE if there were errors.
5101 g_file_load_contents (GFile *file,
5102 GCancellable *cancellable,
5108 GFileInputStream *in;
5109 GByteArray *content;
5114 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5115 g_return_val_if_fail (contents != NULL, FALSE);
5117 in = g_file_read (file, cancellable, error);
5121 content = g_byte_array_new ();
5124 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
5125 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
5126 content->data + pos,
5127 GET_CONTENT_BLOCK_SIZE,
5128 cancellable, error)) > 0)
5131 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
5138 info = g_file_input_stream_query_info (in,
5139 G_FILE_ATTRIBUTE_ETAG_VALUE,
5144 *etag_out = g_strdup (g_file_info_get_etag (info));
5145 g_object_unref (info);
5149 /* Ignore errors on close */
5150 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
5151 g_object_unref (in);
5155 /* error is set already */
5156 g_byte_array_free (content, TRUE);
5163 /* Zero terminate (we got an extra byte allocated for this */
5164 content->data[pos] = 0;
5166 *contents = (char *)g_byte_array_free (content, FALSE);
5174 GCancellable *cancellable;
5175 GFileReadMoreCallback read_more_callback;
5176 GAsyncReadyCallback callback;
5178 GByteArray *content;
5185 load_contents_data_free (LoadContentsData *data)
5188 g_error_free (data->error);
5189 if (data->cancellable)
5190 g_object_unref (data->cancellable);
5192 g_byte_array_free (data->content, TRUE);
5193 g_free (data->etag);
5194 g_object_unref (data->file);
5199 load_contents_close_callback (GObject *obj,
5200 GAsyncResult *close_res,
5203 GInputStream *stream = G_INPUT_STREAM (obj);
5204 LoadContentsData *data = user_data;
5205 GSimpleAsyncResult *res;
5207 /* Ignore errors here, we're only reading anyway */
5208 g_input_stream_close_finish (stream, close_res, NULL);
5209 g_object_unref (stream);
5211 res = g_simple_async_result_new (G_OBJECT (data->file),
5214 g_file_load_contents_async);
5215 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)load_contents_data_free);
5216 g_simple_async_result_complete (res);
5217 g_object_unref (res);
5221 load_contents_fstat_callback (GObject *obj,
5222 GAsyncResult *stat_res,
5225 GInputStream *stream = G_INPUT_STREAM (obj);
5226 LoadContentsData *data = user_data;
5229 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
5233 data->etag = g_strdup (g_file_info_get_etag (info));
5234 g_object_unref (info);
5237 g_input_stream_close_async (stream, 0,
5239 load_contents_close_callback, data);
5243 load_contents_read_callback (GObject *obj,
5244 GAsyncResult *read_res,
5247 GInputStream *stream = G_INPUT_STREAM (obj);
5248 LoadContentsData *data = user_data;
5249 GError *error = NULL;
5252 read_size = g_input_stream_read_finish (stream, read_res, &error);
5256 /* Error or EOF, close the file */
5257 data->error = error;
5258 g_input_stream_close_async (stream, 0,
5260 load_contents_close_callback, data);
5262 else if (read_size == 0)
5264 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
5265 G_FILE_ATTRIBUTE_ETAG_VALUE,
5268 load_contents_fstat_callback,
5271 else if (read_size > 0)
5273 data->pos += read_size;
5275 g_byte_array_set_size (data->content,
5276 data->pos + GET_CONTENT_BLOCK_SIZE);
5279 if (data->read_more_callback &&
5280 !data->read_more_callback ((char *)data->content->data, data->pos, data->user_data))
5281 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
5282 G_FILE_ATTRIBUTE_ETAG_VALUE,
5285 load_contents_fstat_callback,
5288 g_input_stream_read_async (stream,
5289 data->content->data + data->pos,
5290 GET_CONTENT_BLOCK_SIZE,
5293 load_contents_read_callback,
5299 load_contents_open_callback (GObject *obj,
5300 GAsyncResult *open_res,
5303 GFile *file = G_FILE (obj);
5304 GFileInputStream *stream;
5305 LoadContentsData *data = user_data;
5306 GError *error = NULL;
5307 GSimpleAsyncResult *res;
5309 stream = g_file_read_finish (file, open_res, &error);
5313 g_byte_array_set_size (data->content,
5314 data->pos + GET_CONTENT_BLOCK_SIZE);
5315 g_input_stream_read_async (G_INPUT_STREAM (stream),
5316 data->content->data + data->pos,
5317 GET_CONTENT_BLOCK_SIZE,
5320 load_contents_read_callback,
5326 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
5330 g_simple_async_result_complete (res);
5331 g_error_free (error);
5332 load_contents_data_free (data);
5333 g_object_unref (res);
5338 * g_file_load_partial_contents_async:
5339 * @file: input #GFile.
5340 * @cancellable: optional #GCancellable object, %NULL to ignore.
5341 * @read_more_callback: a #GFileReadMoreCallback to receive partial data and to specify whether further data should be read.
5342 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5343 * @user_data: the data to pass to the callback functions.
5345 * Reads the partial contents of a file. A #GFileReadMoreCallback should be
5346 * used to stop reading from the file when appropriate, else this function
5347 * will behave exactly as g_file_load_contents_async(). This operation
5348 * can be finished by g_file_load_partial_contents_finish().
5350 * Users of this function should be aware that @user_data is passed to
5351 * both the @read_more_callback and the @callback.
5353 * If @cancellable is not %NULL, then the operation can be cancelled by
5354 * triggering the cancellable object from another thread. If the operation
5355 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5358 g_file_load_partial_contents_async (GFile *file,
5359 GCancellable *cancellable,
5360 GFileReadMoreCallback read_more_callback,
5361 GAsyncReadyCallback callback,
5364 LoadContentsData *data;
5366 g_return_if_fail (G_IS_FILE (file));
5368 data = g_new0 (LoadContentsData, 1);
5371 data->cancellable = g_object_ref (cancellable);
5372 data->read_more_callback = read_more_callback;
5373 data->callback = callback;
5374 data->user_data = user_data;
5375 data->content = g_byte_array_new ();
5376 data->file = g_object_ref (file);
5378 g_file_read_async (file,
5381 load_contents_open_callback,
5386 * g_file_load_partial_contents_finish:
5387 * @file: input #GFile.
5388 * @res: a #GAsyncResult.
5389 * @contents: a location to place the contents of the file.
5390 * @length: a location to place the length of the contents of the file,
5391 * or %NULL if the length is not needed
5392 * @etag_out: a location to place the current entity tag for the file,
5393 * or %NULL if the entity tag is not needed
5394 * @error: a #GError, or %NULL
5396 * Finishes an asynchronous partial load operation that was started
5397 * with g_file_load_partial_contents_async(). The data is always
5398 * zero-terminated, but this is not included in the resultant @length.
5399 * The returned @content should be freed with g_free() when no longer
5402 * Returns: %TRUE if the load was successful. If %FALSE and @error is
5403 * present, it will be set appropriately.
5406 g_file_load_partial_contents_finish (GFile *file,
5413 GSimpleAsyncResult *simple;
5414 LoadContentsData *data;
5416 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5417 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
5418 g_return_val_if_fail (contents != NULL, FALSE);
5420 simple = G_SIMPLE_ASYNC_RESULT (res);
5422 if (g_simple_async_result_propagate_error (simple, error))
5425 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_load_contents_async);
5427 data = g_simple_async_result_get_op_res_gpointer (simple);
5431 g_propagate_error (error, data->error);
5440 *length = data->pos;
5444 *etag_out = data->etag;
5448 /* Zero terminate */
5449 g_byte_array_set_size (data->content, data->pos + 1);
5450 data->content->data[data->pos] = 0;
5452 *contents = (char *)g_byte_array_free (data->content, FALSE);
5453 data->content = NULL;
5459 * g_file_load_contents_async:
5460 * @file: input #GFile.
5461 * @cancellable: optional #GCancellable object, %NULL to ignore.
5462 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5463 * @user_data: the data to pass to callback function
5465 * Starts an asynchronous load of the @file's contents.
5467 * For more details, see g_file_load_contents() which is
5468 * the synchronous version of this call.
5470 * When the load operation has completed, @callback will be called
5471 * with @user data. To finish the operation, call
5472 * g_file_load_contents_finish() with the #GAsyncResult returned by
5475 * If @cancellable is not %NULL, then the operation can be cancelled by
5476 * triggering the cancellable object from another thread. If the operation
5477 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5480 g_file_load_contents_async (GFile *file,
5481 GCancellable *cancellable,
5482 GAsyncReadyCallback callback,
5485 g_file_load_partial_contents_async (file,
5488 callback, user_data);
5492 * g_file_load_contents_finish:
5493 * @file: input #GFile.
5494 * @res: a #GAsyncResult.
5495 * @contents: a location to place the contents of the file.
5496 * @length: a location to place the length of the contents of the file,
5497 * or %NULL if the length is not needed
5498 * @etag_out: a location to place the current entity tag for the file,
5499 * or %NULL if the entity tag is not needed
5500 * @error: a #GError, or %NULL
5502 * Finishes an asynchronous load of the @file's contents.
5503 * The contents are placed in @contents, and @length is set to the
5504 * size of the @contents string. The @content should be freed with
5505 * g_free() when no longer needed. If @etag_out is present, it will be
5506 * set to the new entity tag for the @file.
5508 * Returns: %TRUE if the load was successful. If %FALSE and @error is
5509 * present, it will be set appropriately.
5512 g_file_load_contents_finish (GFile *file,
5519 return g_file_load_partial_contents_finish (file,
5528 * g_file_replace_contents:
5529 * @file: input #GFile.
5530 * @contents: a string containing the new contents for @file.
5531 * @length: the length of @contents in bytes.
5532 * @etag: the old <link linkend="gfile-etag">entity tag</link>
5533 * for the document, or %NULL
5534 * @make_backup: %TRUE if a backup should be created.
5535 * @flags: a set of #GFileCreateFlags.
5536 * @new_etag: a location to a new <link linkend="gfile-etag">entity tag</link>
5537 * for the document. This should be freed with g_free() when no longer
5539 * @cancellable: optional #GCancellable object, %NULL to ignore.
5540 * @error: a #GError, or %NULL
5542 * Replaces the contents of @file with @contents of @length bytes.
5544 * If @etag is specified (not %NULL) any existing file must have that etag, or
5545 * the error %G_IO_ERROR_WRONG_ETAG will be returned.
5547 * If @make_backup is %TRUE, this function will attempt to make a backup of @file.
5549 * If @cancellable is not %NULL, then the operation can be cancelled by
5550 * triggering the cancellable object from another thread. If the operation
5551 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5553 * The returned @new_etag can be used to verify that the file hasn't changed the
5554 * next time it is saved over.
5556 * Returns: %TRUE if successful. If an error
5557 * has occurred, this function will return %FALSE and set @error
5558 * appropriately if present.
5561 g_file_replace_contents (GFile *file,
5562 const char *contents,
5565 gboolean make_backup,
5566 GFileCreateFlags flags,
5568 GCancellable *cancellable,
5571 GFileOutputStream *out;
5572 gsize pos, remainder;
5576 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5577 g_return_val_if_fail (contents != NULL, FALSE);
5579 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
5585 while (remainder > 0 &&
5586 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
5588 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
5596 if (remainder > 0 && res < 0)
5598 /* Ignore errors on close */
5599 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
5600 g_object_unref (out);
5602 /* error is set already */
5606 ret = g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error);
5609 *new_etag = g_file_output_stream_get_etag (out);
5611 g_object_unref (out);
5619 GCancellable *cancellable;
5620 GAsyncReadyCallback callback;
5622 const char *content;
5626 } ReplaceContentsData;
5629 replace_contents_data_free (ReplaceContentsData *data)
5632 g_error_free (data->error);
5633 if (data->cancellable)
5634 g_object_unref (data->cancellable);
5635 g_object_unref (data->file);
5636 g_free (data->etag);
5641 replace_contents_close_callback (GObject *obj,
5642 GAsyncResult *close_res,
5645 GOutputStream *stream = G_OUTPUT_STREAM (obj);
5646 ReplaceContentsData *data = user_data;
5647 GSimpleAsyncResult *res;
5649 /* Ignore errors here, we're only reading anyway */
5650 g_output_stream_close_finish (stream, close_res, NULL);
5651 g_object_unref (stream);
5653 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
5655 res = g_simple_async_result_new (G_OBJECT (data->file),
5658 g_file_replace_contents_async);
5659 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_contents_data_free);
5660 g_simple_async_result_complete (res);
5661 g_object_unref (res);
5665 replace_contents_write_callback (GObject *obj,
5666 GAsyncResult *read_res,
5669 GOutputStream *stream = G_OUTPUT_STREAM (obj);
5670 ReplaceContentsData *data = user_data;
5671 GError *error = NULL;
5674 write_size = g_output_stream_write_finish (stream, read_res, &error);
5676 if (write_size <= 0)
5678 /* Error or EOF, close the file */
5680 data->error = error;
5681 g_output_stream_close_async (stream, 0,
5683 replace_contents_close_callback, data);
5685 else if (write_size > 0)
5687 data->pos += write_size;
5689 if (data->pos >= data->length)
5690 g_output_stream_close_async (stream, 0,
5692 replace_contents_close_callback, data);
5694 g_output_stream_write_async (stream,
5695 data->content + data->pos,
5696 data->length - data->pos,
5699 replace_contents_write_callback,
5705 replace_contents_open_callback (GObject *obj,
5706 GAsyncResult *open_res,
5709 GFile *file = G_FILE (obj);
5710 GFileOutputStream *stream;
5711 ReplaceContentsData *data = user_data;
5712 GError *error = NULL;
5713 GSimpleAsyncResult *res;
5715 stream = g_file_replace_finish (file, open_res, &error);
5719 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
5720 data->content + data->pos,
5721 data->length - data->pos,
5724 replace_contents_write_callback,
5730 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
5734 g_simple_async_result_complete (res);
5735 g_error_free (error);
5736 replace_contents_data_free (data);
5737 g_object_unref (res);
5742 * g_file_replace_contents_async:
5743 * @file: input #GFile.
5744 * @contents: string of contents to replace the file with.
5745 * @length: the length of @contents in bytes.
5746 * @etag: a new <link linkend="gfile-etag">entity tag</link> for the @file, or %NULL
5747 * @make_backup: %TRUE if a backup should be created.
5748 * @flags: a set of #GFileCreateFlags.
5749 * @cancellable: optional #GCancellable object, %NULL to ignore.
5750 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5751 * @user_data: the data to pass to callback function
5753 * Starts an asynchronous replacement of @file with the given
5754 * @contents of @length bytes. @etag will replace the document's
5755 * current entity tag.
5757 * When this operation has completed, @callback will be called with
5758 * @user_user data, and the operation can be finalized with
5759 * g_file_replace_contents_finish().
5761 * If @cancellable is not %NULL, then the operation can be cancelled by
5762 * triggering the cancellable object from another thread. If the operation
5763 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5765 * If @make_backup is %TRUE, this function will attempt to
5766 * make a backup of @file.
5769 g_file_replace_contents_async (GFile *file,
5770 const char *contents,
5773 gboolean make_backup,
5774 GFileCreateFlags flags,
5775 GCancellable *cancellable,
5776 GAsyncReadyCallback callback,
5779 ReplaceContentsData *data;
5781 g_return_if_fail (G_IS_FILE (file));
5782 g_return_if_fail (contents != NULL);
5784 data = g_new0 (ReplaceContentsData, 1);
5787 data->cancellable = g_object_ref (cancellable);
5788 data->callback = callback;
5789 data->user_data = user_data;
5790 data->content = contents;
5791 data->length = length;
5793 data->file = g_object_ref (file);
5795 g_file_replace_async (file,
5801 replace_contents_open_callback,
5806 * g_file_replace_contents_finish:
5807 * @file: input #GFile.
5808 * @res: a #GAsyncResult.
5809 * @new_etag: a location of a new <link linkend="gfile-etag">entity tag</link>
5810 * for the document. This should be freed with g_free() when it is no
5811 * longer needed, or %NULL
5812 * @error: a #GError, or %NULL
5814 * Finishes an asynchronous replace of the given @file. See
5815 * g_file_replace_contents_async(). Sets @new_etag to the new entity
5816 * tag for the document, if present.
5818 * Returns: %TRUE on success, %FALSE on failure.
5821 g_file_replace_contents_finish (GFile *file,
5826 GSimpleAsyncResult *simple;
5827 ReplaceContentsData *data;
5829 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5830 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
5832 simple = G_SIMPLE_ASYNC_RESULT (res);
5834 if (g_simple_async_result_propagate_error (simple, error))
5837 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_replace_contents_async);
5839 data = g_simple_async_result_get_op_res_gpointer (simple);
5843 g_propagate_error (error, data->error);
5851 *new_etag = data->etag;
5852 data->etag = NULL; /* Take ownership */
5858 #define __G_FILE_C__
5859 #include "gioaliasdef.c"