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:
1721 * @flags: a set of #GFileCreateFlags
1722 * @cancellable: optional #GCancellable object, %NULL to ignore
1723 * @error: return location for 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. Some file systems don't allow all file names,
1739 * and may return an %G_IO_ERROR_INVALID_FILENAME error, and if the name
1740 * is too long, %G_IO_ERROR_FILENAME_TOO_LONG will be returned. Other errors
1741 * are possible too, and depend on what kind of filesystem the file is on.
1743 * Note that in many non-local file cases read and write streams are not
1744 * supported, so make sure you really need to do read and write streaming,
1745 * rather than just opening for reading or writing.
1747 * Returns: a #GFileIOStream for the newly created file, or %NULL on error.
1748 * Free the returned object with g_object_unref().
1753 g_file_create_readwrite (GFile *file,
1754 GFileCreateFlags flags,
1755 GCancellable *cancellable,
1760 g_return_val_if_fail (G_IS_FILE (file), NULL);
1762 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1765 iface = G_FILE_GET_IFACE (file);
1767 if (iface->create_readwrite == NULL)
1769 g_set_error_literal (error, G_IO_ERROR,
1770 G_IO_ERROR_NOT_SUPPORTED,
1771 _("Operation not supported"));
1775 return (* iface->create_readwrite) (file, flags, cancellable, error);
1779 * g_file_replace_readwrite:
1781 * @etag: an optional <link linkend="gfile-etag">entity tag</link> for the
1782 * current #GFile, or #NULL to ignore
1783 * @make_backup: %TRUE if a backup should be created
1784 * @flags: a set of #GFileCreateFlags
1785 * @cancellable: optional #GCancellable object, %NULL to ignore
1786 * @error: return location for a #GError, or %NULL
1788 * Returns an output stream for overwriting the file in readwrite mode,
1789 * possibly creating a backup copy of the file first. If the file doesn't
1790 * exist, it will be created.
1792 * For details about the behaviour, see g_file_replace() which does the same
1793 * thing but returns an output stream only.
1795 * Note that in many non-local file cases read and write streams are not
1796 * supported, so make sure you really need to do read and write streaming,
1797 * rather than just opening for reading or writing.
1799 * Returns: a #GFileIOStream or %NULL on error.
1800 * Free the returned object with g_object_unref().
1805 g_file_replace_readwrite (GFile *file,
1807 gboolean make_backup,
1808 GFileCreateFlags flags,
1809 GCancellable *cancellable,
1814 g_return_val_if_fail (G_IS_FILE (file), NULL);
1816 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1819 iface = G_FILE_GET_IFACE (file);
1821 if (iface->replace_readwrite == NULL)
1823 g_set_error_literal (error, G_IO_ERROR,
1824 G_IO_ERROR_NOT_SUPPORTED,
1825 _("Operation not supported"));
1829 return (* iface->replace_readwrite) (file, etag, make_backup, flags, cancellable, error);
1833 * g_file_read_async:
1834 * @file: input #GFile
1835 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1837 * @cancellable: optional #GCancellable object, %NULL to ignore.
1838 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1839 * @user_data: the data to pass to callback function
1841 * Asynchronously opens @file for reading.
1843 * For more details, see g_file_read() which is
1844 * the synchronous version of this call.
1846 * When the operation is finished, @callback will be called. You can then call
1847 * g_file_read_finish() to get the result of the operation.
1850 g_file_read_async (GFile *file,
1852 GCancellable *cancellable,
1853 GAsyncReadyCallback callback,
1858 g_return_if_fail (G_IS_FILE (file));
1860 iface = G_FILE_GET_IFACE (file);
1861 (* iface->read_async) (file,
1869 * g_file_read_finish:
1870 * @file: input #GFile.
1871 * @res: a #GAsyncResult.
1872 * @error: a #GError, or %NULL
1874 * Finishes an asynchronous file read operation started with
1875 * g_file_read_async().
1877 * Returns: a #GFileInputStream or %NULL on error.
1878 * Free the returned object with g_object_unref().
1881 g_file_read_finish (GFile *file,
1887 g_return_val_if_fail (G_IS_FILE (file), NULL);
1888 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1890 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1892 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1893 if (g_simple_async_result_propagate_error (simple, error))
1897 iface = G_FILE_GET_IFACE (file);
1898 return (* iface->read_finish) (file, res, error);
1902 * g_file_append_to_async:
1903 * @file: input #GFile.
1904 * @flags: a set of #GFileCreateFlags.
1905 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1907 * @cancellable: optional #GCancellable object, %NULL to ignore.
1908 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1909 * @user_data: the data to pass to callback function
1911 * Asynchronously opens @file for appending.
1913 * For more details, see g_file_append_to() which is
1914 * the synchronous version of this call.
1916 * When the operation is finished, @callback will be called. You can then call
1917 * g_file_append_to_finish() to get the result of the operation.
1920 g_file_append_to_async (GFile *file,
1921 GFileCreateFlags flags,
1923 GCancellable *cancellable,
1924 GAsyncReadyCallback callback,
1929 g_return_if_fail (G_IS_FILE (file));
1931 iface = G_FILE_GET_IFACE (file);
1932 (* iface->append_to_async) (file,
1941 * g_file_append_to_finish:
1942 * @file: input #GFile.
1943 * @res: #GAsyncResult
1944 * @error: a #GError, or %NULL
1946 * Finishes an asynchronous file append operation started with
1947 * g_file_append_to_async().
1949 * Returns: a valid #GFileOutputStream or %NULL on error.
1950 * Free the returned object with g_object_unref().
1953 g_file_append_to_finish (GFile *file,
1959 g_return_val_if_fail (G_IS_FILE (file), NULL);
1960 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1962 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1964 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1965 if (g_simple_async_result_propagate_error (simple, error))
1969 iface = G_FILE_GET_IFACE (file);
1970 return (* iface->append_to_finish) (file, res, error);
1974 * g_file_create_async:
1975 * @file: input #GFile.
1976 * @flags: a set of #GFileCreateFlags.
1977 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1979 * @cancellable: optional #GCancellable object, %NULL to ignore.
1980 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1981 * @user_data: the data to pass to callback function
1983 * Asynchronously creates a new file and returns an output stream for writing to it.
1984 * The file must not already exist.
1986 * For more details, see g_file_create() which is
1987 * the synchronous version of this call.
1989 * When the operation is finished, @callback will be called. You can then call
1990 * g_file_create_finish() to get the result of the operation.
1993 g_file_create_async (GFile *file,
1994 GFileCreateFlags flags,
1996 GCancellable *cancellable,
1997 GAsyncReadyCallback callback,
2002 g_return_if_fail (G_IS_FILE (file));
2004 iface = G_FILE_GET_IFACE (file);
2005 (* iface->create_async) (file,
2014 * g_file_create_finish:
2015 * @file: input #GFile.
2016 * @res: a #GAsyncResult.
2017 * @error: a #GError, or %NULL
2019 * Finishes an asynchronous file create operation started with
2020 * g_file_create_async().
2022 * Returns: a #GFileOutputStream or %NULL on error.
2023 * Free the returned object with g_object_unref().
2026 g_file_create_finish (GFile *file,
2032 g_return_val_if_fail (G_IS_FILE (file), NULL);
2033 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2035 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2037 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2038 if (g_simple_async_result_propagate_error (simple, error))
2042 iface = G_FILE_GET_IFACE (file);
2043 return (* iface->create_finish) (file, res, error);
2047 * g_file_replace_async:
2048 * @file: input #GFile.
2049 * @etag: an <link linkend="gfile-etag">entity tag</link> for the
2050 * current #GFile, or NULL to ignore.
2051 * @make_backup: %TRUE if a backup should be created.
2052 * @flags: a set of #GFileCreateFlags.
2053 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2055 * @cancellable: optional #GCancellable object, %NULL to ignore.
2056 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2057 * @user_data: the data to pass to callback function
2059 * Asynchronously overwrites the file, replacing the contents, possibly
2060 * creating a backup copy of the file first.
2062 * For more details, see g_file_replace() which is
2063 * the synchronous version of this call.
2065 * When the operation is finished, @callback will be called. You can then call
2066 * g_file_replace_finish() to get the result of the operation.
2069 g_file_replace_async (GFile *file,
2071 gboolean make_backup,
2072 GFileCreateFlags flags,
2074 GCancellable *cancellable,
2075 GAsyncReadyCallback callback,
2080 g_return_if_fail (G_IS_FILE (file));
2082 iface = G_FILE_GET_IFACE (file);
2083 (* iface->replace_async) (file,
2094 * g_file_replace_finish:
2095 * @file: input #GFile.
2096 * @res: a #GAsyncResult.
2097 * @error: a #GError, or %NULL
2099 * Finishes an asynchronous file replace operation started with
2100 * g_file_replace_async().
2102 * Returns: a #GFileOutputStream, or %NULL on error.
2103 * Free the returned object with g_object_unref().
2106 g_file_replace_finish (GFile *file,
2112 g_return_val_if_fail (G_IS_FILE (file), NULL);
2113 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2115 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2117 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2118 if (g_simple_async_result_propagate_error (simple, error))
2122 iface = G_FILE_GET_IFACE (file);
2123 return (* iface->replace_finish) (file, res, error);
2128 * g_file_open_readwrite_async:
2129 * @file: input #GFile.
2130 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2132 * @cancellable: optional #GCancellable object, %NULL to ignore.
2133 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2134 * @user_data: the data to pass to callback function
2136 * Asynchronously opens @file for reading and writing.
2138 * For more details, see g_file_open_readwrite() which is
2139 * the synchronous version of this call.
2141 * When the operation is finished, @callback will be called. You can then call
2142 * g_file_open_readwrite_finish() to get the result of the operation.
2147 g_file_open_readwrite_async (GFile *file,
2149 GCancellable *cancellable,
2150 GAsyncReadyCallback callback,
2155 g_return_if_fail (G_IS_FILE (file));
2157 iface = G_FILE_GET_IFACE (file);
2158 (* iface->open_readwrite_async) (file,
2166 * g_file_open_readwrite_finish:
2167 * @file: input #GFile.
2168 * @res: a #GAsyncResult.
2169 * @error: a #GError, or %NULL
2171 * Finishes an asynchronous file read operation started with
2172 * g_file_open_readwrite_async().
2174 * Returns: a #GFileIOStream or %NULL on error.
2175 * Free the returned object with g_object_unref().
2180 g_file_open_readwrite_finish (GFile *file,
2186 g_return_val_if_fail (G_IS_FILE (file), NULL);
2187 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2189 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2191 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2192 if (g_simple_async_result_propagate_error (simple, error))
2196 iface = G_FILE_GET_IFACE (file);
2197 return (* iface->open_readwrite_finish) (file, res, error);
2202 * g_file_create_readwrite_async:
2203 * @file: input #GFile
2204 * @flags: a set of #GFileCreateFlags
2205 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2207 * @cancellable: optional #GCancellable object, %NULL to ignore
2208 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2209 * @user_data: the data to pass to callback function
2211 * Asynchronously creates a new file and returns a stream for reading and
2212 * writing to it. The file must not already exist.
2214 * For more details, see g_file_create_readwrite() which is
2215 * the synchronous version of this call.
2217 * When the operation is finished, @callback will be called. You can then
2218 * call g_file_create_readwrite_finish() to get the result of the operation.
2223 g_file_create_readwrite_async (GFile *file,
2224 GFileCreateFlags flags,
2226 GCancellable *cancellable,
2227 GAsyncReadyCallback callback,
2232 g_return_if_fail (G_IS_FILE (file));
2234 iface = G_FILE_GET_IFACE (file);
2235 (* iface->create_readwrite_async) (file,
2244 * g_file_create_readwrite_finish:
2245 * @file: input #GFile
2246 * @res: a #GAsyncResult
2247 * @error: a #GError, or %NULL
2249 * Finishes an asynchronous file create operation started with
2250 * g_file_create_readwrite_async().
2252 * Returns: a #GFileIOStream or %NULL on error.
2253 * Free the returned object with g_object_unref().
2258 g_file_create_readwrite_finish (GFile *file,
2264 g_return_val_if_fail (G_IS_FILE (file), NULL);
2265 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2267 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2269 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2270 if (g_simple_async_result_propagate_error (simple, error))
2274 iface = G_FILE_GET_IFACE (file);
2275 return (* iface->create_readwrite_finish) (file, res, error);
2279 * g_file_replace_readwrite_async:
2280 * @file: input #GFile.
2281 * @etag: an <link linkend="gfile-etag">entity tag</link> for the
2282 * current #GFile, or NULL to ignore.
2283 * @make_backup: %TRUE if a backup should be created.
2284 * @flags: a set of #GFileCreateFlags.
2285 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2287 * @cancellable: optional #GCancellable object, %NULL to ignore.
2288 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2289 * @user_data: the data to pass to callback function
2291 * Asynchronously overwrites the file in read-write mode, replacing the
2292 * contents, possibly creating a backup copy of the file first.
2294 * For more details, see g_file_replace_readwrite() which is
2295 * the synchronous version of this call.
2297 * When the operation is finished, @callback will be called. You can then
2298 * call g_file_replace_readwrite_finish() to get the result of the operation.
2303 g_file_replace_readwrite_async (GFile *file,
2305 gboolean make_backup,
2306 GFileCreateFlags flags,
2308 GCancellable *cancellable,
2309 GAsyncReadyCallback callback,
2314 g_return_if_fail (G_IS_FILE (file));
2316 iface = G_FILE_GET_IFACE (file);
2317 (* iface->replace_readwrite_async) (file,
2328 * g_file_replace_readwrite_finish:
2329 * @file: input #GFile.
2330 * @res: a #GAsyncResult.
2331 * @error: a #GError, or %NULL
2333 * Finishes an asynchronous file replace operation started with
2334 * g_file_replace_readwrite_async().
2336 * Returns: a #GFileIOStream, or %NULL on error.
2337 * Free the returned object with g_object_unref().
2342 g_file_replace_readwrite_finish (GFile *file,
2348 g_return_val_if_fail (G_IS_FILE (file), NULL);
2349 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2351 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2353 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2354 if (g_simple_async_result_propagate_error (simple, error))
2358 iface = G_FILE_GET_IFACE (file);
2359 return (* iface->replace_readwrite_finish) (file, res, error);
2363 copy_symlink (GFile *destination,
2364 GFileCopyFlags flags,
2365 GCancellable *cancellable,
2370 gboolean tried_delete;
2372 GFileType file_type;
2374 tried_delete = FALSE;
2378 if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
2380 /* Maybe it already existed, and we want to overwrite? */
2381 if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) &&
2382 my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
2384 g_error_free (my_error);
2387 /* Don't overwrite if the destination is a directory */
2388 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2389 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2390 cancellable, &my_error);
2393 file_type = g_file_info_get_file_type (info);
2394 g_object_unref (info);
2396 if (file_type == G_FILE_TYPE_DIRECTORY)
2398 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
2399 _("Can't copy over directory"));
2404 if (!g_file_delete (destination, cancellable, error))
2407 tried_delete = TRUE;
2411 g_propagate_error (error, my_error);
2418 static GInputStream *
2419 open_source_for_copy (GFile *source,
2421 GFileCopyFlags flags,
2422 GCancellable *cancellable,
2428 GFileType file_type;
2431 in = (GInputStream *)g_file_read (source, cancellable, &my_error);
2435 /* There was an error opening the source, try to set a good error for it: */
2437 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
2439 /* The source is a directory, don't fail with WOULD_RECURSE immediately,
2440 * as that is less useful to the app. Better check for errors on the
2443 g_error_free (my_error);
2446 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2447 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2448 cancellable, &my_error);
2451 file_type = g_file_info_get_file_type (info);
2452 g_object_unref (info);
2454 if (flags & G_FILE_COPY_OVERWRITE)
2456 if (file_type == G_FILE_TYPE_DIRECTORY)
2458 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
2459 _("Can't copy directory over directory"));
2462 /* continue to would_recurse error */
2466 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
2467 _("Target file exists"));
2473 /* Error getting info from target, return that error
2474 * (except for NOT_FOUND, which is no error here)
2476 if (my_error->domain != G_IO_ERROR && my_error->code != G_IO_ERROR_NOT_FOUND)
2478 g_propagate_error (error, my_error);
2481 g_error_free (my_error);
2484 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
2485 _("Can't recursively copy directory"));
2489 g_propagate_error (error, my_error);
2494 should_copy (GFileAttributeInfo *info,
2496 gboolean skip_perms)
2498 if (skip_perms && strcmp(info->name, "unix::mode") == 0)
2502 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
2503 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
2507 build_attribute_list_for_copy (GFileAttributeInfoList *attributes,
2508 GFileAttributeInfoList *namespaces,
2510 gboolean skip_perms)
2517 s = g_string_new ("");
2521 for (i = 0; i < attributes->n_infos; i++)
2523 if (should_copy (&attributes->infos[i], as_move, skip_perms))
2528 g_string_append_c (s, ',');
2530 g_string_append (s, attributes->infos[i].name);
2537 for (i = 0; i < namespaces->n_infos; i++)
2539 if (should_copy (&namespaces->infos[i], as_move, FALSE))
2544 g_string_append_c (s, ',');
2546 g_string_append (s, namespaces->infos[i].name);
2547 g_string_append (s, ":*");
2552 return g_string_free (s, FALSE);
2556 * g_file_copy_attributes:
2557 * @source: a #GFile with attributes.
2558 * @destination: a #GFile to copy attributes to.
2559 * @flags: a set of #GFileCopyFlags.
2560 * @cancellable: optional #GCancellable object, %NULL to ignore.
2561 * @error: a #GError, %NULL to ignore.
2563 * Copies the file attributes from @source to @destination.
2565 * Normally only a subset of the file attributes are copied,
2566 * those that are copies in a normal file copy operation
2567 * (which for instance does not include e.g. owner). However
2568 * if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
2569 * all the metadata that is possible to copy is copied. This
2570 * is useful when implementing move by copy + delete source.
2572 * Returns: %TRUE if the attributes were copied successfully, %FALSE otherwise.
2575 g_file_copy_attributes (GFile *source,
2577 GFileCopyFlags flags,
2578 GCancellable *cancellable,
2581 GFileAttributeInfoList *attributes, *namespaces;
2582 char *attrs_to_read;
2586 gboolean source_nofollow_symlinks;
2587 gboolean skip_perms;
2589 as_move = flags & G_FILE_COPY_ALL_METADATA;
2590 source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
2591 skip_perms = (flags & G_FILE_COPY_TARGET_DEFAULT_PERMS) != 0;
2593 /* Ignore errors here, if the target supports no attributes there is nothing to copy */
2594 attributes = g_file_query_settable_attributes (destination, cancellable, NULL);
2595 namespaces = g_file_query_writable_namespaces (destination, cancellable, NULL);
2597 if (attributes == NULL && namespaces == NULL)
2600 attrs_to_read = build_attribute_list_for_copy (attributes, namespaces, as_move, skip_perms);
2602 /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
2603 * we just don't copy it.
2605 info = g_file_query_info (source, attrs_to_read,
2606 source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
2610 g_free (attrs_to_read);
2615 res = g_file_set_attributes_from_info (destination,
2617 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2620 g_object_unref (info);
2623 g_file_attribute_info_list_unref (attributes);
2624 g_file_attribute_info_list_unref (namespaces);
2629 /* Closes the streams */
2631 copy_stream_with_progress (GInputStream *in,
2634 GCancellable *cancellable,
2635 GFileProgressCallback progress_callback,
2636 gpointer progress_callback_data,
2639 gssize n_read, n_written;
2640 goffset current_size;
2641 char buffer[1024*64], *p;
2647 /* avoid performance impact of querying total size when it's not needed */
2648 if (progress_callback)
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);
2675 if (total_size == -1)
2682 n_read = g_input_stream_read (in, buffer, sizeof (buffer), cancellable, error);
2692 current_size += n_read;
2697 n_written = g_output_stream_write (out, p, n_read, cancellable, error);
2698 if (n_written == -1)
2705 n_read -= n_written;
2711 if (progress_callback)
2712 progress_callback (current_size, total_size, progress_callback_data);
2716 error = NULL; /* Ignore further errors */
2718 /* Make sure we send full copied size */
2719 if (progress_callback)
2720 progress_callback (current_size, total_size, progress_callback_data);
2722 /* Don't care about errors in source here */
2723 g_input_stream_close (in, cancellable, NULL);
2725 /* But write errors on close are bad! */
2726 if (!g_output_stream_close (out, cancellable, error))
2729 g_object_unref (in);
2730 g_object_unref (out);
2736 file_copy_fallback (GFile *source,
2738 GFileCopyFlags flags,
2739 GCancellable *cancellable,
2740 GFileProgressCallback progress_callback,
2741 gpointer progress_callback_data,
2749 /* need to know the file type */
2750 info = g_file_query_info (source,
2751 G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
2752 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2759 /* Maybe copy the symlink? */
2760 if ((flags & G_FILE_COPY_NOFOLLOW_SYMLINKS) &&
2761 g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK)
2763 target = g_file_info_get_symlink_target (info);
2766 if (!copy_symlink (destination, flags, cancellable, target, error))
2768 g_object_unref (info);
2772 g_object_unref (info);
2775 /* ... else fall back on a regular file copy */
2776 g_object_unref (info);
2778 /* Handle "special" files (pipes, device nodes, ...)? */
2779 else if (g_file_info_get_file_type (info) == G_FILE_TYPE_SPECIAL)
2781 /* FIXME: could try to recreate device nodes and others? */
2783 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2784 _("Can't copy special file"));
2785 g_object_unref (info);
2788 /* Everything else should just fall back on a regular copy. */
2790 g_object_unref (info);
2792 in = open_source_for_copy (source, destination, flags, cancellable, error);
2796 if (flags & G_FILE_COPY_OVERWRITE)
2798 out = (GOutputStream *)g_file_replace (destination,
2800 flags & G_FILE_COPY_BACKUP,
2801 G_FILE_CREATE_REPLACE_DESTINATION,
2802 cancellable, error);
2806 out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
2811 g_object_unref (in);
2815 if (!copy_stream_with_progress (in, out, source, cancellable,
2816 progress_callback, progress_callback_data,
2822 /* Ignore errors here. Failure to copy metadata is not a hard error */
2823 g_file_copy_attributes (source, destination,
2824 flags, cancellable, NULL);
2831 * @source: input #GFile.
2832 * @destination: destination #GFile
2833 * @flags: set of #GFileCopyFlags
2834 * @cancellable: optional #GCancellable object, %NULL to ignore.
2835 * @progress_callback: function to callback with progress information
2836 * @progress_callback_data: user data to pass to @progress_callback
2837 * @error: #GError to set on error, or %NULL
2839 * Copies the file @source to the location specified by @destination.
2840 * Can not handle recursive copies of directories.
2842 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2843 * existing @destination file is overwritten.
2845 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2846 * will be copied as symlinks, otherwise the target of the
2847 * @source symlink will be copied.
2849 * If @cancellable is not %NULL, then the operation can be cancelled by
2850 * triggering the cancellable object from another thread. If the operation
2851 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2853 * If @progress_callback is not %NULL, then the operation can be monitored by
2854 * setting this to a #GFileProgressCallback function. @progress_callback_data
2855 * will be passed to this function. It is guaranteed that this callback will
2856 * be called after all data has been transferred with the total number of bytes
2857 * copied during the operation.
2859 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2860 * error is returned, independent on the status of the @destination.
2862 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2863 * error G_IO_ERROR_EXISTS is returned.
2865 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2866 * error is returned. If trying to overwrite a directory with a directory the
2867 * G_IO_ERROR_WOULD_MERGE error is returned.
2869 * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2870 * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2873 * If you are interested in copying the #GFile object itself (not the on-disk
2874 * file), see g_file_dup().
2876 * Returns: %TRUE on success, %FALSE otherwise.
2879 g_file_copy (GFile *source,
2881 GFileCopyFlags flags,
2882 GCancellable *cancellable,
2883 GFileProgressCallback progress_callback,
2884 gpointer progress_callback_data,
2891 g_return_val_if_fail (G_IS_FILE (source), FALSE);
2892 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2894 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2897 iface = G_FILE_GET_IFACE (destination);
2901 res = (* iface->copy) (source, destination,
2903 progress_callback, progress_callback_data,
2909 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2911 g_propagate_error (error, my_error);
2915 g_clear_error (&my_error);
2918 /* If the types are different, and the destination method failed
2919 also try the source method */
2920 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
2922 iface = G_FILE_GET_IFACE (source);
2927 res = (* iface->copy) (source, destination,
2929 progress_callback, progress_callback_data,
2935 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2937 g_propagate_error (error, my_error);
2941 g_clear_error (&my_error);
2945 return file_copy_fallback (source, destination, flags, cancellable,
2946 progress_callback, progress_callback_data,
2951 * g_file_copy_async:
2952 * @source: input #GFile.
2953 * @destination: destination #GFile
2954 * @flags: set of #GFileCopyFlags
2955 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2957 * @cancellable: optional #GCancellable object, %NULL to ignore.
2958 * @progress_callback: function to callback with progress information
2959 * @progress_callback_data: user data to pass to @progress_callback
2960 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2961 * @user_data: the data to pass to callback function
2963 * Copies the file @source to the location specified by @destination
2964 * asynchronously. For details of the behaviour, see g_file_copy().
2966 * If @progress_callback is not %NULL, then that function that will be called
2967 * just like in g_file_copy(), however the callback will run in the main loop,
2968 * not in the thread that is doing the I/O operation.
2970 * When the operation is finished, @callback will be called. You can then call
2971 * g_file_copy_finish() to get the result of the operation.
2974 g_file_copy_async (GFile *source,
2976 GFileCopyFlags flags,
2978 GCancellable *cancellable,
2979 GFileProgressCallback progress_callback,
2980 gpointer progress_callback_data,
2981 GAsyncReadyCallback callback,
2986 g_return_if_fail (G_IS_FILE (source));
2987 g_return_if_fail (G_IS_FILE (destination));
2989 iface = G_FILE_GET_IFACE (source);
2990 (* iface->copy_async) (source,
2996 progress_callback_data,
3002 * g_file_copy_finish:
3003 * @file: input #GFile.
3004 * @res: a #GAsyncResult.
3005 * @error: a #GError, or %NULL
3007 * Finishes copying the file started with
3008 * g_file_copy_async().
3010 * Returns: a %TRUE on success, %FALSE on error.
3013 g_file_copy_finish (GFile *file,
3019 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3020 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
3022 if (G_IS_SIMPLE_ASYNC_RESULT (res))
3024 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3026 if (g_simple_async_result_propagate_error (simple, error))
3030 iface = G_FILE_GET_IFACE (file);
3031 return (* iface->copy_finish) (file, res, error);
3036 * @source: #GFile pointing to the source location.
3037 * @destination: #GFile pointing to the destination location.
3038 * @flags: set of #GFileCopyFlags.
3039 * @cancellable: optional #GCancellable object, %NULL to ignore.
3040 * @progress_callback: #GFileProgressCallback function for updates.
3041 * @progress_callback_data: gpointer to user data for the callback function.
3042 * @error: #GError for returning error conditions, or %NULL
3045 * Tries to move the file or directory @source to the location specified by @destination.
3046 * If native move operations are supported then this is used, otherwise a copy + delete
3047 * fallback is used. The native implementation may support moving directories (for instance
3048 * on moves inside the same filesystem), but the fallback code does not.
3050 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3051 * existing @destination file is overwritten.
3053 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3054 * will be copied as symlinks, otherwise the target of the
3055 * @source symlink will be copied.
3057 * If @cancellable is not %NULL, then the operation can be cancelled by
3058 * triggering the cancellable object from another thread. If the operation
3059 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3061 * If @progress_callback is not %NULL, then the operation can be monitored by
3062 * setting this to a #GFileProgressCallback function. @progress_callback_data
3063 * will be passed to this function. It is guaranteed that this callback will
3064 * be called after all data has been transferred with the total number of bytes
3065 * copied during the operation.
3067 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
3068 * error is returned, independent on the status of the @destination.
3070 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
3071 * error G_IO_ERROR_EXISTS is returned.
3073 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
3074 * error is returned. If trying to overwrite a directory with a directory the
3075 * G_IO_ERROR_WOULD_MERGE error is returned.
3077 * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
3078 * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
3079 * may be returned (if the native move operation isn't available).
3081 * Returns: %TRUE on successful move, %FALSE otherwise.
3084 g_file_move (GFile *source,
3086 GFileCopyFlags flags,
3087 GCancellable *cancellable,
3088 GFileProgressCallback progress_callback,
3089 gpointer progress_callback_data,
3096 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3097 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3099 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3102 iface = G_FILE_GET_IFACE (destination);
3106 res = (* iface->move) (source, destination,
3108 progress_callback, progress_callback_data,
3114 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3116 g_propagate_error (error, my_error);
3121 /* If the types are different, and the destination method failed
3122 also try the source method */
3123 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3125 iface = G_FILE_GET_IFACE (source);
3130 res = (* iface->move) (source, destination,
3132 progress_callback, progress_callback_data,
3138 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3140 g_propagate_error (error, my_error);
3146 if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
3148 g_set_error_literal (error, G_IO_ERROR,
3149 G_IO_ERROR_NOT_SUPPORTED,
3150 _("Operation not supported"));
3154 flags |= G_FILE_COPY_ALL_METADATA;
3155 if (!g_file_copy (source, destination, flags, cancellable,
3156 progress_callback, progress_callback_data,
3160 return g_file_delete (source, cancellable, error);
3164 * g_file_make_directory
3165 * @file: input #GFile.
3166 * @cancellable: optional #GCancellable object, %NULL to ignore.
3167 * @error: a #GError, or %NULL
3169 * Creates a directory. Note that this will only create a child directory of
3170 * the immediate parent directory of the path or URI given by the #GFile. To
3171 * recursively create directories, see g_file_make_directory_with_parents().
3172 * This function will fail if the parent directory does not exist, setting
3173 * @error to %G_IO_ERROR_NOT_FOUND. If the file system doesn't support creating
3174 * directories, this function will fail, setting @error to
3175 * %G_IO_ERROR_NOT_SUPPORTED.
3177 * For a local #GFile the newly created directory will have the default
3178 * (current) ownership and permissions of the current process.
3180 * If @cancellable is not %NULL, then the operation can be cancelled by
3181 * triggering the cancellable object from another thread. If the operation
3182 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3184 * Returns: %TRUE on successful creation, %FALSE otherwise.
3187 g_file_make_directory (GFile *file,
3188 GCancellable *cancellable,
3193 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3195 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3198 iface = G_FILE_GET_IFACE (file);
3200 if (iface->make_directory == NULL)
3202 g_set_error_literal (error, G_IO_ERROR,
3203 G_IO_ERROR_NOT_SUPPORTED,
3204 _("Operation not supported"));
3208 return (* iface->make_directory) (file, cancellable, error);
3212 * g_file_make_directory_with_parents:
3213 * @file: input #GFile.
3214 * @cancellable: optional #GCancellable object, %NULL to ignore.
3215 * @error: a #GError, or %NULL
3217 * Creates a directory and any parent directories that may not exist similar to
3218 * 'mkdir -p'. If the file system does not support creating directories, this
3219 * function will fail, setting @error to %G_IO_ERROR_NOT_SUPPORTED.
3221 * For a local #GFile the newly created directories will have the default
3222 * (current) ownership and permissions of the current process.
3224 * If @cancellable is not %NULL, then the operation can be cancelled by
3225 * triggering the cancellable object from another thread. If the operation
3226 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3228 * Returns: %TRUE if all directories have been successfully created, %FALSE
3234 g_file_make_directory_with_parents (GFile *file,
3235 GCancellable *cancellable,
3239 GFile *parent_file, *work_file;
3240 GList *list = NULL, *l;
3241 GError *my_error = NULL;
3243 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3246 result = g_file_make_directory (file, cancellable, &my_error);
3247 if (result || my_error->code != G_IO_ERROR_NOT_FOUND)
3250 g_propagate_error (error, my_error);
3256 while (!result && my_error->code == G_IO_ERROR_NOT_FOUND)
3258 g_clear_error (&my_error);
3260 parent_file = g_file_get_parent (work_file);
3261 if (parent_file == NULL)
3263 result = g_file_make_directory (parent_file, cancellable, &my_error);
3265 if (!result && my_error->code == G_IO_ERROR_NOT_FOUND)
3266 list = g_list_prepend (list, parent_file);
3268 work_file = parent_file;
3271 for (l = list; result && l; l = l->next)
3273 result = g_file_make_directory ((GFile *) l->data, cancellable, &my_error);
3277 while (list != NULL)
3279 g_object_unref ((GFile *) list->data);
3280 list = g_list_remove (list, list->data);
3285 g_propagate_error (error, my_error);
3289 return g_file_make_directory (file, cancellable, error);
3293 * g_file_make_symbolic_link:
3294 * @file: input #GFile.
3295 * @symlink_value: a string with the value of the new symlink.
3296 * @cancellable: optional #GCancellable object, %NULL to ignore.
3297 * @error: a #GError.
3299 * Creates a symbolic link.
3301 * If @cancellable is not %NULL, then the operation can be cancelled by
3302 * triggering the cancellable object from another thread. If the operation
3303 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3305 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
3308 g_file_make_symbolic_link (GFile *file,
3309 const char *symlink_value,
3310 GCancellable *cancellable,
3315 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3316 g_return_val_if_fail (symlink_value != NULL, FALSE);
3318 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3321 if (*symlink_value == '\0')
3323 g_set_error_literal (error, G_IO_ERROR,
3324 G_IO_ERROR_INVALID_ARGUMENT,
3325 _("Invalid symlink value given"));
3329 iface = G_FILE_GET_IFACE (file);
3331 if (iface->make_symbolic_link == NULL)
3333 g_set_error_literal (error, G_IO_ERROR,
3334 G_IO_ERROR_NOT_SUPPORTED,
3335 _("Operation not supported"));
3339 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
3344 * @file: input #GFile.
3345 * @cancellable: optional #GCancellable object, %NULL to ignore.
3346 * @error: a #GError, or %NULL
3348 * Deletes a file. If the @file is a directory, it will only be deleted if it
3351 * If @cancellable is not %NULL, then the operation can be cancelled by
3352 * triggering the cancellable object from another thread. If the operation
3353 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3355 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
3358 g_file_delete (GFile *file,
3359 GCancellable *cancellable,
3364 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3366 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3369 iface = G_FILE_GET_IFACE (file);
3371 if (iface->delete_file == NULL)
3373 g_set_error_literal (error, G_IO_ERROR,
3374 G_IO_ERROR_NOT_SUPPORTED,
3375 _("Operation not supported"));
3379 return (* iface->delete_file) (file, cancellable, error);
3384 * @file: #GFile to send to trash.
3385 * @cancellable: optional #GCancellable object, %NULL to ignore.
3386 * @error: a #GError, or %NULL
3388 * Sends @file to the "Trashcan", if possible. This is similar to
3389 * deleting it, but the user can recover it before emptying the trashcan.
3390 * Not all file systems support trashing, so this call can return the
3391 * %G_IO_ERROR_NOT_SUPPORTED error.
3394 * If @cancellable is not %NULL, then the operation can be cancelled by
3395 * triggering the cancellable object from another thread. If the operation
3396 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3398 * Returns: %TRUE on successful trash, %FALSE otherwise.
3401 g_file_trash (GFile *file,
3402 GCancellable *cancellable,
3407 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3409 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3412 iface = G_FILE_GET_IFACE (file);
3414 if (iface->trash == NULL)
3416 g_set_error_literal (error,
3417 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
3418 _("Trash not supported"));
3422 return (* iface->trash) (file, cancellable, error);
3426 * g_file_set_display_name:
3427 * @file: input #GFile.
3428 * @display_name: a string.
3429 * @cancellable: optional #GCancellable object, %NULL to ignore.
3430 * @error: a #GError, or %NULL
3432 * Renames @file to the specified display name.
3434 * The display name is converted from UTF8 to the correct encoding for the target
3435 * filesystem if possible and the @file is renamed to this.
3437 * If you want to implement a rename operation in the user interface the edit name
3438 * (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the initial value in the rename
3439 * widget, and then the result after editing should be passed to g_file_set_display_name().
3441 * On success the resulting converted filename is returned.
3443 * If @cancellable is not %NULL, then the operation can be cancelled by
3444 * triggering the cancellable object from another thread. If the operation
3445 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3447 * Returns: a #GFile specifying what @file was renamed to, or %NULL
3448 * if there was an error.
3449 * Free the returned object with g_object_unref().
3452 g_file_set_display_name (GFile *file,
3453 const char *display_name,
3454 GCancellable *cancellable,
3459 g_return_val_if_fail (G_IS_FILE (file), NULL);
3460 g_return_val_if_fail (display_name != NULL, NULL);
3462 if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
3466 G_IO_ERROR_INVALID_ARGUMENT,
3467 _("File names cannot contain '%c'"), G_DIR_SEPARATOR);
3471 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3474 iface = G_FILE_GET_IFACE (file);
3476 return (* iface->set_display_name) (file, display_name, cancellable, error);
3480 * g_file_set_display_name_async:
3481 * @file: input #GFile.
3482 * @display_name: a string.
3483 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3485 * @cancellable: optional #GCancellable object, %NULL to ignore.
3486 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
3487 * @user_data: the data to pass to callback function
3489 * Asynchronously sets the display name for a given #GFile.
3491 * For more details, see g_file_set_display_name() which is
3492 * the synchronous version of this call.
3494 * When the operation is finished, @callback will be called. You can then call
3495 * g_file_set_display_name_finish() to get the result of the operation.
3498 g_file_set_display_name_async (GFile *file,
3499 const char *display_name,
3501 GCancellable *cancellable,
3502 GAsyncReadyCallback callback,
3507 g_return_if_fail (G_IS_FILE (file));
3508 g_return_if_fail (display_name != NULL);
3510 iface = G_FILE_GET_IFACE (file);
3511 (* iface->set_display_name_async) (file,
3520 * g_file_set_display_name_finish:
3521 * @file: input #GFile.
3522 * @res: a #GAsyncResult.
3523 * @error: a #GError, or %NULL
3525 * Finishes setting a display name started with
3526 * g_file_set_display_name_async().
3528 * Returns: a #GFile or %NULL on error.
3529 * Free the returned object with g_object_unref().
3532 g_file_set_display_name_finish (GFile *file,
3538 g_return_val_if_fail (G_IS_FILE (file), NULL);
3539 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
3541 if (G_IS_SIMPLE_ASYNC_RESULT (res))
3543 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3544 if (g_simple_async_result_propagate_error (simple, error))
3548 iface = G_FILE_GET_IFACE (file);
3549 return (* iface->set_display_name_finish) (file, res, error);
3553 * g_file_query_settable_attributes:
3554 * @file: input #GFile.
3555 * @cancellable: optional #GCancellable object, %NULL to ignore.
3556 * @error: a #GError, or %NULL
3558 * Obtain the list of settable attributes for the file.
3560 * Returns the type and full attribute name of all the attributes
3561 * that can be set on this file. This doesn't mean setting it will always
3562 * succeed though, you might get an access failure, or some specific
3563 * file may not support a specific attribute.
3565 * If @cancellable is not %NULL, then the operation can be cancelled by
3566 * triggering the cancellable object from another thread. If the operation
3567 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3569 * Returns: a #GFileAttributeInfoList describing the settable attributes.
3570 * When you are done with it, release it with g_file_attribute_info_list_unref()
3572 GFileAttributeInfoList *
3573 g_file_query_settable_attributes (GFile *file,
3574 GCancellable *cancellable,
3579 GFileAttributeInfoList *list;
3581 g_return_val_if_fail (G_IS_FILE (file), NULL);
3583 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3586 iface = G_FILE_GET_IFACE (file);
3588 if (iface->query_settable_attributes == NULL)
3589 return g_file_attribute_info_list_new ();
3592 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
3596 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
3598 list = g_file_attribute_info_list_new ();
3599 g_error_free (my_error);
3602 g_propagate_error (error, my_error);
3609 * g_file_query_writable_namespaces:
3610 * @file: input #GFile.
3611 * @cancellable: optional #GCancellable object, %NULL to ignore.
3612 * @error: a #GError, or %NULL
3614 * Obtain the list of attribute namespaces where new attributes
3615 * can be created by a user. An example of this is extended
3616 * attributes (in the "xattr" namespace).
3618 * If @cancellable is not %NULL, then the operation can be cancelled by
3619 * triggering the cancellable object from another thread. If the operation
3620 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3622 * Returns: a #GFileAttributeInfoList describing the writable namespaces.
3623 * When you are done with it, release it with g_file_attribute_info_list_unref()
3625 GFileAttributeInfoList *
3626 g_file_query_writable_namespaces (GFile *file,
3627 GCancellable *cancellable,
3632 GFileAttributeInfoList *list;
3634 g_return_val_if_fail (G_IS_FILE (file), NULL);
3636 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3639 iface = G_FILE_GET_IFACE (file);
3641 if (iface->query_writable_namespaces == NULL)
3642 return g_file_attribute_info_list_new ();
3645 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
3649 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
3651 list = g_file_attribute_info_list_new ();
3652 g_error_free (my_error);
3655 g_propagate_error (error, my_error);
3662 * g_file_set_attribute:
3663 * @file: input #GFile.
3664 * @attribute: a string containing the attribute's name.
3665 * @type: The type of the attribute
3666 * @value_p: a pointer to the value (or the pointer itself if the type is a pointer type)
3667 * @flags: a set of #GFileQueryInfoFlags.
3668 * @cancellable: optional #GCancellable object, %NULL to ignore.
3669 * @error: a #GError, or %NULL
3671 * Sets an attribute in the file with attribute name @attribute to @value.
3673 * If @cancellable is not %NULL, then the operation can be cancelled by
3674 * triggering the cancellable object from another thread. If the operation
3675 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3677 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
3680 g_file_set_attribute (GFile *file,
3681 const char *attribute,
3682 GFileAttributeType type,
3684 GFileQueryInfoFlags flags,
3685 GCancellable *cancellable,
3690 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3691 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
3693 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3696 iface = G_FILE_GET_IFACE (file);
3698 if (iface->set_attribute == NULL)
3700 g_set_error_literal (error, G_IO_ERROR,
3701 G_IO_ERROR_NOT_SUPPORTED,
3702 _("Operation not supported"));
3706 return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
3710 * g_file_set_attributes_from_info:
3711 * @file: input #GFile.
3712 * @info: a #GFileInfo.
3713 * @flags: #GFileQueryInfoFlags
3714 * @cancellable: optional #GCancellable object, %NULL to ignore.
3715 * @error: a #GError, or %NULL
3717 * Tries to set all attributes in the #GFileInfo on the target values,
3718 * not stopping on the first error.
3720 * If there is any error during this operation then @error will be set to
3721 * the first error. Error on particular fields are flagged by setting
3722 * the "status" field in the attribute value to
3723 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can also detect
3726 * If @cancellable is not %NULL, then the operation can be cancelled by
3727 * triggering the cancellable object from another thread. If the operation
3728 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3730 * Returns: %TRUE if there was any error, %FALSE otherwise.
3733 g_file_set_attributes_from_info (GFile *file,
3735 GFileQueryInfoFlags flags,
3736 GCancellable *cancellable,
3741 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3742 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
3744 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3747 g_file_info_clear_status (info);
3749 iface = G_FILE_GET_IFACE (file);
3751 return (* iface->set_attributes_from_info) (file,
3760 g_file_real_set_attributes_from_info (GFile *file,
3762 GFileQueryInfoFlags flags,
3763 GCancellable *cancellable,
3769 GFileAttributeValue *value;
3773 attributes = g_file_info_list_attributes (info, NULL);
3775 for (i = 0; attributes[i] != NULL; i++)
3777 value = _g_file_info_get_attribute_value (info, attributes[i]);
3779 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
3782 if (!g_file_set_attribute (file, attributes[i],
3783 value->type, _g_file_attribute_value_peek_as_pointer (value),
3784 flags, cancellable, error))
3786 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
3788 /* Don't set error multiple times */
3792 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
3795 g_strfreev (attributes);
3801 * g_file_set_attributes_async:
3802 * @file: input #GFile.
3803 * @info: a #GFileInfo.
3804 * @flags: a #GFileQueryInfoFlags.
3805 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3807 * @cancellable: optional #GCancellable object, %NULL to ignore.
3808 * @callback: a #GAsyncReadyCallback.
3809 * @user_data: a #gpointer.
3811 * Asynchronously sets the attributes of @file with @info.
3813 * For more details, see g_file_set_attributes_from_info() which is
3814 * the synchronous version of this call.
3816 * When the operation is finished, @callback will be called. You can then call
3817 * g_file_set_attributes_finish() to get the result of the operation.
3820 g_file_set_attributes_async (GFile *file,
3822 GFileQueryInfoFlags flags,
3824 GCancellable *cancellable,
3825 GAsyncReadyCallback callback,
3830 g_return_if_fail (G_IS_FILE (file));
3831 g_return_if_fail (G_IS_FILE_INFO (info));
3833 iface = G_FILE_GET_IFACE (file);
3834 (* iface->set_attributes_async) (file,
3844 * g_file_set_attributes_finish:
3845 * @file: input #GFile.
3846 * @result: a #GAsyncResult.
3847 * @info: a #GFileInfo.
3848 * @error: a #GError, or %NULL
3850 * Finishes setting an attribute started in g_file_set_attributes_async().
3852 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
3855 g_file_set_attributes_finish (GFile *file,
3856 GAsyncResult *result,
3862 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3863 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3865 /* No standard handling of errors here, as we must set info even
3868 iface = G_FILE_GET_IFACE (file);
3869 return (* iface->set_attributes_finish) (file, result, info, error);
3873 * g_file_set_attribute_string:
3874 * @file: input #GFile.
3875 * @attribute: a string containing the attribute's name.
3876 * @value: a string containing the attribute's value.
3877 * @flags: #GFileQueryInfoFlags.
3878 * @cancellable: optional #GCancellable object, %NULL to ignore.
3879 * @error: a #GError, or %NULL
3881 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
3882 * If @attribute is of a different type, this operation will fail.
3884 * If @cancellable is not %NULL, then the operation can be cancelled by
3885 * triggering the cancellable object from another thread. If the operation
3886 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3888 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
3891 g_file_set_attribute_string (GFile *file,
3892 const char *attribute,
3894 GFileQueryInfoFlags flags,
3895 GCancellable *cancellable,
3898 return g_file_set_attribute (file, attribute,
3899 G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
3900 flags, cancellable, error);
3904 * g_file_set_attribute_byte_string:
3905 * @file: input #GFile.
3906 * @attribute: a string containing the attribute's name.
3907 * @value: a string containing the attribute's new value.
3908 * @flags: a #GFileQueryInfoFlags.
3909 * @cancellable: optional #GCancellable object, %NULL to ignore.
3910 * @error: a #GError, or %NULL
3912 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
3913 * If @attribute is of a different type, this operation will fail,
3916 * If @cancellable is not %NULL, then the operation can be cancelled by
3917 * triggering the cancellable object from another thread. If the operation
3918 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3920 * Returns: %TRUE if the @attribute was successfully set to @value
3921 * in the @file, %FALSE otherwise.
3924 g_file_set_attribute_byte_string (GFile *file,
3925 const char *attribute,
3927 GFileQueryInfoFlags flags,
3928 GCancellable *cancellable,
3931 return g_file_set_attribute (file, attribute,
3932 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
3933 flags, cancellable, error);
3937 * g_file_set_attribute_uint32:
3938 * @file: input #GFile.
3939 * @attribute: a string containing the attribute's name.
3940 * @value: a #guint32 containing the attribute's new value.
3941 * @flags: a #GFileQueryInfoFlags.
3942 * @cancellable: optional #GCancellable object, %NULL to ignore.
3943 * @error: a #GError, or %NULL
3945 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
3946 * If @attribute is of a different type, this operation will fail.
3948 * If @cancellable is not %NULL, then the operation can be cancelled by
3949 * triggering the cancellable object from another thread. If the operation
3950 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3952 * Returns: %TRUE if the @attribute was successfully set to @value
3953 * in the @file, %FALSE otherwise.
3956 g_file_set_attribute_uint32 (GFile *file,
3957 const char *attribute,
3959 GFileQueryInfoFlags flags,
3960 GCancellable *cancellable,
3963 return g_file_set_attribute (file, attribute,
3964 G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
3965 flags, cancellable, error);
3969 * g_file_set_attribute_int32:
3970 * @file: input #GFile.
3971 * @attribute: a string containing the attribute's name.
3972 * @value: a #gint32 containing the attribute's new value.
3973 * @flags: a #GFileQueryInfoFlags.
3974 * @cancellable: optional #GCancellable object, %NULL to ignore.
3975 * @error: a #GError, or %NULL
3977 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
3978 * If @attribute is of a different type, this operation will fail.
3980 * If @cancellable is not %NULL, then the operation can be cancelled by
3981 * triggering the cancellable object from another thread. If the operation
3982 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3984 * Returns: %TRUE if the @attribute was successfully set to @value
3985 * in the @file, %FALSE otherwise.
3988 g_file_set_attribute_int32 (GFile *file,
3989 const char *attribute,
3991 GFileQueryInfoFlags flags,
3992 GCancellable *cancellable,
3995 return g_file_set_attribute (file, attribute,
3996 G_FILE_ATTRIBUTE_TYPE_INT32, &value,
3997 flags, cancellable, error);
4001 * g_file_set_attribute_uint64:
4002 * @file: input #GFile.
4003 * @attribute: a string containing the attribute's name.
4004 * @value: a #guint64 containing the attribute's new value.
4005 * @flags: a #GFileQueryInfoFlags.
4006 * @cancellable: optional #GCancellable object, %NULL to ignore.
4007 * @error: a #GError, or %NULL
4009 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
4010 * If @attribute is of a different type, this operation will fail.
4012 * If @cancellable is not %NULL, then the operation can be cancelled by
4013 * triggering the cancellable object from another thread. If the operation
4014 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4016 * Returns: %TRUE if the @attribute was successfully set to @value
4017 * in the @file, %FALSE otherwise.
4020 g_file_set_attribute_uint64 (GFile *file,
4021 const char *attribute,
4023 GFileQueryInfoFlags flags,
4024 GCancellable *cancellable,
4027 return g_file_set_attribute (file, attribute,
4028 G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
4029 flags, cancellable, error);
4033 * g_file_set_attribute_int64:
4034 * @file: input #GFile.
4035 * @attribute: a string containing the attribute's name.
4036 * @value: a #guint64 containing the attribute's new value.
4037 * @flags: a #GFileQueryInfoFlags.
4038 * @cancellable: optional #GCancellable object, %NULL to ignore.
4039 * @error: a #GError, or %NULL
4041 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
4042 * If @attribute is of a different type, this operation will fail.
4044 * If @cancellable is not %NULL, then the operation can be cancelled by
4045 * triggering the cancellable object from another thread. If the operation
4046 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4048 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4051 g_file_set_attribute_int64 (GFile *file,
4052 const char *attribute,
4054 GFileQueryInfoFlags flags,
4055 GCancellable *cancellable,
4058 return g_file_set_attribute (file, attribute,
4059 G_FILE_ATTRIBUTE_TYPE_INT64, &value,
4060 flags, cancellable, error);
4064 * g_file_mount_mountable:
4065 * @file: input #GFile.
4066 * @flags: flags affecting the operation
4067 * @mount_operation: a #GMountOperation, or %NULL to avoid user interaction.
4068 * @cancellable: optional #GCancellable object, %NULL to ignore.
4069 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4070 * @user_data: the data to pass to callback function
4072 * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
4073 * Using @mount_operation, you can request callbacks when, for instance,
4074 * passwords are needed during authentication.
4076 * If @cancellable is not %NULL, then the operation can be cancelled by
4077 * triggering the cancellable object from another thread. If the operation
4078 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4080 * When the operation is finished, @callback will be called. You can then call
4081 * g_file_mount_mountable_finish() to get the result of the operation.
4084 g_file_mount_mountable (GFile *file,
4085 GMountMountFlags flags,
4086 GMountOperation *mount_operation,
4087 GCancellable *cancellable,
4088 GAsyncReadyCallback callback,
4093 g_return_if_fail (G_IS_FILE (file));
4095 iface = G_FILE_GET_IFACE (file);
4097 if (iface->mount_mountable == NULL)
4099 g_simple_async_report_error_in_idle (G_OBJECT (file),
4103 G_IO_ERROR_NOT_SUPPORTED,
4104 _("Operation not supported"));
4108 (* iface->mount_mountable) (file,
4117 * g_file_mount_mountable_finish:
4118 * @file: input #GFile.
4119 * @result: a #GAsyncResult.
4120 * @error: a #GError, or %NULL
4122 * Finishes a mount operation. See g_file_mount_mountable() for details.
4124 * Finish an asynchronous mount operation that was started
4125 * with g_file_mount_mountable().
4127 * Returns: a #GFile or %NULL on error.
4128 * Free the returned object with g_object_unref().
4131 g_file_mount_mountable_finish (GFile *file,
4132 GAsyncResult *result,
4137 g_return_val_if_fail (G_IS_FILE (file), NULL);
4138 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
4140 if (G_IS_SIMPLE_ASYNC_RESULT (result))
4142 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
4143 if (g_simple_async_result_propagate_error (simple, error))
4147 iface = G_FILE_GET_IFACE (file);
4148 return (* iface->mount_mountable_finish) (file, result, error);
4152 * g_file_unmount_mountable:
4153 * @file: input #GFile.
4154 * @flags: flags affecting the operation
4155 * @cancellable: optional #GCancellable object, %NULL to ignore.
4156 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4157 * @user_data: the data to pass to callback function
4159 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
4161 * If @cancellable is not %NULL, then the operation can be cancelled by
4162 * triggering the cancellable object from another thread. If the operation
4163 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4165 * When the operation is finished, @callback will be called. You can then call
4166 * g_file_unmount_mountable_finish() to get the result of the operation.
4169 g_file_unmount_mountable (GFile *file,
4170 GMountUnmountFlags flags,
4171 GCancellable *cancellable,
4172 GAsyncReadyCallback callback,
4177 g_return_if_fail (G_IS_FILE (file));
4179 iface = G_FILE_GET_IFACE (file);
4181 if (iface->unmount_mountable == NULL)
4183 g_simple_async_report_error_in_idle (G_OBJECT (file),
4187 G_IO_ERROR_NOT_SUPPORTED,
4188 _("Operation not supported"));
4192 (* iface->unmount_mountable) (file,
4200 * g_file_unmount_mountable_finish:
4201 * @file: input #GFile.
4202 * @result: a #GAsyncResult.
4203 * @error: a #GError, or %NULL
4205 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
4207 * Finish an asynchronous unmount operation that was started
4208 * with g_file_unmount_mountable().
4210 * Returns: %TRUE if the operation finished successfully. %FALSE
4214 g_file_unmount_mountable_finish (GFile *file,
4215 GAsyncResult *result,
4220 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4221 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4223 if (G_IS_SIMPLE_ASYNC_RESULT (result))
4225 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
4226 if (g_simple_async_result_propagate_error (simple, error))
4230 iface = G_FILE_GET_IFACE (file);
4231 return (* iface->unmount_mountable_finish) (file, result, error);
4235 * g_file_eject_mountable:
4236 * @file: input #GFile.
4237 * @flags: flags affecting the operation
4238 * @cancellable: optional #GCancellable object, %NULL to ignore.
4239 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4240 * @user_data: the data to pass to callback function
4242 * Starts an asynchronous eject on a mountable.
4243 * When this operation has completed, @callback will be called with
4244 * @user_user data, and the operation can be finalized with
4245 * g_file_eject_mountable_finish().
4247 * If @cancellable is not %NULL, then the operation can be cancelled by
4248 * triggering the cancellable object from another thread. If the operation
4249 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4252 g_file_eject_mountable (GFile *file,
4253 GMountUnmountFlags flags,
4254 GCancellable *cancellable,
4255 GAsyncReadyCallback callback,
4260 g_return_if_fail (G_IS_FILE (file));
4262 iface = G_FILE_GET_IFACE (file);
4264 if (iface->eject_mountable == NULL)
4266 g_simple_async_report_error_in_idle (G_OBJECT (file),
4270 G_IO_ERROR_NOT_SUPPORTED,
4271 _("Operation not supported"));
4275 (* iface->eject_mountable) (file,
4283 * g_file_eject_mountable_finish:
4284 * @file: input #GFile.
4285 * @result: a #GAsyncResult.
4286 * @error: a #GError, or %NULL
4288 * Finishes an asynchronous eject operation started by
4289 * g_file_eject_mountable().
4291 * Returns: %TRUE if the @file was ejected successfully. %FALSE
4295 g_file_eject_mountable_finish (GFile *file,
4296 GAsyncResult *result,
4301 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4302 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4304 if (G_IS_SIMPLE_ASYNC_RESULT (result))
4306 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
4307 if (g_simple_async_result_propagate_error (simple, error))
4311 iface = G_FILE_GET_IFACE (file);
4312 return (* iface->eject_mountable_finish) (file, result, error);
4316 * g_file_monitor_directory:
4317 * @file: input #GFile.
4318 * @flags: a set of #GFileMonitorFlags.
4319 * @cancellable: optional #GCancellable object, %NULL to ignore.
4320 * @error: a #GError, or %NULL.
4322 * Obtains a directory monitor for the given file.
4323 * This may fail if directory monitoring is not supported.
4325 * If @cancellable is not %NULL, then the operation can be cancelled by
4326 * triggering the cancellable object from another thread. If the operation
4327 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4329 * Returns: a #GFileMonitor for the given @file, or %NULL on error.
4330 * Free the returned object with g_object_unref().
4333 g_file_monitor_directory (GFile *file,
4334 GFileMonitorFlags flags,
4335 GCancellable *cancellable,
4340 g_return_val_if_fail (G_IS_FILE (file), NULL);
4342 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4345 iface = G_FILE_GET_IFACE (file);
4347 if (iface->monitor_dir == NULL)
4349 g_set_error_literal (error, G_IO_ERROR,
4350 G_IO_ERROR_NOT_SUPPORTED,
4351 _("Operation not supported"));
4355 return (* iface->monitor_dir) (file, flags, cancellable, error);
4359 * g_file_monitor_file:
4360 * @file: input #GFile.
4361 * @flags: a set of #GFileMonitorFlags.
4362 * @cancellable: optional #GCancellable object, %NULL to ignore.
4363 * @error: a #GError, or %NULL.
4365 * Obtains a file monitor for the given file. If no file notification
4366 * mechanism exists, then regular polling of the file is used.
4368 * If @cancellable is not %NULL, then the operation can be cancelled by
4369 * triggering the cancellable object from another thread. If the operation
4370 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4372 * Returns: a #GFileMonitor for the given @file, or %NULL on error.
4373 * Free the returned object with g_object_unref().
4376 g_file_monitor_file (GFile *file,
4377 GFileMonitorFlags flags,
4378 GCancellable *cancellable,
4382 GFileMonitor *monitor;
4384 g_return_val_if_fail (G_IS_FILE (file), NULL);
4386 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4389 iface = G_FILE_GET_IFACE (file);
4393 if (iface->monitor_file)
4394 monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
4396 /* Fallback to polling */
4397 if (monitor == NULL)
4398 monitor = _g_poll_file_monitor_new (file);
4405 * @file: input #GFile
4406 * @flags: a set of #GFileMonitorFlags
4407 * @cancellable: optional #GCancellable object, %NULL to ignore
4408 * @error: a #GError, or %NULL
4410 * Obtains a file or directory monitor for the given file, depending
4411 * on the type of the file.
4413 * If @cancellable is not %NULL, then the operation can be cancelled by
4414 * triggering the cancellable object from another thread. If the operation
4415 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4417 * Returns: a #GFileMonitor for the given @file, or %NULL on error.
4418 * Free the returned object with g_object_unref().
4423 g_file_monitor (GFile *file,
4424 GFileMonitorFlags flags,
4425 GCancellable *cancellable,
4428 if (g_file_query_file_type (file, 0, cancellable) == G_FILE_TYPE_DIRECTORY)
4429 return g_file_monitor_directory (file, flags, cancellable, error);
4431 return g_file_monitor_file (file, flags, cancellable, error);
4434 /********************************************
4435 * Default implementation of async ops *
4436 ********************************************/
4440 GFileQueryInfoFlags flags;
4442 } QueryInfoAsyncData;
4445 query_info_data_free (QueryInfoAsyncData *data)
4448 g_object_unref (data->info);
4449 g_free (data->attributes);
4454 query_info_async_thread (GSimpleAsyncResult *res,
4456 GCancellable *cancellable)
4458 GError *error = NULL;
4459 QueryInfoAsyncData *data;
4462 data = g_simple_async_result_get_op_res_gpointer (res);
4464 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
4468 g_simple_async_result_set_from_error (res, error);
4469 g_error_free (error);
4476 g_file_real_query_info_async (GFile *file,
4477 const char *attributes,
4478 GFileQueryInfoFlags flags,
4480 GCancellable *cancellable,
4481 GAsyncReadyCallback callback,
4484 GSimpleAsyncResult *res;
4485 QueryInfoAsyncData *data;
4487 data = g_new0 (QueryInfoAsyncData, 1);
4488 data->attributes = g_strdup (attributes);
4489 data->flags = flags;
4491 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_info_async);
4492 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
4494 g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
4495 g_object_unref (res);
4499 g_file_real_query_info_finish (GFile *file,
4503 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4504 QueryInfoAsyncData *data;
4506 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_info_async);
4508 data = g_simple_async_result_get_op_res_gpointer (simple);
4510 return g_object_ref (data->info);
4518 } QueryFilesystemInfoAsyncData;
4521 query_filesystem_info_data_free (QueryFilesystemInfoAsyncData *data)
4524 g_object_unref (data->info);
4525 g_free (data->attributes);
4530 query_filesystem_info_async_thread (GSimpleAsyncResult *res,
4532 GCancellable *cancellable)
4534 GError *error = NULL;
4535 QueryFilesystemInfoAsyncData *data;
4538 data = g_simple_async_result_get_op_res_gpointer (res);
4540 info = g_file_query_filesystem_info (G_FILE (object), data->attributes, cancellable, &error);
4544 g_simple_async_result_set_from_error (res, error);
4545 g_error_free (error);
4552 g_file_real_query_filesystem_info_async (GFile *file,
4553 const char *attributes,
4555 GCancellable *cancellable,
4556 GAsyncReadyCallback callback,
4559 GSimpleAsyncResult *res;
4560 QueryFilesystemInfoAsyncData *data;
4562 data = g_new0 (QueryFilesystemInfoAsyncData, 1);
4563 data->attributes = g_strdup (attributes);
4565 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_filesystem_info_async);
4566 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_filesystem_info_data_free);
4568 g_simple_async_result_run_in_thread (res, query_filesystem_info_async_thread, io_priority, cancellable);
4569 g_object_unref (res);
4573 g_file_real_query_filesystem_info_finish (GFile *file,
4577 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4578 QueryFilesystemInfoAsyncData *data;
4580 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_filesystem_info_async);
4582 data = g_simple_async_result_get_op_res_gpointer (simple);
4584 return g_object_ref (data->info);
4591 GFileQueryInfoFlags flags;
4592 GFileEnumerator *enumerator;
4593 } EnumerateChildrenAsyncData;
4596 enumerate_children_data_free (EnumerateChildrenAsyncData *data)
4598 if (data->enumerator)
4599 g_object_unref (data->enumerator);
4600 g_free (data->attributes);
4605 enumerate_children_async_thread (GSimpleAsyncResult *res,
4607 GCancellable *cancellable)
4609 GError *error = NULL;
4610 EnumerateChildrenAsyncData *data;
4611 GFileEnumerator *enumerator;
4613 data = g_simple_async_result_get_op_res_gpointer (res);
4615 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
4617 if (enumerator == NULL)
4619 g_simple_async_result_set_from_error (res, error);
4620 g_error_free (error);
4623 data->enumerator = enumerator;
4627 g_file_real_enumerate_children_async (GFile *file,
4628 const char *attributes,
4629 GFileQueryInfoFlags flags,
4631 GCancellable *cancellable,
4632 GAsyncReadyCallback callback,
4635 GSimpleAsyncResult *res;
4636 EnumerateChildrenAsyncData *data;
4638 data = g_new0 (EnumerateChildrenAsyncData, 1);
4639 data->attributes = g_strdup (attributes);
4640 data->flags = flags;
4642 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_enumerate_children_async);
4643 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)enumerate_children_data_free);
4645 g_simple_async_result_run_in_thread (res, enumerate_children_async_thread, io_priority, cancellable);
4646 g_object_unref (res);
4649 static GFileEnumerator *
4650 g_file_real_enumerate_children_finish (GFile *file,
4654 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4655 EnumerateChildrenAsyncData *data;
4657 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_enumerate_children_async);
4659 data = g_simple_async_result_get_op_res_gpointer (simple);
4660 if (data->enumerator)
4661 return g_object_ref (data->enumerator);
4667 open_read_async_thread (GSimpleAsyncResult *res,
4669 GCancellable *cancellable)
4672 GFileInputStream *stream;
4673 GError *error = NULL;
4675 iface = G_FILE_GET_IFACE (object);
4677 if (iface->read_fn == NULL)
4679 g_set_error_literal (&error, G_IO_ERROR,
4680 G_IO_ERROR_NOT_SUPPORTED,
4681 _("Operation not supported"));
4683 g_simple_async_result_set_from_error (res, error);
4684 g_error_free (error);
4689 stream = iface->read_fn (G_FILE (object), cancellable, &error);
4693 g_simple_async_result_set_from_error (res, error);
4694 g_error_free (error);
4697 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4701 g_file_real_read_async (GFile *file,
4703 GCancellable *cancellable,
4704 GAsyncReadyCallback callback,
4707 GSimpleAsyncResult *res;
4709 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_read_async);
4711 g_simple_async_result_run_in_thread (res, open_read_async_thread, io_priority, cancellable);
4712 g_object_unref (res);
4715 static GFileInputStream *
4716 g_file_real_read_finish (GFile *file,
4720 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4723 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_read_async);
4725 op = g_simple_async_result_get_op_res_gpointer (simple);
4727 return g_object_ref (op);
4733 append_to_async_thread (GSimpleAsyncResult *res,
4735 GCancellable *cancellable)
4738 GFileCreateFlags *data;
4739 GFileOutputStream *stream;
4740 GError *error = NULL;
4742 iface = G_FILE_GET_IFACE (object);
4744 data = g_simple_async_result_get_op_res_gpointer (res);
4746 stream = iface->append_to (G_FILE (object), *data, cancellable, &error);
4750 g_simple_async_result_set_from_error (res, error);
4751 g_error_free (error);
4754 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4758 g_file_real_append_to_async (GFile *file,
4759 GFileCreateFlags flags,
4761 GCancellable *cancellable,
4762 GAsyncReadyCallback callback,
4765 GFileCreateFlags *data;
4766 GSimpleAsyncResult *res;
4768 data = g_new0 (GFileCreateFlags, 1);
4771 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_append_to_async);
4772 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
4774 g_simple_async_result_run_in_thread (res, append_to_async_thread, io_priority, cancellable);
4775 g_object_unref (res);
4778 static GFileOutputStream *
4779 g_file_real_append_to_finish (GFile *file,
4783 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4786 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_append_to_async);
4788 op = g_simple_async_result_get_op_res_gpointer (simple);
4790 return g_object_ref (op);
4796 create_async_thread (GSimpleAsyncResult *res,
4798 GCancellable *cancellable)
4801 GFileCreateFlags *data;
4802 GFileOutputStream *stream;
4803 GError *error = NULL;
4805 iface = G_FILE_GET_IFACE (object);
4807 data = g_simple_async_result_get_op_res_gpointer (res);
4809 stream = iface->create (G_FILE (object), *data, cancellable, &error);
4813 g_simple_async_result_set_from_error (res, error);
4814 g_error_free (error);
4817 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4821 g_file_real_create_async (GFile *file,
4822 GFileCreateFlags flags,
4824 GCancellable *cancellable,
4825 GAsyncReadyCallback callback,
4828 GFileCreateFlags *data;
4829 GSimpleAsyncResult *res;
4831 data = g_new0 (GFileCreateFlags, 1);
4834 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_create_async);
4835 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
4837 g_simple_async_result_run_in_thread (res, create_async_thread, io_priority, cancellable);
4838 g_object_unref (res);
4841 static GFileOutputStream *
4842 g_file_real_create_finish (GFile *file,
4846 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4849 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_create_async);
4851 op = g_simple_async_result_get_op_res_gpointer (simple);
4853 return g_object_ref (op);
4859 GFileOutputStream *stream;
4861 gboolean make_backup;
4862 GFileCreateFlags flags;
4866 replace_async_data_free (ReplaceAsyncData *data)
4869 g_object_unref (data->stream);
4870 g_free (data->etag);
4875 replace_async_thread (GSimpleAsyncResult *res,
4877 GCancellable *cancellable)
4880 GFileOutputStream *stream;
4881 GError *error = NULL;
4882 ReplaceAsyncData *data;
4884 iface = G_FILE_GET_IFACE (object);
4886 data = g_simple_async_result_get_op_res_gpointer (res);
4888 stream = iface->replace (G_FILE (object),
4897 g_simple_async_result_set_from_error (res, error);
4898 g_error_free (error);
4901 data->stream = stream;
4905 g_file_real_replace_async (GFile *file,
4907 gboolean make_backup,
4908 GFileCreateFlags flags,
4910 GCancellable *cancellable,
4911 GAsyncReadyCallback callback,
4914 GSimpleAsyncResult *res;
4915 ReplaceAsyncData *data;
4917 data = g_new0 (ReplaceAsyncData, 1);
4918 data->etag = g_strdup (etag);
4919 data->make_backup = make_backup;
4920 data->flags = flags;
4922 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_replace_async);
4923 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_async_data_free);
4925 g_simple_async_result_run_in_thread (res, replace_async_thread, io_priority, cancellable);
4926 g_object_unref (res);
4929 static GFileOutputStream *
4930 g_file_real_replace_finish (GFile *file,
4934 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4935 ReplaceAsyncData *data;
4937 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_replace_async);
4939 data = g_simple_async_result_get_op_res_gpointer (simple);
4941 return g_object_ref (data->stream);
4947 open_readwrite_async_thread (GSimpleAsyncResult *res,
4949 GCancellable *cancellable)
4952 GFileIOStream *stream;
4953 GError *error = NULL;
4955 iface = G_FILE_GET_IFACE (object);
4957 if (iface->open_readwrite == NULL)
4959 g_set_error_literal (&error, G_IO_ERROR,
4960 G_IO_ERROR_NOT_SUPPORTED,
4961 _("Operation not supported"));
4963 g_simple_async_result_set_from_error (res, error);
4964 g_error_free (error);
4969 stream = iface->open_readwrite (G_FILE (object), cancellable, &error);
4973 g_simple_async_result_set_from_error (res, error);
4974 g_error_free (error);
4977 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4981 g_file_real_open_readwrite_async (GFile *file,
4983 GCancellable *cancellable,
4984 GAsyncReadyCallback callback,
4987 GSimpleAsyncResult *res;
4989 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_open_readwrite_async);
4991 g_simple_async_result_run_in_thread (res, open_readwrite_async_thread, io_priority, cancellable);
4992 g_object_unref (res);
4995 static GFileIOStream *
4996 g_file_real_open_readwrite_finish (GFile *file,
5000 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5003 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_open_readwrite_async);
5005 op = g_simple_async_result_get_op_res_gpointer (simple);
5007 return g_object_ref (op);
5013 create_readwrite_async_thread (GSimpleAsyncResult *res,
5015 GCancellable *cancellable)
5018 GFileCreateFlags *data;
5019 GFileIOStream *stream;
5020 GError *error = NULL;
5022 iface = G_FILE_GET_IFACE (object);
5024 data = g_simple_async_result_get_op_res_gpointer (res);
5026 if (iface->create_readwrite == NULL)
5028 g_set_error_literal (&error, G_IO_ERROR,
5029 G_IO_ERROR_NOT_SUPPORTED,
5030 _("Operation not supported"));
5032 g_simple_async_result_set_from_error (res, error);
5033 g_error_free (error);
5038 stream = iface->create_readwrite (G_FILE (object), *data, cancellable, &error);
5042 g_simple_async_result_set_from_error (res, error);
5043 g_error_free (error);
5046 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
5050 g_file_real_create_readwrite_async (GFile *file,
5051 GFileCreateFlags flags,
5053 GCancellable *cancellable,
5054 GAsyncReadyCallback callback,
5057 GFileCreateFlags *data;
5058 GSimpleAsyncResult *res;
5060 data = g_new0 (GFileCreateFlags, 1);
5063 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_create_readwrite_async);
5064 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
5066 g_simple_async_result_run_in_thread (res, create_readwrite_async_thread, io_priority, cancellable);
5067 g_object_unref (res);
5070 static GFileIOStream *
5071 g_file_real_create_readwrite_finish (GFile *file,
5075 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5078 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_create_readwrite_async);
5080 op = g_simple_async_result_get_op_res_gpointer (simple);
5082 return g_object_ref (op);
5088 GFileIOStream *stream;
5090 gboolean make_backup;
5091 GFileCreateFlags flags;
5092 } ReplaceRWAsyncData;
5095 replace_rw_async_data_free (ReplaceRWAsyncData *data)
5098 g_object_unref (data->stream);
5099 g_free (data->etag);
5104 replace_readwrite_async_thread (GSimpleAsyncResult *res,
5106 GCancellable *cancellable)
5109 GFileIOStream *stream;
5110 GError *error = NULL;
5111 ReplaceRWAsyncData *data;
5113 iface = G_FILE_GET_IFACE (object);
5115 data = g_simple_async_result_get_op_res_gpointer (res);
5117 stream = iface->replace_readwrite (G_FILE (object),
5126 g_simple_async_result_set_from_error (res, error);
5127 g_error_free (error);
5130 data->stream = stream;
5134 g_file_real_replace_readwrite_async (GFile *file,
5136 gboolean make_backup,
5137 GFileCreateFlags flags,
5139 GCancellable *cancellable,
5140 GAsyncReadyCallback callback,
5143 GSimpleAsyncResult *res;
5144 ReplaceRWAsyncData *data;
5146 data = g_new0 (ReplaceRWAsyncData, 1);
5147 data->etag = g_strdup (etag);
5148 data->make_backup = make_backup;
5149 data->flags = flags;
5151 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_replace_readwrite_async);
5152 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_rw_async_data_free);
5154 g_simple_async_result_run_in_thread (res, replace_readwrite_async_thread, io_priority, cancellable);
5155 g_object_unref (res);
5158 static GFileIOStream *
5159 g_file_real_replace_readwrite_finish (GFile *file,
5163 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5164 ReplaceRWAsyncData *data;
5166 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_replace_readwrite_async);
5168 data = g_simple_async_result_get_op_res_gpointer (simple);
5170 return g_object_ref (data->stream);
5178 } SetDisplayNameAsyncData;
5181 set_display_name_data_free (SetDisplayNameAsyncData *data)
5183 g_free (data->name);
5185 g_object_unref (data->file);
5190 set_display_name_async_thread (GSimpleAsyncResult *res,
5192 GCancellable *cancellable)
5194 GError *error = NULL;
5195 SetDisplayNameAsyncData *data;
5198 data = g_simple_async_result_get_op_res_gpointer (res);
5200 file = g_file_set_display_name (G_FILE (object), data->name, cancellable, &error);
5204 g_simple_async_result_set_from_error (res, error);
5205 g_error_free (error);
5212 g_file_real_set_display_name_async (GFile *file,
5213 const char *display_name,
5215 GCancellable *cancellable,
5216 GAsyncReadyCallback callback,
5219 GSimpleAsyncResult *res;
5220 SetDisplayNameAsyncData *data;
5222 data = g_new0 (SetDisplayNameAsyncData, 1);
5223 data->name = g_strdup (display_name);
5225 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_display_name_async);
5226 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_display_name_data_free);
5228 g_simple_async_result_run_in_thread (res, set_display_name_async_thread, io_priority, cancellable);
5229 g_object_unref (res);
5233 g_file_real_set_display_name_finish (GFile *file,
5237 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5238 SetDisplayNameAsyncData *data;
5240 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_display_name_async);
5242 data = g_simple_async_result_get_op_res_gpointer (simple);
5244 return g_object_ref (data->file);
5250 GFileQueryInfoFlags flags;
5257 set_info_data_free (SetInfoAsyncData *data)
5260 g_object_unref (data->info);
5262 g_error_free (data->error);
5267 set_info_async_thread (GSimpleAsyncResult *res,
5269 GCancellable *cancellable)
5271 SetInfoAsyncData *data;
5273 data = g_simple_async_result_get_op_res_gpointer (res);
5276 data->res = g_file_set_attributes_from_info (G_FILE (object),
5284 g_file_real_set_attributes_async (GFile *file,
5286 GFileQueryInfoFlags flags,
5288 GCancellable *cancellable,
5289 GAsyncReadyCallback callback,
5292 GSimpleAsyncResult *res;
5293 SetInfoAsyncData *data;
5295 data = g_new0 (SetInfoAsyncData, 1);
5296 data->info = g_file_info_dup (info);
5297 data->flags = flags;
5299 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_attributes_async);
5300 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_info_data_free);
5302 g_simple_async_result_run_in_thread (res, set_info_async_thread, io_priority, cancellable);
5303 g_object_unref (res);
5307 g_file_real_set_attributes_finish (GFile *file,
5312 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5313 SetInfoAsyncData *data;
5315 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_attributes_async);
5317 data = g_simple_async_result_get_op_res_gpointer (simple);
5320 *info = g_object_ref (data->info);
5322 if (error != NULL && data->error)
5323 *error = g_error_copy (data->error);
5329 find_enclosing_mount_async_thread (GSimpleAsyncResult *res,
5331 GCancellable *cancellable)
5333 GError *error = NULL;
5336 mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
5340 g_simple_async_result_set_from_error (res, error);
5341 g_error_free (error);
5344 g_simple_async_result_set_op_res_gpointer (res, mount, (GDestroyNotify)g_object_unref);
5348 g_file_real_find_enclosing_mount_async (GFile *file,
5350 GCancellable *cancellable,
5351 GAsyncReadyCallback callback,
5354 GSimpleAsyncResult *res;
5356 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_find_enclosing_mount_async);
5358 g_simple_async_result_run_in_thread (res, find_enclosing_mount_async_thread, io_priority, cancellable);
5359 g_object_unref (res);
5363 g_file_real_find_enclosing_mount_finish (GFile *file,
5367 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5370 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_find_enclosing_mount_async);
5372 mount = g_simple_async_result_get_op_res_gpointer (simple);
5373 return g_object_ref (mount);
5380 GFileCopyFlags flags;
5381 GFileProgressCallback progress_cb;
5382 gpointer progress_cb_data;
5383 GIOSchedulerJob *job;
5387 copy_async_data_free (CopyAsyncData *data)
5389 g_object_unref (data->source);
5390 g_object_unref (data->destination);
5395 CopyAsyncData *data;
5396 goffset current_num_bytes;
5397 goffset total_num_bytes;
5401 copy_async_progress_in_main (gpointer user_data)
5403 ProgressData *progress = user_data;
5404 CopyAsyncData *data = progress->data;
5406 data->progress_cb (progress->current_num_bytes,
5407 progress->total_num_bytes,
5408 data->progress_cb_data);
5414 mainloop_barrier (gpointer user_data)
5416 /* Does nothing, but ensures all queued idles before
5423 copy_async_progress_callback (goffset current_num_bytes,
5424 goffset total_num_bytes,
5427 CopyAsyncData *data = user_data;
5428 ProgressData *progress;
5430 progress = g_new (ProgressData, 1);
5431 progress->data = data;
5432 progress->current_num_bytes = current_num_bytes;
5433 progress->total_num_bytes = total_num_bytes;
5435 g_io_scheduler_job_send_to_mainloop_async (data->job,
5436 copy_async_progress_in_main,
5442 copy_async_thread (GIOSchedulerJob *job,
5443 GCancellable *cancellable,
5446 GSimpleAsyncResult *res;
5447 CopyAsyncData *data;
5452 data = g_simple_async_result_get_op_res_gpointer (res);
5456 result = g_file_copy (data->source,
5460 (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
5464 /* Ensure all progress callbacks are done running in main thread */
5465 if (data->progress_cb != NULL)
5466 g_io_scheduler_job_send_to_mainloop (job,
5472 g_simple_async_result_set_from_error (res, error);
5473 g_error_free (error);
5476 g_simple_async_result_complete_in_idle (res);
5482 g_file_real_copy_async (GFile *source,
5484 GFileCopyFlags flags,
5486 GCancellable *cancellable,
5487 GFileProgressCallback progress_callback,
5488 gpointer progress_callback_data,
5489 GAsyncReadyCallback callback,
5492 GSimpleAsyncResult *res;
5493 CopyAsyncData *data;
5495 data = g_new0 (CopyAsyncData, 1);
5496 data->source = g_object_ref (source);
5497 data->destination = g_object_ref (destination);
5498 data->flags = flags;
5499 data->progress_cb = progress_callback;
5500 data->progress_cb_data = progress_callback_data;
5502 res = g_simple_async_result_new (G_OBJECT (source), callback, user_data, g_file_real_copy_async);
5503 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)copy_async_data_free);
5505 g_io_scheduler_push_job (copy_async_thread, res, g_object_unref, io_priority, cancellable);
5509 g_file_real_copy_finish (GFile *file,
5513 /* Error handled in g_file_copy_finish() */
5518 /********************************************
5519 * Default VFS operations *
5520 ********************************************/
5523 * g_file_new_for_path:
5524 * @path: a string containing a relative or absolute path.
5526 * Constructs a #GFile for a given path. This operation never
5527 * fails, but the returned object might not support any I/O
5528 * operation if @path is malformed.
5530 * Returns: a new #GFile for the given @path.
5533 g_file_new_for_path (const char *path)
5535 g_return_val_if_fail (path != NULL, NULL);
5537 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
5541 * g_file_new_for_uri:
5542 * @uri: a string containing a URI.
5544 * Constructs a #GFile for a given URI. This operation never
5545 * fails, but the returned object might not support any I/O
5546 * operation if @uri is malformed or if the uri type is
5549 * Returns: a #GFile for the given @uri.
5552 g_file_new_for_uri (const char *uri)
5554 g_return_val_if_fail (uri != NULL, NULL);
5556 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
5560 * g_file_parse_name:
5561 * @parse_name: a file name or path to be parsed.
5563 * Constructs a #GFile with the given @parse_name (i.e. something given by g_file_get_parse_name()).
5564 * This operation never fails, but the returned object might not support any I/O
5565 * operation if the @parse_name cannot be parsed.
5567 * Returns: a new #GFile.
5570 g_file_parse_name (const char *parse_name)
5572 g_return_val_if_fail (parse_name != NULL, NULL);
5574 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
5578 is_valid_scheme_character (char c)
5580 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
5583 /* Following RFC 2396, valid schemes are built like:
5584 * scheme = alpha *( alpha | digit | "+" | "-" | "." )
5587 has_valid_scheme (const char *uri)
5593 if (!g_ascii_isalpha (*p))
5598 } while (is_valid_scheme_character (*p));
5604 * g_file_new_for_commandline_arg:
5605 * @arg: a command line string.
5607 * Creates a #GFile with the given argument from the command line. The value of
5608 * @arg can be either a URI, an absolute path or a relative path resolved
5609 * relative to the current working directory.
5610 * This operation never fails, but the returned object might not support any
5611 * I/O operation if @arg points to a malformed path.
5613 * Returns: a new #GFile.
5616 g_file_new_for_commandline_arg (const char *arg)
5622 g_return_val_if_fail (arg != NULL, NULL);
5624 if (g_path_is_absolute (arg))
5625 return g_file_new_for_path (arg);
5627 if (has_valid_scheme (arg))
5628 return g_file_new_for_uri (arg);
5630 current_dir = g_get_current_dir ();
5631 filename = g_build_filename (current_dir, arg, NULL);
5632 g_free (current_dir);
5634 file = g_file_new_for_path (filename);
5641 * g_file_mount_enclosing_volume:
5642 * @location: input #GFile.
5643 * @flags: flags affecting the operation
5644 * @mount_operation: a #GMountOperation or %NULL to avoid user interaction.
5645 * @cancellable: optional #GCancellable object, %NULL to ignore.
5646 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
5647 * @user_data: the data to pass to callback function
5649 * Starts a @mount_operation, mounting the volume that contains the file @location.
5651 * When this operation has completed, @callback will be called with
5652 * @user_user data, and the operation can be finalized with
5653 * g_file_mount_enclosing_volume_finish().
5655 * If @cancellable is not %NULL, then the operation can be cancelled by
5656 * triggering the cancellable object from another thread. If the operation
5657 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5660 g_file_mount_enclosing_volume (GFile *location,
5661 GMountMountFlags flags,
5662 GMountOperation *mount_operation,
5663 GCancellable *cancellable,
5664 GAsyncReadyCallback callback,
5669 g_return_if_fail (G_IS_FILE (location));
5671 iface = G_FILE_GET_IFACE (location);
5673 if (iface->mount_enclosing_volume == NULL)
5675 g_simple_async_report_error_in_idle (G_OBJECT (location),
5676 callback, user_data,
5677 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5678 _("volume doesn't implement mount"));
5683 (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
5688 * g_file_mount_enclosing_volume_finish:
5689 * @location: input #GFile.
5690 * @result: a #GAsyncResult.
5691 * @error: a #GError, or %NULL
5693 * Finishes a mount operation started by g_file_mount_enclosing_volume().
5695 * Returns: %TRUE if successful. If an error
5696 * has occurred, this function will return %FALSE and set @error
5697 * appropriately if present.
5700 g_file_mount_enclosing_volume_finish (GFile *location,
5701 GAsyncResult *result,
5706 g_return_val_if_fail (G_IS_FILE (location), FALSE);
5707 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5709 if (G_IS_SIMPLE_ASYNC_RESULT (result))
5711 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
5712 if (g_simple_async_result_propagate_error (simple, error))
5716 iface = G_FILE_GET_IFACE (location);
5718 return (* iface->mount_enclosing_volume_finish) (location, result, error);
5721 /********************************************
5722 * Utility functions *
5723 ********************************************/
5726 * g_file_query_default_handler:
5727 * @file: a #GFile to open.
5728 * @cancellable: optional #GCancellable object, %NULL to ignore.
5729 * @error: a #GError, or %NULL
5731 * Returns the #GAppInfo that is registered as the default
5732 * application to handle the file specified by @file.
5734 * If @cancellable is not %NULL, then the operation can be cancelled by
5735 * triggering the cancellable object from another thread. If the operation
5736 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5738 * Returns: a #GAppInfo if the handle was found, %NULL if there were errors.
5739 * When you are done with it, release it with g_object_unref()
5742 g_file_query_default_handler (GFile *file,
5743 GCancellable *cancellable,
5747 const char *content_type;
5752 uri_scheme = g_file_get_uri_scheme (file);
5753 appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
5754 g_free (uri_scheme);
5756 if (appinfo != NULL)
5759 info = g_file_query_info (file,
5760 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
5769 content_type = g_file_info_get_content_type (info);
5772 /* Don't use is_native(), as we want to support fuse paths if availible */
5773 path = g_file_get_path (file);
5774 appinfo = g_app_info_get_default_for_type (content_type,
5779 g_object_unref (info);
5781 if (appinfo != NULL)
5784 g_set_error_literal (error, G_IO_ERROR,
5785 G_IO_ERROR_NOT_SUPPORTED,
5786 _("No application is registered as handling this file"));
5792 #define GET_CONTENT_BLOCK_SIZE 8192
5795 * g_file_load_contents:
5796 * @file: input #GFile.
5797 * @cancellable: optional #GCancellable object, %NULL to ignore.
5798 * @contents: a location to place the contents of the file.
5799 * @length: a location to place the length of the contents of the file,
5800 * or %NULL if the length is not needed
5801 * @etag_out: a location to place the current entity tag for the file,
5802 * or %NULL if the entity tag is not needed
5803 * @error: a #GError, or %NULL
5805 * Loads the content of the file into memory. The data is always
5806 * zero-terminated, but this is not included in the resultant @length.
5807 * The returned @content should be freed with g_free() when no longer
5810 * If @cancellable is not %NULL, then the operation can be cancelled by
5811 * triggering the cancellable object from another thread. If the operation
5812 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5814 * Returns: %TRUE if the @file's contents were successfully loaded.
5815 * %FALSE if there were errors.
5818 g_file_load_contents (GFile *file,
5819 GCancellable *cancellable,
5825 GFileInputStream *in;
5826 GByteArray *content;
5831 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5832 g_return_val_if_fail (contents != NULL, FALSE);
5834 in = g_file_read (file, cancellable, error);
5838 content = g_byte_array_new ();
5841 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
5842 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
5843 content->data + pos,
5844 GET_CONTENT_BLOCK_SIZE,
5845 cancellable, error)) > 0)
5848 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
5855 info = g_file_input_stream_query_info (in,
5856 G_FILE_ATTRIBUTE_ETAG_VALUE,
5861 *etag_out = g_strdup (g_file_info_get_etag (info));
5862 g_object_unref (info);
5866 /* Ignore errors on close */
5867 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
5868 g_object_unref (in);
5872 /* error is set already */
5873 g_byte_array_free (content, TRUE);
5880 /* Zero terminate (we got an extra byte allocated for this */
5881 content->data[pos] = 0;
5883 *contents = (char *)g_byte_array_free (content, FALSE);
5891 GCancellable *cancellable;
5892 GFileReadMoreCallback read_more_callback;
5893 GAsyncReadyCallback callback;
5895 GByteArray *content;
5902 load_contents_data_free (LoadContentsData *data)
5905 g_error_free (data->error);
5906 if (data->cancellable)
5907 g_object_unref (data->cancellable);
5909 g_byte_array_free (data->content, TRUE);
5910 g_free (data->etag);
5911 g_object_unref (data->file);
5916 load_contents_close_callback (GObject *obj,
5917 GAsyncResult *close_res,
5920 GInputStream *stream = G_INPUT_STREAM (obj);
5921 LoadContentsData *data = user_data;
5922 GSimpleAsyncResult *res;
5924 /* Ignore errors here, we're only reading anyway */
5925 g_input_stream_close_finish (stream, close_res, NULL);
5926 g_object_unref (stream);
5928 res = g_simple_async_result_new (G_OBJECT (data->file),
5931 g_file_load_contents_async);
5932 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)load_contents_data_free);
5933 g_simple_async_result_complete (res);
5934 g_object_unref (res);
5938 load_contents_fstat_callback (GObject *obj,
5939 GAsyncResult *stat_res,
5942 GInputStream *stream = G_INPUT_STREAM (obj);
5943 LoadContentsData *data = user_data;
5946 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
5950 data->etag = g_strdup (g_file_info_get_etag (info));
5951 g_object_unref (info);
5954 g_input_stream_close_async (stream, 0,
5956 load_contents_close_callback, data);
5960 load_contents_read_callback (GObject *obj,
5961 GAsyncResult *read_res,
5964 GInputStream *stream = G_INPUT_STREAM (obj);
5965 LoadContentsData *data = user_data;
5966 GError *error = NULL;
5969 read_size = g_input_stream_read_finish (stream, read_res, &error);
5973 /* Error or EOF, close the file */
5974 data->error = error;
5975 g_input_stream_close_async (stream, 0,
5977 load_contents_close_callback, data);
5979 else if (read_size == 0)
5981 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
5982 G_FILE_ATTRIBUTE_ETAG_VALUE,
5985 load_contents_fstat_callback,
5988 else if (read_size > 0)
5990 data->pos += read_size;
5992 g_byte_array_set_size (data->content,
5993 data->pos + GET_CONTENT_BLOCK_SIZE);
5996 if (data->read_more_callback &&
5997 !data->read_more_callback ((char *)data->content->data, data->pos, data->user_data))
5998 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
5999 G_FILE_ATTRIBUTE_ETAG_VALUE,
6002 load_contents_fstat_callback,
6005 g_input_stream_read_async (stream,
6006 data->content->data + data->pos,
6007 GET_CONTENT_BLOCK_SIZE,
6010 load_contents_read_callback,
6016 load_contents_open_callback (GObject *obj,
6017 GAsyncResult *open_res,
6020 GFile *file = G_FILE (obj);
6021 GFileInputStream *stream;
6022 LoadContentsData *data = user_data;
6023 GError *error = NULL;
6024 GSimpleAsyncResult *res;
6026 stream = g_file_read_finish (file, open_res, &error);
6030 g_byte_array_set_size (data->content,
6031 data->pos + GET_CONTENT_BLOCK_SIZE);
6032 g_input_stream_read_async (G_INPUT_STREAM (stream),
6033 data->content->data + data->pos,
6034 GET_CONTENT_BLOCK_SIZE,
6037 load_contents_read_callback,
6043 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
6047 g_simple_async_result_complete (res);
6048 g_error_free (error);
6049 load_contents_data_free (data);
6050 g_object_unref (res);
6055 * g_file_load_partial_contents_async:
6056 * @file: input #GFile.
6057 * @cancellable: optional #GCancellable object, %NULL to ignore.
6058 * @read_more_callback: a #GFileReadMoreCallback to receive partial data and to specify whether further data should be read.
6059 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6060 * @user_data: the data to pass to the callback functions.
6062 * Reads the partial contents of a file. A #GFileReadMoreCallback should be
6063 * used to stop reading from the file when appropriate, else this function
6064 * will behave exactly as g_file_load_contents_async(). This operation
6065 * can be finished by g_file_load_partial_contents_finish().
6067 * Users of this function should be aware that @user_data is passed to
6068 * both the @read_more_callback and the @callback.
6070 * If @cancellable is not %NULL, then the operation can be cancelled by
6071 * triggering the cancellable object from another thread. If the operation
6072 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6075 g_file_load_partial_contents_async (GFile *file,
6076 GCancellable *cancellable,
6077 GFileReadMoreCallback read_more_callback,
6078 GAsyncReadyCallback callback,
6081 LoadContentsData *data;
6083 g_return_if_fail (G_IS_FILE (file));
6085 data = g_new0 (LoadContentsData, 1);
6088 data->cancellable = g_object_ref (cancellable);
6089 data->read_more_callback = read_more_callback;
6090 data->callback = callback;
6091 data->user_data = user_data;
6092 data->content = g_byte_array_new ();
6093 data->file = g_object_ref (file);
6095 g_file_read_async (file,
6098 load_contents_open_callback,
6103 * g_file_load_partial_contents_finish:
6104 * @file: input #GFile.
6105 * @res: a #GAsyncResult.
6106 * @contents: a location to place the contents of the file.
6107 * @length: a location to place the length of the contents of the file,
6108 * or %NULL if the length is not needed
6109 * @etag_out: a location to place the current entity tag for the file,
6110 * or %NULL if the entity tag is not needed
6111 * @error: a #GError, or %NULL
6113 * Finishes an asynchronous partial load operation that was started
6114 * with g_file_load_partial_contents_async(). The data is always
6115 * zero-terminated, but this is not included in the resultant @length.
6116 * The returned @content should be freed with g_free() when no longer
6119 * Returns: %TRUE if the load was successful. If %FALSE and @error is
6120 * present, it will be set appropriately.
6123 g_file_load_partial_contents_finish (GFile *file,
6130 GSimpleAsyncResult *simple;
6131 LoadContentsData *data;
6133 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6134 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
6135 g_return_val_if_fail (contents != NULL, FALSE);
6137 simple = G_SIMPLE_ASYNC_RESULT (res);
6139 if (g_simple_async_result_propagate_error (simple, error))
6142 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_load_contents_async);
6144 data = g_simple_async_result_get_op_res_gpointer (simple);
6148 g_propagate_error (error, data->error);
6157 *length = data->pos;
6161 *etag_out = data->etag;
6165 /* Zero terminate */
6166 g_byte_array_set_size (data->content, data->pos + 1);
6167 data->content->data[data->pos] = 0;
6169 *contents = (char *)g_byte_array_free (data->content, FALSE);
6170 data->content = NULL;
6176 * g_file_load_contents_async:
6177 * @file: input #GFile.
6178 * @cancellable: optional #GCancellable object, %NULL to ignore.
6179 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6180 * @user_data: the data to pass to callback function
6182 * Starts an asynchronous load of the @file's contents.
6184 * For more details, see g_file_load_contents() which is
6185 * the synchronous version of this call.
6187 * When the load operation has completed, @callback will be called
6188 * with @user data. To finish the operation, call
6189 * g_file_load_contents_finish() with the #GAsyncResult returned by
6192 * If @cancellable is not %NULL, then the operation can be cancelled by
6193 * triggering the cancellable object from another thread. If the operation
6194 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6197 g_file_load_contents_async (GFile *file,
6198 GCancellable *cancellable,
6199 GAsyncReadyCallback callback,
6202 g_file_load_partial_contents_async (file,
6205 callback, user_data);
6209 * g_file_load_contents_finish:
6210 * @file: input #GFile.
6211 * @res: a #GAsyncResult.
6212 * @contents: a location to place the contents of the file.
6213 * @length: a location to place the length of the contents of the file,
6214 * or %NULL if the length is not needed
6215 * @etag_out: a location to place the current entity tag for the file,
6216 * or %NULL if the entity tag is not needed
6217 * @error: a #GError, or %NULL
6219 * Finishes an asynchronous load of the @file's contents.
6220 * The contents are placed in @contents, and @length is set to the
6221 * size of the @contents string. The @content should be freed with
6222 * g_free() when no longer needed. If @etag_out is present, it will be
6223 * set to the new entity tag for the @file.
6225 * Returns: %TRUE if the load was successful. If %FALSE and @error is
6226 * present, it will be set appropriately.
6229 g_file_load_contents_finish (GFile *file,
6236 return g_file_load_partial_contents_finish (file,
6245 * g_file_replace_contents:
6246 * @file: input #GFile.
6247 * @contents: a string containing the new contents for @file.
6248 * @length: the length of @contents in bytes.
6249 * @etag: the old <link linkend="gfile-etag">entity tag</link>
6250 * for the document, or %NULL
6251 * @make_backup: %TRUE if a backup should be created.
6252 * @flags: a set of #GFileCreateFlags.
6253 * @new_etag: a location to a new <link linkend="gfile-etag">entity tag</link>
6254 * for the document. This should be freed with g_free() when no longer
6256 * @cancellable: optional #GCancellable object, %NULL to ignore.
6257 * @error: a #GError, or %NULL
6259 * Replaces the contents of @file with @contents of @length bytes.
6261 * If @etag is specified (not %NULL) any existing file must have that etag, or
6262 * the error %G_IO_ERROR_WRONG_ETAG will be returned.
6264 * If @make_backup is %TRUE, this function will attempt to make a backup of @file.
6266 * If @cancellable is not %NULL, then the operation can be cancelled by
6267 * triggering the cancellable object from another thread. If the operation
6268 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6270 * The returned @new_etag can be used to verify that the file hasn't changed the
6271 * next time it is saved over.
6273 * Returns: %TRUE if successful. If an error
6274 * has occurred, this function will return %FALSE and set @error
6275 * appropriately if present.
6278 g_file_replace_contents (GFile *file,
6279 const char *contents,
6282 gboolean make_backup,
6283 GFileCreateFlags flags,
6285 GCancellable *cancellable,
6288 GFileOutputStream *out;
6289 gsize pos, remainder;
6293 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6294 g_return_val_if_fail (contents != NULL, FALSE);
6296 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
6302 while (remainder > 0 &&
6303 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
6305 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
6313 if (remainder > 0 && res < 0)
6315 /* Ignore errors on close */
6316 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
6317 g_object_unref (out);
6319 /* error is set already */
6323 ret = g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error);
6326 *new_etag = g_file_output_stream_get_etag (out);
6328 g_object_unref (out);
6336 GCancellable *cancellable;
6337 GAsyncReadyCallback callback;
6339 const char *content;
6343 } ReplaceContentsData;
6346 replace_contents_data_free (ReplaceContentsData *data)
6349 g_error_free (data->error);
6350 if (data->cancellable)
6351 g_object_unref (data->cancellable);
6352 g_object_unref (data->file);
6353 g_free (data->etag);
6358 replace_contents_close_callback (GObject *obj,
6359 GAsyncResult *close_res,
6362 GOutputStream *stream = G_OUTPUT_STREAM (obj);
6363 ReplaceContentsData *data = user_data;
6364 GSimpleAsyncResult *res;
6366 /* Ignore errors here, we're only reading anyway */
6367 g_output_stream_close_finish (stream, close_res, NULL);
6368 g_object_unref (stream);
6370 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
6372 res = g_simple_async_result_new (G_OBJECT (data->file),
6375 g_file_replace_contents_async);
6376 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_contents_data_free);
6377 g_simple_async_result_complete (res);
6378 g_object_unref (res);
6382 replace_contents_write_callback (GObject *obj,
6383 GAsyncResult *read_res,
6386 GOutputStream *stream = G_OUTPUT_STREAM (obj);
6387 ReplaceContentsData *data = user_data;
6388 GError *error = NULL;
6391 write_size = g_output_stream_write_finish (stream, read_res, &error);
6393 if (write_size <= 0)
6395 /* Error or EOF, close the file */
6397 data->error = error;
6398 g_output_stream_close_async (stream, 0,
6400 replace_contents_close_callback, data);
6402 else if (write_size > 0)
6404 data->pos += write_size;
6406 if (data->pos >= data->length)
6407 g_output_stream_close_async (stream, 0,
6409 replace_contents_close_callback, data);
6411 g_output_stream_write_async (stream,
6412 data->content + data->pos,
6413 data->length - data->pos,
6416 replace_contents_write_callback,
6422 replace_contents_open_callback (GObject *obj,
6423 GAsyncResult *open_res,
6426 GFile *file = G_FILE (obj);
6427 GFileOutputStream *stream;
6428 ReplaceContentsData *data = user_data;
6429 GError *error = NULL;
6430 GSimpleAsyncResult *res;
6432 stream = g_file_replace_finish (file, open_res, &error);
6436 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
6437 data->content + data->pos,
6438 data->length - data->pos,
6441 replace_contents_write_callback,
6447 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
6451 g_simple_async_result_complete (res);
6452 g_error_free (error);
6453 replace_contents_data_free (data);
6454 g_object_unref (res);
6459 * g_file_replace_contents_async:
6460 * @file: input #GFile.
6461 * @contents: string of contents to replace the file with.
6462 * @length: the length of @contents in bytes.
6463 * @etag: a new <link linkend="gfile-etag">entity tag</link> for the @file, or %NULL
6464 * @make_backup: %TRUE if a backup should be created.
6465 * @flags: a set of #GFileCreateFlags.
6466 * @cancellable: optional #GCancellable object, %NULL to ignore.
6467 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6468 * @user_data: the data to pass to callback function
6470 * Starts an asynchronous replacement of @file with the given
6471 * @contents of @length bytes. @etag will replace the document's
6472 * current entity tag.
6474 * When this operation has completed, @callback will be called with
6475 * @user_user data, and the operation can be finalized with
6476 * g_file_replace_contents_finish().
6478 * If @cancellable is not %NULL, then the operation can be cancelled by
6479 * triggering the cancellable object from another thread. If the operation
6480 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6482 * If @make_backup is %TRUE, this function will attempt to
6483 * make a backup of @file.
6486 g_file_replace_contents_async (GFile *file,
6487 const char *contents,
6490 gboolean make_backup,
6491 GFileCreateFlags flags,
6492 GCancellable *cancellable,
6493 GAsyncReadyCallback callback,
6496 ReplaceContentsData *data;
6498 g_return_if_fail (G_IS_FILE (file));
6499 g_return_if_fail (contents != NULL);
6501 data = g_new0 (ReplaceContentsData, 1);
6504 data->cancellable = g_object_ref (cancellable);
6505 data->callback = callback;
6506 data->user_data = user_data;
6507 data->content = contents;
6508 data->length = length;
6510 data->file = g_object_ref (file);
6512 g_file_replace_async (file,
6518 replace_contents_open_callback,
6523 * g_file_replace_contents_finish:
6524 * @file: input #GFile.
6525 * @res: a #GAsyncResult.
6526 * @new_etag: a location of a new <link linkend="gfile-etag">entity tag</link>
6527 * for the document. This should be freed with g_free() when it is no
6528 * longer needed, or %NULL
6529 * @error: a #GError, or %NULL
6531 * Finishes an asynchronous replace of the given @file. See
6532 * g_file_replace_contents_async(). Sets @new_etag to the new entity
6533 * tag for the document, if present.
6535 * Returns: %TRUE on success, %FALSE on failure.
6538 g_file_replace_contents_finish (GFile *file,
6543 GSimpleAsyncResult *simple;
6544 ReplaceContentsData *data;
6546 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6547 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
6549 simple = G_SIMPLE_ASYNC_RESULT (res);
6551 if (g_simple_async_result_propagate_error (simple, error))
6554 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_replace_contents_async);
6556 data = g_simple_async_result_get_op_res_gpointer (simple);
6560 g_propagate_error (error, data->error);
6568 *new_etag = data->etag;
6569 data->etag = NULL; /* Take ownership */
6575 #define __G_FILE_C__
6576 #include "gioaliasdef.c"