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 void g_file_real_open_readwrite_async (GFile *file,
197 GCancellable *cancellable,
198 GAsyncReadyCallback callback,
200 static GFileIOStream * g_file_real_open_readwrite_finish (GFile *file,
203 static void g_file_real_create_readwrite_async (GFile *file,
204 GFileCreateFlags flags,
206 GCancellable *cancellable,
207 GAsyncReadyCallback callback,
209 static GFileIOStream * g_file_real_create_readwrite_finish (GFile *file,
212 static void g_file_real_replace_readwrite_async (GFile *file,
214 gboolean make_backup,
215 GFileCreateFlags flags,
217 GCancellable *cancellable,
218 GAsyncReadyCallback callback,
220 static GFileIOStream * g_file_real_replace_readwrite_finish (GFile *file,
223 static gboolean g_file_real_set_attributes_from_info (GFile *file,
225 GFileQueryInfoFlags flags,
226 GCancellable *cancellable,
228 static void g_file_real_set_display_name_async (GFile *file,
229 const char *display_name,
231 GCancellable *cancellable,
232 GAsyncReadyCallback callback,
234 static GFile * g_file_real_set_display_name_finish (GFile *file,
237 static void g_file_real_set_attributes_async (GFile *file,
239 GFileQueryInfoFlags flags,
241 GCancellable *cancellable,
242 GAsyncReadyCallback callback,
244 static gboolean g_file_real_set_attributes_finish (GFile *file,
248 static void g_file_real_find_enclosing_mount_async (GFile *file,
250 GCancellable *cancellable,
251 GAsyncReadyCallback callback,
253 static GMount * g_file_real_find_enclosing_mount_finish (GFile *file,
256 static void g_file_real_copy_async (GFile *source,
258 GFileCopyFlags flags,
260 GCancellable *cancellable,
261 GFileProgressCallback progress_callback,
262 gpointer progress_callback_data,
263 GAsyncReadyCallback callback,
265 static gboolean g_file_real_copy_finish (GFile *file,
270 g_file_get_type (void)
272 static volatile gsize g_define_type_id__volatile = 0;
274 if (g_once_init_enter (&g_define_type_id__volatile))
276 const GTypeInfo file_info =
278 sizeof (GFileIface), /* class_size */
279 g_file_base_init, /* base_init */
280 NULL, /* base_finalize */
282 NULL, /* class_finalize */
283 NULL, /* class_data */
288 GType g_define_type_id =
289 g_type_register_static (G_TYPE_INTERFACE, I_("GFile"),
292 g_type_interface_add_prerequisite (g_define_type_id, G_TYPE_OBJECT);
294 g_once_init_leave (&g_define_type_id__volatile, g_define_type_id);
297 return g_define_type_id__volatile;
301 g_file_class_init (gpointer g_class,
304 GFileIface *iface = g_class;
306 iface->enumerate_children_async = g_file_real_enumerate_children_async;
307 iface->enumerate_children_finish = g_file_real_enumerate_children_finish;
308 iface->set_display_name_async = g_file_real_set_display_name_async;
309 iface->set_display_name_finish = g_file_real_set_display_name_finish;
310 iface->query_info_async = g_file_real_query_info_async;
311 iface->query_info_finish = g_file_real_query_info_finish;
312 iface->query_filesystem_info_async = g_file_real_query_filesystem_info_async;
313 iface->query_filesystem_info_finish = g_file_real_query_filesystem_info_finish;
314 iface->set_attributes_async = g_file_real_set_attributes_async;
315 iface->set_attributes_finish = g_file_real_set_attributes_finish;
316 iface->read_async = g_file_real_read_async;
317 iface->read_finish = g_file_real_read_finish;
318 iface->append_to_async = g_file_real_append_to_async;
319 iface->append_to_finish = g_file_real_append_to_finish;
320 iface->create_async = g_file_real_create_async;
321 iface->create_finish = g_file_real_create_finish;
322 iface->replace_async = g_file_real_replace_async;
323 iface->replace_finish = g_file_real_replace_finish;
324 iface->open_readwrite_async = g_file_real_open_readwrite_async;
325 iface->open_readwrite_finish = g_file_real_open_readwrite_finish;
326 iface->create_readwrite_async = g_file_real_create_readwrite_async;
327 iface->create_readwrite_finish = g_file_real_create_readwrite_finish;
328 iface->replace_readwrite_async = g_file_real_replace_readwrite_async;
329 iface->replace_readwrite_finish = g_file_real_replace_readwrite_finish;
330 iface->find_enclosing_mount_async = g_file_real_find_enclosing_mount_async;
331 iface->find_enclosing_mount_finish = g_file_real_find_enclosing_mount_finish;
332 iface->set_attributes_from_info = g_file_real_set_attributes_from_info;
333 iface->copy_async = g_file_real_copy_async;
334 iface->copy_finish = g_file_real_copy_finish;
338 g_file_base_init (gpointer g_class)
345 * @file: input #GFile.
347 * Checks to see if a file is native to the platform.
349 * A native file s one expressed in the platform-native filename format,
350 * e.g. "C:\Windows" or "/usr/bin/". This does not mean the file is local,
351 * as it might be on a locally mounted remote filesystem.
353 * On some systems non-native files may be available using
354 * the native filesystem via a userspace filesystem (FUSE), in
355 * these cases this call will return %FALSE, but g_file_get_path()
356 * will still return a native path.
358 * This call does no blocking i/o.
360 * Returns: %TRUE if file is native.
363 g_file_is_native (GFile *file)
367 g_return_val_if_fail (G_IS_FILE (file), FALSE);
369 iface = G_FILE_GET_IFACE (file);
371 return (* iface->is_native) (file);
376 * g_file_has_uri_scheme:
377 * @file: input #GFile.
378 * @uri_scheme: a string containing a URI scheme.
380 * Checks to see if a #GFile has a given URI scheme.
382 * This call does no blocking i/o.
384 * Returns: %TRUE if #GFile's backend supports the
385 * given URI scheme, %FALSE if URI scheme is %NULL,
386 * not supported, or #GFile is invalid.
389 g_file_has_uri_scheme (GFile *file,
390 const char *uri_scheme)
394 g_return_val_if_fail (G_IS_FILE (file), FALSE);
395 g_return_val_if_fail (uri_scheme != NULL, FALSE);
397 iface = G_FILE_GET_IFACE (file);
399 return (* iface->has_uri_scheme) (file, uri_scheme);
404 * g_file_get_uri_scheme:
405 * @file: input #GFile.
407 * Gets the URI scheme for a #GFile.
408 * RFC 3986 decodes the scheme as:
410 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
412 * Common schemes include "file", "http", "ftp", etc.
414 * This call does no blocking i/o.
416 * Returns: a string containing the URI scheme for the given
417 * #GFile. The returned string should be freed with g_free()
418 * when no longer needed.
421 g_file_get_uri_scheme (GFile *file)
425 g_return_val_if_fail (G_IS_FILE (file), NULL);
427 iface = G_FILE_GET_IFACE (file);
429 return (* iface->get_uri_scheme) (file);
434 * g_file_get_basename:
435 * @file: input #GFile.
437 * Gets the base name (the last component of the path) for a given #GFile.
439 * If called for the top level of a system (such as the filesystem root
440 * or a uri like sftp://host/) it will return a single directory separator
441 * (and on Windows, possibly a drive letter).
443 * The base name is a byte string (*not* UTF-8). It has no defined encoding
444 * or rules other than it may not contain zero bytes. If you want to use
445 * filenames in a user interface you should use the display name that you
446 * can get by requesting the %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME
447 * attribute with g_file_query_info().
449 * This call does no blocking i/o.
451 * Returns: string containing the #GFile's base name, or %NULL
452 * if given #GFile is invalid. The returned string should be
453 * freed with g_free() when no longer needed.
456 g_file_get_basename (GFile *file)
460 g_return_val_if_fail (G_IS_FILE (file), NULL);
462 iface = G_FILE_GET_IFACE (file);
464 return (* iface->get_basename) (file);
469 * @file: input #GFile.
471 * Gets the local pathname for #GFile, if one exists.
473 * This call does no blocking i/o.
475 * Returns: string containing the #GFile's path, or %NULL if
476 * no such path exists. The returned string should be
477 * freed with g_free() when no longer needed.
480 g_file_get_path (GFile *file)
484 g_return_val_if_fail (G_IS_FILE (file), NULL);
486 iface = G_FILE_GET_IFACE (file);
488 return (* iface->get_path) (file);
493 * @file: input #GFile.
495 * Gets the URI for the @file.
497 * This call does no blocking i/o.
499 * Returns: a string containing the #GFile's URI.
500 * The returned string should be freed with g_free() when no longer needed.
503 g_file_get_uri (GFile *file)
507 g_return_val_if_fail (G_IS_FILE (file), NULL);
509 iface = G_FILE_GET_IFACE (file);
511 return (* iface->get_uri) (file);
515 * g_file_get_parse_name:
516 * @file: input #GFile.
518 * Gets the parse name of the @file.
519 * A parse name is a UTF-8 string that describes the
520 * file such that one can get the #GFile back using
521 * g_file_parse_name().
523 * This is generally used to show the #GFile as a nice
524 * full-pathname kind of string in a user interface,
525 * like in a location entry.
527 * For local files with names that can safely be converted
528 * to UTF8 the pathname is used, otherwise the IRI is used
529 * (a form of URI that allows UTF8 characters unescaped).
531 * This call does no blocking i/o.
533 * Returns: a string containing the #GFile's parse name. The returned
534 * string should be freed with g_free() when no longer needed.
537 g_file_get_parse_name (GFile *file)
541 g_return_val_if_fail (G_IS_FILE (file), NULL);
543 iface = G_FILE_GET_IFACE (file);
545 return (* iface->get_parse_name) (file);
550 * @file: input #GFile.
552 * Duplicates a #GFile handle. This operation does not duplicate
553 * the actual file or directory represented by the #GFile; see
554 * g_file_copy() if attempting to copy a file.
556 * This call does no blocking i/o.
558 * Returns: a new #GFile that is a duplicate of the given #GFile.
561 g_file_dup (GFile *file)
565 g_return_val_if_fail (G_IS_FILE (file), NULL);
567 iface = G_FILE_GET_IFACE (file);
569 return (* iface->dup) (file);
574 * @file: #gconstpointer to a #GFile.
576 * Creates a hash value for a #GFile.
578 * This call does no blocking i/o.
580 * Returns: 0 if @file is not a valid #GFile, otherwise an
581 * integer that can be used as hash value for the #GFile.
582 * This function is intended for easily hashing a #GFile to
583 * add to a #GHashTable or similar data structure.
586 g_file_hash (gconstpointer file)
590 g_return_val_if_fail (G_IS_FILE (file), 0);
592 iface = G_FILE_GET_IFACE (file);
594 return (* iface->hash) ((GFile *)file);
599 * @file1: the first #GFile.
600 * @file2: the second #GFile.
602 * Checks equality of two given #GFile<!-- -->s. Note that two
603 * #GFile<!-- -->s that differ can still refer to the same
604 * file on the filesystem due to various forms of filename
607 * This call does no blocking i/o.
609 * Returns: %TRUE if @file1 and @file2 are equal.
610 * %FALSE if either is not a #GFile.
613 g_file_equal (GFile *file1,
618 g_return_val_if_fail (G_IS_FILE (file1), FALSE);
619 g_return_val_if_fail (G_IS_FILE (file2), FALSE);
621 if (G_TYPE_FROM_INSTANCE (file1) != G_TYPE_FROM_INSTANCE (file2))
624 iface = G_FILE_GET_IFACE (file1);
626 return (* iface->equal) (file1, file2);
632 * @file: input #GFile.
634 * Gets the parent directory for the @file.
635 * If the @file represents the root directory of the
636 * file system, then %NULL will be returned.
638 * This call does no blocking i/o.
640 * Returns: a #GFile structure to the parent of the given
641 * #GFile or %NULL if there is no parent.
642 * Free the returned object with g_object_unref().
645 g_file_get_parent (GFile *file)
649 g_return_val_if_fail (G_IS_FILE (file), NULL);
651 iface = G_FILE_GET_IFACE (file);
653 return (* iface->get_parent) (file);
658 * @file: input #GFile.
659 * @name: string containing the child's basename.
661 * Gets a child of @file with basename equal to @name.
663 * Note that the file with that specific name might not exist, but
664 * you can still have a #GFile that points to it. You can use this
665 * for instance to create that file.
667 * This call does no blocking i/o.
669 * Returns: a #GFile to a child specified by @name.
670 * Free the returned object with g_object_unref().
673 g_file_get_child (GFile *file,
676 g_return_val_if_fail (G_IS_FILE (file), NULL);
677 g_return_val_if_fail (name != NULL, NULL);
679 return g_file_resolve_relative_path (file, name);
683 * g_file_get_child_for_display_name:
684 * @file: input #GFile.
685 * @display_name: string to a possible child.
688 * Gets the child of @file for a given @display_name (i.e. a UTF8
689 * version of the name). If this function fails, it returns %NULL and @error will be
690 * set. This is very useful when constructing a GFile for a new file
691 * and the user entered the filename in the user interface, for instance
692 * when you select a directory and type a filename in the file selector.
694 * This call does no blocking i/o.
696 * Returns: a #GFile to the specified child, or
697 * %NULL if the display name couldn't be converted.
698 * Free the returned object with g_object_unref().
701 g_file_get_child_for_display_name (GFile *file,
702 const char *display_name,
707 g_return_val_if_fail (G_IS_FILE (file), NULL);
708 g_return_val_if_fail (display_name != NULL, NULL);
710 iface = G_FILE_GET_IFACE (file);
712 return (* iface->get_child_for_display_name) (file, display_name, error);
717 * @file: input #GFile.
718 * @prefix: input #GFile.
720 * Checks whether @file has the prefix specified by @prefix. In other word,
721 * if the names of inital elements of @file<!-- -->s pathname match @prefix.
722 * Only full pathname elements are matched, so a path like /foo is not
723 * considered a prefix of /foobar, only of /foo/bar.
725 * This call does no i/o, as it works purely on names. As such it can
726 * sometimes return %FALSE even if @file is inside a @prefix (from a
727 * filesystem point of view), because the prefix of @file is an alias
730 * Returns: %TRUE if the @files's parent, grandparent, etc is @prefix.
734 g_file_has_prefix (GFile *file,
739 g_return_val_if_fail (G_IS_FILE (file), FALSE);
740 g_return_val_if_fail (G_IS_FILE (prefix), FALSE);
742 if (G_TYPE_FROM_INSTANCE (file) != G_TYPE_FROM_INSTANCE (prefix))
745 iface = G_FILE_GET_IFACE (file);
747 /* The vtable function differs in arg order since we're
748 using the old contains_file call */
749 return (* iface->prefix_matches) (prefix, file);
753 * g_file_get_relative_path:
754 * @parent: input #GFile.
755 * @descendant: input #GFile.
757 * Gets the path for @descendant relative to @parent.
759 * This call does no blocking i/o.
761 * Returns: string with the relative path from @descendant
762 * to @parent, or %NULL if @descendant doesn't have @parent as prefix.
763 * The returned string should be freed with g_free() when no longer needed.
766 g_file_get_relative_path (GFile *parent,
771 g_return_val_if_fail (G_IS_FILE (parent), NULL);
772 g_return_val_if_fail (G_IS_FILE (descendant), NULL);
774 if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
777 iface = G_FILE_GET_IFACE (parent);
779 return (* iface->get_relative_path) (parent, descendant);
783 * g_file_resolve_relative_path:
784 * @file: input #GFile.
785 * @relative_path: a given relative path string.
787 * Resolves a relative path for @file to an absolute path.
789 * This call does no blocking i/o.
791 * Returns: #GFile to the resolved path. %NULL if @relative_path
792 * is %NULL or if @file is invalid.
793 * Free the returned object with g_object_unref().
796 g_file_resolve_relative_path (GFile *file,
797 const char *relative_path)
801 g_return_val_if_fail (G_IS_FILE (file), NULL);
802 g_return_val_if_fail (relative_path != NULL, NULL);
804 iface = G_FILE_GET_IFACE (file);
806 return (* iface->resolve_relative_path) (file, relative_path);
810 * g_file_enumerate_children:
811 * @file: input #GFile.
812 * @attributes: an attribute query string.
813 * @flags: a set of #GFileQueryInfoFlags.
814 * @cancellable: optional #GCancellable object, %NULL to ignore.
815 * @error: #GError for error reporting.
817 * Gets the requested information about the files in a directory. The result
818 * is a #GFileEnumerator object that will give out #GFileInfo objects for
819 * all the files in the directory.
821 * The @attribute value is a string that specifies the file attributes that
822 * should be gathered. It is not an error if it's not possible to read a particular
823 * requested attribute from a file - it just won't be set. @attribute should
824 * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
825 * means all attributes, and a wildcard like "standard::*" means all attributes in the standard
826 * namespace. An example attribute query be "standard::*,owner::user".
827 * The standard attributes are available as defines, like #G_FILE_ATTRIBUTE_STANDARD_NAME.
829 * If @cancellable is not %NULL, then the operation can be cancelled by
830 * triggering the cancellable object from another thread. If the operation
831 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
833 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
834 * If the file is not a directory, the G_FILE_ERROR_NOTDIR error will be returned.
835 * Other errors are possible too.
837 * Returns: A #GFileEnumerator if successful, %NULL on error.
838 * Free the returned object with g_object_unref().
841 g_file_enumerate_children (GFile *file,
842 const char *attributes,
843 GFileQueryInfoFlags flags,
844 GCancellable *cancellable,
850 g_return_val_if_fail (G_IS_FILE (file), NULL);
852 if (g_cancellable_set_error_if_cancelled (cancellable, error))
855 iface = G_FILE_GET_IFACE (file);
857 if (iface->enumerate_children == NULL)
859 g_set_error_literal (error, G_IO_ERROR,
860 G_IO_ERROR_NOT_SUPPORTED,
861 _("Operation not supported"));
865 return (* iface->enumerate_children) (file, attributes, flags,
870 * g_file_enumerate_children_async:
871 * @file: input #GFile.
872 * @attributes: an attribute query string.
873 * @flags: a set of #GFileQueryInfoFlags.
874 * @io_priority: the <link linkend="io-priority">I/O priority</link>
876 * @cancellable: optional #GCancellable object, %NULL to ignore.
877 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
878 * @user_data: the data to pass to callback function
880 * Asynchronously gets the requested information about the files in a directory. The result
881 * is a #GFileEnumerator object that will give out #GFileInfo objects for
882 * all the files in the directory.
884 * For more details, see g_file_enumerate_children() which is
885 * the synchronous version of this call.
887 * When the operation is finished, @callback will be called. You can then call
888 * g_file_enumerate_children_finish() to get the result of the operation.
891 g_file_enumerate_children_async (GFile *file,
892 const char *attributes,
893 GFileQueryInfoFlags flags,
895 GCancellable *cancellable,
896 GAsyncReadyCallback callback,
901 g_return_if_fail (G_IS_FILE (file));
903 iface = G_FILE_GET_IFACE (file);
904 (* iface->enumerate_children_async) (file,
914 * g_file_enumerate_children_finish:
915 * @file: input #GFile.
916 * @res: a #GAsyncResult.
919 * Finishes an async enumerate children operation.
920 * See g_file_enumerate_children_async().
922 * Returns: a #GFileEnumerator or %NULL if an error occurred.
923 * Free the returned object with g_object_unref().
926 g_file_enumerate_children_finish (GFile *file,
932 g_return_val_if_fail (G_IS_FILE (file), NULL);
933 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
935 if (G_IS_SIMPLE_ASYNC_RESULT (res))
937 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
938 if (g_simple_async_result_propagate_error (simple, error))
942 iface = G_FILE_GET_IFACE (file);
943 return (* iface->enumerate_children_finish) (file, res, error);
947 * g_file_query_exists:
948 * @file: input #GFile.
949 * @cancellable: optional #GCancellable object, %NULL to ignore.
951 * Utility function to check if a particular file exists. This is
952 * implemented using g_file_query_info() and as such does blocking I/O.
954 * Note that in many cases it is racy to first check for file existence
955 * and then execute something based on the outcome of that, because the
956 * file might have been created or removed in between the operations. The
957 * general approach to handling that is to not check, but just do the
958 * operation and handle the errors as they come.
960 * As an example of race-free checking, take the case of reading a file, and
961 * if it doesn't exist, creating it. There are two racy versions: read it, and
962 * on error create it; and: check if it exists, if not create it. These
963 * can both result in two processes creating the file (with perhaps a partially
964 * written file as the result). The correct approach is to always try to create
965 * the file with g_file_create() which will either atomically create the file
966 * or fail with a G_IO_ERROR_EXISTS error.
968 * However, in many cases an existence check is useful in a user
969 * interface, for instance to make a menu item sensitive/insensitive, so that
970 * you don't have to fool users that something is possible and then just show
971 * and error dialog. If you do this, you should make sure to also handle the
972 * errors that can happen due to races when you execute the operation.
974 * Returns: %TRUE if the file exists (and can be detected without error), %FALSE otherwise (or if cancelled).
977 g_file_query_exists (GFile *file,
978 GCancellable *cancellable)
982 g_return_val_if_fail (G_IS_FILE(file), FALSE);
984 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE,
985 G_FILE_QUERY_INFO_NONE, cancellable, NULL);
988 g_object_unref (info);
996 * g_file_query_file_type:
997 * @file: input #GFile.
998 * @flags: a set of #GFileQueryInfoFlags passed to g_file_query_info().
999 * @cancellable: optional #GCancellable object, %NULL to ignore.
1001 * Utility function to inspect the #GFileType of a file. This is
1002 * implemented using g_file_query_info() and as such does blocking I/O.
1004 * The primary use case of this method is to check if a file is a regular file,
1005 * directory, or symlink.
1007 * Returns: The #GFileType of the file and #G_FILE_TYPE_UNKNOWN if the file
1013 g_file_query_file_type (GFile *file,
1014 GFileQueryInfoFlags flags,
1015 GCancellable *cancellable)
1018 GFileType file_type;
1020 g_return_val_if_fail (G_IS_FILE(file), G_FILE_TYPE_UNKNOWN);
1021 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE, flags,
1025 file_type = g_file_info_get_file_type (info);
1026 g_object_unref (info);
1029 file_type = G_FILE_TYPE_UNKNOWN;
1035 * g_file_query_info:
1036 * @file: input #GFile.
1037 * @attributes: an attribute query string.
1038 * @flags: a set of #GFileQueryInfoFlags.
1039 * @cancellable: optional #GCancellable object, %NULL to ignore.
1040 * @error: a #GError.
1042 * Gets the requested information about specified @file. The result
1043 * is a #GFileInfo object that contains key-value attributes (such as
1044 * the type or size of the file).
1046 * The @attribute value is a string that specifies the file attributes that
1047 * should be gathered. It is not an error if it's not possible to read a particular
1048 * requested attribute from a file - it just won't be set. @attribute should
1049 * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
1050 * means all attributes, and a wildcard like "standard::*" means all attributes in the standard
1051 * namespace. An example attribute query be "standard::*,owner::user".
1052 * The standard attributes are available as defines, like #G_FILE_ATTRIBUTE_STANDARD_NAME.
1054 * If @cancellable is not %NULL, then the operation can be cancelled by
1055 * triggering the cancellable object from another thread. If the operation
1056 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1058 * For symlinks, normally the information about the target of the
1059 * symlink is returned, rather than information about the symlink itself.
1060 * However if you pass #G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS in @flags the
1061 * information about the symlink itself will be returned. Also, for symlinks
1062 * that point to non-existing files the information about the symlink itself
1065 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1066 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1068 * Returns: a #GFileInfo for the given @file, or %NULL on error.
1069 * Free the returned object with g_object_unref().
1072 g_file_query_info (GFile *file,
1073 const char *attributes,
1074 GFileQueryInfoFlags flags,
1075 GCancellable *cancellable,
1080 g_return_val_if_fail (G_IS_FILE (file), NULL);
1082 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1085 iface = G_FILE_GET_IFACE (file);
1087 if (iface->query_info == NULL)
1089 g_set_error_literal (error, G_IO_ERROR,
1090 G_IO_ERROR_NOT_SUPPORTED,
1091 _("Operation not supported"));
1095 return (* iface->query_info) (file, attributes, flags, cancellable, error);
1099 * g_file_query_info_async:
1100 * @file: input #GFile.
1101 * @attributes: an attribute query string.
1102 * @flags: a set of #GFileQueryInfoFlags.
1103 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1105 * @cancellable: optional #GCancellable object, %NULL to ignore.
1106 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1107 * @user_data: the data to pass to callback function
1109 * Asynchronously gets the requested information about specified @file. The result
1110 * is a #GFileInfo object that contains key-value attributes (such as type or size
1113 * For more details, see g_file_query_info() which is
1114 * the synchronous version of this call.
1116 * When the operation is finished, @callback will be called. You can then call
1117 * g_file_query_info_finish() to get the result of the operation.
1120 g_file_query_info_async (GFile *file,
1121 const char *attributes,
1122 GFileQueryInfoFlags flags,
1124 GCancellable *cancellable,
1125 GAsyncReadyCallback callback,
1130 g_return_if_fail (G_IS_FILE (file));
1132 iface = G_FILE_GET_IFACE (file);
1133 (* iface->query_info_async) (file,
1143 * g_file_query_info_finish:
1144 * @file: input #GFile.
1145 * @res: a #GAsyncResult.
1146 * @error: a #GError.
1148 * Finishes an asynchronous file info query.
1149 * See g_file_query_info_async().
1151 * Returns: #GFileInfo for given @file or %NULL on error.
1152 * Free the returned object with g_object_unref().
1155 g_file_query_info_finish (GFile *file,
1161 g_return_val_if_fail (G_IS_FILE (file), NULL);
1162 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1164 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1166 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1167 if (g_simple_async_result_propagate_error (simple, error))
1171 iface = G_FILE_GET_IFACE (file);
1172 return (* iface->query_info_finish) (file, res, error);
1176 * g_file_query_filesystem_info:
1177 * @file: input #GFile.
1178 * @attributes: an attribute query string.
1179 * @cancellable: optional #GCancellable object, %NULL to ignore.
1180 * @error: a #GError.
1182 * Similar to g_file_query_info(), but obtains information
1183 * about the filesystem the @file is on, rather than the file itself.
1184 * For instance the amount of space available and the type of
1187 * The @attribute value is a string that specifies the file attributes that
1188 * should be gathered. It is not an error if it's not possible to read a particular
1189 * requested attribute from a file - it just won't be set. @attribute should
1190 * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
1191 * means all attributes, and a wildcard like "fs:*" means all attributes in the fs
1192 * namespace. The standard namespace for filesystem attributes is "fs".
1193 * Common attributes of interest are #G_FILE_ATTRIBUTE_FILESYSTEM_SIZE
1194 * (the total size of the filesystem in bytes), #G_FILE_ATTRIBUTE_FILESYSTEM_FREE (number of
1195 * bytes available), and #G_FILE_ATTRIBUTE_FILESYSTEM_TYPE (type of the filesystem).
1197 * If @cancellable is not %NULL, then the operation can be cancelled by
1198 * triggering the cancellable object from another thread. If the operation
1199 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1201 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1202 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1204 * Returns: a #GFileInfo or %NULL if there was an error.
1205 * Free the returned object with g_object_unref().
1208 g_file_query_filesystem_info (GFile *file,
1209 const char *attributes,
1210 GCancellable *cancellable,
1215 g_return_val_if_fail (G_IS_FILE (file), NULL);
1217 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1220 iface = G_FILE_GET_IFACE (file);
1222 if (iface->query_filesystem_info == NULL)
1224 g_set_error_literal (error, G_IO_ERROR,
1225 G_IO_ERROR_NOT_SUPPORTED,
1226 _("Operation not supported"));
1230 return (* iface->query_filesystem_info) (file, attributes, cancellable, error);
1234 * g_file_query_filesystem_info_async:
1235 * @file: input #GFile.
1236 * @attributes: an attribute query string.
1237 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1239 * @cancellable: optional #GCancellable object, %NULL to ignore.
1240 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1241 * @user_data: the data to pass to callback function
1243 * Asynchronously gets the requested information about the filesystem
1244 * that the specified @file is on. The result is a #GFileInfo object
1245 * that contains key-value attributes (such as type or size for the
1248 * For more details, see g_file_query_filesystem_info() which is the
1249 * synchronous version of this call.
1251 * When the operation is finished, @callback will be called. You can
1252 * then call g_file_query_info_finish() to get the result of the
1256 g_file_query_filesystem_info_async (GFile *file,
1257 const char *attributes,
1259 GCancellable *cancellable,
1260 GAsyncReadyCallback callback,
1265 g_return_if_fail (G_IS_FILE (file));
1267 iface = G_FILE_GET_IFACE (file);
1268 (* iface->query_filesystem_info_async) (file,
1277 * g_file_query_filesystem_info_finish:
1278 * @file: input #GFile.
1279 * @res: a #GAsyncResult.
1280 * @error: a #GError.
1282 * Finishes an asynchronous filesystem info query. See
1283 * g_file_query_filesystem_info_async().
1285 * Returns: #GFileInfo for given @file or %NULL on error.
1286 * Free the returned object with g_object_unref().
1289 g_file_query_filesystem_info_finish (GFile *file,
1295 g_return_val_if_fail (G_IS_FILE (file), NULL);
1296 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1298 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1300 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1301 if (g_simple_async_result_propagate_error (simple, error))
1305 iface = G_FILE_GET_IFACE (file);
1306 return (* iface->query_filesystem_info_finish) (file, res, error);
1310 * g_file_find_enclosing_mount:
1311 * @file: input #GFile.
1312 * @cancellable: optional #GCancellable object, %NULL to ignore.
1313 * @error: a #GError.
1315 * Gets a #GMount for the #GFile.
1317 * If the #GFileIface for @file does not have a mount (e.g. possibly a
1318 * remote share), @error will be set to %G_IO_ERROR_NOT_FOUND and %NULL
1321 * If @cancellable is not %NULL, then the operation can be cancelled by
1322 * triggering the cancellable object from another thread. If the operation
1323 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1325 * Returns: a #GMount where the @file is located or %NULL on error.
1326 * Free the returned object with g_object_unref().
1329 g_file_find_enclosing_mount (GFile *file,
1330 GCancellable *cancellable,
1335 g_return_val_if_fail (G_IS_FILE (file), NULL);
1337 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1340 iface = G_FILE_GET_IFACE (file);
1341 if (iface->find_enclosing_mount == NULL)
1344 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1345 /* Translators: This is an error message when trying to find the
1346 * enclosing (user visible) mount of a file, but none exists. */
1347 _("Containing mount does not exist"));
1351 return (* iface->find_enclosing_mount) (file, cancellable, error);
1355 * g_file_find_enclosing_mount_async:
1357 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1359 * @cancellable: optional #GCancellable object, %NULL to ignore.
1360 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1361 * @user_data: the data to pass to callback function
1363 * Asynchronously gets the mount for the file.
1365 * For more details, see g_file_find_enclosing_mount() which is
1366 * the synchronous version of this call.
1368 * When the operation is finished, @callback will be called. You can then call
1369 * g_file_find_enclosing_mount_finish() to get the result of the operation.
1372 g_file_find_enclosing_mount_async (GFile *file,
1374 GCancellable *cancellable,
1375 GAsyncReadyCallback callback,
1380 g_return_if_fail (G_IS_FILE (file));
1382 iface = G_FILE_GET_IFACE (file);
1383 (* iface->find_enclosing_mount_async) (file,
1391 * g_file_find_enclosing_mount_finish:
1393 * @res: a #GAsyncResult
1396 * Finishes an asynchronous find mount request.
1397 * See g_file_find_enclosing_mount_async().
1399 * Returns: #GMount for given @file or %NULL on error.
1400 * Free the returned object with g_object_unref().
1403 g_file_find_enclosing_mount_finish (GFile *file,
1409 g_return_val_if_fail (G_IS_FILE (file), NULL);
1410 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1412 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1414 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1415 if (g_simple_async_result_propagate_error (simple, error))
1419 iface = G_FILE_GET_IFACE (file);
1420 return (* iface->find_enclosing_mount_finish) (file, res, error);
1426 * @file: #GFile to read.
1427 * @cancellable: a #GCancellable
1428 * @error: a #GError, or %NULL
1430 * Opens a file for reading. The result is a #GFileInputStream that
1431 * can be used to read the contents of the file.
1433 * If @cancellable is not %NULL, then the operation can be cancelled by
1434 * triggering the cancellable object from another thread. If the operation
1435 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1437 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1438 * If the file is a directory, the G_IO_ERROR_IS_DIRECTORY error will be returned.
1439 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1441 * Returns: #GFileInputStream or %NULL on error.
1442 * Free the returned object with g_object_unref().
1445 g_file_read (GFile *file,
1446 GCancellable *cancellable,
1451 g_return_val_if_fail (G_IS_FILE (file), NULL);
1453 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1456 iface = G_FILE_GET_IFACE (file);
1458 if (iface->read_fn == NULL)
1460 g_set_error_literal (error, G_IO_ERROR,
1461 G_IO_ERROR_NOT_SUPPORTED,
1462 _("Operation not supported"));
1466 return (* iface->read_fn) (file, cancellable, error);
1471 * @file: input #GFile.
1472 * @flags: a set of #GFileCreateFlags.
1473 * @cancellable: optional #GCancellable object, %NULL to ignore.
1474 * @error: a #GError, or %NULL
1476 * Gets an output stream for appending data to the file. If
1477 * the file doesn't already exist it is created.
1479 * By default files created are generally readable by everyone,
1480 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1481 * will be made readable only to the current user, to the level that
1482 * is supported on the target filesystem.
1484 * If @cancellable is not %NULL, then the operation can be cancelled by
1485 * triggering the cancellable object from another thread. If the operation
1486 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1488 * Some file systems don't allow all file names, and may
1489 * return an %G_IO_ERROR_INVALID_FILENAME error.
1490 * If the file is a directory the %G_IO_ERROR_IS_DIRECTORY error will be
1491 * returned. Other errors are possible too, and depend on what kind of
1492 * filesystem the file is on.
1494 * Returns: a #GFileOutputStream, or %NULL on error.
1495 * Free the returned object with g_object_unref().
1498 g_file_append_to (GFile *file,
1499 GFileCreateFlags flags,
1500 GCancellable *cancellable,
1505 g_return_val_if_fail (G_IS_FILE (file), NULL);
1507 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1510 iface = G_FILE_GET_IFACE (file);
1512 if (iface->append_to == NULL)
1514 g_set_error_literal (error, G_IO_ERROR,
1515 G_IO_ERROR_NOT_SUPPORTED,
1516 _("Operation not supported"));
1520 return (* iface->append_to) (file, flags, cancellable, error);
1525 * @file: input #GFile.
1526 * @flags: a set of #GFileCreateFlags.
1527 * @cancellable: optional #GCancellable object, %NULL to ignore.
1528 * @error: a #GError, or %NULL
1530 * Creates a new file and returns an output stream for writing to it.
1531 * The file must not already exist.
1533 * By default files created are generally readable by everyone,
1534 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1535 * will be made readable only to the current user, to the level that
1536 * is supported on the target filesystem.
1538 * If @cancellable is not %NULL, then the operation can be cancelled by
1539 * triggering the cancellable object from another thread. If the operation
1540 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1542 * If a file or directory with this name already exists the G_IO_ERROR_EXISTS
1543 * error will be returned.
1544 * Some file systems don't allow all file names, and may
1545 * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1546 * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1547 * Other errors are possible too, and depend on what kind of
1548 * filesystem the file is on.
1550 * Returns: a #GFileOutputStream for the newly created file, or
1552 * Free the returned object with g_object_unref().
1555 g_file_create (GFile *file,
1556 GFileCreateFlags flags,
1557 GCancellable *cancellable,
1562 g_return_val_if_fail (G_IS_FILE (file), NULL);
1564 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1567 iface = G_FILE_GET_IFACE (file);
1569 if (iface->create == NULL)
1571 g_set_error_literal (error, G_IO_ERROR,
1572 G_IO_ERROR_NOT_SUPPORTED,
1573 _("Operation not supported"));
1577 return (* iface->create) (file, flags, cancellable, error);
1582 * @file: input #GFile.
1583 * @etag: an optional <link linkend="gfile-etag">entity tag</link> for the
1584 * current #GFile, or #NULL to ignore.
1585 * @make_backup: %TRUE if a backup should be created.
1586 * @flags: a set of #GFileCreateFlags.
1587 * @cancellable: optional #GCancellable object, %NULL to ignore.
1588 * @error: a #GError, or %NULL
1590 * Returns an output stream for overwriting the file, possibly
1591 * creating a backup copy of the file first. If the file doesn't exist,
1592 * it will be created.
1594 * This will try to replace the file in the safest way possible so
1595 * that any errors during the writing will not affect an already
1596 * existing copy of the file. For instance, for local files it
1597 * may write to a temporary file and then atomically rename over
1598 * the destination when the stream is closed.
1600 * By default files created are generally readable by everyone,
1601 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1602 * will be made readable only to the current user, to the level that
1603 * is supported on the target filesystem.
1605 * If @cancellable is not %NULL, then the operation can be cancelled by
1606 * triggering the cancellable object from another thread. If the operation
1607 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1609 * If you pass in a non-#NULL @etag value, then this value is
1610 * compared to the current entity tag of the file, and if they differ
1611 * an G_IO_ERROR_WRONG_ETAG error is returned. This generally means
1612 * that the file has been changed since you last read it. You can get
1613 * the new etag from g_file_output_stream_get_etag() after you've
1614 * finished writing and closed the #GFileOutputStream. When you load
1615 * a new file you can use g_file_input_stream_query_info() to get
1616 * the etag of the file.
1618 * If @make_backup is %TRUE, this function will attempt to make a backup
1619 * of the current file before overwriting it. If this fails a G_IO_ERROR_CANT_CREATE_BACKUP
1620 * error will be returned. If you want to replace anyway, try again with
1621 * @make_backup set to %FALSE.
1623 * If the file is a directory the G_IO_ERROR_IS_DIRECTORY error will be returned,
1624 * and if the file is some other form of non-regular file then a
1625 * G_IO_ERROR_NOT_REGULAR_FILE error will be returned.
1626 * Some file systems don't allow all file names, and may
1627 * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1628 * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1629 * Other errors are possible too, and depend on what kind of
1630 * filesystem the file is on.
1632 * Returns: a #GFileOutputStream or %NULL on error.
1633 * Free the returned object with g_object_unref().
1636 g_file_replace (GFile *file,
1638 gboolean make_backup,
1639 GFileCreateFlags flags,
1640 GCancellable *cancellable,
1645 g_return_val_if_fail (G_IS_FILE (file), NULL);
1647 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1650 iface = G_FILE_GET_IFACE (file);
1652 if (iface->replace == NULL)
1654 g_set_error_literal (error, G_IO_ERROR,
1655 G_IO_ERROR_NOT_SUPPORTED,
1656 _("Operation not supported"));
1661 /* Handle empty tag string as NULL in consistent way. */
1662 if (etag && *etag == 0)
1665 return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1669 * g_file_open_readwrite:
1670 * @file: #GFile to open
1671 * @cancellable: a #GCancellable
1672 * @error: a #GError, or %NULL
1674 * Opens an existing file for reading and writing. The result is
1675 * a #GFileIOStream that can be used to read and write the contents of the file.
1677 * If @cancellable is not %NULL, then the operation can be cancelled by
1678 * triggering the cancellable object from another thread. If the operation
1679 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1681 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1682 * If the file is a directory, the G_IO_ERROR_IS_DIRECTORY error will be returned.
1683 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1684 * Note that in many non-local file cases read and write streams are not supported,
1685 * so make sure you really need to do read and write streaming, rather than
1686 * just opening for reading or writing.
1688 * Returns: #GFileIOStream or %NULL on error.
1689 * Free the returned object with g_object_unref().
1694 g_file_open_readwrite (GFile *file,
1695 GCancellable *cancellable,
1700 g_return_val_if_fail (G_IS_FILE (file), NULL);
1702 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1705 iface = G_FILE_GET_IFACE (file);
1707 if (iface->open_readwrite == NULL)
1709 g_set_error_literal (error, G_IO_ERROR,
1710 G_IO_ERROR_NOT_SUPPORTED,
1711 _("Operation not supported"));
1715 return (* iface->open_readwrite) (file, cancellable, error);
1719 * g_file_create_readwrite:
1720 * @file: input #GFile.
1721 * @flags: a set of #GFileCreateFlags.
1722 * @cancellable: optional #GCancellable object, %NULL to ignore.
1723 * @error: a #GError, or %NULL
1725 * Creates a new file and returns a stream for reading and writing to it.
1726 * The file must not already exist.
1728 * By default files created are generally readable by everyone,
1729 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1730 * will be made readable only to the current user, to the level that
1731 * is supported on the target filesystem.
1733 * If @cancellable is not %NULL, then the operation can be cancelled by
1734 * triggering the cancellable object from another thread. If the operation
1735 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1737 * If a file or directory with this name already exists the G_IO_ERROR_EXISTS
1738 * error will be returned.
1739 * Some file systems don't allow all file names, and may
1740 * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1741 * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1742 * Other errors are possible too, and depend on what kind of
1743 * filesystem the file is on.
1745 * Note that in many non-local file cases read and write streams are not supported,
1746 * so make sure you really need to do read and write streaming, rather than
1747 * just opening for reading or writing.
1749 * Returns: a #GFileIOStream for the newly created file, or
1751 * Free the returned object with g_object_unref().
1756 g_file_create_readwrite (GFile *file,
1757 GFileCreateFlags flags,
1758 GCancellable *cancellable,
1763 g_return_val_if_fail (G_IS_FILE (file), NULL);
1765 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1768 iface = G_FILE_GET_IFACE (file);
1770 if (iface->create_readwrite == NULL)
1772 g_set_error_literal (error, G_IO_ERROR,
1773 G_IO_ERROR_NOT_SUPPORTED,
1774 _("Operation not supported"));
1778 return (* iface->create_readwrite) (file, flags, cancellable, error);
1782 * g_file_replace_readwrite:
1783 * @file: input #GFile.
1784 * @etag: an optional <link linkend="gfile-etag">entity tag</link> for the
1785 * current #GFile, or #NULL to ignore.
1786 * @make_backup: %TRUE if a backup should be created.
1787 * @flags: a set of #GFileCreateFlags.
1788 * @cancellable: optional #GCancellable object, %NULL to ignore.
1789 * @error: a #GError, or %NULL
1791 * Returns an output stream for overwriting the file in readwrite mode,
1792 * possibly creating a backup copy of the file first. If the file doesn't exist,
1793 * it will be created.
1795 * For details about the behaviour, see g_file_replace() which does the same
1796 * thing but returns an output stream only.
1798 * Note that in many non-local file cases read and write streams are not supported,
1799 * so make sure you really need to do read and write streaming, rather than
1800 * just opening for reading or writing.
1802 * Returns: a #GFileIOStream or %NULL on error.
1803 * Free the returned object with g_object_unref().
1808 g_file_replace_readwrite (GFile *file,
1810 gboolean make_backup,
1811 GFileCreateFlags flags,
1812 GCancellable *cancellable,
1817 g_return_val_if_fail (G_IS_FILE (file), NULL);
1819 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1822 iface = G_FILE_GET_IFACE (file);
1824 if (iface->replace_readwrite == NULL)
1826 g_set_error_literal (error, G_IO_ERROR,
1827 G_IO_ERROR_NOT_SUPPORTED,
1828 _("Operation not supported"));
1832 return (* iface->replace_readwrite) (file, etag, make_backup, flags, cancellable, error);
1836 * g_file_read_async:
1837 * @file: input #GFile.
1838 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1840 * @cancellable: optional #GCancellable object, %NULL to ignore.
1841 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1842 * @user_data: the data to pass to callback function
1844 * Asynchronously opens @file for reading.
1846 * For more details, see g_file_read() which is
1847 * the synchronous version of this call.
1849 * When the operation is finished, @callback will be called. You can then call
1850 * g_file_read_finish() to get the result of the operation.
1853 g_file_read_async (GFile *file,
1855 GCancellable *cancellable,
1856 GAsyncReadyCallback callback,
1861 g_return_if_fail (G_IS_FILE (file));
1863 iface = G_FILE_GET_IFACE (file);
1864 (* iface->read_async) (file,
1872 * g_file_read_finish:
1873 * @file: input #GFile.
1874 * @res: a #GAsyncResult.
1875 * @error: a #GError, or %NULL
1877 * Finishes an asynchronous file read operation started with
1878 * g_file_read_async().
1880 * Returns: a #GFileInputStream or %NULL on error.
1881 * Free the returned object with g_object_unref().
1884 g_file_read_finish (GFile *file,
1890 g_return_val_if_fail (G_IS_FILE (file), NULL);
1891 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1893 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1895 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1896 if (g_simple_async_result_propagate_error (simple, error))
1900 iface = G_FILE_GET_IFACE (file);
1901 return (* iface->read_finish) (file, res, error);
1905 * g_file_append_to_async:
1906 * @file: input #GFile.
1907 * @flags: a set of #GFileCreateFlags.
1908 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1910 * @cancellable: optional #GCancellable object, %NULL to ignore.
1911 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1912 * @user_data: the data to pass to callback function
1914 * Asynchronously opens @file for appending.
1916 * For more details, see g_file_append_to() which is
1917 * the synchronous version of this call.
1919 * When the operation is finished, @callback will be called. You can then call
1920 * g_file_append_to_finish() to get the result of the operation.
1923 g_file_append_to_async (GFile *file,
1924 GFileCreateFlags flags,
1926 GCancellable *cancellable,
1927 GAsyncReadyCallback callback,
1932 g_return_if_fail (G_IS_FILE (file));
1934 iface = G_FILE_GET_IFACE (file);
1935 (* iface->append_to_async) (file,
1944 * g_file_append_to_finish:
1945 * @file: input #GFile.
1946 * @res: #GAsyncResult
1947 * @error: a #GError, or %NULL
1949 * Finishes an asynchronous file append operation started with
1950 * g_file_append_to_async().
1952 * Returns: a valid #GFileOutputStream or %NULL on error.
1953 * Free the returned object with g_object_unref().
1956 g_file_append_to_finish (GFile *file,
1962 g_return_val_if_fail (G_IS_FILE (file), NULL);
1963 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1965 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1967 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1968 if (g_simple_async_result_propagate_error (simple, error))
1972 iface = G_FILE_GET_IFACE (file);
1973 return (* iface->append_to_finish) (file, res, error);
1977 * g_file_create_async:
1978 * @file: input #GFile.
1979 * @flags: a set of #GFileCreateFlags.
1980 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1982 * @cancellable: optional #GCancellable object, %NULL to ignore.
1983 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1984 * @user_data: the data to pass to callback function
1986 * Asynchronously creates a new file and returns an output stream for writing to it.
1987 * The file must not already exist.
1989 * For more details, see g_file_create() which is
1990 * the synchronous version of this call.
1992 * When the operation is finished, @callback will be called. You can then call
1993 * g_file_create_finish() to get the result of the operation.
1996 g_file_create_async (GFile *file,
1997 GFileCreateFlags flags,
1999 GCancellable *cancellable,
2000 GAsyncReadyCallback callback,
2005 g_return_if_fail (G_IS_FILE (file));
2007 iface = G_FILE_GET_IFACE (file);
2008 (* iface->create_async) (file,
2017 * g_file_create_finish:
2018 * @file: input #GFile.
2019 * @res: a #GAsyncResult.
2020 * @error: a #GError, or %NULL
2022 * Finishes an asynchronous file create operation started with
2023 * g_file_create_async().
2025 * Returns: a #GFileOutputStream or %NULL on error.
2026 * Free the returned object with g_object_unref().
2029 g_file_create_finish (GFile *file,
2035 g_return_val_if_fail (G_IS_FILE (file), NULL);
2036 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2038 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2040 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2041 if (g_simple_async_result_propagate_error (simple, error))
2045 iface = G_FILE_GET_IFACE (file);
2046 return (* iface->create_finish) (file, res, error);
2050 * g_file_replace_async:
2051 * @file: input #GFile.
2052 * @etag: an <link linkend="gfile-etag">entity tag</link> for the
2053 * current #GFile, or NULL to ignore.
2054 * @make_backup: %TRUE if a backup should be created.
2055 * @flags: a set of #GFileCreateFlags.
2056 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2058 * @cancellable: optional #GCancellable object, %NULL to ignore.
2059 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2060 * @user_data: the data to pass to callback function
2062 * Asynchronously overwrites the file, replacing the contents, possibly
2063 * creating a backup copy of the file first.
2065 * For more details, see g_file_replace() which is
2066 * the synchronous version of this call.
2068 * When the operation is finished, @callback will be called. You can then call
2069 * g_file_replace_finish() to get the result of the operation.
2072 g_file_replace_async (GFile *file,
2074 gboolean make_backup,
2075 GFileCreateFlags flags,
2077 GCancellable *cancellable,
2078 GAsyncReadyCallback callback,
2083 g_return_if_fail (G_IS_FILE (file));
2085 iface = G_FILE_GET_IFACE (file);
2086 (* iface->replace_async) (file,
2097 * g_file_replace_finish:
2098 * @file: input #GFile.
2099 * @res: a #GAsyncResult.
2100 * @error: a #GError, or %NULL
2102 * Finishes an asynchronous file replace operation started with
2103 * g_file_replace_async().
2105 * Returns: a #GFileOutputStream, or %NULL on error.
2106 * Free the returned object with g_object_unref().
2109 g_file_replace_finish (GFile *file,
2115 g_return_val_if_fail (G_IS_FILE (file), NULL);
2116 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2118 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2120 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2121 if (g_simple_async_result_propagate_error (simple, error))
2125 iface = G_FILE_GET_IFACE (file);
2126 return (* iface->replace_finish) (file, res, error);
2131 * g_file_open_readwrite_async:
2132 * @file: input #GFile.
2133 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2135 * @cancellable: optional #GCancellable object, %NULL to ignore.
2136 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2137 * @user_data: the data to pass to callback function
2139 * Asynchronously opens @file for reading and writing.
2141 * For more details, see g_file_open_readwrite() which is
2142 * the synchronous version of this call.
2144 * When the operation is finished, @callback will be called. You can then call
2145 * g_file_open_readwrite_finish() to get the result of the operation.
2150 g_file_open_readwrite_async (GFile *file,
2152 GCancellable *cancellable,
2153 GAsyncReadyCallback callback,
2158 g_return_if_fail (G_IS_FILE (file));
2160 iface = G_FILE_GET_IFACE (file);
2161 (* iface->open_readwrite_async) (file,
2169 * g_file_open_readwrite_finish:
2170 * @file: input #GFile.
2171 * @res: a #GAsyncResult.
2172 * @error: a #GError, or %NULL
2174 * Finishes an asynchronous file read operation started with
2175 * g_file_open_readwrite_async().
2177 * Returns: a #GFileIOStream or %NULL on error.
2178 * Free the returned object with g_object_unref().
2183 g_file_open_readwrite_finish (GFile *file,
2189 g_return_val_if_fail (G_IS_FILE (file), NULL);
2190 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2192 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2194 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2195 if (g_simple_async_result_propagate_error (simple, error))
2199 iface = G_FILE_GET_IFACE (file);
2200 return (* iface->open_readwrite_finish) (file, res, error);
2205 * g_file_create_readwrite_async:
2206 * @file: input #GFile.
2207 * @flags: a set of #GFileCreateFlags.
2208 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2210 * @cancellable: optional #GCancellable object, %NULL to ignore.
2211 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2212 * @user_data: the data to pass to callback function
2214 * Asynchronously creates a new file and returns a stream for reading and writing
2215 * to it. The file must not already exist.
2217 * For more details, see g_file_create_readwrite() which is
2218 * the synchronous version of this call.
2220 * When the operation is finished, @callback will be called. You can then call
2221 * g_file_create_readwrite_finish() to get the result of the operation.
2226 g_file_create_readwrite_async (GFile *file,
2227 GFileCreateFlags flags,
2229 GCancellable *cancellable,
2230 GAsyncReadyCallback callback,
2235 g_return_if_fail (G_IS_FILE (file));
2237 iface = G_FILE_GET_IFACE (file);
2238 (* iface->create_readwrite_async) (file,
2247 * g_file_create_readwrite_finish:
2248 * @file: input #GFile.
2249 * @res: a #GAsyncResult.
2250 * @error: a #GError, or %NULL
2252 * Finishes an asynchronous file create operation started with
2253 * g_file_create_readwrite_async().
2255 * Returns: a #GFileIOStream or %NULL on error.
2256 * Free the returned object with g_object_unref().
2261 g_file_create_readwrite_finish (GFile *file,
2267 g_return_val_if_fail (G_IS_FILE (file), NULL);
2268 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2270 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2272 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2273 if (g_simple_async_result_propagate_error (simple, error))
2277 iface = G_FILE_GET_IFACE (file);
2278 return (* iface->create_readwrite_finish) (file, res, error);
2282 * g_file_replace_readwrite_async:
2283 * @file: input #GFile.
2284 * @etag: an <link linkend="gfile-etag">entity tag</link> for the
2285 * current #GFile, or NULL to ignore.
2286 * @make_backup: %TRUE if a backup should be created.
2287 * @flags: a set of #GFileCreateFlags.
2288 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2290 * @cancellable: optional #GCancellable object, %NULL to ignore.
2291 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2292 * @user_data: the data to pass to callback function
2294 * Asynchronously overwrites the file in read-write mode, replacing the contents,
2295 * possibly creating a backup copy of the file first.
2297 * For more details, see g_file_replace_readwrite() which is
2298 * the synchronous version of this call.
2300 * When the operation is finished, @callback will be called. You can then call
2301 * g_file_replace_readwrite_finish() to get the result of the operation.
2306 g_file_replace_readwrite_async (GFile *file,
2308 gboolean make_backup,
2309 GFileCreateFlags flags,
2311 GCancellable *cancellable,
2312 GAsyncReadyCallback callback,
2317 g_return_if_fail (G_IS_FILE (file));
2319 iface = G_FILE_GET_IFACE (file);
2320 (* iface->replace_readwrite_async) (file,
2331 * g_file_replace_readwrite_finish:
2332 * @file: input #GFile.
2333 * @res: a #GAsyncResult.
2334 * @error: a #GError, or %NULL
2336 * Finishes an asynchronous file replace operation started with
2337 * g_file_replace_readwrite_async().
2339 * Returns: a #GFileIOStream, or %NULL on error.
2340 * Free the returned object with g_object_unref().
2345 g_file_replace_readwrite_finish (GFile *file,
2351 g_return_val_if_fail (G_IS_FILE (file), NULL);
2352 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2354 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2356 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2357 if (g_simple_async_result_propagate_error (simple, error))
2361 iface = G_FILE_GET_IFACE (file);
2362 return (* iface->replace_readwrite_finish) (file, res, error);
2366 copy_symlink (GFile *destination,
2367 GFileCopyFlags flags,
2368 GCancellable *cancellable,
2373 gboolean tried_delete;
2375 GFileType file_type;
2377 tried_delete = FALSE;
2381 if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
2383 /* Maybe it already existed, and we want to overwrite? */
2384 if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) &&
2385 my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
2387 g_error_free (my_error);
2390 /* Don't overwrite if the destination is a directory */
2391 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2392 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2393 cancellable, &my_error);
2396 file_type = g_file_info_get_file_type (info);
2397 g_object_unref (info);
2399 if (file_type == G_FILE_TYPE_DIRECTORY)
2401 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
2402 _("Can't copy over directory"));
2407 if (!g_file_delete (destination, cancellable, error))
2410 tried_delete = TRUE;
2414 g_propagate_error (error, my_error);
2421 static GInputStream *
2422 open_source_for_copy (GFile *source,
2424 GFileCopyFlags flags,
2425 GCancellable *cancellable,
2431 GFileType file_type;
2434 in = (GInputStream *)g_file_read (source, cancellable, &my_error);
2438 /* There was an error opening the source, try to set a good error for it: */
2440 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
2442 /* The source is a directory, don't fail with WOULD_RECURSE immediately,
2443 * as that is less useful to the app. Better check for errors on the
2446 g_error_free (my_error);
2449 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2450 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2451 cancellable, &my_error);
2454 file_type = g_file_info_get_file_type (info);
2455 g_object_unref (info);
2457 if (flags & G_FILE_COPY_OVERWRITE)
2459 if (file_type == G_FILE_TYPE_DIRECTORY)
2461 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
2462 _("Can't copy directory over directory"));
2465 /* continue to would_recurse error */
2469 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
2470 _("Target file exists"));
2476 /* Error getting info from target, return that error
2477 * (except for NOT_FOUND, which is no error here)
2479 if (my_error->domain != G_IO_ERROR && my_error->code != G_IO_ERROR_NOT_FOUND)
2481 g_propagate_error (error, my_error);
2484 g_error_free (my_error);
2487 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
2488 _("Can't recursively copy directory"));
2492 g_propagate_error (error, my_error);
2497 should_copy (GFileAttributeInfo *info,
2499 gboolean skip_perms)
2501 if (skip_perms && strcmp(info->name, "unix::mode") == 0)
2505 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
2506 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
2510 build_attribute_list_for_copy (GFileAttributeInfoList *attributes,
2511 GFileAttributeInfoList *namespaces,
2513 gboolean skip_perms)
2520 s = g_string_new ("");
2524 for (i = 0; i < attributes->n_infos; i++)
2526 if (should_copy (&attributes->infos[i], as_move, skip_perms))
2531 g_string_append_c (s, ',');
2533 g_string_append (s, attributes->infos[i].name);
2540 for (i = 0; i < namespaces->n_infos; i++)
2542 if (should_copy (&namespaces->infos[i], as_move, FALSE))
2547 g_string_append_c (s, ',');
2549 g_string_append (s, namespaces->infos[i].name);
2550 g_string_append (s, ":*");
2555 return g_string_free (s, FALSE);
2559 * g_file_copy_attributes:
2560 * @source: a #GFile with attributes.
2561 * @destination: a #GFile to copy attributes to.
2562 * @flags: a set of #GFileCopyFlags.
2563 * @cancellable: optional #GCancellable object, %NULL to ignore.
2564 * @error: a #GError, %NULL to ignore.
2566 * Copies the file attributes from @source to @destination.
2568 * Normally only a subset of the file attributes are copied,
2569 * those that are copies in a normal file copy operation
2570 * (which for instance does not include e.g. owner). However
2571 * if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
2572 * all the metadata that is possible to copy is copied. This
2573 * is useful when implementing move by copy + delete source.
2575 * Returns: %TRUE if the attributes were copied successfully, %FALSE otherwise.
2578 g_file_copy_attributes (GFile *source,
2580 GFileCopyFlags flags,
2581 GCancellable *cancellable,
2584 GFileAttributeInfoList *attributes, *namespaces;
2585 char *attrs_to_read;
2589 gboolean source_nofollow_symlinks;
2590 gboolean skip_perms;
2592 as_move = flags & G_FILE_COPY_ALL_METADATA;
2593 source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
2594 skip_perms = (flags & G_FILE_COPY_TARGET_DEFAULT_PERMS) != 0;
2596 /* Ignore errors here, if the target supports no attributes there is nothing to copy */
2597 attributes = g_file_query_settable_attributes (destination, cancellable, NULL);
2598 namespaces = g_file_query_writable_namespaces (destination, cancellable, NULL);
2600 if (attributes == NULL && namespaces == NULL)
2603 attrs_to_read = build_attribute_list_for_copy (attributes, namespaces, as_move, skip_perms);
2605 /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
2606 * we just don't copy it.
2608 info = g_file_query_info (source, attrs_to_read,
2609 source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
2613 g_free (attrs_to_read);
2618 res = g_file_set_attributes_from_info (destination,
2620 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2623 g_object_unref (info);
2626 g_file_attribute_info_list_unref (attributes);
2627 g_file_attribute_info_list_unref (namespaces);
2632 /* Closes the streams */
2634 copy_stream_with_progress (GInputStream *in,
2637 GCancellable *cancellable,
2638 GFileProgressCallback progress_callback,
2639 gpointer progress_callback_data,
2642 gssize n_read, n_written;
2643 goffset current_size;
2644 char buffer[1024*64], *p;
2650 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
2651 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2655 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2656 total_size = g_file_info_get_size (info);
2657 g_object_unref (info);
2660 if (total_size == -1)
2662 info = g_file_query_info (source,
2663 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2664 G_FILE_QUERY_INFO_NONE,
2668 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2669 total_size = g_file_info_get_size (info);
2670 g_object_unref (info);
2674 if (total_size == -1)
2681 n_read = g_input_stream_read (in, buffer, sizeof (buffer), cancellable, error);
2691 current_size += n_read;
2696 n_written = g_output_stream_write (out, p, n_read, cancellable, error);
2697 if (n_written == -1)
2704 n_read -= n_written;
2710 if (progress_callback)
2711 progress_callback (current_size, total_size, progress_callback_data);
2715 error = NULL; /* Ignore further errors */
2717 /* Make sure we send full copied size */
2718 if (progress_callback)
2719 progress_callback (current_size, total_size, progress_callback_data);
2721 /* Don't care about errors in source here */
2722 g_input_stream_close (in, cancellable, NULL);
2724 /* But write errors on close are bad! */
2725 if (!g_output_stream_close (out, cancellable, error))
2728 g_object_unref (in);
2729 g_object_unref (out);
2735 file_copy_fallback (GFile *source,
2737 GFileCopyFlags flags,
2738 GCancellable *cancellable,
2739 GFileProgressCallback progress_callback,
2740 gpointer progress_callback_data,
2748 /* need to know the file type */
2749 info = g_file_query_info (source,
2750 G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
2751 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2758 /* Maybe copy the symlink? */
2759 if ((flags & G_FILE_COPY_NOFOLLOW_SYMLINKS) &&
2760 g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK)
2762 target = g_file_info_get_symlink_target (info);
2765 if (!copy_symlink (destination, flags, cancellable, target, error))
2767 g_object_unref (info);
2771 g_object_unref (info);
2774 /* ... else fall back on a regular file copy */
2775 g_object_unref (info);
2777 /* Handle "special" files (pipes, device nodes, ...)? */
2778 else if (g_file_info_get_file_type (info) == G_FILE_TYPE_SPECIAL)
2780 /* FIXME: could try to recreate device nodes and others? */
2782 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2783 _("Can't copy special file"));
2784 g_object_unref (info);
2787 /* Everything else should just fall back on a regular copy. */
2789 g_object_unref (info);
2791 in = open_source_for_copy (source, destination, flags, cancellable, error);
2795 if (flags & G_FILE_COPY_OVERWRITE)
2797 out = (GOutputStream *)g_file_replace (destination,
2799 flags & G_FILE_COPY_BACKUP,
2800 G_FILE_CREATE_REPLACE_DESTINATION,
2801 cancellable, error);
2805 out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
2810 g_object_unref (in);
2814 if (!copy_stream_with_progress (in, out, source, cancellable,
2815 progress_callback, progress_callback_data,
2821 /* Ignore errors here. Failure to copy metadata is not a hard error */
2822 g_file_copy_attributes (source, destination,
2823 flags, cancellable, NULL);
2830 * @source: input #GFile.
2831 * @destination: destination #GFile
2832 * @flags: set of #GFileCopyFlags
2833 * @cancellable: optional #GCancellable object, %NULL to ignore.
2834 * @progress_callback: function to callback with progress information
2835 * @progress_callback_data: user data to pass to @progress_callback
2836 * @error: #GError to set on error, or %NULL
2838 * Copies the file @source to the location specified by @destination.
2839 * Can not handle recursive copies of directories.
2841 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2842 * existing @destination file is overwritten.
2844 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2845 * will be copied as symlinks, otherwise the target of the
2846 * @source symlink will be copied.
2848 * If @cancellable is not %NULL, then the operation can be cancelled by
2849 * triggering the cancellable object from another thread. If the operation
2850 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2852 * If @progress_callback is not %NULL, then the operation can be monitored by
2853 * setting this to a #GFileProgressCallback function. @progress_callback_data
2854 * will be passed to this function. It is guaranteed that this callback will
2855 * be called after all data has been transferred with the total number of bytes
2856 * copied during the operation.
2858 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2859 * error is returned, independent on the status of the @destination.
2861 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2862 * error G_IO_ERROR_EXISTS is returned.
2864 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2865 * error is returned. If trying to overwrite a directory with a directory the
2866 * G_IO_ERROR_WOULD_MERGE error is returned.
2868 * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2869 * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2872 * If you are interested in copying the #GFile object itself (not the on-disk
2873 * file), see g_file_dup().
2875 * Returns: %TRUE on success, %FALSE otherwise.
2878 g_file_copy (GFile *source,
2880 GFileCopyFlags flags,
2881 GCancellable *cancellable,
2882 GFileProgressCallback progress_callback,
2883 gpointer progress_callback_data,
2890 g_return_val_if_fail (G_IS_FILE (source), FALSE);
2891 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2893 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2896 iface = G_FILE_GET_IFACE (destination);
2900 res = (* iface->copy) (source, destination,
2902 progress_callback, progress_callback_data,
2908 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2910 g_propagate_error (error, my_error);
2914 g_clear_error (&my_error);
2917 /* If the types are different, and the destination method failed
2918 also try the source method */
2919 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
2921 iface = G_FILE_GET_IFACE (source);
2926 res = (* iface->copy) (source, destination,
2928 progress_callback, progress_callback_data,
2934 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2936 g_propagate_error (error, my_error);
2940 g_clear_error (&my_error);
2944 return file_copy_fallback (source, destination, flags, cancellable,
2945 progress_callback, progress_callback_data,
2950 * g_file_copy_async:
2951 * @source: input #GFile.
2952 * @destination: destination #GFile
2953 * @flags: set of #GFileCopyFlags
2954 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2956 * @cancellable: optional #GCancellable object, %NULL to ignore.
2957 * @progress_callback: function to callback with progress information
2958 * @progress_callback_data: user data to pass to @progress_callback
2959 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2960 * @user_data: the data to pass to callback function
2962 * Copies the file @source to the location specified by @destination
2963 * asynchronously. For details of the behaviour, see g_file_copy().
2965 * If @progress_callback is not %NULL, then that function that will be called
2966 * just like in g_file_copy(), however the callback will run in the main loop,
2967 * not in the thread that is doing the I/O operation.
2969 * When the operation is finished, @callback will be called. You can then call
2970 * g_file_copy_finish() to get the result of the operation.
2973 g_file_copy_async (GFile *source,
2975 GFileCopyFlags flags,
2977 GCancellable *cancellable,
2978 GFileProgressCallback progress_callback,
2979 gpointer progress_callback_data,
2980 GAsyncReadyCallback callback,
2985 g_return_if_fail (G_IS_FILE (source));
2986 g_return_if_fail (G_IS_FILE (destination));
2988 iface = G_FILE_GET_IFACE (source);
2989 (* iface->copy_async) (source,
2995 progress_callback_data,
3001 * g_file_copy_finish:
3002 * @file: input #GFile.
3003 * @res: a #GAsyncResult.
3004 * @error: a #GError, or %NULL
3006 * Finishes copying the file started with
3007 * g_file_copy_async().
3009 * Returns: a %TRUE on success, %FALSE on error.
3012 g_file_copy_finish (GFile *file,
3018 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3019 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
3021 if (G_IS_SIMPLE_ASYNC_RESULT (res))
3023 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3025 if (g_simple_async_result_propagate_error (simple, error))
3029 iface = G_FILE_GET_IFACE (file);
3030 return (* iface->copy_finish) (file, res, error);
3035 * @source: #GFile pointing to the source location.
3036 * @destination: #GFile pointing to the destination location.
3037 * @flags: set of #GFileCopyFlags.
3038 * @cancellable: optional #GCancellable object, %NULL to ignore.
3039 * @progress_callback: #GFileProgressCallback function for updates.
3040 * @progress_callback_data: gpointer to user data for the callback function.
3041 * @error: #GError for returning error conditions, or %NULL
3044 * Tries to move the file or directory @source to the location specified by @destination.
3045 * If native move operations are supported then this is used, otherwise a copy + delete
3046 * fallback is used. The native implementation may support moving directories (for instance
3047 * on moves inside the same filesystem), but the fallback code does not.
3049 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3050 * existing @destination file is overwritten.
3052 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3053 * will be copied as symlinks, otherwise the target of the
3054 * @source symlink will be copied.
3056 * If @cancellable is not %NULL, then the operation can be cancelled by
3057 * triggering the cancellable object from another thread. If the operation
3058 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3060 * If @progress_callback is not %NULL, then the operation can be monitored by
3061 * setting this to a #GFileProgressCallback function. @progress_callback_data
3062 * will be passed to this function. It is guaranteed that this callback will
3063 * be called after all data has been transferred with the total number of bytes
3064 * copied during the operation.
3066 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
3067 * error is returned, independent on the status of the @destination.
3069 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
3070 * error G_IO_ERROR_EXISTS is returned.
3072 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
3073 * error is returned. If trying to overwrite a directory with a directory the
3074 * G_IO_ERROR_WOULD_MERGE error is returned.
3076 * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
3077 * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
3078 * may be returned (if the native move operation isn't available).
3080 * Returns: %TRUE on successful move, %FALSE otherwise.
3083 g_file_move (GFile *source,
3085 GFileCopyFlags flags,
3086 GCancellable *cancellable,
3087 GFileProgressCallback progress_callback,
3088 gpointer progress_callback_data,
3095 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3096 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3098 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3101 iface = G_FILE_GET_IFACE (destination);
3105 res = (* iface->move) (source, destination,
3107 progress_callback, progress_callback_data,
3113 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3115 g_propagate_error (error, my_error);
3120 /* If the types are different, and the destination method failed
3121 also try the source method */
3122 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3124 iface = G_FILE_GET_IFACE (source);
3129 res = (* iface->move) (source, destination,
3131 progress_callback, progress_callback_data,
3137 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3139 g_propagate_error (error, my_error);
3145 if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
3147 g_set_error_literal (error, G_IO_ERROR,
3148 G_IO_ERROR_NOT_SUPPORTED,
3149 _("Operation not supported"));
3153 flags |= G_FILE_COPY_ALL_METADATA;
3154 if (!g_file_copy (source, destination, flags, cancellable,
3155 progress_callback, progress_callback_data,
3159 return g_file_delete (source, cancellable, error);
3163 * g_file_make_directory
3164 * @file: input #GFile.
3165 * @cancellable: optional #GCancellable object, %NULL to ignore.
3166 * @error: a #GError, or %NULL
3168 * Creates a directory. Note that this will only create a child directory of
3169 * the immediate parent directory of the path or URI given by the #GFile. To
3170 * recursively create directories, see g_file_make_directory_with_parents().
3171 * This function will fail if the parent directory does not exist, setting
3172 * @error to %G_IO_ERROR_NOT_FOUND. If the file system doesn't support creating
3173 * directories, this function will fail, setting @error to
3174 * %G_IO_ERROR_NOT_SUPPORTED.
3176 * For a local #GFile the newly created directory will have the default
3177 * (current) ownership and permissions of the current process.
3179 * If @cancellable is not %NULL, then the operation can be cancelled by
3180 * triggering the cancellable object from another thread. If the operation
3181 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3183 * Returns: %TRUE on successful creation, %FALSE otherwise.
3186 g_file_make_directory (GFile *file,
3187 GCancellable *cancellable,
3192 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3194 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3197 iface = G_FILE_GET_IFACE (file);
3199 if (iface->make_directory == NULL)
3201 g_set_error_literal (error, G_IO_ERROR,
3202 G_IO_ERROR_NOT_SUPPORTED,
3203 _("Operation not supported"));
3207 return (* iface->make_directory) (file, cancellable, error);
3211 * g_file_make_directory_with_parents:
3212 * @file: input #GFile.
3213 * @cancellable: optional #GCancellable object, %NULL to ignore.
3214 * @error: a #GError, or %NULL
3216 * Creates a directory and any parent directories that may not exist similar to
3217 * 'mkdir -p'. If the file system does not support creating directories, this
3218 * function will fail, setting @error to %G_IO_ERROR_NOT_SUPPORTED.
3220 * For a local #GFile the newly created directories will have the default
3221 * (current) ownership and permissions of the current process.
3223 * If @cancellable is not %NULL, then the operation can be cancelled by
3224 * triggering the cancellable object from another thread. If the operation
3225 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3227 * Returns: %TRUE if all directories have been successfully created, %FALSE
3233 g_file_make_directory_with_parents (GFile *file,
3234 GCancellable *cancellable,
3238 GFile *parent_file, *work_file;
3239 GList *list = NULL, *l;
3240 GError *my_error = NULL;
3242 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3245 result = g_file_make_directory (file, cancellable, &my_error);
3246 if (result || my_error->code != G_IO_ERROR_NOT_FOUND)
3249 g_propagate_error (error, my_error);
3255 while (!result && my_error->code == G_IO_ERROR_NOT_FOUND)
3257 g_clear_error (&my_error);
3259 parent_file = g_file_get_parent (work_file);
3260 if (parent_file == NULL)
3262 result = g_file_make_directory (parent_file, cancellable, &my_error);
3264 if (!result && my_error->code == G_IO_ERROR_NOT_FOUND)
3265 list = g_list_prepend (list, parent_file);
3267 work_file = parent_file;
3270 for (l = list; result && l; l = l->next)
3272 result = g_file_make_directory ((GFile *) l->data, cancellable, &my_error);
3276 while (list != NULL)
3278 g_object_unref ((GFile *) list->data);
3279 list = g_list_remove (list, list->data);
3284 g_propagate_error (error, my_error);
3288 return g_file_make_directory (file, cancellable, error);
3292 * g_file_make_symbolic_link:
3293 * @file: input #GFile.
3294 * @symlink_value: a string with the value of the new symlink.
3295 * @cancellable: optional #GCancellable object, %NULL to ignore.
3296 * @error: a #GError.
3298 * Creates a symbolic link.
3300 * If @cancellable is not %NULL, then the operation can be cancelled by
3301 * triggering the cancellable object from another thread. If the operation
3302 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3304 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
3307 g_file_make_symbolic_link (GFile *file,
3308 const char *symlink_value,
3309 GCancellable *cancellable,
3314 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3315 g_return_val_if_fail (symlink_value != NULL, FALSE);
3317 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3320 if (*symlink_value == '\0')
3322 g_set_error_literal (error, G_IO_ERROR,
3323 G_IO_ERROR_INVALID_ARGUMENT,
3324 _("Invalid symlink value given"));
3328 iface = G_FILE_GET_IFACE (file);
3330 if (iface->make_symbolic_link == NULL)
3332 g_set_error_literal (error, G_IO_ERROR,
3333 G_IO_ERROR_NOT_SUPPORTED,
3334 _("Operation not supported"));
3338 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
3343 * @file: input #GFile.
3344 * @cancellable: optional #GCancellable object, %NULL to ignore.
3345 * @error: a #GError, or %NULL
3347 * Deletes a file. If the @file is a directory, it will only be deleted if it
3350 * If @cancellable is not %NULL, then the operation can be cancelled by
3351 * triggering the cancellable object from another thread. If the operation
3352 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3354 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
3357 g_file_delete (GFile *file,
3358 GCancellable *cancellable,
3363 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3365 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3368 iface = G_FILE_GET_IFACE (file);
3370 if (iface->delete_file == NULL)
3372 g_set_error_literal (error, G_IO_ERROR,
3373 G_IO_ERROR_NOT_SUPPORTED,
3374 _("Operation not supported"));
3378 return (* iface->delete_file) (file, cancellable, error);
3383 * @file: #GFile to send to trash.
3384 * @cancellable: optional #GCancellable object, %NULL to ignore.
3385 * @error: a #GError, or %NULL
3387 * Sends @file to the "Trashcan", if possible. This is similar to
3388 * deleting it, but the user can recover it before emptying the trashcan.
3389 * Not all file systems support trashing, so this call can return the
3390 * %G_IO_ERROR_NOT_SUPPORTED error.
3393 * If @cancellable is not %NULL, then the operation can be cancelled by
3394 * triggering the cancellable object from another thread. If the operation
3395 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3397 * Returns: %TRUE on successful trash, %FALSE otherwise.
3400 g_file_trash (GFile *file,
3401 GCancellable *cancellable,
3406 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3408 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3411 iface = G_FILE_GET_IFACE (file);
3413 if (iface->trash == NULL)
3415 g_set_error_literal (error,
3416 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
3417 _("Trash not supported"));
3421 return (* iface->trash) (file, cancellable, error);
3425 * g_file_set_display_name:
3426 * @file: input #GFile.
3427 * @display_name: a string.
3428 * @cancellable: optional #GCancellable object, %NULL to ignore.
3429 * @error: a #GError, or %NULL
3431 * Renames @file to the specified display name.
3433 * The display name is converted from UTF8 to the correct encoding for the target
3434 * filesystem if possible and the @file is renamed to this.
3436 * If you want to implement a rename operation in the user interface the edit name
3437 * (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the initial value in the rename
3438 * widget, and then the result after editing should be passed to g_file_set_display_name().
3440 * On success the resulting converted filename is returned.
3442 * If @cancellable is not %NULL, then the operation can be cancelled by
3443 * triggering the cancellable object from another thread. If the operation
3444 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3446 * Returns: a #GFile specifying what @file was renamed to, or %NULL
3447 * if there was an error.
3448 * Free the returned object with g_object_unref().
3451 g_file_set_display_name (GFile *file,
3452 const char *display_name,
3453 GCancellable *cancellable,
3458 g_return_val_if_fail (G_IS_FILE (file), NULL);
3459 g_return_val_if_fail (display_name != NULL, NULL);
3461 if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
3465 G_IO_ERROR_INVALID_ARGUMENT,
3466 _("File names cannot contain '%c'"), G_DIR_SEPARATOR);
3470 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3473 iface = G_FILE_GET_IFACE (file);
3475 return (* iface->set_display_name) (file, display_name, cancellable, error);
3479 * g_file_set_display_name_async:
3480 * @file: input #GFile.
3481 * @display_name: a string.
3482 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3484 * @cancellable: optional #GCancellable object, %NULL to ignore.
3485 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
3486 * @user_data: the data to pass to callback function
3488 * Asynchronously sets the display name for a given #GFile.
3490 * For more details, see g_file_set_display_name() which is
3491 * the synchronous version of this call.
3493 * When the operation is finished, @callback will be called. You can then call
3494 * g_file_set_display_name_finish() to get the result of the operation.
3497 g_file_set_display_name_async (GFile *file,
3498 const char *display_name,
3500 GCancellable *cancellable,
3501 GAsyncReadyCallback callback,
3506 g_return_if_fail (G_IS_FILE (file));
3507 g_return_if_fail (display_name != NULL);
3509 iface = G_FILE_GET_IFACE (file);
3510 (* iface->set_display_name_async) (file,
3519 * g_file_set_display_name_finish:
3520 * @file: input #GFile.
3521 * @res: a #GAsyncResult.
3522 * @error: a #GError, or %NULL
3524 * Finishes setting a display name started with
3525 * g_file_set_display_name_async().
3527 * Returns: a #GFile or %NULL on error.
3528 * Free the returned object with g_object_unref().
3531 g_file_set_display_name_finish (GFile *file,
3537 g_return_val_if_fail (G_IS_FILE (file), NULL);
3538 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
3540 if (G_IS_SIMPLE_ASYNC_RESULT (res))
3542 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3543 if (g_simple_async_result_propagate_error (simple, error))
3547 iface = G_FILE_GET_IFACE (file);
3548 return (* iface->set_display_name_finish) (file, res, error);
3552 * g_file_query_settable_attributes:
3553 * @file: input #GFile.
3554 * @cancellable: optional #GCancellable object, %NULL to ignore.
3555 * @error: a #GError, or %NULL
3557 * Obtain the list of settable attributes for the file.
3559 * Returns the type and full attribute name of all the attributes
3560 * that can be set on this file. This doesn't mean setting it will always
3561 * succeed though, you might get an access failure, or some specific
3562 * file may not support a specific attribute.
3564 * If @cancellable is not %NULL, then the operation can be cancelled by
3565 * triggering the cancellable object from another thread. If the operation
3566 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3568 * Returns: a #GFileAttributeInfoList describing the settable attributes.
3569 * When you are done with it, release it with g_file_attribute_info_list_unref()
3571 GFileAttributeInfoList *
3572 g_file_query_settable_attributes (GFile *file,
3573 GCancellable *cancellable,
3578 GFileAttributeInfoList *list;
3580 g_return_val_if_fail (G_IS_FILE (file), NULL);
3582 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3585 iface = G_FILE_GET_IFACE (file);
3587 if (iface->query_settable_attributes == NULL)
3588 return g_file_attribute_info_list_new ();
3591 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
3595 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
3597 list = g_file_attribute_info_list_new ();
3598 g_error_free (my_error);
3601 g_propagate_error (error, my_error);
3608 * g_file_query_writable_namespaces:
3609 * @file: input #GFile.
3610 * @cancellable: optional #GCancellable object, %NULL to ignore.
3611 * @error: a #GError, or %NULL
3613 * Obtain the list of attribute namespaces where new attributes
3614 * can be created by a user. An example of this is extended
3615 * attributes (in the "xattr" namespace).
3617 * If @cancellable is not %NULL, then the operation can be cancelled by
3618 * triggering the cancellable object from another thread. If the operation
3619 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3621 * Returns: a #GFileAttributeInfoList describing the writable namespaces.
3622 * When you are done with it, release it with g_file_attribute_info_list_unref()
3624 GFileAttributeInfoList *
3625 g_file_query_writable_namespaces (GFile *file,
3626 GCancellable *cancellable,
3631 GFileAttributeInfoList *list;
3633 g_return_val_if_fail (G_IS_FILE (file), NULL);
3635 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3638 iface = G_FILE_GET_IFACE (file);
3640 if (iface->query_writable_namespaces == NULL)
3641 return g_file_attribute_info_list_new ();
3644 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
3648 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
3650 list = g_file_attribute_info_list_new ();
3651 g_error_free (my_error);
3654 g_propagate_error (error, my_error);
3661 * g_file_set_attribute:
3662 * @file: input #GFile.
3663 * @attribute: a string containing the attribute's name.
3664 * @type: The type of the attribute
3665 * @value_p: a pointer to the value (or the pointer itself if the type is a pointer type)
3666 * @flags: a set of #GFileQueryInfoFlags.
3667 * @cancellable: optional #GCancellable object, %NULL to ignore.
3668 * @error: a #GError, or %NULL
3670 * Sets an attribute in the file with attribute name @attribute to @value.
3672 * If @cancellable is not %NULL, then the operation can be cancelled by
3673 * triggering the cancellable object from another thread. If the operation
3674 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3676 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
3679 g_file_set_attribute (GFile *file,
3680 const char *attribute,
3681 GFileAttributeType type,
3683 GFileQueryInfoFlags flags,
3684 GCancellable *cancellable,
3689 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3690 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
3692 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3695 iface = G_FILE_GET_IFACE (file);
3697 if (iface->set_attribute == NULL)
3699 g_set_error_literal (error, G_IO_ERROR,
3700 G_IO_ERROR_NOT_SUPPORTED,
3701 _("Operation not supported"));
3705 return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
3709 * g_file_set_attributes_from_info:
3710 * @file: input #GFile.
3711 * @info: a #GFileInfo.
3712 * @flags: #GFileQueryInfoFlags
3713 * @cancellable: optional #GCancellable object, %NULL to ignore.
3714 * @error: a #GError, or %NULL
3716 * Tries to set all attributes in the #GFileInfo on the target values,
3717 * not stopping on the first error.
3719 * If there is any error during this operation then @error will be set to
3720 * the first error. Error on particular fields are flagged by setting
3721 * the "status" field in the attribute value to
3722 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can also detect
3725 * If @cancellable is not %NULL, then the operation can be cancelled by
3726 * triggering the cancellable object from another thread. If the operation
3727 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3729 * Returns: %TRUE if there was any error, %FALSE otherwise.
3732 g_file_set_attributes_from_info (GFile *file,
3734 GFileQueryInfoFlags flags,
3735 GCancellable *cancellable,
3740 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3741 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
3743 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3746 g_file_info_clear_status (info);
3748 iface = G_FILE_GET_IFACE (file);
3750 return (* iface->set_attributes_from_info) (file,
3759 g_file_real_set_attributes_from_info (GFile *file,
3761 GFileQueryInfoFlags flags,
3762 GCancellable *cancellable,
3768 GFileAttributeValue *value;
3772 attributes = g_file_info_list_attributes (info, NULL);
3774 for (i = 0; attributes[i] != NULL; i++)
3776 value = _g_file_info_get_attribute_value (info, attributes[i]);
3778 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
3781 if (!g_file_set_attribute (file, attributes[i],
3782 value->type, _g_file_attribute_value_peek_as_pointer (value),
3783 flags, cancellable, error))
3785 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
3787 /* Don't set error multiple times */
3791 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
3794 g_strfreev (attributes);
3800 * g_file_set_attributes_async:
3801 * @file: input #GFile.
3802 * @info: a #GFileInfo.
3803 * @flags: a #GFileQueryInfoFlags.
3804 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3806 * @cancellable: optional #GCancellable object, %NULL to ignore.
3807 * @callback: a #GAsyncReadyCallback.
3808 * @user_data: a #gpointer.
3810 * Asynchronously sets the attributes of @file with @info.
3812 * For more details, see g_file_set_attributes_from_info() which is
3813 * the synchronous version of this call.
3815 * When the operation is finished, @callback will be called. You can then call
3816 * g_file_set_attributes_finish() to get the result of the operation.
3819 g_file_set_attributes_async (GFile *file,
3821 GFileQueryInfoFlags flags,
3823 GCancellable *cancellable,
3824 GAsyncReadyCallback callback,
3829 g_return_if_fail (G_IS_FILE (file));
3830 g_return_if_fail (G_IS_FILE_INFO (info));
3832 iface = G_FILE_GET_IFACE (file);
3833 (* iface->set_attributes_async) (file,
3843 * g_file_set_attributes_finish:
3844 * @file: input #GFile.
3845 * @result: a #GAsyncResult.
3846 * @info: a #GFileInfo.
3847 * @error: a #GError, or %NULL
3849 * Finishes setting an attribute started in g_file_set_attributes_async().
3851 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
3854 g_file_set_attributes_finish (GFile *file,
3855 GAsyncResult *result,
3861 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3862 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3864 /* No standard handling of errors here, as we must set info even
3867 iface = G_FILE_GET_IFACE (file);
3868 return (* iface->set_attributes_finish) (file, result, info, error);
3872 * g_file_set_attribute_string:
3873 * @file: input #GFile.
3874 * @attribute: a string containing the attribute's name.
3875 * @value: a string containing the attribute's value.
3876 * @flags: #GFileQueryInfoFlags.
3877 * @cancellable: optional #GCancellable object, %NULL to ignore.
3878 * @error: a #GError, or %NULL
3880 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
3881 * If @attribute is of a different type, this operation will fail.
3883 * If @cancellable is not %NULL, then the operation can be cancelled by
3884 * triggering the cancellable object from another thread. If the operation
3885 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3887 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
3890 g_file_set_attribute_string (GFile *file,
3891 const char *attribute,
3893 GFileQueryInfoFlags flags,
3894 GCancellable *cancellable,
3897 return g_file_set_attribute (file, attribute,
3898 G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
3899 flags, cancellable, error);
3903 * g_file_set_attribute_byte_string:
3904 * @file: input #GFile.
3905 * @attribute: a string containing the attribute's name.
3906 * @value: a string containing the attribute's new value.
3907 * @flags: a #GFileQueryInfoFlags.
3908 * @cancellable: optional #GCancellable object, %NULL to ignore.
3909 * @error: a #GError, or %NULL
3911 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
3912 * If @attribute is of a different type, this operation will fail,
3915 * If @cancellable is not %NULL, then the operation can be cancelled by
3916 * triggering the cancellable object from another thread. If the operation
3917 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3919 * Returns: %TRUE if the @attribute was successfully set to @value
3920 * in the @file, %FALSE otherwise.
3923 g_file_set_attribute_byte_string (GFile *file,
3924 const char *attribute,
3926 GFileQueryInfoFlags flags,
3927 GCancellable *cancellable,
3930 return g_file_set_attribute (file, attribute,
3931 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
3932 flags, cancellable, error);
3936 * g_file_set_attribute_uint32:
3937 * @file: input #GFile.
3938 * @attribute: a string containing the attribute's name.
3939 * @value: a #guint32 containing the attribute's new value.
3940 * @flags: a #GFileQueryInfoFlags.
3941 * @cancellable: optional #GCancellable object, %NULL to ignore.
3942 * @error: a #GError, or %NULL
3944 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
3945 * If @attribute is of a different type, this operation will fail.
3947 * If @cancellable is not %NULL, then the operation can be cancelled by
3948 * triggering the cancellable object from another thread. If the operation
3949 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3951 * Returns: %TRUE if the @attribute was successfully set to @value
3952 * in the @file, %FALSE otherwise.
3955 g_file_set_attribute_uint32 (GFile *file,
3956 const char *attribute,
3958 GFileQueryInfoFlags flags,
3959 GCancellable *cancellable,
3962 return g_file_set_attribute (file, attribute,
3963 G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
3964 flags, cancellable, error);
3968 * g_file_set_attribute_int32:
3969 * @file: input #GFile.
3970 * @attribute: a string containing the attribute's name.
3971 * @value: a #gint32 containing the attribute's new value.
3972 * @flags: a #GFileQueryInfoFlags.
3973 * @cancellable: optional #GCancellable object, %NULL to ignore.
3974 * @error: a #GError, or %NULL
3976 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
3977 * If @attribute is of a different type, this operation will fail.
3979 * If @cancellable is not %NULL, then the operation can be cancelled by
3980 * triggering the cancellable object from another thread. If the operation
3981 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3983 * Returns: %TRUE if the @attribute was successfully set to @value
3984 * in the @file, %FALSE otherwise.
3987 g_file_set_attribute_int32 (GFile *file,
3988 const char *attribute,
3990 GFileQueryInfoFlags flags,
3991 GCancellable *cancellable,
3994 return g_file_set_attribute (file, attribute,
3995 G_FILE_ATTRIBUTE_TYPE_INT32, &value,
3996 flags, cancellable, error);
4000 * g_file_set_attribute_uint64:
4001 * @file: input #GFile.
4002 * @attribute: a string containing the attribute's name.
4003 * @value: a #guint64 containing the attribute's new value.
4004 * @flags: a #GFileQueryInfoFlags.
4005 * @cancellable: optional #GCancellable object, %NULL to ignore.
4006 * @error: a #GError, or %NULL
4008 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
4009 * If @attribute is of a different type, this operation will fail.
4011 * If @cancellable is not %NULL, then the operation can be cancelled by
4012 * triggering the cancellable object from another thread. If the operation
4013 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4015 * Returns: %TRUE if the @attribute was successfully set to @value
4016 * in the @file, %FALSE otherwise.
4019 g_file_set_attribute_uint64 (GFile *file,
4020 const char *attribute,
4022 GFileQueryInfoFlags flags,
4023 GCancellable *cancellable,
4026 return g_file_set_attribute (file, attribute,
4027 G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
4028 flags, cancellable, error);
4032 * g_file_set_attribute_int64:
4033 * @file: input #GFile.
4034 * @attribute: a string containing the attribute's name.
4035 * @value: a #guint64 containing the attribute's new value.
4036 * @flags: a #GFileQueryInfoFlags.
4037 * @cancellable: optional #GCancellable object, %NULL to ignore.
4038 * @error: a #GError, or %NULL
4040 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
4041 * If @attribute is of a different type, this operation will fail.
4043 * If @cancellable is not %NULL, then the operation can be cancelled by
4044 * triggering the cancellable object from another thread. If the operation
4045 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4047 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4050 g_file_set_attribute_int64 (GFile *file,
4051 const char *attribute,
4053 GFileQueryInfoFlags flags,
4054 GCancellable *cancellable,
4057 return g_file_set_attribute (file, attribute,
4058 G_FILE_ATTRIBUTE_TYPE_INT64, &value,
4059 flags, cancellable, error);
4063 * g_file_mount_mountable:
4064 * @file: input #GFile.
4065 * @flags: flags affecting the operation
4066 * @mount_operation: a #GMountOperation, or %NULL to avoid user interaction.
4067 * @cancellable: optional #GCancellable object, %NULL to ignore.
4068 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4069 * @user_data: the data to pass to callback function
4071 * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
4072 * Using @mount_operation, you can request callbacks when, for instance,
4073 * passwords are needed during authentication.
4075 * If @cancellable is not %NULL, then the operation can be cancelled by
4076 * triggering the cancellable object from another thread. If the operation
4077 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4079 * When the operation is finished, @callback will be called. You can then call
4080 * g_file_mount_mountable_finish() to get the result of the operation.
4083 g_file_mount_mountable (GFile *file,
4084 GMountMountFlags flags,
4085 GMountOperation *mount_operation,
4086 GCancellable *cancellable,
4087 GAsyncReadyCallback callback,
4092 g_return_if_fail (G_IS_FILE (file));
4094 iface = G_FILE_GET_IFACE (file);
4096 if (iface->mount_mountable == NULL)
4098 g_simple_async_report_error_in_idle (G_OBJECT (file),
4102 G_IO_ERROR_NOT_SUPPORTED,
4103 _("Operation not supported"));
4107 (* iface->mount_mountable) (file,
4116 * g_file_mount_mountable_finish:
4117 * @file: input #GFile.
4118 * @result: a #GAsyncResult.
4119 * @error: a #GError, or %NULL
4121 * Finishes a mount operation. See g_file_mount_mountable() for details.
4123 * Finish an asynchronous mount operation that was started
4124 * with g_file_mount_mountable().
4126 * Returns: a #GFile or %NULL on error.
4127 * Free the returned object with g_object_unref().
4130 g_file_mount_mountable_finish (GFile *file,
4131 GAsyncResult *result,
4136 g_return_val_if_fail (G_IS_FILE (file), NULL);
4137 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
4139 if (G_IS_SIMPLE_ASYNC_RESULT (result))
4141 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
4142 if (g_simple_async_result_propagate_error (simple, error))
4146 iface = G_FILE_GET_IFACE (file);
4147 return (* iface->mount_mountable_finish) (file, result, error);
4151 * g_file_unmount_mountable:
4152 * @file: input #GFile.
4153 * @flags: flags affecting the operation
4154 * @cancellable: optional #GCancellable object, %NULL to ignore.
4155 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4156 * @user_data: the data to pass to callback function
4158 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
4160 * If @cancellable is not %NULL, then the operation can be cancelled by
4161 * triggering the cancellable object from another thread. If the operation
4162 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4164 * When the operation is finished, @callback will be called. You can then call
4165 * g_file_unmount_mountable_finish() to get the result of the operation.
4168 g_file_unmount_mountable (GFile *file,
4169 GMountUnmountFlags flags,
4170 GCancellable *cancellable,
4171 GAsyncReadyCallback callback,
4176 g_return_if_fail (G_IS_FILE (file));
4178 iface = G_FILE_GET_IFACE (file);
4180 if (iface->unmount_mountable == NULL)
4182 g_simple_async_report_error_in_idle (G_OBJECT (file),
4186 G_IO_ERROR_NOT_SUPPORTED,
4187 _("Operation not supported"));
4191 (* iface->unmount_mountable) (file,
4199 * g_file_unmount_mountable_finish:
4200 * @file: input #GFile.
4201 * @result: a #GAsyncResult.
4202 * @error: a #GError, or %NULL
4204 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
4206 * Finish an asynchronous unmount operation that was started
4207 * with g_file_unmount_mountable().
4209 * Returns: %TRUE if the operation finished successfully. %FALSE
4213 g_file_unmount_mountable_finish (GFile *file,
4214 GAsyncResult *result,
4219 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4220 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4222 if (G_IS_SIMPLE_ASYNC_RESULT (result))
4224 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
4225 if (g_simple_async_result_propagate_error (simple, error))
4229 iface = G_FILE_GET_IFACE (file);
4230 return (* iface->unmount_mountable_finish) (file, result, error);
4234 * g_file_eject_mountable:
4235 * @file: input #GFile.
4236 * @flags: flags affecting the operation
4237 * @cancellable: optional #GCancellable object, %NULL to ignore.
4238 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4239 * @user_data: the data to pass to callback function
4241 * Starts an asynchronous eject on a mountable.
4242 * When this operation has completed, @callback will be called with
4243 * @user_user data, and the operation can be finalized with
4244 * g_file_eject_mountable_finish().
4246 * If @cancellable is not %NULL, then the operation can be cancelled by
4247 * triggering the cancellable object from another thread. If the operation
4248 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4251 g_file_eject_mountable (GFile *file,
4252 GMountUnmountFlags flags,
4253 GCancellable *cancellable,
4254 GAsyncReadyCallback callback,
4259 g_return_if_fail (G_IS_FILE (file));
4261 iface = G_FILE_GET_IFACE (file);
4263 if (iface->eject_mountable == NULL)
4265 g_simple_async_report_error_in_idle (G_OBJECT (file),
4269 G_IO_ERROR_NOT_SUPPORTED,
4270 _("Operation not supported"));
4274 (* iface->eject_mountable) (file,
4282 * g_file_eject_mountable_finish:
4283 * @file: input #GFile.
4284 * @result: a #GAsyncResult.
4285 * @error: a #GError, or %NULL
4287 * Finishes an asynchronous eject operation started by
4288 * g_file_eject_mountable().
4290 * Returns: %TRUE if the @file was ejected successfully. %FALSE
4294 g_file_eject_mountable_finish (GFile *file,
4295 GAsyncResult *result,
4300 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4301 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4303 if (G_IS_SIMPLE_ASYNC_RESULT (result))
4305 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
4306 if (g_simple_async_result_propagate_error (simple, error))
4310 iface = G_FILE_GET_IFACE (file);
4311 return (* iface->eject_mountable_finish) (file, result, error);
4315 * g_file_monitor_directory:
4316 * @file: input #GFile.
4317 * @flags: a set of #GFileMonitorFlags.
4318 * @cancellable: optional #GCancellable object, %NULL to ignore.
4319 * @error: a #GError, or %NULL.
4321 * Obtains a directory monitor for the given file.
4322 * This may fail if directory monitoring is not supported.
4324 * If @cancellable is not %NULL, then the operation can be cancelled by
4325 * triggering the cancellable object from another thread. If the operation
4326 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4328 * Returns: a #GFileMonitor for the given @file, or %NULL on error.
4329 * Free the returned object with g_object_unref().
4332 g_file_monitor_directory (GFile *file,
4333 GFileMonitorFlags flags,
4334 GCancellable *cancellable,
4339 g_return_val_if_fail (G_IS_FILE (file), NULL);
4341 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4344 iface = G_FILE_GET_IFACE (file);
4346 if (iface->monitor_dir == NULL)
4348 g_set_error_literal (error, G_IO_ERROR,
4349 G_IO_ERROR_NOT_SUPPORTED,
4350 _("Operation not supported"));
4354 return (* iface->monitor_dir) (file, flags, cancellable, error);
4358 * g_file_monitor_file:
4359 * @file: input #GFile.
4360 * @flags: a set of #GFileMonitorFlags.
4361 * @cancellable: optional #GCancellable object, %NULL to ignore.
4362 * @error: a #GError, or %NULL.
4364 * Obtains a file monitor for the given file. If no file notification
4365 * mechanism exists, then regular polling of the file is used.
4367 * If @cancellable is not %NULL, then the operation can be cancelled by
4368 * triggering the cancellable object from another thread. If the operation
4369 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4371 * Returns: a #GFileMonitor for the given @file, or %NULL on error.
4372 * Free the returned object with g_object_unref().
4375 g_file_monitor_file (GFile *file,
4376 GFileMonitorFlags flags,
4377 GCancellable *cancellable,
4381 GFileMonitor *monitor;
4383 g_return_val_if_fail (G_IS_FILE (file), NULL);
4385 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4388 iface = G_FILE_GET_IFACE (file);
4392 if (iface->monitor_file)
4393 monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
4395 /* Fallback to polling */
4396 if (monitor == NULL)
4397 monitor = _g_poll_file_monitor_new (file);
4404 * @file: input #GFile
4405 * @flags: a set of #GFileMonitorFlags
4406 * @cancellable: optional #GCancellable object, %NULL to ignore
4407 * @error: a #GError, or %NULL
4409 * Obtains a file or directory monitor for the given file, depending
4410 * on the type of the file.
4412 * If @cancellable is not %NULL, then the operation can be cancelled by
4413 * triggering the cancellable object from another thread. If the operation
4414 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4416 * Returns: a #GFileMonitor for the given @file, or %NULL on error.
4417 * Free the returned object with g_object_unref().
4422 g_file_monitor (GFile *file,
4423 GFileMonitorFlags flags,
4424 GCancellable *cancellable,
4427 if (g_file_query_file_type (file, 0, cancellable) == G_FILE_TYPE_DIRECTORY)
4428 return g_file_monitor_directory (file, flags, cancellable, error);
4430 return g_file_monitor_file (file, flags, cancellable, error);
4433 /********************************************
4434 * Default implementation of async ops *
4435 ********************************************/
4439 GFileQueryInfoFlags flags;
4441 } QueryInfoAsyncData;
4444 query_info_data_free (QueryInfoAsyncData *data)
4447 g_object_unref (data->info);
4448 g_free (data->attributes);
4453 query_info_async_thread (GSimpleAsyncResult *res,
4455 GCancellable *cancellable)
4457 GError *error = NULL;
4458 QueryInfoAsyncData *data;
4461 data = g_simple_async_result_get_op_res_gpointer (res);
4463 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
4467 g_simple_async_result_set_from_error (res, error);
4468 g_error_free (error);
4475 g_file_real_query_info_async (GFile *file,
4476 const char *attributes,
4477 GFileQueryInfoFlags flags,
4479 GCancellable *cancellable,
4480 GAsyncReadyCallback callback,
4483 GSimpleAsyncResult *res;
4484 QueryInfoAsyncData *data;
4486 data = g_new0 (QueryInfoAsyncData, 1);
4487 data->attributes = g_strdup (attributes);
4488 data->flags = flags;
4490 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_info_async);
4491 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
4493 g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
4494 g_object_unref (res);
4498 g_file_real_query_info_finish (GFile *file,
4502 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4503 QueryInfoAsyncData *data;
4505 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_info_async);
4507 data = g_simple_async_result_get_op_res_gpointer (simple);
4509 return g_object_ref (data->info);
4517 } QueryFilesystemInfoAsyncData;
4520 query_filesystem_info_data_free (QueryFilesystemInfoAsyncData *data)
4523 g_object_unref (data->info);
4524 g_free (data->attributes);
4529 query_filesystem_info_async_thread (GSimpleAsyncResult *res,
4531 GCancellable *cancellable)
4533 GError *error = NULL;
4534 QueryFilesystemInfoAsyncData *data;
4537 data = g_simple_async_result_get_op_res_gpointer (res);
4539 info = g_file_query_filesystem_info (G_FILE (object), data->attributes, cancellable, &error);
4543 g_simple_async_result_set_from_error (res, error);
4544 g_error_free (error);
4551 g_file_real_query_filesystem_info_async (GFile *file,
4552 const char *attributes,
4554 GCancellable *cancellable,
4555 GAsyncReadyCallback callback,
4558 GSimpleAsyncResult *res;
4559 QueryFilesystemInfoAsyncData *data;
4561 data = g_new0 (QueryFilesystemInfoAsyncData, 1);
4562 data->attributes = g_strdup (attributes);
4564 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_filesystem_info_async);
4565 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_filesystem_info_data_free);
4567 g_simple_async_result_run_in_thread (res, query_filesystem_info_async_thread, io_priority, cancellable);
4568 g_object_unref (res);
4572 g_file_real_query_filesystem_info_finish (GFile *file,
4576 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4577 QueryFilesystemInfoAsyncData *data;
4579 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_filesystem_info_async);
4581 data = g_simple_async_result_get_op_res_gpointer (simple);
4583 return g_object_ref (data->info);
4590 GFileQueryInfoFlags flags;
4591 GFileEnumerator *enumerator;
4592 } EnumerateChildrenAsyncData;
4595 enumerate_children_data_free (EnumerateChildrenAsyncData *data)
4597 if (data->enumerator)
4598 g_object_unref (data->enumerator);
4599 g_free (data->attributes);
4604 enumerate_children_async_thread (GSimpleAsyncResult *res,
4606 GCancellable *cancellable)
4608 GError *error = NULL;
4609 EnumerateChildrenAsyncData *data;
4610 GFileEnumerator *enumerator;
4612 data = g_simple_async_result_get_op_res_gpointer (res);
4614 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
4616 if (enumerator == NULL)
4618 g_simple_async_result_set_from_error (res, error);
4619 g_error_free (error);
4622 data->enumerator = enumerator;
4626 g_file_real_enumerate_children_async (GFile *file,
4627 const char *attributes,
4628 GFileQueryInfoFlags flags,
4630 GCancellable *cancellable,
4631 GAsyncReadyCallback callback,
4634 GSimpleAsyncResult *res;
4635 EnumerateChildrenAsyncData *data;
4637 data = g_new0 (EnumerateChildrenAsyncData, 1);
4638 data->attributes = g_strdup (attributes);
4639 data->flags = flags;
4641 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_enumerate_children_async);
4642 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)enumerate_children_data_free);
4644 g_simple_async_result_run_in_thread (res, enumerate_children_async_thread, io_priority, cancellable);
4645 g_object_unref (res);
4648 static GFileEnumerator *
4649 g_file_real_enumerate_children_finish (GFile *file,
4653 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4654 EnumerateChildrenAsyncData *data;
4656 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_enumerate_children_async);
4658 data = g_simple_async_result_get_op_res_gpointer (simple);
4659 if (data->enumerator)
4660 return g_object_ref (data->enumerator);
4666 open_read_async_thread (GSimpleAsyncResult *res,
4668 GCancellable *cancellable)
4671 GFileInputStream *stream;
4672 GError *error = NULL;
4674 iface = G_FILE_GET_IFACE (object);
4676 if (iface->read_fn == NULL)
4678 g_set_error_literal (&error, G_IO_ERROR,
4679 G_IO_ERROR_NOT_SUPPORTED,
4680 _("Operation not supported"));
4682 g_simple_async_result_set_from_error (res, error);
4683 g_error_free (error);
4688 stream = iface->read_fn (G_FILE (object), cancellable, &error);
4692 g_simple_async_result_set_from_error (res, error);
4693 g_error_free (error);
4696 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4700 g_file_real_read_async (GFile *file,
4702 GCancellable *cancellable,
4703 GAsyncReadyCallback callback,
4706 GSimpleAsyncResult *res;
4708 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_read_async);
4710 g_simple_async_result_run_in_thread (res, open_read_async_thread, io_priority, cancellable);
4711 g_object_unref (res);
4714 static GFileInputStream *
4715 g_file_real_read_finish (GFile *file,
4719 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4722 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_read_async);
4724 op = g_simple_async_result_get_op_res_gpointer (simple);
4726 return g_object_ref (op);
4732 append_to_async_thread (GSimpleAsyncResult *res,
4734 GCancellable *cancellable)
4737 GFileCreateFlags *data;
4738 GFileOutputStream *stream;
4739 GError *error = NULL;
4741 iface = G_FILE_GET_IFACE (object);
4743 data = g_simple_async_result_get_op_res_gpointer (res);
4745 stream = iface->append_to (G_FILE (object), *data, cancellable, &error);
4749 g_simple_async_result_set_from_error (res, error);
4750 g_error_free (error);
4753 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4757 g_file_real_append_to_async (GFile *file,
4758 GFileCreateFlags flags,
4760 GCancellable *cancellable,
4761 GAsyncReadyCallback callback,
4764 GFileCreateFlags *data;
4765 GSimpleAsyncResult *res;
4767 data = g_new0 (GFileCreateFlags, 1);
4770 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_append_to_async);
4771 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
4773 g_simple_async_result_run_in_thread (res, append_to_async_thread, io_priority, cancellable);
4774 g_object_unref (res);
4777 static GFileOutputStream *
4778 g_file_real_append_to_finish (GFile *file,
4782 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4785 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_append_to_async);
4787 op = g_simple_async_result_get_op_res_gpointer (simple);
4789 return g_object_ref (op);
4795 create_async_thread (GSimpleAsyncResult *res,
4797 GCancellable *cancellable)
4800 GFileCreateFlags *data;
4801 GFileOutputStream *stream;
4802 GError *error = NULL;
4804 iface = G_FILE_GET_IFACE (object);
4806 data = g_simple_async_result_get_op_res_gpointer (res);
4808 stream = iface->create (G_FILE (object), *data, cancellable, &error);
4812 g_simple_async_result_set_from_error (res, error);
4813 g_error_free (error);
4816 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4820 g_file_real_create_async (GFile *file,
4821 GFileCreateFlags flags,
4823 GCancellable *cancellable,
4824 GAsyncReadyCallback callback,
4827 GFileCreateFlags *data;
4828 GSimpleAsyncResult *res;
4830 data = g_new0 (GFileCreateFlags, 1);
4833 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_create_async);
4834 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
4836 g_simple_async_result_run_in_thread (res, create_async_thread, io_priority, cancellable);
4837 g_object_unref (res);
4840 static GFileOutputStream *
4841 g_file_real_create_finish (GFile *file,
4845 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4848 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_create_async);
4850 op = g_simple_async_result_get_op_res_gpointer (simple);
4852 return g_object_ref (op);
4858 GFileOutputStream *stream;
4860 gboolean make_backup;
4861 GFileCreateFlags flags;
4865 replace_async_data_free (ReplaceAsyncData *data)
4868 g_object_unref (data->stream);
4869 g_free (data->etag);
4874 replace_async_thread (GSimpleAsyncResult *res,
4876 GCancellable *cancellable)
4879 GFileOutputStream *stream;
4880 GError *error = NULL;
4881 ReplaceAsyncData *data;
4883 iface = G_FILE_GET_IFACE (object);
4885 data = g_simple_async_result_get_op_res_gpointer (res);
4887 stream = iface->replace (G_FILE (object),
4896 g_simple_async_result_set_from_error (res, error);
4897 g_error_free (error);
4900 data->stream = stream;
4904 g_file_real_replace_async (GFile *file,
4906 gboolean make_backup,
4907 GFileCreateFlags flags,
4909 GCancellable *cancellable,
4910 GAsyncReadyCallback callback,
4913 GSimpleAsyncResult *res;
4914 ReplaceAsyncData *data;
4916 data = g_new0 (ReplaceAsyncData, 1);
4917 data->etag = g_strdup (etag);
4918 data->make_backup = make_backup;
4919 data->flags = flags;
4921 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_replace_async);
4922 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_async_data_free);
4924 g_simple_async_result_run_in_thread (res, replace_async_thread, io_priority, cancellable);
4925 g_object_unref (res);
4928 static GFileOutputStream *
4929 g_file_real_replace_finish (GFile *file,
4933 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4934 ReplaceAsyncData *data;
4936 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_replace_async);
4938 data = g_simple_async_result_get_op_res_gpointer (simple);
4940 return g_object_ref (data->stream);
4946 open_readwrite_async_thread (GSimpleAsyncResult *res,
4948 GCancellable *cancellable)
4951 GFileIOStream *stream;
4952 GError *error = NULL;
4954 iface = G_FILE_GET_IFACE (object);
4956 if (iface->open_readwrite == NULL)
4958 g_set_error_literal (&error, G_IO_ERROR,
4959 G_IO_ERROR_NOT_SUPPORTED,
4960 _("Operation not supported"));
4962 g_simple_async_result_set_from_error (res, error);
4963 g_error_free (error);
4968 stream = iface->open_readwrite (G_FILE (object), cancellable, &error);
4972 g_simple_async_result_set_from_error (res, error);
4973 g_error_free (error);
4976 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4980 g_file_real_open_readwrite_async (GFile *file,
4982 GCancellable *cancellable,
4983 GAsyncReadyCallback callback,
4986 GSimpleAsyncResult *res;
4988 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_open_readwrite_async);
4990 g_simple_async_result_run_in_thread (res, open_readwrite_async_thread, io_priority, cancellable);
4991 g_object_unref (res);
4994 static GFileIOStream *
4995 g_file_real_open_readwrite_finish (GFile *file,
4999 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5002 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_open_readwrite_async);
5004 op = g_simple_async_result_get_op_res_gpointer (simple);
5006 return g_object_ref (op);
5012 create_readwrite_async_thread (GSimpleAsyncResult *res,
5014 GCancellable *cancellable)
5017 GFileCreateFlags *data;
5018 GFileIOStream *stream;
5019 GError *error = NULL;
5021 iface = G_FILE_GET_IFACE (object);
5023 data = g_simple_async_result_get_op_res_gpointer (res);
5025 if (iface->create_readwrite == NULL)
5027 g_set_error_literal (&error, G_IO_ERROR,
5028 G_IO_ERROR_NOT_SUPPORTED,
5029 _("Operation not supported"));
5031 g_simple_async_result_set_from_error (res, error);
5032 g_error_free (error);
5037 stream = iface->create_readwrite (G_FILE (object), *data, cancellable, &error);
5041 g_simple_async_result_set_from_error (res, error);
5042 g_error_free (error);
5045 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
5049 g_file_real_create_readwrite_async (GFile *file,
5050 GFileCreateFlags flags,
5052 GCancellable *cancellable,
5053 GAsyncReadyCallback callback,
5056 GFileCreateFlags *data;
5057 GSimpleAsyncResult *res;
5059 data = g_new0 (GFileCreateFlags, 1);
5062 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_create_readwrite_async);
5063 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
5065 g_simple_async_result_run_in_thread (res, create_readwrite_async_thread, io_priority, cancellable);
5066 g_object_unref (res);
5069 static GFileIOStream *
5070 g_file_real_create_readwrite_finish (GFile *file,
5074 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5077 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_create_readwrite_async);
5079 op = g_simple_async_result_get_op_res_gpointer (simple);
5081 return g_object_ref (op);
5087 GFileIOStream *stream;
5089 gboolean make_backup;
5090 GFileCreateFlags flags;
5091 } ReplaceRWAsyncData;
5094 replace_rw_async_data_free (ReplaceRWAsyncData *data)
5097 g_object_unref (data->stream);
5098 g_free (data->etag);
5103 replace_readwrite_async_thread (GSimpleAsyncResult *res,
5105 GCancellable *cancellable)
5108 GFileIOStream *stream;
5109 GError *error = NULL;
5110 ReplaceRWAsyncData *data;
5112 iface = G_FILE_GET_IFACE (object);
5114 data = g_simple_async_result_get_op_res_gpointer (res);
5116 stream = iface->replace_readwrite (G_FILE (object),
5125 g_simple_async_result_set_from_error (res, error);
5126 g_error_free (error);
5129 data->stream = stream;
5133 g_file_real_replace_readwrite_async (GFile *file,
5135 gboolean make_backup,
5136 GFileCreateFlags flags,
5138 GCancellable *cancellable,
5139 GAsyncReadyCallback callback,
5142 GSimpleAsyncResult *res;
5143 ReplaceRWAsyncData *data;
5145 data = g_new0 (ReplaceRWAsyncData, 1);
5146 data->etag = g_strdup (etag);
5147 data->make_backup = make_backup;
5148 data->flags = flags;
5150 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_replace_readwrite_async);
5151 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_rw_async_data_free);
5153 g_simple_async_result_run_in_thread (res, replace_readwrite_async_thread, io_priority, cancellable);
5154 g_object_unref (res);
5157 static GFileIOStream *
5158 g_file_real_replace_readwrite_finish (GFile *file,
5162 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5163 ReplaceRWAsyncData *data;
5165 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_replace_readwrite_async);
5167 data = g_simple_async_result_get_op_res_gpointer (simple);
5169 return g_object_ref (data->stream);
5177 } SetDisplayNameAsyncData;
5180 set_display_name_data_free (SetDisplayNameAsyncData *data)
5182 g_free (data->name);
5184 g_object_unref (data->file);
5189 set_display_name_async_thread (GSimpleAsyncResult *res,
5191 GCancellable *cancellable)
5193 GError *error = NULL;
5194 SetDisplayNameAsyncData *data;
5197 data = g_simple_async_result_get_op_res_gpointer (res);
5199 file = g_file_set_display_name (G_FILE (object), data->name, cancellable, &error);
5203 g_simple_async_result_set_from_error (res, error);
5204 g_error_free (error);
5211 g_file_real_set_display_name_async (GFile *file,
5212 const char *display_name,
5214 GCancellable *cancellable,
5215 GAsyncReadyCallback callback,
5218 GSimpleAsyncResult *res;
5219 SetDisplayNameAsyncData *data;
5221 data = g_new0 (SetDisplayNameAsyncData, 1);
5222 data->name = g_strdup (display_name);
5224 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_display_name_async);
5225 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_display_name_data_free);
5227 g_simple_async_result_run_in_thread (res, set_display_name_async_thread, io_priority, cancellable);
5228 g_object_unref (res);
5232 g_file_real_set_display_name_finish (GFile *file,
5236 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5237 SetDisplayNameAsyncData *data;
5239 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_display_name_async);
5241 data = g_simple_async_result_get_op_res_gpointer (simple);
5243 return g_object_ref (data->file);
5249 GFileQueryInfoFlags flags;
5256 set_info_data_free (SetInfoAsyncData *data)
5259 g_object_unref (data->info);
5261 g_error_free (data->error);
5266 set_info_async_thread (GSimpleAsyncResult *res,
5268 GCancellable *cancellable)
5270 SetInfoAsyncData *data;
5272 data = g_simple_async_result_get_op_res_gpointer (res);
5275 data->res = g_file_set_attributes_from_info (G_FILE (object),
5283 g_file_real_set_attributes_async (GFile *file,
5285 GFileQueryInfoFlags flags,
5287 GCancellable *cancellable,
5288 GAsyncReadyCallback callback,
5291 GSimpleAsyncResult *res;
5292 SetInfoAsyncData *data;
5294 data = g_new0 (SetInfoAsyncData, 1);
5295 data->info = g_file_info_dup (info);
5296 data->flags = flags;
5298 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_attributes_async);
5299 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_info_data_free);
5301 g_simple_async_result_run_in_thread (res, set_info_async_thread, io_priority, cancellable);
5302 g_object_unref (res);
5306 g_file_real_set_attributes_finish (GFile *file,
5311 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5312 SetInfoAsyncData *data;
5314 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_attributes_async);
5316 data = g_simple_async_result_get_op_res_gpointer (simple);
5319 *info = g_object_ref (data->info);
5321 if (error != NULL && data->error)
5322 *error = g_error_copy (data->error);
5328 find_enclosing_mount_async_thread (GSimpleAsyncResult *res,
5330 GCancellable *cancellable)
5332 GError *error = NULL;
5335 mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
5339 g_simple_async_result_set_from_error (res, error);
5340 g_error_free (error);
5343 g_simple_async_result_set_op_res_gpointer (res, mount, (GDestroyNotify)g_object_unref);
5347 g_file_real_find_enclosing_mount_async (GFile *file,
5349 GCancellable *cancellable,
5350 GAsyncReadyCallback callback,
5353 GSimpleAsyncResult *res;
5355 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_find_enclosing_mount_async);
5357 g_simple_async_result_run_in_thread (res, find_enclosing_mount_async_thread, io_priority, cancellable);
5358 g_object_unref (res);
5362 g_file_real_find_enclosing_mount_finish (GFile *file,
5366 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5369 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_find_enclosing_mount_async);
5371 mount = g_simple_async_result_get_op_res_gpointer (simple);
5372 return g_object_ref (mount);
5379 GFileCopyFlags flags;
5380 GFileProgressCallback progress_cb;
5381 gpointer progress_cb_data;
5382 GIOSchedulerJob *job;
5386 copy_async_data_free (CopyAsyncData *data)
5388 g_object_unref (data->source);
5389 g_object_unref (data->destination);
5394 CopyAsyncData *data;
5395 goffset current_num_bytes;
5396 goffset total_num_bytes;
5400 copy_async_progress_in_main (gpointer user_data)
5402 ProgressData *progress = user_data;
5403 CopyAsyncData *data = progress->data;
5405 data->progress_cb (progress->current_num_bytes,
5406 progress->total_num_bytes,
5407 data->progress_cb_data);
5413 mainloop_barrier (gpointer user_data)
5415 /* Does nothing, but ensures all queued idles before
5422 copy_async_progress_callback (goffset current_num_bytes,
5423 goffset total_num_bytes,
5426 CopyAsyncData *data = user_data;
5427 ProgressData *progress;
5429 progress = g_new (ProgressData, 1);
5430 progress->data = data;
5431 progress->current_num_bytes = current_num_bytes;
5432 progress->total_num_bytes = total_num_bytes;
5434 g_io_scheduler_job_send_to_mainloop_async (data->job,
5435 copy_async_progress_in_main,
5441 copy_async_thread (GIOSchedulerJob *job,
5442 GCancellable *cancellable,
5445 GSimpleAsyncResult *res;
5446 CopyAsyncData *data;
5451 data = g_simple_async_result_get_op_res_gpointer (res);
5455 result = g_file_copy (data->source,
5459 (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
5463 /* Ensure all progress callbacks are done running in main thread */
5464 if (data->progress_cb != NULL)
5465 g_io_scheduler_job_send_to_mainloop (job,
5471 g_simple_async_result_set_from_error (res, error);
5472 g_error_free (error);
5475 g_simple_async_result_complete_in_idle (res);
5481 g_file_real_copy_async (GFile *source,
5483 GFileCopyFlags flags,
5485 GCancellable *cancellable,
5486 GFileProgressCallback progress_callback,
5487 gpointer progress_callback_data,
5488 GAsyncReadyCallback callback,
5491 GSimpleAsyncResult *res;
5492 CopyAsyncData *data;
5494 data = g_new0 (CopyAsyncData, 1);
5495 data->source = g_object_ref (source);
5496 data->destination = g_object_ref (destination);
5497 data->flags = flags;
5498 data->progress_cb = progress_callback;
5499 data->progress_cb_data = progress_callback_data;
5501 res = g_simple_async_result_new (G_OBJECT (source), callback, user_data, g_file_real_copy_async);
5502 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)copy_async_data_free);
5504 g_io_scheduler_push_job (copy_async_thread, res, g_object_unref, io_priority, cancellable);
5508 g_file_real_copy_finish (GFile *file,
5512 /* Error handled in g_file_copy_finish() */
5517 /********************************************
5518 * Default VFS operations *
5519 ********************************************/
5522 * g_file_new_for_path:
5523 * @path: a string containing a relative or absolute path.
5525 * Constructs a #GFile for a given path. This operation never
5526 * fails, but the returned object might not support any I/O
5527 * operation if @path is malformed.
5529 * Returns: a new #GFile for the given @path.
5532 g_file_new_for_path (const char *path)
5534 g_return_val_if_fail (path != NULL, NULL);
5536 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
5540 * g_file_new_for_uri:
5541 * @uri: a string containing a URI.
5543 * Constructs a #GFile for a given URI. This operation never
5544 * fails, but the returned object might not support any I/O
5545 * operation if @uri is malformed or if the uri type is
5548 * Returns: a #GFile for the given @uri.
5551 g_file_new_for_uri (const char *uri)
5553 g_return_val_if_fail (uri != NULL, NULL);
5555 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
5559 * g_file_parse_name:
5560 * @parse_name: a file name or path to be parsed.
5562 * Constructs a #GFile with the given @parse_name (i.e. something given by g_file_get_parse_name()).
5563 * This operation never fails, but the returned object might not support any I/O
5564 * operation if the @parse_name cannot be parsed.
5566 * Returns: a new #GFile.
5569 g_file_parse_name (const char *parse_name)
5571 g_return_val_if_fail (parse_name != NULL, NULL);
5573 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
5577 is_valid_scheme_character (char c)
5579 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
5582 /* Following RFC 2396, valid schemes are built like:
5583 * scheme = alpha *( alpha | digit | "+" | "-" | "." )
5586 has_valid_scheme (const char *uri)
5592 if (!g_ascii_isalpha (*p))
5597 } while (is_valid_scheme_character (*p));
5603 * g_file_new_for_commandline_arg:
5604 * @arg: a command line string.
5606 * Creates a #GFile with the given argument from the command line. The value of
5607 * @arg can be either a URI, an absolute path or a relative path resolved
5608 * relative to the current working directory.
5609 * This operation never fails, but the returned object might not support any
5610 * I/O operation if @arg points to a malformed path.
5612 * Returns: a new #GFile.
5615 g_file_new_for_commandline_arg (const char *arg)
5621 g_return_val_if_fail (arg != NULL, NULL);
5623 if (g_path_is_absolute (arg))
5624 return g_file_new_for_path (arg);
5626 if (has_valid_scheme (arg))
5627 return g_file_new_for_uri (arg);
5629 current_dir = g_get_current_dir ();
5630 filename = g_build_filename (current_dir, arg, NULL);
5631 g_free (current_dir);
5633 file = g_file_new_for_path (filename);
5640 * g_file_mount_enclosing_volume:
5641 * @location: input #GFile.
5642 * @flags: flags affecting the operation
5643 * @mount_operation: a #GMountOperation or %NULL to avoid user interaction.
5644 * @cancellable: optional #GCancellable object, %NULL to ignore.
5645 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
5646 * @user_data: the data to pass to callback function
5648 * Starts a @mount_operation, mounting the volume that contains the file @location.
5650 * When this operation has completed, @callback will be called with
5651 * @user_user data, and the operation can be finalized with
5652 * g_file_mount_enclosing_volume_finish().
5654 * If @cancellable is not %NULL, then the operation can be cancelled by
5655 * triggering the cancellable object from another thread. If the operation
5656 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5659 g_file_mount_enclosing_volume (GFile *location,
5660 GMountMountFlags flags,
5661 GMountOperation *mount_operation,
5662 GCancellable *cancellable,
5663 GAsyncReadyCallback callback,
5668 g_return_if_fail (G_IS_FILE (location));
5670 iface = G_FILE_GET_IFACE (location);
5672 if (iface->mount_enclosing_volume == NULL)
5674 g_simple_async_report_error_in_idle (G_OBJECT (location),
5675 callback, user_data,
5676 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5677 _("volume doesn't implement mount"));
5682 (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
5687 * g_file_mount_enclosing_volume_finish:
5688 * @location: input #GFile.
5689 * @result: a #GAsyncResult.
5690 * @error: a #GError, or %NULL
5692 * Finishes a mount operation started by g_file_mount_enclosing_volume().
5694 * Returns: %TRUE if successful. If an error
5695 * has occurred, this function will return %FALSE and set @error
5696 * appropriately if present.
5699 g_file_mount_enclosing_volume_finish (GFile *location,
5700 GAsyncResult *result,
5705 g_return_val_if_fail (G_IS_FILE (location), FALSE);
5706 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5708 if (G_IS_SIMPLE_ASYNC_RESULT (result))
5710 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
5711 if (g_simple_async_result_propagate_error (simple, error))
5715 iface = G_FILE_GET_IFACE (location);
5717 return (* iface->mount_enclosing_volume_finish) (location, result, error);
5720 /********************************************
5721 * Utility functions *
5722 ********************************************/
5725 * g_file_query_default_handler:
5726 * @file: a #GFile to open.
5727 * @cancellable: optional #GCancellable object, %NULL to ignore.
5728 * @error: a #GError, or %NULL
5730 * Returns the #GAppInfo that is registered as the default
5731 * application to handle the file specified by @file.
5733 * If @cancellable is not %NULL, then the operation can be cancelled by
5734 * triggering the cancellable object from another thread. If the operation
5735 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5737 * Returns: a #GAppInfo if the handle was found, %NULL if there were errors.
5738 * When you are done with it, release it with g_object_unref()
5741 g_file_query_default_handler (GFile *file,
5742 GCancellable *cancellable,
5746 const char *content_type;
5751 uri_scheme = g_file_get_uri_scheme (file);
5752 appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
5753 g_free (uri_scheme);
5755 if (appinfo != NULL)
5758 info = g_file_query_info (file,
5759 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
5768 content_type = g_file_info_get_content_type (info);
5771 /* Don't use is_native(), as we want to support fuse paths if availible */
5772 path = g_file_get_path (file);
5773 appinfo = g_app_info_get_default_for_type (content_type,
5778 g_object_unref (info);
5780 if (appinfo != NULL)
5783 g_set_error_literal (error, G_IO_ERROR,
5784 G_IO_ERROR_NOT_SUPPORTED,
5785 _("No application is registered as handling this file"));
5791 #define GET_CONTENT_BLOCK_SIZE 8192
5794 * g_file_load_contents:
5795 * @file: input #GFile.
5796 * @cancellable: optional #GCancellable object, %NULL to ignore.
5797 * @contents: a location to place the contents of the file.
5798 * @length: a location to place the length of the contents of the file,
5799 * or %NULL if the length is not needed
5800 * @etag_out: a location to place the current entity tag for the file,
5801 * or %NULL if the entity tag is not needed
5802 * @error: a #GError, or %NULL
5804 * Loads the content of the file into memory. The data is always
5805 * zero-terminated, but this is not included in the resultant @length.
5806 * The returned @content should be freed with g_free() when no longer
5809 * If @cancellable is not %NULL, then the operation can be cancelled by
5810 * triggering the cancellable object from another thread. If the operation
5811 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5813 * Returns: %TRUE if the @file's contents were successfully loaded.
5814 * %FALSE if there were errors.
5817 g_file_load_contents (GFile *file,
5818 GCancellable *cancellable,
5824 GFileInputStream *in;
5825 GByteArray *content;
5830 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5831 g_return_val_if_fail (contents != NULL, FALSE);
5833 in = g_file_read (file, cancellable, error);
5837 content = g_byte_array_new ();
5840 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
5841 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
5842 content->data + pos,
5843 GET_CONTENT_BLOCK_SIZE,
5844 cancellable, error)) > 0)
5847 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
5854 info = g_file_input_stream_query_info (in,
5855 G_FILE_ATTRIBUTE_ETAG_VALUE,
5860 *etag_out = g_strdup (g_file_info_get_etag (info));
5861 g_object_unref (info);
5865 /* Ignore errors on close */
5866 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
5867 g_object_unref (in);
5871 /* error is set already */
5872 g_byte_array_free (content, TRUE);
5879 /* Zero terminate (we got an extra byte allocated for this */
5880 content->data[pos] = 0;
5882 *contents = (char *)g_byte_array_free (content, FALSE);
5890 GCancellable *cancellable;
5891 GFileReadMoreCallback read_more_callback;
5892 GAsyncReadyCallback callback;
5894 GByteArray *content;
5901 load_contents_data_free (LoadContentsData *data)
5904 g_error_free (data->error);
5905 if (data->cancellable)
5906 g_object_unref (data->cancellable);
5908 g_byte_array_free (data->content, TRUE);
5909 g_free (data->etag);
5910 g_object_unref (data->file);
5915 load_contents_close_callback (GObject *obj,
5916 GAsyncResult *close_res,
5919 GInputStream *stream = G_INPUT_STREAM (obj);
5920 LoadContentsData *data = user_data;
5921 GSimpleAsyncResult *res;
5923 /* Ignore errors here, we're only reading anyway */
5924 g_input_stream_close_finish (stream, close_res, NULL);
5925 g_object_unref (stream);
5927 res = g_simple_async_result_new (G_OBJECT (data->file),
5930 g_file_load_contents_async);
5931 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)load_contents_data_free);
5932 g_simple_async_result_complete (res);
5933 g_object_unref (res);
5937 load_contents_fstat_callback (GObject *obj,
5938 GAsyncResult *stat_res,
5941 GInputStream *stream = G_INPUT_STREAM (obj);
5942 LoadContentsData *data = user_data;
5945 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
5949 data->etag = g_strdup (g_file_info_get_etag (info));
5950 g_object_unref (info);
5953 g_input_stream_close_async (stream, 0,
5955 load_contents_close_callback, data);
5959 load_contents_read_callback (GObject *obj,
5960 GAsyncResult *read_res,
5963 GInputStream *stream = G_INPUT_STREAM (obj);
5964 LoadContentsData *data = user_data;
5965 GError *error = NULL;
5968 read_size = g_input_stream_read_finish (stream, read_res, &error);
5972 /* Error or EOF, close the file */
5973 data->error = error;
5974 g_input_stream_close_async (stream, 0,
5976 load_contents_close_callback, data);
5978 else if (read_size == 0)
5980 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
5981 G_FILE_ATTRIBUTE_ETAG_VALUE,
5984 load_contents_fstat_callback,
5987 else if (read_size > 0)
5989 data->pos += read_size;
5991 g_byte_array_set_size (data->content,
5992 data->pos + GET_CONTENT_BLOCK_SIZE);
5995 if (data->read_more_callback &&
5996 !data->read_more_callback ((char *)data->content->data, data->pos, data->user_data))
5997 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
5998 G_FILE_ATTRIBUTE_ETAG_VALUE,
6001 load_contents_fstat_callback,
6004 g_input_stream_read_async (stream,
6005 data->content->data + data->pos,
6006 GET_CONTENT_BLOCK_SIZE,
6009 load_contents_read_callback,
6015 load_contents_open_callback (GObject *obj,
6016 GAsyncResult *open_res,
6019 GFile *file = G_FILE (obj);
6020 GFileInputStream *stream;
6021 LoadContentsData *data = user_data;
6022 GError *error = NULL;
6023 GSimpleAsyncResult *res;
6025 stream = g_file_read_finish (file, open_res, &error);
6029 g_byte_array_set_size (data->content,
6030 data->pos + GET_CONTENT_BLOCK_SIZE);
6031 g_input_stream_read_async (G_INPUT_STREAM (stream),
6032 data->content->data + data->pos,
6033 GET_CONTENT_BLOCK_SIZE,
6036 load_contents_read_callback,
6042 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
6046 g_simple_async_result_complete (res);
6047 g_error_free (error);
6048 load_contents_data_free (data);
6049 g_object_unref (res);
6054 * g_file_load_partial_contents_async:
6055 * @file: input #GFile.
6056 * @cancellable: optional #GCancellable object, %NULL to ignore.
6057 * @read_more_callback: a #GFileReadMoreCallback to receive partial data and to specify whether further data should be read.
6058 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6059 * @user_data: the data to pass to the callback functions.
6061 * Reads the partial contents of a file. A #GFileReadMoreCallback should be
6062 * used to stop reading from the file when appropriate, else this function
6063 * will behave exactly as g_file_load_contents_async(). This operation
6064 * can be finished by g_file_load_partial_contents_finish().
6066 * Users of this function should be aware that @user_data is passed to
6067 * both the @read_more_callback and the @callback.
6069 * If @cancellable is not %NULL, then the operation can be cancelled by
6070 * triggering the cancellable object from another thread. If the operation
6071 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6074 g_file_load_partial_contents_async (GFile *file,
6075 GCancellable *cancellable,
6076 GFileReadMoreCallback read_more_callback,
6077 GAsyncReadyCallback callback,
6080 LoadContentsData *data;
6082 g_return_if_fail (G_IS_FILE (file));
6084 data = g_new0 (LoadContentsData, 1);
6087 data->cancellable = g_object_ref (cancellable);
6088 data->read_more_callback = read_more_callback;
6089 data->callback = callback;
6090 data->user_data = user_data;
6091 data->content = g_byte_array_new ();
6092 data->file = g_object_ref (file);
6094 g_file_read_async (file,
6097 load_contents_open_callback,
6102 * g_file_load_partial_contents_finish:
6103 * @file: input #GFile.
6104 * @res: a #GAsyncResult.
6105 * @contents: a location to place the contents of the file.
6106 * @length: a location to place the length of the contents of the file,
6107 * or %NULL if the length is not needed
6108 * @etag_out: a location to place the current entity tag for the file,
6109 * or %NULL if the entity tag is not needed
6110 * @error: a #GError, or %NULL
6112 * Finishes an asynchronous partial load operation that was started
6113 * with g_file_load_partial_contents_async(). The data is always
6114 * zero-terminated, but this is not included in the resultant @length.
6115 * The returned @content should be freed with g_free() when no longer
6118 * Returns: %TRUE if the load was successful. If %FALSE and @error is
6119 * present, it will be set appropriately.
6122 g_file_load_partial_contents_finish (GFile *file,
6129 GSimpleAsyncResult *simple;
6130 LoadContentsData *data;
6132 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6133 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
6134 g_return_val_if_fail (contents != NULL, FALSE);
6136 simple = G_SIMPLE_ASYNC_RESULT (res);
6138 if (g_simple_async_result_propagate_error (simple, error))
6141 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_load_contents_async);
6143 data = g_simple_async_result_get_op_res_gpointer (simple);
6147 g_propagate_error (error, data->error);
6156 *length = data->pos;
6160 *etag_out = data->etag;
6164 /* Zero terminate */
6165 g_byte_array_set_size (data->content, data->pos + 1);
6166 data->content->data[data->pos] = 0;
6168 *contents = (char *)g_byte_array_free (data->content, FALSE);
6169 data->content = NULL;
6175 * g_file_load_contents_async:
6176 * @file: input #GFile.
6177 * @cancellable: optional #GCancellable object, %NULL to ignore.
6178 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6179 * @user_data: the data to pass to callback function
6181 * Starts an asynchronous load of the @file's contents.
6183 * For more details, see g_file_load_contents() which is
6184 * the synchronous version of this call.
6186 * When the load operation has completed, @callback will be called
6187 * with @user data. To finish the operation, call
6188 * g_file_load_contents_finish() with the #GAsyncResult returned by
6191 * If @cancellable is not %NULL, then the operation can be cancelled by
6192 * triggering the cancellable object from another thread. If the operation
6193 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6196 g_file_load_contents_async (GFile *file,
6197 GCancellable *cancellable,
6198 GAsyncReadyCallback callback,
6201 g_file_load_partial_contents_async (file,
6204 callback, user_data);
6208 * g_file_load_contents_finish:
6209 * @file: input #GFile.
6210 * @res: a #GAsyncResult.
6211 * @contents: a location to place the contents of the file.
6212 * @length: a location to place the length of the contents of the file,
6213 * or %NULL if the length is not needed
6214 * @etag_out: a location to place the current entity tag for the file,
6215 * or %NULL if the entity tag is not needed
6216 * @error: a #GError, or %NULL
6218 * Finishes an asynchronous load of the @file's contents.
6219 * The contents are placed in @contents, and @length is set to the
6220 * size of the @contents string. The @content should be freed with
6221 * g_free() when no longer needed. If @etag_out is present, it will be
6222 * set to the new entity tag for the @file.
6224 * Returns: %TRUE if the load was successful. If %FALSE and @error is
6225 * present, it will be set appropriately.
6228 g_file_load_contents_finish (GFile *file,
6235 return g_file_load_partial_contents_finish (file,
6244 * g_file_replace_contents:
6245 * @file: input #GFile.
6246 * @contents: a string containing the new contents for @file.
6247 * @length: the length of @contents in bytes.
6248 * @etag: the old <link linkend="gfile-etag">entity tag</link>
6249 * for the document, or %NULL
6250 * @make_backup: %TRUE if a backup should be created.
6251 * @flags: a set of #GFileCreateFlags.
6252 * @new_etag: a location to a new <link linkend="gfile-etag">entity tag</link>
6253 * for the document. This should be freed with g_free() when no longer
6255 * @cancellable: optional #GCancellable object, %NULL to ignore.
6256 * @error: a #GError, or %NULL
6258 * Replaces the contents of @file with @contents of @length bytes.
6260 * If @etag is specified (not %NULL) any existing file must have that etag, or
6261 * the error %G_IO_ERROR_WRONG_ETAG will be returned.
6263 * If @make_backup is %TRUE, this function will attempt to make a backup of @file.
6265 * If @cancellable is not %NULL, then the operation can be cancelled by
6266 * triggering the cancellable object from another thread. If the operation
6267 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6269 * The returned @new_etag can be used to verify that the file hasn't changed the
6270 * next time it is saved over.
6272 * Returns: %TRUE if successful. If an error
6273 * has occurred, this function will return %FALSE and set @error
6274 * appropriately if present.
6277 g_file_replace_contents (GFile *file,
6278 const char *contents,
6281 gboolean make_backup,
6282 GFileCreateFlags flags,
6284 GCancellable *cancellable,
6287 GFileOutputStream *out;
6288 gsize pos, remainder;
6292 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6293 g_return_val_if_fail (contents != NULL, FALSE);
6295 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
6301 while (remainder > 0 &&
6302 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
6304 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
6312 if (remainder > 0 && res < 0)
6314 /* Ignore errors on close */
6315 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
6316 g_object_unref (out);
6318 /* error is set already */
6322 ret = g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error);
6325 *new_etag = g_file_output_stream_get_etag (out);
6327 g_object_unref (out);
6335 GCancellable *cancellable;
6336 GAsyncReadyCallback callback;
6338 const char *content;
6342 } ReplaceContentsData;
6345 replace_contents_data_free (ReplaceContentsData *data)
6348 g_error_free (data->error);
6349 if (data->cancellable)
6350 g_object_unref (data->cancellable);
6351 g_object_unref (data->file);
6352 g_free (data->etag);
6357 replace_contents_close_callback (GObject *obj,
6358 GAsyncResult *close_res,
6361 GOutputStream *stream = G_OUTPUT_STREAM (obj);
6362 ReplaceContentsData *data = user_data;
6363 GSimpleAsyncResult *res;
6365 /* Ignore errors here, we're only reading anyway */
6366 g_output_stream_close_finish (stream, close_res, NULL);
6367 g_object_unref (stream);
6369 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
6371 res = g_simple_async_result_new (G_OBJECT (data->file),
6374 g_file_replace_contents_async);
6375 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_contents_data_free);
6376 g_simple_async_result_complete (res);
6377 g_object_unref (res);
6381 replace_contents_write_callback (GObject *obj,
6382 GAsyncResult *read_res,
6385 GOutputStream *stream = G_OUTPUT_STREAM (obj);
6386 ReplaceContentsData *data = user_data;
6387 GError *error = NULL;
6390 write_size = g_output_stream_write_finish (stream, read_res, &error);
6392 if (write_size <= 0)
6394 /* Error or EOF, close the file */
6396 data->error = error;
6397 g_output_stream_close_async (stream, 0,
6399 replace_contents_close_callback, data);
6401 else if (write_size > 0)
6403 data->pos += write_size;
6405 if (data->pos >= data->length)
6406 g_output_stream_close_async (stream, 0,
6408 replace_contents_close_callback, data);
6410 g_output_stream_write_async (stream,
6411 data->content + data->pos,
6412 data->length - data->pos,
6415 replace_contents_write_callback,
6421 replace_contents_open_callback (GObject *obj,
6422 GAsyncResult *open_res,
6425 GFile *file = G_FILE (obj);
6426 GFileOutputStream *stream;
6427 ReplaceContentsData *data = user_data;
6428 GError *error = NULL;
6429 GSimpleAsyncResult *res;
6431 stream = g_file_replace_finish (file, open_res, &error);
6435 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
6436 data->content + data->pos,
6437 data->length - data->pos,
6440 replace_contents_write_callback,
6446 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
6450 g_simple_async_result_complete (res);
6451 g_error_free (error);
6452 replace_contents_data_free (data);
6453 g_object_unref (res);
6458 * g_file_replace_contents_async:
6459 * @file: input #GFile.
6460 * @contents: string of contents to replace the file with.
6461 * @length: the length of @contents in bytes.
6462 * @etag: a new <link linkend="gfile-etag">entity tag</link> for the @file, or %NULL
6463 * @make_backup: %TRUE if a backup should be created.
6464 * @flags: a set of #GFileCreateFlags.
6465 * @cancellable: optional #GCancellable object, %NULL to ignore.
6466 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6467 * @user_data: the data to pass to callback function
6469 * Starts an asynchronous replacement of @file with the given
6470 * @contents of @length bytes. @etag will replace the document's
6471 * current entity tag.
6473 * When this operation has completed, @callback will be called with
6474 * @user_user data, and the operation can be finalized with
6475 * g_file_replace_contents_finish().
6477 * If @cancellable is not %NULL, then the operation can be cancelled by
6478 * triggering the cancellable object from another thread. If the operation
6479 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6481 * If @make_backup is %TRUE, this function will attempt to
6482 * make a backup of @file.
6485 g_file_replace_contents_async (GFile *file,
6486 const char *contents,
6489 gboolean make_backup,
6490 GFileCreateFlags flags,
6491 GCancellable *cancellable,
6492 GAsyncReadyCallback callback,
6495 ReplaceContentsData *data;
6497 g_return_if_fail (G_IS_FILE (file));
6498 g_return_if_fail (contents != NULL);
6500 data = g_new0 (ReplaceContentsData, 1);
6503 data->cancellable = g_object_ref (cancellable);
6504 data->callback = callback;
6505 data->user_data = user_data;
6506 data->content = contents;
6507 data->length = length;
6509 data->file = g_object_ref (file);
6511 g_file_replace_async (file,
6517 replace_contents_open_callback,
6522 * g_file_replace_contents_finish:
6523 * @file: input #GFile.
6524 * @res: a #GAsyncResult.
6525 * @new_etag: a location of a new <link linkend="gfile-etag">entity tag</link>
6526 * for the document. This should be freed with g_free() when it is no
6527 * longer needed, or %NULL
6528 * @error: a #GError, or %NULL
6530 * Finishes an asynchronous replace of the given @file. See
6531 * g_file_replace_contents_async(). Sets @new_etag to the new entity
6532 * tag for the document, if present.
6534 * Returns: %TRUE on success, %FALSE on failure.
6537 g_file_replace_contents_finish (GFile *file,
6542 GSimpleAsyncResult *simple;
6543 ReplaceContentsData *data;
6545 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6546 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
6548 simple = G_SIMPLE_ASYNC_RESULT (res);
6550 if (g_simple_async_result_propagate_error (simple, error))
6553 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_replace_contents_async);
6555 data = g_simple_async_result_get_op_res_gpointer (simple);
6559 g_propagate_error (error, data->error);
6567 *new_etag = data->etag;
6568 data->etag = NULL; /* Take ownership */
6574 #define __G_FILE_C__
6575 #include "gioaliasdef.c"