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 "gsimpleasyncresult.h"
35 #include "gfileattribute-priv.h"
36 #include "gpollfilemonitor.h"
38 #include "gfileinputstream.h"
39 #include "gfileoutputstream.h"
40 #include "gcancellable.h"
41 #include "gasyncresult.h"
49 * @short_description: File and Directory Handling
51 * @see_also: #GFileInfo, #GFileEnumerator
53 * #GFile is a high level abstraction for manipulating files on a
54 * virtual file system. #GFile<!-- -->s are lightweight, immutable
55 * objects that do no I/O upon creation. It is necessary to understand that
56 * #GFile objects do not represent files, merely an identifier for a file. All
57 * file content I/O is implemented as streaming operations (see #GInputStream and
60 * To construct a #GFile, you can use:
61 * g_file_new_for_path() if you have a path.
62 * g_file_new_for_uri() if you have a URI.
63 * g_file_new_for_commandline_arg() for a command line argument.
64 * g_file_parse_name() from a utf8 string gotten from g_file_get_parse_name().
66 * One way to think of a #GFile is as an abstraction of a pathname. For normal
67 * files the system pathname is what is stored internally, but as #GFile<!-- -->s
68 * are extensible it could also be something else that corresponds to a pathname
69 * in a userspace implementation of a filesystem.
71 * #GFile<!-- -->s make up hierarchies of directories and files that correspond to the
72 * files on a filesystem. You can move through the file system with #GFile using
73 * g_file_get_parent() to get an identifier for the parent directory, g_file_get_child()
74 * to get a child within a directory, g_file_resolve_relative_path() to resolve a relative
75 * path between two #GFile<!-- -->s. There can be multiple hierarchies, so you may not
76 * end up at the same root if you repeatedly call g_file_get_parent() on two different
79 * All #GFile<!-- -->s have a basename (get with g_file_get_basename()). These names
80 * are byte strings that are used to identify the file on the filesystem (relative to
81 * its parent directory) and there is no guarantees that they have any particular charset
82 * encoding or even make any sense at all. If you want to use filenames in a user
83 * interface you should use the display name that you can get by requesting the
84 * %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME attribute with g_file_query_info().
85 * This is guaranteed to be in utf8 and can be used in a user interface. But always
86 * store the real basename or the #GFile to use to actually access the file, because
87 * there is no way to go from a display name to the actual name.
89 * Using #GFile as an identifier has the same weaknesses as using a path in that
90 * there may be multiple aliases for the same file. For instance, hard or
91 * soft links may cause two different #GFile<!-- -->s to refer to the same file.
92 * Other possible causes for aliases are: case insensitive filesystems, short
93 * and long names on Fat/NTFS, or bind mounts in Linux. If you want to check if
94 * two #GFile<!-- -->s point to the same file you can query for the
95 * %G_FILE_ATTRIBUTE_ID_FILE attribute. Note that #GFile does some trivial
96 * canonicalization of pathnames passed in, so that trivial differences in the
97 * path string used at creation (duplicated slashes, slash at end of path, "."
98 * or ".." path segments, etc) does not create different #GFile<!-- -->s.
100 * Many #GFile operations have both synchronous and asynchronous versions
101 * to suit your application. Asynchronous versions of synchronous functions
102 * simply have _async() appended to their function names. The asynchronous
103 * I/O functions call a #GAsyncReadyCallback which is then used to finalize
104 * the operation, producing a GAsyncResult which is then passed to the
105 * function's matching _finish() operation.
107 * Some #GFile operations do not have synchronous analogs, as they may
108 * take a very long time to finish, and blocking may leave an application
109 * unusable. Notable cases include:
110 * g_file_mount_mountable() to mount a mountable file.
111 * g_file_unmount_mountable_with_operation() to unmount a mountable file.
112 * g_file_eject_mountable_with_operation() to eject a mountable file.
114 * <para id="gfile-etag"><indexterm><primary>entity tag</primary></indexterm>
115 * One notable feature of #GFile<!-- -->s are entity tags, or "etags" for
116 * short. Entity tags are somewhat like a more abstract version of the
117 * traditional mtime, and can be used to quickly determine if the file has
118 * been modified from the version on the file system. See the HTTP 1.1
119 * <ulink url="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html">specification</ulink>
120 * for HTTP Etag headers, which are a very similar concept.
124 static void g_file_real_query_info_async (GFile *file,
125 const char *attributes,
126 GFileQueryInfoFlags flags,
128 GCancellable *cancellable,
129 GAsyncReadyCallback callback,
131 static GFileInfo * g_file_real_query_info_finish (GFile *file,
134 static void g_file_real_query_filesystem_info_async (GFile *file,
135 const char *attributes,
137 GCancellable *cancellable,
138 GAsyncReadyCallback callback,
140 static GFileInfo * g_file_real_query_filesystem_info_finish (GFile *file,
143 static void g_file_real_enumerate_children_async (GFile *file,
144 const char *attributes,
145 GFileQueryInfoFlags flags,
147 GCancellable *cancellable,
148 GAsyncReadyCallback callback,
150 static GFileEnumerator * g_file_real_enumerate_children_finish (GFile *file,
153 static void g_file_real_read_async (GFile *file,
155 GCancellable *cancellable,
156 GAsyncReadyCallback callback,
158 static GFileInputStream * g_file_real_read_finish (GFile *file,
161 static void g_file_real_append_to_async (GFile *file,
162 GFileCreateFlags flags,
164 GCancellable *cancellable,
165 GAsyncReadyCallback callback,
167 static GFileOutputStream *g_file_real_append_to_finish (GFile *file,
170 static void g_file_real_create_async (GFile *file,
171 GFileCreateFlags flags,
173 GCancellable *cancellable,
174 GAsyncReadyCallback callback,
176 static GFileOutputStream *g_file_real_create_finish (GFile *file,
179 static void g_file_real_replace_async (GFile *file,
181 gboolean make_backup,
182 GFileCreateFlags flags,
184 GCancellable *cancellable,
185 GAsyncReadyCallback callback,
187 static GFileOutputStream *g_file_real_replace_finish (GFile *file,
190 static void g_file_real_open_readwrite_async (GFile *file,
192 GCancellable *cancellable,
193 GAsyncReadyCallback callback,
195 static GFileIOStream * g_file_real_open_readwrite_finish (GFile *file,
198 static void g_file_real_create_readwrite_async (GFile *file,
199 GFileCreateFlags flags,
201 GCancellable *cancellable,
202 GAsyncReadyCallback callback,
204 static GFileIOStream * g_file_real_create_readwrite_finish (GFile *file,
207 static void g_file_real_replace_readwrite_async (GFile *file,
209 gboolean make_backup,
210 GFileCreateFlags flags,
212 GCancellable *cancellable,
213 GAsyncReadyCallback callback,
215 static GFileIOStream * g_file_real_replace_readwrite_finish (GFile *file,
218 static gboolean g_file_real_set_attributes_from_info (GFile *file,
220 GFileQueryInfoFlags flags,
221 GCancellable *cancellable,
223 static void g_file_real_set_display_name_async (GFile *file,
224 const char *display_name,
226 GCancellable *cancellable,
227 GAsyncReadyCallback callback,
229 static GFile * g_file_real_set_display_name_finish (GFile *file,
232 static void g_file_real_set_attributes_async (GFile *file,
234 GFileQueryInfoFlags flags,
236 GCancellable *cancellable,
237 GAsyncReadyCallback callback,
239 static gboolean g_file_real_set_attributes_finish (GFile *file,
243 static void g_file_real_find_enclosing_mount_async (GFile *file,
245 GCancellable *cancellable,
246 GAsyncReadyCallback callback,
248 static GMount * g_file_real_find_enclosing_mount_finish (GFile *file,
251 static void g_file_real_copy_async (GFile *source,
253 GFileCopyFlags flags,
255 GCancellable *cancellable,
256 GFileProgressCallback progress_callback,
257 gpointer progress_callback_data,
258 GAsyncReadyCallback callback,
260 static gboolean g_file_real_copy_finish (GFile *file,
264 typedef GFileIface GFileInterface;
265 G_DEFINE_INTERFACE (GFile, g_file, G_TYPE_OBJECT)
268 g_file_default_init (GFileIface *iface)
270 iface->enumerate_children_async = g_file_real_enumerate_children_async;
271 iface->enumerate_children_finish = g_file_real_enumerate_children_finish;
272 iface->set_display_name_async = g_file_real_set_display_name_async;
273 iface->set_display_name_finish = g_file_real_set_display_name_finish;
274 iface->query_info_async = g_file_real_query_info_async;
275 iface->query_info_finish = g_file_real_query_info_finish;
276 iface->query_filesystem_info_async = g_file_real_query_filesystem_info_async;
277 iface->query_filesystem_info_finish = g_file_real_query_filesystem_info_finish;
278 iface->set_attributes_async = g_file_real_set_attributes_async;
279 iface->set_attributes_finish = g_file_real_set_attributes_finish;
280 iface->read_async = g_file_real_read_async;
281 iface->read_finish = g_file_real_read_finish;
282 iface->append_to_async = g_file_real_append_to_async;
283 iface->append_to_finish = g_file_real_append_to_finish;
284 iface->create_async = g_file_real_create_async;
285 iface->create_finish = g_file_real_create_finish;
286 iface->replace_async = g_file_real_replace_async;
287 iface->replace_finish = g_file_real_replace_finish;
288 iface->open_readwrite_async = g_file_real_open_readwrite_async;
289 iface->open_readwrite_finish = g_file_real_open_readwrite_finish;
290 iface->create_readwrite_async = g_file_real_create_readwrite_async;
291 iface->create_readwrite_finish = g_file_real_create_readwrite_finish;
292 iface->replace_readwrite_async = g_file_real_replace_readwrite_async;
293 iface->replace_readwrite_finish = g_file_real_replace_readwrite_finish;
294 iface->find_enclosing_mount_async = g_file_real_find_enclosing_mount_async;
295 iface->find_enclosing_mount_finish = g_file_real_find_enclosing_mount_finish;
296 iface->set_attributes_from_info = g_file_real_set_attributes_from_info;
297 iface->copy_async = g_file_real_copy_async;
298 iface->copy_finish = g_file_real_copy_finish;
304 * @file: input #GFile.
306 * Checks to see if a file is native to the platform.
308 * A native file s one expressed in the platform-native filename format,
309 * e.g. "C:\Windows" or "/usr/bin/". This does not mean the file is local,
310 * as it might be on a locally mounted remote filesystem.
312 * On some systems non-native files may be available using
313 * the native filesystem via a userspace filesystem (FUSE), in
314 * these cases this call will return %FALSE, but g_file_get_path()
315 * will still return a native path.
317 * This call does no blocking i/o.
319 * Returns: %TRUE if file is native.
322 g_file_is_native (GFile *file)
326 g_return_val_if_fail (G_IS_FILE (file), FALSE);
328 iface = G_FILE_GET_IFACE (file);
330 return (* iface->is_native) (file);
335 * g_file_has_uri_scheme:
336 * @file: input #GFile.
337 * @uri_scheme: a string containing a URI scheme.
339 * Checks to see if a #GFile has a given URI scheme.
341 * This call does no blocking i/o.
343 * Returns: %TRUE if #GFile's backend supports the
344 * given URI scheme, %FALSE if URI scheme is %NULL,
345 * not supported, or #GFile is invalid.
348 g_file_has_uri_scheme (GFile *file,
349 const char *uri_scheme)
353 g_return_val_if_fail (G_IS_FILE (file), FALSE);
354 g_return_val_if_fail (uri_scheme != NULL, FALSE);
356 iface = G_FILE_GET_IFACE (file);
358 return (* iface->has_uri_scheme) (file, uri_scheme);
363 * g_file_get_uri_scheme:
364 * @file: input #GFile.
366 * Gets the URI scheme for a #GFile.
367 * RFC 3986 decodes the scheme as:
369 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
371 * Common schemes include "file", "http", "ftp", etc.
373 * This call does no blocking i/o.
375 * Returns: a string containing the URI scheme for the given
376 * #GFile. The returned string should be freed with g_free()
377 * when no longer needed.
380 g_file_get_uri_scheme (GFile *file)
384 g_return_val_if_fail (G_IS_FILE (file), NULL);
386 iface = G_FILE_GET_IFACE (file);
388 return (* iface->get_uri_scheme) (file);
393 * g_file_get_basename:
394 * @file: input #GFile.
396 * Gets the base name (the last component of the path) for a given #GFile.
398 * If called for the top level of a system (such as the filesystem root
399 * or a uri like sftp://host/) it will return a single directory separator
400 * (and on Windows, possibly a drive letter).
402 * The base name is a byte string (*not* UTF-8). It has no defined encoding
403 * or rules other than it may not contain zero bytes. If you want to use
404 * filenames in a user interface you should use the display name that you
405 * can get by requesting the %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME
406 * attribute with g_file_query_info().
408 * This call does no blocking i/o.
410 * Returns: string containing the #GFile's base name, or %NULL
411 * if given #GFile is invalid. The returned string should be
412 * freed with g_free() when no longer needed.
415 g_file_get_basename (GFile *file)
419 g_return_val_if_fail (G_IS_FILE (file), NULL);
421 iface = G_FILE_GET_IFACE (file);
423 return (* iface->get_basename) (file);
428 * @file: input #GFile.
430 * Gets the local pathname for #GFile, if one exists.
432 * This call does no blocking i/o.
434 * Returns: string containing the #GFile's path, or %NULL if
435 * no such path exists. The returned string should be
436 * freed with g_free() when no longer needed.
439 g_file_get_path (GFile *file)
443 g_return_val_if_fail (G_IS_FILE (file), NULL);
445 iface = G_FILE_GET_IFACE (file);
447 return (* iface->get_path) (file);
452 * @file: input #GFile.
454 * Gets the URI for the @file.
456 * This call does no blocking i/o.
458 * Returns: a string containing the #GFile's URI.
459 * The returned string should be freed with g_free() when no longer needed.
462 g_file_get_uri (GFile *file)
466 g_return_val_if_fail (G_IS_FILE (file), NULL);
468 iface = G_FILE_GET_IFACE (file);
470 return (* iface->get_uri) (file);
474 * g_file_get_parse_name:
475 * @file: input #GFile.
477 * Gets the parse name of the @file.
478 * A parse name is a UTF-8 string that describes the
479 * file such that one can get the #GFile back using
480 * g_file_parse_name().
482 * This is generally used to show the #GFile as a nice
483 * full-pathname kind of string in a user interface,
484 * like in a location entry.
486 * For local files with names that can safely be converted
487 * to UTF8 the pathname is used, otherwise the IRI is used
488 * (a form of URI that allows UTF8 characters unescaped).
490 * This call does no blocking i/o.
492 * Returns: a string containing the #GFile's parse name. The returned
493 * string should be freed with g_free() when no longer needed.
496 g_file_get_parse_name (GFile *file)
500 g_return_val_if_fail (G_IS_FILE (file), NULL);
502 iface = G_FILE_GET_IFACE (file);
504 return (* iface->get_parse_name) (file);
509 * @file: input #GFile.
511 * Duplicates a #GFile handle. This operation does not duplicate
512 * the actual file or directory represented by the #GFile; see
513 * g_file_copy() if attempting to copy a file.
515 * This call does no blocking i/o.
517 * Returns: a new #GFile that is a duplicate of the given #GFile.
520 g_file_dup (GFile *file)
524 g_return_val_if_fail (G_IS_FILE (file), NULL);
526 iface = G_FILE_GET_IFACE (file);
528 return (* iface->dup) (file);
533 * @file: #gconstpointer to a #GFile.
535 * Creates a hash value for a #GFile.
537 * This call does no blocking i/o.
539 * Returns: 0 if @file is not a valid #GFile, otherwise an
540 * integer that can be used as hash value for the #GFile.
541 * This function is intended for easily hashing a #GFile to
542 * add to a #GHashTable or similar data structure.
545 g_file_hash (gconstpointer file)
549 g_return_val_if_fail (G_IS_FILE (file), 0);
551 iface = G_FILE_GET_IFACE (file);
553 return (* iface->hash) ((GFile *)file);
558 * @file1: the first #GFile.
559 * @file2: the second #GFile.
561 * Checks equality of two given #GFile<!-- -->s. Note that two
562 * #GFile<!-- -->s that differ can still refer to the same
563 * file on the filesystem due to various forms of filename
566 * This call does no blocking i/o.
568 * Returns: %TRUE if @file1 and @file2 are equal.
569 * %FALSE if either is not a #GFile.
572 g_file_equal (GFile *file1,
577 g_return_val_if_fail (G_IS_FILE (file1), FALSE);
578 g_return_val_if_fail (G_IS_FILE (file2), FALSE);
580 if (G_TYPE_FROM_INSTANCE (file1) != G_TYPE_FROM_INSTANCE (file2))
583 iface = G_FILE_GET_IFACE (file1);
585 return (* iface->equal) (file1, file2);
591 * @file: input #GFile.
593 * Gets the parent directory for the @file.
594 * If the @file represents the root directory of the
595 * file system, then %NULL will be returned.
597 * This call does no blocking i/o.
599 * Returns: a #GFile structure to the parent of the given
600 * #GFile or %NULL if there is no parent.
601 * Free the returned object with g_object_unref().
604 g_file_get_parent (GFile *file)
608 g_return_val_if_fail (G_IS_FILE (file), NULL);
610 iface = G_FILE_GET_IFACE (file);
612 return (* iface->get_parent) (file);
617 * @file: input #GFile
618 * @parent: the parent to check for, or %NULL
620 * Checks if @file has a parent, and optionally, if it is @parent.
622 * If @parent is %NULL then this function returns %TRUE if @file has any
623 * parent at all. If @parent is non-%NULL then %TRUE is only returned
624 * if @file is a child of @parent.
626 * Returns: %TRUE if @file is a child of @parent (or any parent in the
627 * case that @parent is %NULL).
632 g_file_has_parent (GFile *file,
635 GFile *actual_parent;
638 g_return_val_if_fail (G_IS_FILE (file), FALSE);
639 g_return_val_if_fail (parent == NULL || G_IS_FILE (parent), FALSE);
641 actual_parent = g_file_get_parent (file);
643 if (actual_parent != NULL)
646 result = g_file_equal (parent, actual_parent);
650 g_object_unref (actual_parent);
660 * @file: input #GFile.
661 * @name: string containing the child's basename.
663 * Gets a child of @file with basename equal to @name.
665 * Note that the file with that specific name might not exist, but
666 * you can still have a #GFile that points to it. You can use this
667 * for instance to create that file.
669 * This call does no blocking i/o.
671 * Returns: a #GFile to a child specified by @name.
672 * Free the returned object with g_object_unref().
675 g_file_get_child (GFile *file,
678 g_return_val_if_fail (G_IS_FILE (file), NULL);
679 g_return_val_if_fail (name != NULL, NULL);
681 return g_file_resolve_relative_path (file, name);
685 * g_file_get_child_for_display_name:
686 * @file: input #GFile.
687 * @display_name: string to a possible child.
690 * Gets the child of @file for a given @display_name (i.e. a UTF8
691 * version of the name). If this function fails, it returns %NULL and @error will be
692 * set. This is very useful when constructing a GFile for a new file
693 * and the user entered the filename in the user interface, for instance
694 * when you select a directory and type a filename in the file selector.
696 * This call does no blocking i/o.
698 * Returns: a #GFile to the specified child, or
699 * %NULL if the display name couldn't be converted.
700 * Free the returned object with g_object_unref().
703 g_file_get_child_for_display_name (GFile *file,
704 const char *display_name,
709 g_return_val_if_fail (G_IS_FILE (file), NULL);
710 g_return_val_if_fail (display_name != NULL, NULL);
712 iface = G_FILE_GET_IFACE (file);
714 return (* iface->get_child_for_display_name) (file, display_name, error);
719 * @file: input #GFile.
720 * @prefix: input #GFile.
722 * Checks whether @file has the prefix specified by @prefix. In other word,
723 * if the names of inital elements of @file<!-- -->s pathname match @prefix.
724 * Only full pathname elements are matched, so a path like /foo is not
725 * considered a prefix of /foobar, only of /foo/bar.
727 * This call does no i/o, as it works purely on names. As such it can
728 * sometimes return %FALSE even if @file is inside a @prefix (from a
729 * filesystem point of view), because the prefix of @file is an alias
732 * Returns: %TRUE if the @files's parent, grandparent, etc is @prefix.
736 g_file_has_prefix (GFile *file,
741 g_return_val_if_fail (G_IS_FILE (file), FALSE);
742 g_return_val_if_fail (G_IS_FILE (prefix), FALSE);
744 if (G_TYPE_FROM_INSTANCE (file) != G_TYPE_FROM_INSTANCE (prefix))
747 iface = G_FILE_GET_IFACE (file);
749 /* The vtable function differs in arg order since we're
750 using the old contains_file call */
751 return (* iface->prefix_matches) (prefix, file);
755 * g_file_get_relative_path:
756 * @parent: input #GFile.
757 * @descendant: input #GFile.
759 * Gets the path for @descendant relative to @parent.
761 * This call does no blocking i/o.
763 * Returns: string with the relative path from @descendant
764 * to @parent, or %NULL if @descendant doesn't have @parent as prefix.
765 * The returned string should be freed with g_free() when no longer needed.
768 g_file_get_relative_path (GFile *parent,
773 g_return_val_if_fail (G_IS_FILE (parent), NULL);
774 g_return_val_if_fail (G_IS_FILE (descendant), NULL);
776 if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
779 iface = G_FILE_GET_IFACE (parent);
781 return (* iface->get_relative_path) (parent, descendant);
785 * g_file_resolve_relative_path:
786 * @file: input #GFile.
787 * @relative_path: a given relative path string.
789 * Resolves a relative path for @file to an absolute path.
791 * This call does no blocking i/o.
793 * Returns: #GFile to the resolved path. %NULL if @relative_path
794 * is %NULL or if @file is invalid.
795 * Free the returned object with g_object_unref().
798 g_file_resolve_relative_path (GFile *file,
799 const char *relative_path)
803 g_return_val_if_fail (G_IS_FILE (file), NULL);
804 g_return_val_if_fail (relative_path != NULL, NULL);
806 iface = G_FILE_GET_IFACE (file);
808 return (* iface->resolve_relative_path) (file, relative_path);
812 * g_file_enumerate_children:
813 * @file: input #GFile.
814 * @attributes: an attribute query string.
815 * @flags: a set of #GFileQueryInfoFlags.
816 * @cancellable: optional #GCancellable object, %NULL to ignore.
817 * @error: #GError for error reporting.
819 * Gets the requested information about the files in a directory. The result
820 * is a #GFileEnumerator object that will give out #GFileInfo objects for
821 * all the files in the directory.
823 * The @attribute value is a string that specifies the file attributes that
824 * should be gathered. It is not an error if it's not possible to read a particular
825 * requested attribute from a file - it just won't be set. @attribute should
826 * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
827 * means all attributes, and a wildcard like "standard::*" means all attributes in the standard
828 * namespace. An example attribute query be "standard::*,owner::user".
829 * The standard attributes are available as defines, like #G_FILE_ATTRIBUTE_STANDARD_NAME.
831 * If @cancellable is not %NULL, then the operation can be cancelled by
832 * triggering the cancellable object from another thread. If the operation
833 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
835 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
836 * If the file is not a directory, the G_FILE_ERROR_NOTDIR error will be returned.
837 * Other errors are possible too.
839 * Returns: A #GFileEnumerator if successful, %NULL on error.
840 * Free the returned object with g_object_unref().
843 g_file_enumerate_children (GFile *file,
844 const char *attributes,
845 GFileQueryInfoFlags flags,
846 GCancellable *cancellable,
852 g_return_val_if_fail (G_IS_FILE (file), NULL);
854 if (g_cancellable_set_error_if_cancelled (cancellable, error))
857 iface = G_FILE_GET_IFACE (file);
859 if (iface->enumerate_children == NULL)
861 g_set_error_literal (error, G_IO_ERROR,
862 G_IO_ERROR_NOT_SUPPORTED,
863 _("Operation not supported"));
867 return (* iface->enumerate_children) (file, attributes, flags,
872 * g_file_enumerate_children_async:
873 * @file: input #GFile.
874 * @attributes: an attribute query string.
875 * @flags: a set of #GFileQueryInfoFlags.
876 * @io_priority: the <link linkend="io-priority">I/O priority</link>
878 * @cancellable: optional #GCancellable object, %NULL to ignore.
879 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
880 * @user_data: the data to pass to callback function
882 * Asynchronously gets the requested information about the files in a directory. The result
883 * is a #GFileEnumerator object that will give out #GFileInfo objects for
884 * all the files in the directory.
886 * For more details, see g_file_enumerate_children() which is
887 * the synchronous version of this call.
889 * When the operation is finished, @callback will be called. You can then call
890 * g_file_enumerate_children_finish() to get the result of the operation.
893 g_file_enumerate_children_async (GFile *file,
894 const char *attributes,
895 GFileQueryInfoFlags flags,
897 GCancellable *cancellable,
898 GAsyncReadyCallback callback,
903 g_return_if_fail (G_IS_FILE (file));
905 iface = G_FILE_GET_IFACE (file);
906 (* iface->enumerate_children_async) (file,
916 * g_file_enumerate_children_finish:
917 * @file: input #GFile.
918 * @res: a #GAsyncResult.
921 * Finishes an async enumerate children operation.
922 * See g_file_enumerate_children_async().
924 * Returns: a #GFileEnumerator or %NULL if an error occurred.
925 * Free the returned object with g_object_unref().
928 g_file_enumerate_children_finish (GFile *file,
934 g_return_val_if_fail (G_IS_FILE (file), NULL);
935 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
937 if (G_IS_SIMPLE_ASYNC_RESULT (res))
939 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
940 if (g_simple_async_result_propagate_error (simple, error))
944 iface = G_FILE_GET_IFACE (file);
945 return (* iface->enumerate_children_finish) (file, res, error);
949 * g_file_query_exists:
950 * @file: input #GFile.
951 * @cancellable: optional #GCancellable object, %NULL to ignore.
953 * Utility function to check if a particular file exists. This is
954 * implemented using g_file_query_info() and as such does blocking I/O.
956 * Note that in many cases it is racy to first check for file existence
957 * and then execute something based on the outcome of that, because the
958 * file might have been created or removed in between the operations. The
959 * general approach to handling that is to not check, but just do the
960 * operation and handle the errors as they come.
962 * As an example of race-free checking, take the case of reading a file, and
963 * if it doesn't exist, creating it. There are two racy versions: read it, and
964 * on error create it; and: check if it exists, if not create it. These
965 * can both result in two processes creating the file (with perhaps a partially
966 * written file as the result). The correct approach is to always try to create
967 * the file with g_file_create() which will either atomically create the file
968 * or fail with a G_IO_ERROR_EXISTS error.
970 * However, in many cases an existence check is useful in a user
971 * interface, for instance to make a menu item sensitive/insensitive, so that
972 * you don't have to fool users that something is possible and then just show
973 * and error dialog. If you do this, you should make sure to also handle the
974 * errors that can happen due to races when you execute the operation.
976 * Returns: %TRUE if the file exists (and can be detected without error), %FALSE otherwise (or if cancelled).
979 g_file_query_exists (GFile *file,
980 GCancellable *cancellable)
984 g_return_val_if_fail (G_IS_FILE(file), FALSE);
986 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE,
987 G_FILE_QUERY_INFO_NONE, cancellable, NULL);
990 g_object_unref (info);
998 * g_file_query_file_type:
999 * @file: input #GFile.
1000 * @flags: a set of #GFileQueryInfoFlags passed to g_file_query_info().
1001 * @cancellable: optional #GCancellable object, %NULL to ignore.
1003 * Utility function to inspect the #GFileType of a file. This is
1004 * implemented using g_file_query_info() and as such does blocking I/O.
1006 * The primary use case of this method is to check if a file is a regular file,
1007 * directory, or symlink.
1009 * Returns: The #GFileType of the file and #G_FILE_TYPE_UNKNOWN if the file
1015 g_file_query_file_type (GFile *file,
1016 GFileQueryInfoFlags flags,
1017 GCancellable *cancellable)
1020 GFileType file_type;
1022 g_return_val_if_fail (G_IS_FILE(file), G_FILE_TYPE_UNKNOWN);
1023 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE, flags,
1027 file_type = g_file_info_get_file_type (info);
1028 g_object_unref (info);
1031 file_type = G_FILE_TYPE_UNKNOWN;
1037 * g_file_query_info:
1038 * @file: input #GFile.
1039 * @attributes: an attribute query string.
1040 * @flags: a set of #GFileQueryInfoFlags.
1041 * @cancellable: optional #GCancellable object, %NULL to ignore.
1042 * @error: a #GError.
1044 * Gets the requested information about specified @file. The result
1045 * is a #GFileInfo object that contains key-value attributes (such as
1046 * the type or size of the file).
1048 * The @attribute value is a string that specifies the file attributes that
1049 * should be gathered. It is not an error if it's not possible to read a particular
1050 * requested attribute from a file - it just won't be set. @attribute should
1051 * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
1052 * means all attributes, and a wildcard like "standard::*" means all attributes in the standard
1053 * namespace. An example attribute query be "standard::*,owner::user".
1054 * The standard attributes are available as defines, like #G_FILE_ATTRIBUTE_STANDARD_NAME.
1056 * If @cancellable is not %NULL, then the operation can be cancelled by
1057 * triggering the cancellable object from another thread. If the operation
1058 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1060 * For symlinks, normally the information about the target of the
1061 * symlink is returned, rather than information about the symlink itself.
1062 * However if you pass #G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS in @flags the
1063 * information about the symlink itself will be returned. Also, for symlinks
1064 * that point to non-existing files the information about the symlink itself
1067 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1068 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1070 * Returns: a #GFileInfo for the given @file, or %NULL on error.
1071 * Free the returned object with g_object_unref().
1074 g_file_query_info (GFile *file,
1075 const char *attributes,
1076 GFileQueryInfoFlags flags,
1077 GCancellable *cancellable,
1082 g_return_val_if_fail (G_IS_FILE (file), NULL);
1084 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1087 iface = G_FILE_GET_IFACE (file);
1089 if (iface->query_info == NULL)
1091 g_set_error_literal (error, G_IO_ERROR,
1092 G_IO_ERROR_NOT_SUPPORTED,
1093 _("Operation not supported"));
1097 return (* iface->query_info) (file, attributes, flags, cancellable, error);
1101 * g_file_query_info_async:
1102 * @file: input #GFile.
1103 * @attributes: an attribute query string.
1104 * @flags: a set of #GFileQueryInfoFlags.
1105 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1107 * @cancellable: optional #GCancellable object, %NULL to ignore.
1108 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1109 * @user_data: the data to pass to callback function
1111 * Asynchronously gets the requested information about specified @file. The result
1112 * is a #GFileInfo object that contains key-value attributes (such as type or size
1115 * For more details, see g_file_query_info() which is
1116 * the synchronous version of this call.
1118 * When the operation is finished, @callback will be called. You can then call
1119 * g_file_query_info_finish() to get the result of the operation.
1122 g_file_query_info_async (GFile *file,
1123 const char *attributes,
1124 GFileQueryInfoFlags flags,
1126 GCancellable *cancellable,
1127 GAsyncReadyCallback callback,
1132 g_return_if_fail (G_IS_FILE (file));
1134 iface = G_FILE_GET_IFACE (file);
1135 (* iface->query_info_async) (file,
1145 * g_file_query_info_finish:
1146 * @file: input #GFile.
1147 * @res: a #GAsyncResult.
1148 * @error: a #GError.
1150 * Finishes an asynchronous file info query.
1151 * See g_file_query_info_async().
1153 * Returns: #GFileInfo for given @file or %NULL on error.
1154 * Free the returned object with g_object_unref().
1157 g_file_query_info_finish (GFile *file,
1163 g_return_val_if_fail (G_IS_FILE (file), NULL);
1164 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1166 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1168 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1169 if (g_simple_async_result_propagate_error (simple, error))
1173 iface = G_FILE_GET_IFACE (file);
1174 return (* iface->query_info_finish) (file, res, error);
1178 * g_file_query_filesystem_info:
1179 * @file: input #GFile.
1180 * @attributes: an attribute query string.
1181 * @cancellable: optional #GCancellable object, %NULL to ignore.
1182 * @error: a #GError.
1184 * Similar to g_file_query_info(), but obtains information
1185 * about the filesystem the @file is on, rather than the file itself.
1186 * For instance the amount of space available and the type of
1189 * The @attribute value is a string that specifies the file attributes that
1190 * should be gathered. It is not an error if it's not possible to read a particular
1191 * requested attribute from a file - it just won't be set. @attribute should
1192 * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
1193 * means all attributes, and a wildcard like "fs:*" means all attributes in the fs
1194 * namespace. The standard namespace for filesystem attributes is "fs".
1195 * Common attributes of interest are #G_FILE_ATTRIBUTE_FILESYSTEM_SIZE
1196 * (the total size of the filesystem in bytes), #G_FILE_ATTRIBUTE_FILESYSTEM_FREE (number of
1197 * bytes available), and #G_FILE_ATTRIBUTE_FILESYSTEM_TYPE (type of the filesystem).
1199 * If @cancellable is not %NULL, then the operation can be cancelled by
1200 * triggering the cancellable object from another thread. If the operation
1201 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1203 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1204 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1206 * Returns: a #GFileInfo or %NULL if there was an error.
1207 * Free the returned object with g_object_unref().
1210 g_file_query_filesystem_info (GFile *file,
1211 const char *attributes,
1212 GCancellable *cancellable,
1217 g_return_val_if_fail (G_IS_FILE (file), NULL);
1219 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1222 iface = G_FILE_GET_IFACE (file);
1224 if (iface->query_filesystem_info == NULL)
1226 g_set_error_literal (error, G_IO_ERROR,
1227 G_IO_ERROR_NOT_SUPPORTED,
1228 _("Operation not supported"));
1232 return (* iface->query_filesystem_info) (file, attributes, cancellable, error);
1236 * g_file_query_filesystem_info_async:
1237 * @file: input #GFile.
1238 * @attributes: an attribute query string.
1239 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1241 * @cancellable: optional #GCancellable object, %NULL to ignore.
1242 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1243 * @user_data: the data to pass to callback function
1245 * Asynchronously gets the requested information about the filesystem
1246 * that the specified @file is on. The result is a #GFileInfo object
1247 * that contains key-value attributes (such as type or size for the
1250 * For more details, see g_file_query_filesystem_info() which is the
1251 * synchronous version of this call.
1253 * When the operation is finished, @callback will be called. You can
1254 * then call g_file_query_info_finish() to get the result of the
1258 g_file_query_filesystem_info_async (GFile *file,
1259 const char *attributes,
1261 GCancellable *cancellable,
1262 GAsyncReadyCallback callback,
1267 g_return_if_fail (G_IS_FILE (file));
1269 iface = G_FILE_GET_IFACE (file);
1270 (* iface->query_filesystem_info_async) (file,
1279 * g_file_query_filesystem_info_finish:
1280 * @file: input #GFile.
1281 * @res: a #GAsyncResult.
1282 * @error: a #GError.
1284 * Finishes an asynchronous filesystem info query. See
1285 * g_file_query_filesystem_info_async().
1287 * Returns: #GFileInfo for given @file or %NULL on error.
1288 * Free the returned object with g_object_unref().
1291 g_file_query_filesystem_info_finish (GFile *file,
1297 g_return_val_if_fail (G_IS_FILE (file), NULL);
1298 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1300 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1302 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1303 if (g_simple_async_result_propagate_error (simple, error))
1307 iface = G_FILE_GET_IFACE (file);
1308 return (* iface->query_filesystem_info_finish) (file, res, error);
1312 * g_file_find_enclosing_mount:
1313 * @file: input #GFile.
1314 * @cancellable: optional #GCancellable object, %NULL to ignore.
1315 * @error: a #GError.
1317 * Gets a #GMount for the #GFile.
1319 * If the #GFileIface for @file does not have a mount (e.g. possibly a
1320 * remote share), @error will be set to %G_IO_ERROR_NOT_FOUND and %NULL
1323 * If @cancellable is not %NULL, then the operation can be cancelled by
1324 * triggering the cancellable object from another thread. If the operation
1325 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1327 * Returns: a #GMount where the @file is located or %NULL on error.
1328 * Free the returned object with g_object_unref().
1331 g_file_find_enclosing_mount (GFile *file,
1332 GCancellable *cancellable,
1337 g_return_val_if_fail (G_IS_FILE (file), NULL);
1339 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1342 iface = G_FILE_GET_IFACE (file);
1343 if (iface->find_enclosing_mount == NULL)
1346 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1347 /* Translators: This is an error message when trying to find the
1348 * enclosing (user visible) mount of a file, but none exists. */
1349 _("Containing mount does not exist"));
1353 return (* iface->find_enclosing_mount) (file, cancellable, error);
1357 * g_file_find_enclosing_mount_async:
1359 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1361 * @cancellable: optional #GCancellable object, %NULL to ignore.
1362 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1363 * @user_data: the data to pass to callback function
1365 * Asynchronously gets the mount for the file.
1367 * For more details, see g_file_find_enclosing_mount() which is
1368 * the synchronous version of this call.
1370 * When the operation is finished, @callback will be called. You can then call
1371 * g_file_find_enclosing_mount_finish() to get the result of the operation.
1374 g_file_find_enclosing_mount_async (GFile *file,
1376 GCancellable *cancellable,
1377 GAsyncReadyCallback callback,
1382 g_return_if_fail (G_IS_FILE (file));
1384 iface = G_FILE_GET_IFACE (file);
1385 (* iface->find_enclosing_mount_async) (file,
1393 * g_file_find_enclosing_mount_finish:
1395 * @res: a #GAsyncResult
1398 * Finishes an asynchronous find mount request.
1399 * See g_file_find_enclosing_mount_async().
1401 * Returns: #GMount for given @file or %NULL on error.
1402 * Free the returned object with g_object_unref().
1405 g_file_find_enclosing_mount_finish (GFile *file,
1411 g_return_val_if_fail (G_IS_FILE (file), NULL);
1412 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1414 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1416 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1417 if (g_simple_async_result_propagate_error (simple, error))
1421 iface = G_FILE_GET_IFACE (file);
1422 return (* iface->find_enclosing_mount_finish) (file, res, error);
1428 * @file: #GFile to read.
1429 * @cancellable: a #GCancellable
1430 * @error: a #GError, or %NULL
1432 * Opens a file for reading. The result is a #GFileInputStream that
1433 * can be used to read the contents of the file.
1435 * If @cancellable is not %NULL, then the operation can be cancelled by
1436 * triggering the cancellable object from another thread. If the operation
1437 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1439 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1440 * If the file is a directory, the G_IO_ERROR_IS_DIRECTORY error will be returned.
1441 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1443 * Returns: #GFileInputStream or %NULL on error.
1444 * Free the returned object with g_object_unref().
1447 g_file_read (GFile *file,
1448 GCancellable *cancellable,
1453 g_return_val_if_fail (G_IS_FILE (file), NULL);
1455 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1458 iface = G_FILE_GET_IFACE (file);
1460 if (iface->read_fn == NULL)
1462 g_set_error_literal (error, G_IO_ERROR,
1463 G_IO_ERROR_NOT_SUPPORTED,
1464 _("Operation not supported"));
1468 return (* iface->read_fn) (file, cancellable, error);
1473 * @file: input #GFile.
1474 * @flags: a set of #GFileCreateFlags.
1475 * @cancellable: optional #GCancellable object, %NULL to ignore.
1476 * @error: a #GError, or %NULL
1478 * Gets an output stream for appending data to the file. If
1479 * the file doesn't already exist it is created.
1481 * By default files created are generally readable by everyone,
1482 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1483 * will be made readable only to the current user, to the level that
1484 * is supported on the target filesystem.
1486 * If @cancellable is not %NULL, then the operation can be cancelled by
1487 * triggering the cancellable object from another thread. If the operation
1488 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1490 * Some file systems don't allow all file names, and may
1491 * return an %G_IO_ERROR_INVALID_FILENAME error.
1492 * If the file is a directory the %G_IO_ERROR_IS_DIRECTORY error will be
1493 * returned. Other errors are possible too, and depend on what kind of
1494 * filesystem the file is on.
1496 * Returns: a #GFileOutputStream, or %NULL on error.
1497 * Free the returned object with g_object_unref().
1500 g_file_append_to (GFile *file,
1501 GFileCreateFlags flags,
1502 GCancellable *cancellable,
1507 g_return_val_if_fail (G_IS_FILE (file), NULL);
1509 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1512 iface = G_FILE_GET_IFACE (file);
1514 if (iface->append_to == NULL)
1516 g_set_error_literal (error, G_IO_ERROR,
1517 G_IO_ERROR_NOT_SUPPORTED,
1518 _("Operation not supported"));
1522 return (* iface->append_to) (file, flags, cancellable, error);
1527 * @file: input #GFile.
1528 * @flags: a set of #GFileCreateFlags.
1529 * @cancellable: optional #GCancellable object, %NULL to ignore.
1530 * @error: a #GError, or %NULL
1532 * Creates a new file and returns an output stream for writing to it.
1533 * The file must not already exist.
1535 * By default files created are generally readable by everyone,
1536 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1537 * will be made readable only to the current user, to the level that
1538 * is supported on the target filesystem.
1540 * If @cancellable is not %NULL, then the operation can be cancelled by
1541 * triggering the cancellable object from another thread. If the operation
1542 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1544 * If a file or directory with this name already exists the G_IO_ERROR_EXISTS
1545 * error will be returned.
1546 * Some file systems don't allow all file names, and may
1547 * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1548 * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1549 * Other errors are possible too, and depend on what kind of
1550 * filesystem the file is on.
1552 * Returns: a #GFileOutputStream for the newly created file, or
1554 * Free the returned object with g_object_unref().
1557 g_file_create (GFile *file,
1558 GFileCreateFlags flags,
1559 GCancellable *cancellable,
1564 g_return_val_if_fail (G_IS_FILE (file), NULL);
1566 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1569 iface = G_FILE_GET_IFACE (file);
1571 if (iface->create == NULL)
1573 g_set_error_literal (error, G_IO_ERROR,
1574 G_IO_ERROR_NOT_SUPPORTED,
1575 _("Operation not supported"));
1579 return (* iface->create) (file, flags, cancellable, error);
1584 * @file: input #GFile.
1585 * @etag: an optional <link linkend="gfile-etag">entity tag</link> for the
1586 * current #GFile, or #NULL to ignore.
1587 * @make_backup: %TRUE if a backup should be created.
1588 * @flags: a set of #GFileCreateFlags.
1589 * @cancellable: optional #GCancellable object, %NULL to ignore.
1590 * @error: a #GError, or %NULL
1592 * Returns an output stream for overwriting the file, possibly
1593 * creating a backup copy of the file first. If the file doesn't exist,
1594 * it will be created.
1596 * This will try to replace the file in the safest way possible so
1597 * that any errors during the writing will not affect an already
1598 * existing copy of the file. For instance, for local files it
1599 * may write to a temporary file and then atomically rename over
1600 * the destination when the stream is closed.
1602 * By default files created are generally readable by everyone,
1603 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1604 * will be made readable only to the current user, to the level that
1605 * is supported on the target filesystem.
1607 * If @cancellable is not %NULL, then the operation can be cancelled by
1608 * triggering the cancellable object from another thread. If the operation
1609 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1611 * If you pass in a non-#NULL @etag value, then this value is
1612 * compared to the current entity tag of the file, and if they differ
1613 * an G_IO_ERROR_WRONG_ETAG error is returned. This generally means
1614 * that the file has been changed since you last read it. You can get
1615 * the new etag from g_file_output_stream_get_etag() after you've
1616 * finished writing and closed the #GFileOutputStream. When you load
1617 * a new file you can use g_file_input_stream_query_info() to get
1618 * the etag of the file.
1620 * If @make_backup is %TRUE, this function will attempt to make a backup
1621 * of the current file before overwriting it. If this fails a G_IO_ERROR_CANT_CREATE_BACKUP
1622 * error will be returned. If you want to replace anyway, try again with
1623 * @make_backup set to %FALSE.
1625 * If the file is a directory the G_IO_ERROR_IS_DIRECTORY error will be returned,
1626 * and if the file is some other form of non-regular file then a
1627 * G_IO_ERROR_NOT_REGULAR_FILE error will be returned.
1628 * Some file systems don't allow all file names, and may
1629 * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1630 * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1631 * Other errors are possible too, and depend on what kind of
1632 * filesystem the file is on.
1634 * Returns: a #GFileOutputStream or %NULL on error.
1635 * Free the returned object with g_object_unref().
1638 g_file_replace (GFile *file,
1640 gboolean make_backup,
1641 GFileCreateFlags flags,
1642 GCancellable *cancellable,
1647 g_return_val_if_fail (G_IS_FILE (file), NULL);
1649 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1652 iface = G_FILE_GET_IFACE (file);
1654 if (iface->replace == NULL)
1656 g_set_error_literal (error, G_IO_ERROR,
1657 G_IO_ERROR_NOT_SUPPORTED,
1658 _("Operation not supported"));
1663 /* Handle empty tag string as NULL in consistent way. */
1664 if (etag && *etag == 0)
1667 return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1671 * g_file_open_readwrite:
1672 * @file: #GFile to open
1673 * @cancellable: a #GCancellable
1674 * @error: a #GError, or %NULL
1676 * Opens an existing file for reading and writing. The result is
1677 * a #GFileIOStream that can be used to read and write the contents of the file.
1679 * If @cancellable is not %NULL, then the operation can be cancelled by
1680 * triggering the cancellable object from another thread. If the operation
1681 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1683 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1684 * If the file is a directory, the G_IO_ERROR_IS_DIRECTORY error will be returned.
1685 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1686 * Note that in many non-local file cases read and write streams are not supported,
1687 * so make sure you really need to do read and write streaming, rather than
1688 * just opening for reading or writing.
1690 * Returns: #GFileIOStream or %NULL on error.
1691 * Free the returned object with g_object_unref().
1696 g_file_open_readwrite (GFile *file,
1697 GCancellable *cancellable,
1702 g_return_val_if_fail (G_IS_FILE (file), NULL);
1704 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1707 iface = G_FILE_GET_IFACE (file);
1709 if (iface->open_readwrite == NULL)
1711 g_set_error_literal (error, G_IO_ERROR,
1712 G_IO_ERROR_NOT_SUPPORTED,
1713 _("Operation not supported"));
1717 return (* iface->open_readwrite) (file, cancellable, error);
1721 * g_file_create_readwrite:
1723 * @flags: a set of #GFileCreateFlags
1724 * @cancellable: optional #GCancellable object, %NULL to ignore
1725 * @error: return location for a #GError, or %NULL
1727 * Creates a new file and returns a stream for reading and writing to it.
1728 * The file must not already exist.
1730 * By default files created are generally readable by everyone,
1731 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1732 * will be made readable only to the current user, to the level that
1733 * is supported on the target filesystem.
1735 * If @cancellable is not %NULL, then the operation can be cancelled by
1736 * triggering the cancellable object from another thread. If the operation
1737 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1739 * If a file or directory with this name already exists the %G_IO_ERROR_EXISTS
1740 * error will be returned. Some file systems don't allow all file names,
1741 * and may return an %G_IO_ERROR_INVALID_FILENAME error, and if the name
1742 * is too long, %G_IO_ERROR_FILENAME_TOO_LONG will be returned. Other errors
1743 * are possible too, and depend on what kind of filesystem the file is on.
1745 * Note that in many non-local file cases read and write streams are not
1746 * supported, so make sure you really need to do read and write streaming,
1747 * rather than just opening for reading or writing.
1749 * Returns: a #GFileIOStream for the newly created file, or %NULL on error.
1750 * Free the returned object with g_object_unref().
1755 g_file_create_readwrite (GFile *file,
1756 GFileCreateFlags flags,
1757 GCancellable *cancellable,
1762 g_return_val_if_fail (G_IS_FILE (file), NULL);
1764 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1767 iface = G_FILE_GET_IFACE (file);
1769 if (iface->create_readwrite == NULL)
1771 g_set_error_literal (error, G_IO_ERROR,
1772 G_IO_ERROR_NOT_SUPPORTED,
1773 _("Operation not supported"));
1777 return (* iface->create_readwrite) (file, flags, cancellable, error);
1781 * g_file_replace_readwrite:
1783 * @etag: an optional <link linkend="gfile-etag">entity tag</link> for the
1784 * current #GFile, or #NULL to ignore
1785 * @make_backup: %TRUE if a backup should be created
1786 * @flags: a set of #GFileCreateFlags
1787 * @cancellable: optional #GCancellable object, %NULL to ignore
1788 * @error: return location for a #GError, or %NULL
1790 * Returns an output stream for overwriting the file in readwrite mode,
1791 * possibly creating a backup copy of the file first. If the file doesn't
1792 * exist, it will be created.
1794 * For details about the behaviour, see g_file_replace() which does the same
1795 * thing but returns an output stream only.
1797 * Note that in many non-local file cases read and write streams are not
1798 * supported, so make sure you really need to do read and write streaming,
1799 * rather than just opening for reading or writing.
1801 * Returns: a #GFileIOStream or %NULL on error.
1802 * Free the returned object with g_object_unref().
1807 g_file_replace_readwrite (GFile *file,
1809 gboolean make_backup,
1810 GFileCreateFlags flags,
1811 GCancellable *cancellable,
1816 g_return_val_if_fail (G_IS_FILE (file), NULL);
1818 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1821 iface = G_FILE_GET_IFACE (file);
1823 if (iface->replace_readwrite == NULL)
1825 g_set_error_literal (error, G_IO_ERROR,
1826 G_IO_ERROR_NOT_SUPPORTED,
1827 _("Operation not supported"));
1831 return (* iface->replace_readwrite) (file, etag, make_backup, flags, cancellable, error);
1835 * g_file_read_async:
1836 * @file: input #GFile
1837 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1839 * @cancellable: optional #GCancellable object, %NULL to ignore.
1840 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1841 * @user_data: the data to pass to callback function
1843 * Asynchronously opens @file for reading.
1845 * For more details, see g_file_read() which is
1846 * the synchronous version of this call.
1848 * When the operation is finished, @callback will be called. You can then call
1849 * g_file_read_finish() to get the result of the operation.
1852 g_file_read_async (GFile *file,
1854 GCancellable *cancellable,
1855 GAsyncReadyCallback callback,
1860 g_return_if_fail (G_IS_FILE (file));
1862 iface = G_FILE_GET_IFACE (file);
1863 (* iface->read_async) (file,
1871 * g_file_read_finish:
1872 * @file: input #GFile.
1873 * @res: a #GAsyncResult.
1874 * @error: a #GError, or %NULL
1876 * Finishes an asynchronous file read operation started with
1877 * g_file_read_async().
1879 * Returns: a #GFileInputStream or %NULL on error.
1880 * Free the returned object with g_object_unref().
1883 g_file_read_finish (GFile *file,
1889 g_return_val_if_fail (G_IS_FILE (file), NULL);
1890 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1892 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1894 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1895 if (g_simple_async_result_propagate_error (simple, error))
1899 iface = G_FILE_GET_IFACE (file);
1900 return (* iface->read_finish) (file, res, error);
1904 * g_file_append_to_async:
1905 * @file: input #GFile.
1906 * @flags: a set of #GFileCreateFlags.
1907 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1909 * @cancellable: optional #GCancellable object, %NULL to ignore.
1910 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1911 * @user_data: the data to pass to callback function
1913 * Asynchronously opens @file for appending.
1915 * For more details, see g_file_append_to() which is
1916 * the synchronous version of this call.
1918 * When the operation is finished, @callback will be called. You can then call
1919 * g_file_append_to_finish() to get the result of the operation.
1922 g_file_append_to_async (GFile *file,
1923 GFileCreateFlags flags,
1925 GCancellable *cancellable,
1926 GAsyncReadyCallback callback,
1931 g_return_if_fail (G_IS_FILE (file));
1933 iface = G_FILE_GET_IFACE (file);
1934 (* iface->append_to_async) (file,
1943 * g_file_append_to_finish:
1944 * @file: input #GFile.
1945 * @res: #GAsyncResult
1946 * @error: a #GError, or %NULL
1948 * Finishes an asynchronous file append operation started with
1949 * g_file_append_to_async().
1951 * Returns: a valid #GFileOutputStream or %NULL on error.
1952 * Free the returned object with g_object_unref().
1955 g_file_append_to_finish (GFile *file,
1961 g_return_val_if_fail (G_IS_FILE (file), NULL);
1962 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1964 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1966 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1967 if (g_simple_async_result_propagate_error (simple, error))
1971 iface = G_FILE_GET_IFACE (file);
1972 return (* iface->append_to_finish) (file, res, error);
1976 * g_file_create_async:
1977 * @file: input #GFile.
1978 * @flags: a set of #GFileCreateFlags.
1979 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1981 * @cancellable: optional #GCancellable object, %NULL to ignore.
1982 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1983 * @user_data: the data to pass to callback function
1985 * Asynchronously creates a new file and returns an output stream for writing to it.
1986 * The file must not already exist.
1988 * For more details, see g_file_create() which is
1989 * the synchronous version of this call.
1991 * When the operation is finished, @callback will be called. You can then call
1992 * g_file_create_finish() to get the result of the operation.
1995 g_file_create_async (GFile *file,
1996 GFileCreateFlags flags,
1998 GCancellable *cancellable,
1999 GAsyncReadyCallback callback,
2004 g_return_if_fail (G_IS_FILE (file));
2006 iface = G_FILE_GET_IFACE (file);
2007 (* iface->create_async) (file,
2016 * g_file_create_finish:
2017 * @file: input #GFile.
2018 * @res: a #GAsyncResult.
2019 * @error: a #GError, or %NULL
2021 * Finishes an asynchronous file create operation started with
2022 * g_file_create_async().
2024 * Returns: a #GFileOutputStream or %NULL on error.
2025 * Free the returned object with g_object_unref().
2028 g_file_create_finish (GFile *file,
2034 g_return_val_if_fail (G_IS_FILE (file), NULL);
2035 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2037 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2039 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2040 if (g_simple_async_result_propagate_error (simple, error))
2044 iface = G_FILE_GET_IFACE (file);
2045 return (* iface->create_finish) (file, res, error);
2049 * g_file_replace_async:
2050 * @file: input #GFile.
2051 * @etag: an <link linkend="gfile-etag">entity tag</link> for the
2052 * current #GFile, or NULL to ignore.
2053 * @make_backup: %TRUE if a backup should be created.
2054 * @flags: a set of #GFileCreateFlags.
2055 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2057 * @cancellable: optional #GCancellable object, %NULL to ignore.
2058 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2059 * @user_data: the data to pass to callback function
2061 * Asynchronously overwrites the file, replacing the contents, possibly
2062 * creating a backup copy of the file first.
2064 * For more details, see g_file_replace() which is
2065 * the synchronous version of this call.
2067 * When the operation is finished, @callback will be called. You can then call
2068 * g_file_replace_finish() to get the result of the operation.
2071 g_file_replace_async (GFile *file,
2073 gboolean make_backup,
2074 GFileCreateFlags flags,
2076 GCancellable *cancellable,
2077 GAsyncReadyCallback callback,
2082 g_return_if_fail (G_IS_FILE (file));
2084 iface = G_FILE_GET_IFACE (file);
2085 (* iface->replace_async) (file,
2096 * g_file_replace_finish:
2097 * @file: input #GFile.
2098 * @res: a #GAsyncResult.
2099 * @error: a #GError, or %NULL
2101 * Finishes an asynchronous file replace operation started with
2102 * g_file_replace_async().
2104 * Returns: a #GFileOutputStream, or %NULL on error.
2105 * Free the returned object with g_object_unref().
2108 g_file_replace_finish (GFile *file,
2114 g_return_val_if_fail (G_IS_FILE (file), NULL);
2115 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2117 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2119 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2120 if (g_simple_async_result_propagate_error (simple, error))
2124 iface = G_FILE_GET_IFACE (file);
2125 return (* iface->replace_finish) (file, res, error);
2130 * g_file_open_readwrite_async:
2131 * @file: input #GFile.
2132 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2134 * @cancellable: optional #GCancellable object, %NULL to ignore.
2135 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2136 * @user_data: the data to pass to callback function
2138 * Asynchronously opens @file for reading and writing.
2140 * For more details, see g_file_open_readwrite() which is
2141 * the synchronous version of this call.
2143 * When the operation is finished, @callback will be called. You can then call
2144 * g_file_open_readwrite_finish() to get the result of the operation.
2149 g_file_open_readwrite_async (GFile *file,
2151 GCancellable *cancellable,
2152 GAsyncReadyCallback callback,
2157 g_return_if_fail (G_IS_FILE (file));
2159 iface = G_FILE_GET_IFACE (file);
2160 (* iface->open_readwrite_async) (file,
2168 * g_file_open_readwrite_finish:
2169 * @file: input #GFile.
2170 * @res: a #GAsyncResult.
2171 * @error: a #GError, or %NULL
2173 * Finishes an asynchronous file read operation started with
2174 * g_file_open_readwrite_async().
2176 * Returns: a #GFileIOStream or %NULL on error.
2177 * Free the returned object with g_object_unref().
2182 g_file_open_readwrite_finish (GFile *file,
2188 g_return_val_if_fail (G_IS_FILE (file), NULL);
2189 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2191 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2193 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2194 if (g_simple_async_result_propagate_error (simple, error))
2198 iface = G_FILE_GET_IFACE (file);
2199 return (* iface->open_readwrite_finish) (file, res, error);
2204 * g_file_create_readwrite_async:
2205 * @file: input #GFile
2206 * @flags: a set of #GFileCreateFlags
2207 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2209 * @cancellable: optional #GCancellable object, %NULL to ignore
2210 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2211 * @user_data: the data to pass to callback function
2213 * Asynchronously creates a new file and returns a stream for reading and
2214 * writing to it. The file must not already exist.
2216 * For more details, see g_file_create_readwrite() which is
2217 * the synchronous version of this call.
2219 * When the operation is finished, @callback will be called. You can then
2220 * call g_file_create_readwrite_finish() to get the result of the operation.
2225 g_file_create_readwrite_async (GFile *file,
2226 GFileCreateFlags flags,
2228 GCancellable *cancellable,
2229 GAsyncReadyCallback callback,
2234 g_return_if_fail (G_IS_FILE (file));
2236 iface = G_FILE_GET_IFACE (file);
2237 (* iface->create_readwrite_async) (file,
2246 * g_file_create_readwrite_finish:
2247 * @file: input #GFile
2248 * @res: a #GAsyncResult
2249 * @error: a #GError, or %NULL
2251 * Finishes an asynchronous file create operation started with
2252 * g_file_create_readwrite_async().
2254 * Returns: a #GFileIOStream or %NULL on error.
2255 * Free the returned object with g_object_unref().
2260 g_file_create_readwrite_finish (GFile *file,
2266 g_return_val_if_fail (G_IS_FILE (file), NULL);
2267 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2269 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2271 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2272 if (g_simple_async_result_propagate_error (simple, error))
2276 iface = G_FILE_GET_IFACE (file);
2277 return (* iface->create_readwrite_finish) (file, res, error);
2281 * g_file_replace_readwrite_async:
2282 * @file: input #GFile.
2283 * @etag: an <link linkend="gfile-etag">entity tag</link> for the
2284 * current #GFile, or NULL to ignore.
2285 * @make_backup: %TRUE if a backup should be created.
2286 * @flags: a set of #GFileCreateFlags.
2287 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2289 * @cancellable: optional #GCancellable object, %NULL to ignore.
2290 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2291 * @user_data: the data to pass to callback function
2293 * Asynchronously overwrites the file in read-write mode, replacing the
2294 * contents, possibly creating a backup copy of the file first.
2296 * For more details, see g_file_replace_readwrite() which is
2297 * the synchronous version of this call.
2299 * When the operation is finished, @callback will be called. You can then
2300 * call g_file_replace_readwrite_finish() to get the result of the operation.
2305 g_file_replace_readwrite_async (GFile *file,
2307 gboolean make_backup,
2308 GFileCreateFlags flags,
2310 GCancellable *cancellable,
2311 GAsyncReadyCallback callback,
2316 g_return_if_fail (G_IS_FILE (file));
2318 iface = G_FILE_GET_IFACE (file);
2319 (* iface->replace_readwrite_async) (file,
2330 * g_file_replace_readwrite_finish:
2331 * @file: input #GFile.
2332 * @res: a #GAsyncResult.
2333 * @error: a #GError, or %NULL
2335 * Finishes an asynchronous file replace operation started with
2336 * g_file_replace_readwrite_async().
2338 * Returns: a #GFileIOStream, or %NULL on error.
2339 * Free the returned object with g_object_unref().
2344 g_file_replace_readwrite_finish (GFile *file,
2350 g_return_val_if_fail (G_IS_FILE (file), NULL);
2351 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2353 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2355 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2356 if (g_simple_async_result_propagate_error (simple, error))
2360 iface = G_FILE_GET_IFACE (file);
2361 return (* iface->replace_readwrite_finish) (file, res, error);
2365 copy_symlink (GFile *destination,
2366 GFileCopyFlags flags,
2367 GCancellable *cancellable,
2372 gboolean tried_delete;
2374 GFileType file_type;
2376 tried_delete = FALSE;
2380 if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
2382 /* Maybe it already existed, and we want to overwrite? */
2383 if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) &&
2384 my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
2386 g_error_free (my_error);
2389 /* Don't overwrite if the destination is a directory */
2390 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2391 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2392 cancellable, &my_error);
2395 file_type = g_file_info_get_file_type (info);
2396 g_object_unref (info);
2398 if (file_type == G_FILE_TYPE_DIRECTORY)
2400 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
2401 _("Can't copy over directory"));
2406 if (!g_file_delete (destination, cancellable, error))
2409 tried_delete = TRUE;
2413 g_propagate_error (error, my_error);
2420 static GInputStream *
2421 open_source_for_copy (GFile *source,
2423 GFileCopyFlags flags,
2424 GCancellable *cancellable,
2430 GFileType file_type;
2433 in = (GInputStream *)g_file_read (source, cancellable, &my_error);
2437 /* There was an error opening the source, try to set a good error for it: */
2439 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
2441 /* The source is a directory, don't fail with WOULD_RECURSE immediately,
2442 * as that is less useful to the app. Better check for errors on the
2445 g_error_free (my_error);
2448 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2449 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2450 cancellable, &my_error);
2453 file_type = g_file_info_get_file_type (info);
2454 g_object_unref (info);
2456 if (flags & G_FILE_COPY_OVERWRITE)
2458 if (file_type == G_FILE_TYPE_DIRECTORY)
2460 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
2461 _("Can't copy directory over directory"));
2464 /* continue to would_recurse error */
2468 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
2469 _("Target file exists"));
2475 /* Error getting info from target, return that error
2476 * (except for NOT_FOUND, which is no error here)
2478 if (my_error->domain != G_IO_ERROR && my_error->code != G_IO_ERROR_NOT_FOUND)
2480 g_propagate_error (error, my_error);
2483 g_error_free (my_error);
2486 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
2487 _("Can't recursively copy directory"));
2491 g_propagate_error (error, my_error);
2496 should_copy (GFileAttributeInfo *info,
2498 gboolean skip_perms)
2500 if (skip_perms && strcmp(info->name, "unix::mode") == 0)
2504 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
2505 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
2509 build_attribute_list_for_copy (GFileAttributeInfoList *attributes,
2510 GFileAttributeInfoList *namespaces,
2512 gboolean skip_perms)
2519 s = g_string_new ("");
2523 for (i = 0; i < attributes->n_infos; i++)
2525 if (should_copy (&attributes->infos[i], as_move, skip_perms))
2530 g_string_append_c (s, ',');
2532 g_string_append (s, attributes->infos[i].name);
2539 for (i = 0; i < namespaces->n_infos; i++)
2541 if (should_copy (&namespaces->infos[i], as_move, FALSE))
2546 g_string_append_c (s, ',');
2548 g_string_append (s, namespaces->infos[i].name);
2549 g_string_append (s, "::*");
2554 return g_string_free (s, FALSE);
2558 * g_file_copy_attributes:
2559 * @source: a #GFile with attributes.
2560 * @destination: a #GFile to copy attributes to.
2561 * @flags: a set of #GFileCopyFlags.
2562 * @cancellable: optional #GCancellable object, %NULL to ignore.
2563 * @error: a #GError, %NULL to ignore.
2565 * Copies the file attributes from @source to @destination.
2567 * Normally only a subset of the file attributes are copied,
2568 * those that are copies in a normal file copy operation
2569 * (which for instance does not include e.g. owner). However
2570 * if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
2571 * all the metadata that is possible to copy is copied. This
2572 * is useful when implementing move by copy + delete source.
2574 * Returns: %TRUE if the attributes were copied successfully, %FALSE otherwise.
2577 g_file_copy_attributes (GFile *source,
2579 GFileCopyFlags flags,
2580 GCancellable *cancellable,
2583 GFileAttributeInfoList *attributes, *namespaces;
2584 char *attrs_to_read;
2588 gboolean source_nofollow_symlinks;
2589 gboolean skip_perms;
2591 as_move = flags & G_FILE_COPY_ALL_METADATA;
2592 source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
2593 skip_perms = (flags & G_FILE_COPY_TARGET_DEFAULT_PERMS) != 0;
2595 /* Ignore errors here, if the target supports no attributes there is nothing to copy */
2596 attributes = g_file_query_settable_attributes (destination, cancellable, NULL);
2597 namespaces = g_file_query_writable_namespaces (destination, cancellable, NULL);
2599 if (attributes == NULL && namespaces == NULL)
2602 attrs_to_read = build_attribute_list_for_copy (attributes, namespaces, as_move, skip_perms);
2604 /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
2605 * we just don't copy it.
2607 info = g_file_query_info (source, attrs_to_read,
2608 source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
2612 g_free (attrs_to_read);
2617 res = g_file_set_attributes_from_info (destination,
2619 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2622 g_object_unref (info);
2625 g_file_attribute_info_list_unref (attributes);
2626 g_file_attribute_info_list_unref (namespaces);
2631 /* Closes the streams */
2633 copy_stream_with_progress (GInputStream *in,
2636 GCancellable *cancellable,
2637 GFileProgressCallback progress_callback,
2638 gpointer progress_callback_data,
2641 gssize n_read, n_written;
2642 goffset current_size;
2643 char buffer[1024*64], *p;
2649 /* avoid performance impact of querying total size when it's not needed */
2650 if (progress_callback)
2652 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
2653 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2657 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2658 total_size = g_file_info_get_size (info);
2659 g_object_unref (info);
2662 if (total_size == -1)
2664 info = g_file_query_info (source,
2665 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2666 G_FILE_QUERY_INFO_NONE,
2670 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2671 total_size = g_file_info_get_size (info);
2672 g_object_unref (info);
2677 if (total_size == -1)
2684 n_read = g_input_stream_read (in, buffer, sizeof (buffer), cancellable, error);
2694 current_size += n_read;
2699 n_written = g_output_stream_write (out, p, n_read, cancellable, error);
2700 if (n_written == -1)
2707 n_read -= n_written;
2713 if (progress_callback)
2714 progress_callback (current_size, total_size, progress_callback_data);
2718 error = NULL; /* Ignore further errors */
2720 /* Make sure we send full copied size */
2721 if (progress_callback)
2722 progress_callback (current_size, total_size, progress_callback_data);
2724 /* Don't care about errors in source here */
2725 g_input_stream_close (in, cancellable, NULL);
2727 /* But write errors on close are bad! */
2728 if (!g_output_stream_close (out, cancellable, error))
2731 g_object_unref (in);
2732 g_object_unref (out);
2738 file_copy_fallback (GFile *source,
2740 GFileCopyFlags flags,
2741 GCancellable *cancellable,
2742 GFileProgressCallback progress_callback,
2743 gpointer progress_callback_data,
2751 /* need to know the file type */
2752 info = g_file_query_info (source,
2753 G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
2754 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2761 /* Maybe copy the symlink? */
2762 if ((flags & G_FILE_COPY_NOFOLLOW_SYMLINKS) &&
2763 g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK)
2765 target = g_file_info_get_symlink_target (info);
2768 if (!copy_symlink (destination, flags, cancellable, target, error))
2770 g_object_unref (info);
2774 g_object_unref (info);
2777 /* ... else fall back on a regular file copy */
2778 g_object_unref (info);
2780 /* Handle "special" files (pipes, device nodes, ...)? */
2781 else if (g_file_info_get_file_type (info) == G_FILE_TYPE_SPECIAL)
2783 /* FIXME: could try to recreate device nodes and others? */
2785 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2786 _("Can't copy special file"));
2787 g_object_unref (info);
2790 /* Everything else should just fall back on a regular copy. */
2792 g_object_unref (info);
2794 in = open_source_for_copy (source, destination, flags, cancellable, error);
2798 if (flags & G_FILE_COPY_OVERWRITE)
2800 out = (GOutputStream *)g_file_replace (destination,
2802 flags & G_FILE_COPY_BACKUP,
2803 G_FILE_CREATE_REPLACE_DESTINATION,
2804 cancellable, error);
2808 out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
2813 g_object_unref (in);
2817 if (!copy_stream_with_progress (in, out, source, cancellable,
2818 progress_callback, progress_callback_data,
2824 /* Ignore errors here. Failure to copy metadata is not a hard error */
2825 g_file_copy_attributes (source, destination,
2826 flags, cancellable, NULL);
2833 * @source: input #GFile.
2834 * @destination: destination #GFile
2835 * @flags: set of #GFileCopyFlags
2836 * @cancellable: optional #GCancellable object, %NULL to ignore.
2837 * @progress_callback: function to callback with progress information
2838 * @progress_callback_data: user data to pass to @progress_callback
2839 * @error: #GError to set on error, or %NULL
2841 * Copies the file @source to the location specified by @destination.
2842 * Can not handle recursive copies of directories.
2844 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2845 * existing @destination file is overwritten.
2847 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2848 * will be copied as symlinks, otherwise the target of the
2849 * @source symlink will be copied.
2851 * If @cancellable is not %NULL, then the operation can be cancelled by
2852 * triggering the cancellable object from another thread. If the operation
2853 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2855 * If @progress_callback is not %NULL, then the operation can be monitored by
2856 * setting this to a #GFileProgressCallback function. @progress_callback_data
2857 * will be passed to this function. It is guaranteed that this callback will
2858 * be called after all data has been transferred with the total number of bytes
2859 * copied during the operation.
2861 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2862 * error is returned, independent on the status of the @destination.
2864 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2865 * error G_IO_ERROR_EXISTS is returned.
2867 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2868 * error is returned. If trying to overwrite a directory with a directory the
2869 * G_IO_ERROR_WOULD_MERGE error is returned.
2871 * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2872 * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2875 * If you are interested in copying the #GFile object itself (not the on-disk
2876 * file), see g_file_dup().
2878 * Returns: %TRUE on success, %FALSE otherwise.
2881 g_file_copy (GFile *source,
2883 GFileCopyFlags flags,
2884 GCancellable *cancellable,
2885 GFileProgressCallback progress_callback,
2886 gpointer progress_callback_data,
2893 g_return_val_if_fail (G_IS_FILE (source), FALSE);
2894 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2896 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2899 iface = G_FILE_GET_IFACE (destination);
2903 res = (* iface->copy) (source, destination,
2905 progress_callback, progress_callback_data,
2911 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2913 g_propagate_error (error, my_error);
2917 g_clear_error (&my_error);
2920 /* If the types are different, and the destination method failed
2921 also try the source method */
2922 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
2924 iface = G_FILE_GET_IFACE (source);
2929 res = (* iface->copy) (source, destination,
2931 progress_callback, progress_callback_data,
2937 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2939 g_propagate_error (error, my_error);
2943 g_clear_error (&my_error);
2947 return file_copy_fallback (source, destination, flags, cancellable,
2948 progress_callback, progress_callback_data,
2953 * g_file_copy_async:
2954 * @source: input #GFile.
2955 * @destination: destination #GFile
2956 * @flags: set of #GFileCopyFlags
2957 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2959 * @cancellable: optional #GCancellable object, %NULL to ignore.
2960 * @progress_callback: function to callback with progress information
2961 * @progress_callback_data: user data to pass to @progress_callback
2962 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2963 * @user_data: the data to pass to callback function
2965 * Copies the file @source to the location specified by @destination
2966 * asynchronously. For details of the behaviour, see g_file_copy().
2968 * If @progress_callback is not %NULL, then that function that will be called
2969 * just like in g_file_copy(), however the callback will run in the main loop,
2970 * not in the thread that is doing the I/O operation.
2972 * When the operation is finished, @callback will be called. You can then call
2973 * g_file_copy_finish() to get the result of the operation.
2976 g_file_copy_async (GFile *source,
2978 GFileCopyFlags flags,
2980 GCancellable *cancellable,
2981 GFileProgressCallback progress_callback,
2982 gpointer progress_callback_data,
2983 GAsyncReadyCallback callback,
2988 g_return_if_fail (G_IS_FILE (source));
2989 g_return_if_fail (G_IS_FILE (destination));
2991 iface = G_FILE_GET_IFACE (source);
2992 (* iface->copy_async) (source,
2998 progress_callback_data,
3004 * g_file_copy_finish:
3005 * @file: input #GFile.
3006 * @res: a #GAsyncResult.
3007 * @error: a #GError, or %NULL
3009 * Finishes copying the file started with
3010 * g_file_copy_async().
3012 * Returns: a %TRUE on success, %FALSE on error.
3015 g_file_copy_finish (GFile *file,
3021 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3022 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
3024 if (G_IS_SIMPLE_ASYNC_RESULT (res))
3026 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3028 if (g_simple_async_result_propagate_error (simple, error))
3032 iface = G_FILE_GET_IFACE (file);
3033 return (* iface->copy_finish) (file, res, error);
3038 * @source: #GFile pointing to the source location.
3039 * @destination: #GFile pointing to the destination location.
3040 * @flags: set of #GFileCopyFlags.
3041 * @cancellable: optional #GCancellable object, %NULL to ignore.
3042 * @progress_callback: #GFileProgressCallback function for updates.
3043 * @progress_callback_data: gpointer to user data for the callback function.
3044 * @error: #GError for returning error conditions, or %NULL
3047 * Tries to move the file or directory @source to the location specified by @destination.
3048 * If native move operations are supported then this is used, otherwise a copy + delete
3049 * fallback is used. The native implementation may support moving directories (for instance
3050 * on moves inside the same filesystem), but the fallback code does not.
3052 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3053 * existing @destination file is overwritten.
3055 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3056 * will be copied as symlinks, otherwise the target of the
3057 * @source symlink will be copied.
3059 * If @cancellable is not %NULL, then the operation can be cancelled by
3060 * triggering the cancellable object from another thread. If the operation
3061 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3063 * If @progress_callback is not %NULL, then the operation can be monitored by
3064 * setting this to a #GFileProgressCallback function. @progress_callback_data
3065 * will be passed to this function. It is guaranteed that this callback will
3066 * be called after all data has been transferred with the total number of bytes
3067 * copied during the operation.
3069 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
3070 * error is returned, independent on the status of the @destination.
3072 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
3073 * error G_IO_ERROR_EXISTS is returned.
3075 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
3076 * error is returned. If trying to overwrite a directory with a directory the
3077 * G_IO_ERROR_WOULD_MERGE error is returned.
3079 * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
3080 * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
3081 * may be returned (if the native move operation isn't available).
3083 * Returns: %TRUE on successful move, %FALSE otherwise.
3086 g_file_move (GFile *source,
3088 GFileCopyFlags flags,
3089 GCancellable *cancellable,
3090 GFileProgressCallback progress_callback,
3091 gpointer progress_callback_data,
3098 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3099 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3101 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3104 iface = G_FILE_GET_IFACE (destination);
3108 res = (* iface->move) (source, destination,
3110 progress_callback, progress_callback_data,
3116 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3118 g_propagate_error (error, my_error);
3123 /* If the types are different, and the destination method failed
3124 also try the source method */
3125 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3127 iface = G_FILE_GET_IFACE (source);
3132 res = (* iface->move) (source, destination,
3134 progress_callback, progress_callback_data,
3140 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3142 g_propagate_error (error, my_error);
3148 if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
3150 g_set_error_literal (error, G_IO_ERROR,
3151 G_IO_ERROR_NOT_SUPPORTED,
3152 _("Operation not supported"));
3156 flags |= G_FILE_COPY_ALL_METADATA;
3157 if (!g_file_copy (source, destination, flags, cancellable,
3158 progress_callback, progress_callback_data,
3162 return g_file_delete (source, cancellable, error);
3166 * g_file_make_directory
3167 * @file: input #GFile.
3168 * @cancellable: optional #GCancellable object, %NULL to ignore.
3169 * @error: a #GError, or %NULL
3171 * Creates a directory. Note that this will only create a child directory of
3172 * the immediate parent directory of the path or URI given by the #GFile. To
3173 * recursively create directories, see g_file_make_directory_with_parents().
3174 * This function will fail if the parent directory does not exist, setting
3175 * @error to %G_IO_ERROR_NOT_FOUND. If the file system doesn't support creating
3176 * directories, this function will fail, setting @error to
3177 * %G_IO_ERROR_NOT_SUPPORTED.
3179 * For a local #GFile the newly created directory will have the default
3180 * (current) ownership and permissions of the current process.
3182 * If @cancellable is not %NULL, then the operation can be cancelled by
3183 * triggering the cancellable object from another thread. If the operation
3184 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3186 * Returns: %TRUE on successful creation, %FALSE otherwise.
3189 g_file_make_directory (GFile *file,
3190 GCancellable *cancellable,
3195 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3197 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3200 iface = G_FILE_GET_IFACE (file);
3202 if (iface->make_directory == NULL)
3204 g_set_error_literal (error, G_IO_ERROR,
3205 G_IO_ERROR_NOT_SUPPORTED,
3206 _("Operation not supported"));
3210 return (* iface->make_directory) (file, cancellable, error);
3214 * g_file_make_directory_with_parents:
3215 * @file: input #GFile.
3216 * @cancellable: optional #GCancellable object, %NULL to ignore.
3217 * @error: a #GError, or %NULL
3219 * Creates a directory and any parent directories that may not exist similar to
3220 * 'mkdir -p'. If the file system does not support creating directories, this
3221 * function will fail, setting @error to %G_IO_ERROR_NOT_SUPPORTED.
3223 * For a local #GFile the newly created directories will have the default
3224 * (current) ownership and permissions of the current process.
3226 * If @cancellable is not %NULL, then the operation can be cancelled by
3227 * triggering the cancellable object from another thread. If the operation
3228 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3230 * Returns: %TRUE if all directories have been successfully created, %FALSE
3236 g_file_make_directory_with_parents (GFile *file,
3237 GCancellable *cancellable,
3241 GFile *parent_file, *work_file;
3242 GList *list = NULL, *l;
3243 GError *my_error = NULL;
3245 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3248 result = g_file_make_directory (file, cancellable, &my_error);
3249 if (result || my_error->code != G_IO_ERROR_NOT_FOUND)
3252 g_propagate_error (error, my_error);
3258 while (!result && my_error->code == G_IO_ERROR_NOT_FOUND)
3260 g_clear_error (&my_error);
3262 parent_file = g_file_get_parent (work_file);
3263 if (parent_file == NULL)
3265 result = g_file_make_directory (parent_file, cancellable, &my_error);
3267 if (!result && my_error->code == G_IO_ERROR_NOT_FOUND)
3268 list = g_list_prepend (list, parent_file);
3270 work_file = parent_file;
3273 for (l = list; result && l; l = l->next)
3275 result = g_file_make_directory ((GFile *) l->data, cancellable, &my_error);
3279 while (list != NULL)
3281 g_object_unref ((GFile *) list->data);
3282 list = g_list_remove (list, list->data);
3287 g_propagate_error (error, my_error);
3291 return g_file_make_directory (file, cancellable, error);
3295 * g_file_make_symbolic_link:
3296 * @file: input #GFile.
3297 * @symlink_value: a string with the value of the new symlink.
3298 * @cancellable: optional #GCancellable object, %NULL to ignore.
3299 * @error: a #GError.
3301 * Creates a symbolic link.
3303 * If @cancellable is not %NULL, then the operation can be cancelled by
3304 * triggering the cancellable object from another thread. If the operation
3305 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3307 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
3310 g_file_make_symbolic_link (GFile *file,
3311 const char *symlink_value,
3312 GCancellable *cancellable,
3317 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3318 g_return_val_if_fail (symlink_value != NULL, FALSE);
3320 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3323 if (*symlink_value == '\0')
3325 g_set_error_literal (error, G_IO_ERROR,
3326 G_IO_ERROR_INVALID_ARGUMENT,
3327 _("Invalid symlink value given"));
3331 iface = G_FILE_GET_IFACE (file);
3333 if (iface->make_symbolic_link == NULL)
3335 g_set_error_literal (error, G_IO_ERROR,
3336 G_IO_ERROR_NOT_SUPPORTED,
3337 _("Operation not supported"));
3341 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
3346 * @file: input #GFile.
3347 * @cancellable: optional #GCancellable object, %NULL to ignore.
3348 * @error: a #GError, or %NULL
3350 * Deletes a file. If the @file is a directory, it will only be deleted if it
3353 * If @cancellable is not %NULL, then the operation can be cancelled by
3354 * triggering the cancellable object from another thread. If the operation
3355 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3357 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
3360 g_file_delete (GFile *file,
3361 GCancellable *cancellable,
3366 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3368 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3371 iface = G_FILE_GET_IFACE (file);
3373 if (iface->delete_file == NULL)
3375 g_set_error_literal (error, G_IO_ERROR,
3376 G_IO_ERROR_NOT_SUPPORTED,
3377 _("Operation not supported"));
3381 return (* iface->delete_file) (file, cancellable, error);
3386 * @file: #GFile to send to trash.
3387 * @cancellable: optional #GCancellable object, %NULL to ignore.
3388 * @error: a #GError, or %NULL
3390 * Sends @file to the "Trashcan", if possible. This is similar to
3391 * deleting it, but the user can recover it before emptying the trashcan.
3392 * Not all file systems support trashing, so this call can return the
3393 * %G_IO_ERROR_NOT_SUPPORTED error.
3396 * If @cancellable is not %NULL, then the operation can be cancelled by
3397 * triggering the cancellable object from another thread. If the operation
3398 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3400 * Returns: %TRUE on successful trash, %FALSE otherwise.
3403 g_file_trash (GFile *file,
3404 GCancellable *cancellable,
3409 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3411 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3414 iface = G_FILE_GET_IFACE (file);
3416 if (iface->trash == NULL)
3418 g_set_error_literal (error,
3419 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
3420 _("Trash not supported"));
3424 return (* iface->trash) (file, cancellable, error);
3428 * g_file_set_display_name:
3429 * @file: input #GFile.
3430 * @display_name: a string.
3431 * @cancellable: optional #GCancellable object, %NULL to ignore.
3432 * @error: a #GError, or %NULL
3434 * Renames @file to the specified display name.
3436 * The display name is converted from UTF8 to the correct encoding for the target
3437 * filesystem if possible and the @file is renamed to this.
3439 * If you want to implement a rename operation in the user interface the edit name
3440 * (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the initial value in the rename
3441 * widget, and then the result after editing should be passed to g_file_set_display_name().
3443 * On success the resulting converted filename is returned.
3445 * If @cancellable is not %NULL, then the operation can be cancelled by
3446 * triggering the cancellable object from another thread. If the operation
3447 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3449 * Returns: a #GFile specifying what @file was renamed to, or %NULL
3450 * if there was an error.
3451 * Free the returned object with g_object_unref().
3454 g_file_set_display_name (GFile *file,
3455 const char *display_name,
3456 GCancellable *cancellable,
3461 g_return_val_if_fail (G_IS_FILE (file), NULL);
3462 g_return_val_if_fail (display_name != NULL, NULL);
3464 if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
3468 G_IO_ERROR_INVALID_ARGUMENT,
3469 _("File names cannot contain '%c'"), G_DIR_SEPARATOR);
3473 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3476 iface = G_FILE_GET_IFACE (file);
3478 return (* iface->set_display_name) (file, display_name, cancellable, error);
3482 * g_file_set_display_name_async:
3483 * @file: input #GFile.
3484 * @display_name: a string.
3485 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3487 * @cancellable: optional #GCancellable object, %NULL to ignore.
3488 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
3489 * @user_data: the data to pass to callback function
3491 * Asynchronously sets the display name for a given #GFile.
3493 * For more details, see g_file_set_display_name() which is
3494 * the synchronous version of this call.
3496 * When the operation is finished, @callback will be called. You can then call
3497 * g_file_set_display_name_finish() to get the result of the operation.
3500 g_file_set_display_name_async (GFile *file,
3501 const char *display_name,
3503 GCancellable *cancellable,
3504 GAsyncReadyCallback callback,
3509 g_return_if_fail (G_IS_FILE (file));
3510 g_return_if_fail (display_name != NULL);
3512 iface = G_FILE_GET_IFACE (file);
3513 (* iface->set_display_name_async) (file,
3522 * g_file_set_display_name_finish:
3523 * @file: input #GFile.
3524 * @res: a #GAsyncResult.
3525 * @error: a #GError, or %NULL
3527 * Finishes setting a display name started with
3528 * g_file_set_display_name_async().
3530 * Returns: a #GFile or %NULL on error.
3531 * Free the returned object with g_object_unref().
3534 g_file_set_display_name_finish (GFile *file,
3540 g_return_val_if_fail (G_IS_FILE (file), NULL);
3541 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
3543 if (G_IS_SIMPLE_ASYNC_RESULT (res))
3545 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3546 if (g_simple_async_result_propagate_error (simple, error))
3550 iface = G_FILE_GET_IFACE (file);
3551 return (* iface->set_display_name_finish) (file, res, error);
3555 * g_file_query_settable_attributes:
3556 * @file: input #GFile.
3557 * @cancellable: optional #GCancellable object, %NULL to ignore.
3558 * @error: a #GError, or %NULL
3560 * Obtain the list of settable attributes for the file.
3562 * Returns the type and full attribute name of all the attributes
3563 * that can be set on this file. This doesn't mean setting it will always
3564 * succeed though, you might get an access failure, or some specific
3565 * file may not support a specific attribute.
3567 * If @cancellable is not %NULL, then the operation can be cancelled by
3568 * triggering the cancellable object from another thread. If the operation
3569 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3571 * Returns: a #GFileAttributeInfoList describing the settable attributes.
3572 * When you are done with it, release it with g_file_attribute_info_list_unref()
3574 GFileAttributeInfoList *
3575 g_file_query_settable_attributes (GFile *file,
3576 GCancellable *cancellable,
3581 GFileAttributeInfoList *list;
3583 g_return_val_if_fail (G_IS_FILE (file), NULL);
3585 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3588 iface = G_FILE_GET_IFACE (file);
3590 if (iface->query_settable_attributes == NULL)
3591 return g_file_attribute_info_list_new ();
3594 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
3598 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
3600 list = g_file_attribute_info_list_new ();
3601 g_error_free (my_error);
3604 g_propagate_error (error, my_error);
3611 * g_file_query_writable_namespaces:
3612 * @file: input #GFile.
3613 * @cancellable: optional #GCancellable object, %NULL to ignore.
3614 * @error: a #GError, or %NULL
3616 * Obtain the list of attribute namespaces where new attributes
3617 * can be created by a user. An example of this is extended
3618 * attributes (in the "xattr" namespace).
3620 * If @cancellable is not %NULL, then the operation can be cancelled by
3621 * triggering the cancellable object from another thread. If the operation
3622 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3624 * Returns: a #GFileAttributeInfoList describing the writable namespaces.
3625 * When you are done with it, release it with g_file_attribute_info_list_unref()
3627 GFileAttributeInfoList *
3628 g_file_query_writable_namespaces (GFile *file,
3629 GCancellable *cancellable,
3634 GFileAttributeInfoList *list;
3636 g_return_val_if_fail (G_IS_FILE (file), NULL);
3638 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3641 iface = G_FILE_GET_IFACE (file);
3643 if (iface->query_writable_namespaces == NULL)
3644 return g_file_attribute_info_list_new ();
3647 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
3651 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
3653 list = g_file_attribute_info_list_new ();
3654 g_error_free (my_error);
3657 g_propagate_error (error, my_error);
3664 * g_file_set_attribute:
3665 * @file: input #GFile.
3666 * @attribute: a string containing the attribute's name.
3667 * @type: The type of the attribute
3668 * @value_p: a pointer to the value (or the pointer itself if the type is a pointer type)
3669 * @flags: a set of #GFileQueryInfoFlags.
3670 * @cancellable: optional #GCancellable object, %NULL to ignore.
3671 * @error: a #GError, or %NULL
3673 * Sets an attribute in the file with attribute name @attribute to @value.
3675 * If @cancellable is not %NULL, then the operation can be cancelled by
3676 * triggering the cancellable object from another thread. If the operation
3677 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3679 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
3682 g_file_set_attribute (GFile *file,
3683 const char *attribute,
3684 GFileAttributeType type,
3686 GFileQueryInfoFlags flags,
3687 GCancellable *cancellable,
3692 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3693 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
3695 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3698 iface = G_FILE_GET_IFACE (file);
3700 if (iface->set_attribute == NULL)
3702 g_set_error_literal (error, G_IO_ERROR,
3703 G_IO_ERROR_NOT_SUPPORTED,
3704 _("Operation not supported"));
3708 return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
3712 * g_file_set_attributes_from_info:
3713 * @file: input #GFile.
3714 * @info: a #GFileInfo.
3715 * @flags: #GFileQueryInfoFlags
3716 * @cancellable: optional #GCancellable object, %NULL to ignore.
3717 * @error: a #GError, or %NULL
3719 * Tries to set all attributes in the #GFileInfo on the target values,
3720 * not stopping on the first error.
3722 * If there is any error during this operation then @error will be set to
3723 * the first error. Error on particular fields are flagged by setting
3724 * the "status" field in the attribute value to
3725 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can also detect
3728 * If @cancellable is not %NULL, then the operation can be cancelled by
3729 * triggering the cancellable object from another thread. If the operation
3730 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3732 * Returns: %TRUE if there was any error, %FALSE otherwise.
3735 g_file_set_attributes_from_info (GFile *file,
3737 GFileQueryInfoFlags flags,
3738 GCancellable *cancellable,
3743 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3744 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
3746 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3749 g_file_info_clear_status (info);
3751 iface = G_FILE_GET_IFACE (file);
3753 return (* iface->set_attributes_from_info) (file,
3762 g_file_real_set_attributes_from_info (GFile *file,
3764 GFileQueryInfoFlags flags,
3765 GCancellable *cancellable,
3771 GFileAttributeValue *value;
3775 attributes = g_file_info_list_attributes (info, NULL);
3777 for (i = 0; attributes[i] != NULL; i++)
3779 value = _g_file_info_get_attribute_value (info, attributes[i]);
3781 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
3784 if (!g_file_set_attribute (file, attributes[i],
3785 value->type, _g_file_attribute_value_peek_as_pointer (value),
3786 flags, cancellable, error))
3788 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
3790 /* Don't set error multiple times */
3794 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
3797 g_strfreev (attributes);
3803 * g_file_set_attributes_async:
3804 * @file: input #GFile.
3805 * @info: a #GFileInfo.
3806 * @flags: a #GFileQueryInfoFlags.
3807 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3809 * @cancellable: optional #GCancellable object, %NULL to ignore.
3810 * @callback: a #GAsyncReadyCallback.
3811 * @user_data: a #gpointer.
3813 * Asynchronously sets the attributes of @file with @info.
3815 * For more details, see g_file_set_attributes_from_info() which is
3816 * the synchronous version of this call.
3818 * When the operation is finished, @callback will be called. You can then call
3819 * g_file_set_attributes_finish() to get the result of the operation.
3822 g_file_set_attributes_async (GFile *file,
3824 GFileQueryInfoFlags flags,
3826 GCancellable *cancellable,
3827 GAsyncReadyCallback callback,
3832 g_return_if_fail (G_IS_FILE (file));
3833 g_return_if_fail (G_IS_FILE_INFO (info));
3835 iface = G_FILE_GET_IFACE (file);
3836 (* iface->set_attributes_async) (file,
3846 * g_file_set_attributes_finish:
3847 * @file: input #GFile.
3848 * @result: a #GAsyncResult.
3849 * @info: a #GFileInfo.
3850 * @error: a #GError, or %NULL
3852 * Finishes setting an attribute started in g_file_set_attributes_async().
3854 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
3857 g_file_set_attributes_finish (GFile *file,
3858 GAsyncResult *result,
3864 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3865 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3867 /* No standard handling of errors here, as we must set info even
3870 iface = G_FILE_GET_IFACE (file);
3871 return (* iface->set_attributes_finish) (file, result, info, error);
3875 * g_file_set_attribute_string:
3876 * @file: input #GFile.
3877 * @attribute: a string containing the attribute's name.
3878 * @value: a string containing the attribute's value.
3879 * @flags: #GFileQueryInfoFlags.
3880 * @cancellable: optional #GCancellable object, %NULL to ignore.
3881 * @error: a #GError, or %NULL
3883 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
3884 * If @attribute is of a different type, this operation will fail.
3886 * If @cancellable is not %NULL, then the operation can be cancelled by
3887 * triggering the cancellable object from another thread. If the operation
3888 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3890 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
3893 g_file_set_attribute_string (GFile *file,
3894 const char *attribute,
3896 GFileQueryInfoFlags flags,
3897 GCancellable *cancellable,
3900 return g_file_set_attribute (file, attribute,
3901 G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
3902 flags, cancellable, error);
3906 * g_file_set_attribute_byte_string:
3907 * @file: input #GFile.
3908 * @attribute: a string containing the attribute's name.
3909 * @value: a string containing the attribute's new value.
3910 * @flags: a #GFileQueryInfoFlags.
3911 * @cancellable: optional #GCancellable object, %NULL to ignore.
3912 * @error: a #GError, or %NULL
3914 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
3915 * If @attribute is of a different type, this operation will fail,
3918 * If @cancellable is not %NULL, then the operation can be cancelled by
3919 * triggering the cancellable object from another thread. If the operation
3920 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3922 * Returns: %TRUE if the @attribute was successfully set to @value
3923 * in the @file, %FALSE otherwise.
3926 g_file_set_attribute_byte_string (GFile *file,
3927 const char *attribute,
3929 GFileQueryInfoFlags flags,
3930 GCancellable *cancellable,
3933 return g_file_set_attribute (file, attribute,
3934 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
3935 flags, cancellable, error);
3939 * g_file_set_attribute_uint32:
3940 * @file: input #GFile.
3941 * @attribute: a string containing the attribute's name.
3942 * @value: a #guint32 containing the attribute's new value.
3943 * @flags: a #GFileQueryInfoFlags.
3944 * @cancellable: optional #GCancellable object, %NULL to ignore.
3945 * @error: a #GError, or %NULL
3947 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
3948 * If @attribute is of a different type, this operation will fail.
3950 * If @cancellable is not %NULL, then the operation can be cancelled by
3951 * triggering the cancellable object from another thread. If the operation
3952 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3954 * Returns: %TRUE if the @attribute was successfully set to @value
3955 * in the @file, %FALSE otherwise.
3958 g_file_set_attribute_uint32 (GFile *file,
3959 const char *attribute,
3961 GFileQueryInfoFlags flags,
3962 GCancellable *cancellable,
3965 return g_file_set_attribute (file, attribute,
3966 G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
3967 flags, cancellable, error);
3971 * g_file_set_attribute_int32:
3972 * @file: input #GFile.
3973 * @attribute: a string containing the attribute's name.
3974 * @value: a #gint32 containing the attribute's new value.
3975 * @flags: a #GFileQueryInfoFlags.
3976 * @cancellable: optional #GCancellable object, %NULL to ignore.
3977 * @error: a #GError, or %NULL
3979 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
3980 * If @attribute is of a different type, this operation will fail.
3982 * If @cancellable is not %NULL, then the operation can be cancelled by
3983 * triggering the cancellable object from another thread. If the operation
3984 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3986 * Returns: %TRUE if the @attribute was successfully set to @value
3987 * in the @file, %FALSE otherwise.
3990 g_file_set_attribute_int32 (GFile *file,
3991 const char *attribute,
3993 GFileQueryInfoFlags flags,
3994 GCancellable *cancellable,
3997 return g_file_set_attribute (file, attribute,
3998 G_FILE_ATTRIBUTE_TYPE_INT32, &value,
3999 flags, cancellable, error);
4003 * g_file_set_attribute_uint64:
4004 * @file: input #GFile.
4005 * @attribute: a string containing the attribute's name.
4006 * @value: a #guint64 containing the attribute's new value.
4007 * @flags: a #GFileQueryInfoFlags.
4008 * @cancellable: optional #GCancellable object, %NULL to ignore.
4009 * @error: a #GError, or %NULL
4011 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
4012 * If @attribute is of a different type, this operation will fail.
4014 * If @cancellable is not %NULL, then the operation can be cancelled by
4015 * triggering the cancellable object from another thread. If the operation
4016 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4018 * Returns: %TRUE if the @attribute was successfully set to @value
4019 * in the @file, %FALSE otherwise.
4022 g_file_set_attribute_uint64 (GFile *file,
4023 const char *attribute,
4025 GFileQueryInfoFlags flags,
4026 GCancellable *cancellable,
4029 return g_file_set_attribute (file, attribute,
4030 G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
4031 flags, cancellable, error);
4035 * g_file_set_attribute_int64:
4036 * @file: input #GFile.
4037 * @attribute: a string containing the attribute's name.
4038 * @value: a #guint64 containing the attribute's new value.
4039 * @flags: a #GFileQueryInfoFlags.
4040 * @cancellable: optional #GCancellable object, %NULL to ignore.
4041 * @error: a #GError, or %NULL
4043 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
4044 * If @attribute is of a different type, this operation will fail.
4046 * If @cancellable is not %NULL, then the operation can be cancelled by
4047 * triggering the cancellable object from another thread. If the operation
4048 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4050 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4053 g_file_set_attribute_int64 (GFile *file,
4054 const char *attribute,
4056 GFileQueryInfoFlags flags,
4057 GCancellable *cancellable,
4060 return g_file_set_attribute (file, attribute,
4061 G_FILE_ATTRIBUTE_TYPE_INT64, &value,
4062 flags, cancellable, error);
4066 * g_file_mount_mountable:
4067 * @file: input #GFile.
4068 * @flags: flags affecting the operation
4069 * @mount_operation: a #GMountOperation, or %NULL to avoid user interaction.
4070 * @cancellable: optional #GCancellable object, %NULL to ignore.
4071 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4072 * @user_data: the data to pass to callback function
4074 * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
4075 * Using @mount_operation, you can request callbacks when, for instance,
4076 * passwords are needed during authentication.
4078 * If @cancellable is not %NULL, then the operation can be cancelled by
4079 * triggering the cancellable object from another thread. If the operation
4080 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4082 * When the operation is finished, @callback will be called. You can then call
4083 * g_file_mount_mountable_finish() to get the result of the operation.
4086 g_file_mount_mountable (GFile *file,
4087 GMountMountFlags flags,
4088 GMountOperation *mount_operation,
4089 GCancellable *cancellable,
4090 GAsyncReadyCallback callback,
4095 g_return_if_fail (G_IS_FILE (file));
4097 iface = G_FILE_GET_IFACE (file);
4099 if (iface->mount_mountable == NULL)
4101 g_simple_async_report_error_in_idle (G_OBJECT (file),
4105 G_IO_ERROR_NOT_SUPPORTED,
4106 _("Operation not supported"));
4110 (* iface->mount_mountable) (file,
4119 * g_file_mount_mountable_finish:
4120 * @file: input #GFile.
4121 * @result: a #GAsyncResult.
4122 * @error: a #GError, or %NULL
4124 * Finishes a mount operation. See g_file_mount_mountable() for details.
4126 * Finish an asynchronous mount operation that was started
4127 * with g_file_mount_mountable().
4129 * Returns: a #GFile or %NULL on error.
4130 * Free the returned object with g_object_unref().
4133 g_file_mount_mountable_finish (GFile *file,
4134 GAsyncResult *result,
4139 g_return_val_if_fail (G_IS_FILE (file), NULL);
4140 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
4142 if (G_IS_SIMPLE_ASYNC_RESULT (result))
4144 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
4145 if (g_simple_async_result_propagate_error (simple, error))
4149 iface = G_FILE_GET_IFACE (file);
4150 return (* iface->mount_mountable_finish) (file, result, error);
4154 * g_file_unmount_mountable:
4155 * @file: input #GFile.
4156 * @flags: flags affecting the operation
4157 * @cancellable: optional #GCancellable object, %NULL to ignore.
4158 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4159 * @user_data: the data to pass to callback function
4161 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
4163 * If @cancellable is not %NULL, then the operation can be cancelled by
4164 * triggering the cancellable object from another thread. If the operation
4165 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4167 * When the operation is finished, @callback will be called. You can then call
4168 * g_file_unmount_mountable_finish() to get the result of the operation.
4170 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation() instead.
4173 g_file_unmount_mountable (GFile *file,
4174 GMountUnmountFlags flags,
4175 GCancellable *cancellable,
4176 GAsyncReadyCallback callback,
4181 g_return_if_fail (G_IS_FILE (file));
4183 iface = G_FILE_GET_IFACE (file);
4185 if (iface->unmount_mountable == NULL)
4187 g_simple_async_report_error_in_idle (G_OBJECT (file),
4191 G_IO_ERROR_NOT_SUPPORTED,
4192 _("Operation not supported"));
4196 (* iface->unmount_mountable) (file,
4204 * g_file_unmount_mountable_finish:
4205 * @file: input #GFile.
4206 * @result: a #GAsyncResult.
4207 * @error: a #GError, or %NULL
4209 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
4211 * Finish an asynchronous unmount operation that was started
4212 * with g_file_unmount_mountable().
4214 * Returns: %TRUE if the operation finished successfully. %FALSE
4217 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation_finish() instead.
4220 g_file_unmount_mountable_finish (GFile *file,
4221 GAsyncResult *result,
4226 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4227 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4229 if (G_IS_SIMPLE_ASYNC_RESULT (result))
4231 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
4232 if (g_simple_async_result_propagate_error (simple, error))
4236 iface = G_FILE_GET_IFACE (file);
4237 return (* iface->unmount_mountable_finish) (file, result, error);
4241 * g_file_unmount_mountable_with_operation:
4242 * @file: input #GFile.
4243 * @flags: flags affecting the operation
4244 * @mount_operation: a #GMountOperation, or %NULL to avoid user interaction.
4245 * @cancellable: optional #GCancellable object, %NULL to ignore.
4246 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4247 * @user_data: the data to pass to callback function
4249 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
4251 * If @cancellable is not %NULL, then the operation can be cancelled by
4252 * triggering the cancellable object from another thread. If the operation
4253 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4255 * When the operation is finished, @callback will be called. You can then call
4256 * g_file_unmount_mountable_finish() to get the result of the operation.
4261 g_file_unmount_mountable_with_operation (GFile *file,
4262 GMountUnmountFlags flags,
4263 GMountOperation *mount_operation,
4264 GCancellable *cancellable,
4265 GAsyncReadyCallback callback,
4270 g_return_if_fail (G_IS_FILE (file));
4272 iface = G_FILE_GET_IFACE (file);
4274 if (iface->unmount_mountable == NULL && iface->unmount_mountable_with_operation == NULL)
4276 g_simple_async_report_error_in_idle (G_OBJECT (file),
4280 G_IO_ERROR_NOT_SUPPORTED,
4281 _("Operation not supported"));
4285 if (iface->unmount_mountable_with_operation != NULL)
4286 (* iface->unmount_mountable_with_operation) (file,
4293 (* iface->unmount_mountable) (file,
4301 * g_file_unmount_mountable_with_operation_finish:
4302 * @file: input #GFile.
4303 * @result: a #GAsyncResult.
4304 * @error: a #GError, or %NULL
4306 * Finishes an unmount operation, see g_file_unmount_mountable_with_operation() for details.
4308 * Finish an asynchronous unmount operation that was started
4309 * with g_file_unmount_mountable_with_operation().
4311 * Returns: %TRUE if the operation finished successfully. %FALSE
4317 g_file_unmount_mountable_with_operation_finish (GFile *file,
4318 GAsyncResult *result,
4323 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4324 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4326 if (G_IS_SIMPLE_ASYNC_RESULT (result))
4328 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
4329 if (g_simple_async_result_propagate_error (simple, error))
4333 iface = G_FILE_GET_IFACE (file);
4334 if (iface->unmount_mountable_with_operation_finish != NULL)
4335 return (* iface->unmount_mountable_with_operation_finish) (file, result, error);
4337 return (* iface->unmount_mountable_finish) (file, result, error);
4341 * g_file_eject_mountable:
4342 * @file: input #GFile.
4343 * @flags: flags affecting the operation
4344 * @cancellable: optional #GCancellable object, %NULL to ignore.
4345 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4346 * @user_data: the data to pass to callback function
4348 * Starts an asynchronous eject on a mountable.
4349 * When this operation has completed, @callback will be called with
4350 * @user_user data, and the operation can be finalized with
4351 * g_file_eject_mountable_finish().
4353 * If @cancellable is not %NULL, then the operation can be cancelled by
4354 * triggering the cancellable object from another thread. If the operation
4355 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4357 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation() instead.
4360 g_file_eject_mountable (GFile *file,
4361 GMountUnmountFlags flags,
4362 GCancellable *cancellable,
4363 GAsyncReadyCallback callback,
4368 g_return_if_fail (G_IS_FILE (file));
4370 iface = G_FILE_GET_IFACE (file);
4372 if (iface->eject_mountable == NULL)
4374 g_simple_async_report_error_in_idle (G_OBJECT (file),
4378 G_IO_ERROR_NOT_SUPPORTED,
4379 _("Operation not supported"));
4383 (* iface->eject_mountable) (file,
4391 * g_file_eject_mountable_finish:
4392 * @file: input #GFile.
4393 * @result: a #GAsyncResult.
4394 * @error: a #GError, or %NULL
4396 * Finishes an asynchronous eject operation started by
4397 * g_file_eject_mountable().
4399 * Returns: %TRUE if the @file was ejected successfully. %FALSE
4402 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation_finish() instead.
4405 g_file_eject_mountable_finish (GFile *file,
4406 GAsyncResult *result,
4411 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4412 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4414 if (G_IS_SIMPLE_ASYNC_RESULT (result))
4416 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
4417 if (g_simple_async_result_propagate_error (simple, error))
4421 iface = G_FILE_GET_IFACE (file);
4422 return (* iface->eject_mountable_finish) (file, result, error);
4426 * g_file_eject_mountable_with_operation:
4427 * @file: input #GFile.
4428 * @flags: flags affecting the operation
4429 * @mount_operation: a #GMountOperation, or %NULL to avoid user interaction.
4430 * @cancellable: optional #GCancellable object, %NULL to ignore.
4431 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4432 * @user_data: the data to pass to callback function
4434 * Starts an asynchronous eject on a mountable.
4435 * When this operation has completed, @callback will be called with
4436 * @user_user data, and the operation can be finalized with
4437 * g_file_eject_mountable_with_operation_finish().
4439 * If @cancellable is not %NULL, then the operation can be cancelled by
4440 * triggering the cancellable object from another thread. If the operation
4441 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4446 g_file_eject_mountable_with_operation (GFile *file,
4447 GMountUnmountFlags flags,
4448 GMountOperation *mount_operation,
4449 GCancellable *cancellable,
4450 GAsyncReadyCallback callback,
4455 g_return_if_fail (G_IS_FILE (file));
4457 iface = G_FILE_GET_IFACE (file);
4459 if (iface->eject_mountable == NULL && iface->eject_mountable_with_operation == NULL)
4461 g_simple_async_report_error_in_idle (G_OBJECT (file),
4465 G_IO_ERROR_NOT_SUPPORTED,
4466 _("Operation not supported"));
4470 if (iface->eject_mountable_with_operation != NULL)
4471 (* iface->eject_mountable_with_operation) (file,
4478 (* iface->eject_mountable) (file,
4486 * g_file_eject_mountable_with_operation_finish:
4487 * @file: input #GFile.
4488 * @result: a #GAsyncResult.
4489 * @error: a #GError, or %NULL
4491 * Finishes an asynchronous eject operation started by
4492 * g_file_eject_mountable_with_operation().
4494 * Returns: %TRUE if the @file was ejected successfully. %FALSE
4500 g_file_eject_mountable_with_operation_finish (GFile *file,
4501 GAsyncResult *result,
4506 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4507 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4509 if (G_IS_SIMPLE_ASYNC_RESULT (result))
4511 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
4512 if (g_simple_async_result_propagate_error (simple, error))
4516 iface = G_FILE_GET_IFACE (file);
4517 if (iface->eject_mountable_with_operation_finish != NULL)
4518 return (* iface->eject_mountable_with_operation_finish) (file, result, error);
4520 return (* iface->eject_mountable_finish) (file, result, error);
4524 * g_file_monitor_directory:
4525 * @file: input #GFile.
4526 * @flags: a set of #GFileMonitorFlags.
4527 * @cancellable: optional #GCancellable object, %NULL to ignore.
4528 * @error: a #GError, or %NULL.
4530 * Obtains a directory monitor for the given file.
4531 * This may fail if directory monitoring is not supported.
4533 * If @cancellable is not %NULL, then the operation can be cancelled by
4534 * triggering the cancellable object from another thread. If the operation
4535 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4537 * Returns: a #GFileMonitor for the given @file, or %NULL on error.
4538 * Free the returned object with g_object_unref().
4541 g_file_monitor_directory (GFile *file,
4542 GFileMonitorFlags flags,
4543 GCancellable *cancellable,
4548 g_return_val_if_fail (G_IS_FILE (file), NULL);
4550 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4553 iface = G_FILE_GET_IFACE (file);
4555 if (iface->monitor_dir == NULL)
4557 g_set_error_literal (error, G_IO_ERROR,
4558 G_IO_ERROR_NOT_SUPPORTED,
4559 _("Operation not supported"));
4563 return (* iface->monitor_dir) (file, flags, cancellable, error);
4567 * g_file_monitor_file:
4568 * @file: input #GFile.
4569 * @flags: a set of #GFileMonitorFlags.
4570 * @cancellable: optional #GCancellable object, %NULL to ignore.
4571 * @error: a #GError, or %NULL.
4573 * Obtains a file monitor for the given file. If no file notification
4574 * mechanism exists, then regular polling of the file is used.
4576 * If @cancellable is not %NULL, then the operation can be cancelled by
4577 * triggering the cancellable object from another thread. If the operation
4578 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4580 * Returns: a #GFileMonitor for the given @file, or %NULL on error.
4581 * Free the returned object with g_object_unref().
4584 g_file_monitor_file (GFile *file,
4585 GFileMonitorFlags flags,
4586 GCancellable *cancellable,
4590 GFileMonitor *monitor;
4592 g_return_val_if_fail (G_IS_FILE (file), NULL);
4594 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4597 iface = G_FILE_GET_IFACE (file);
4601 if (iface->monitor_file)
4602 monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
4604 /* Fallback to polling */
4605 if (monitor == NULL)
4606 monitor = _g_poll_file_monitor_new (file);
4613 * @file: input #GFile
4614 * @flags: a set of #GFileMonitorFlags
4615 * @cancellable: optional #GCancellable object, %NULL to ignore
4616 * @error: a #GError, or %NULL
4618 * Obtains a file or directory monitor for the given file, depending
4619 * on the type of the file.
4621 * If @cancellable is not %NULL, then the operation can be cancelled by
4622 * triggering the cancellable object from another thread. If the operation
4623 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4625 * Returns: a #GFileMonitor for the given @file, or %NULL on error.
4626 * Free the returned object with g_object_unref().
4631 g_file_monitor (GFile *file,
4632 GFileMonitorFlags flags,
4633 GCancellable *cancellable,
4636 if (g_file_query_file_type (file, 0, cancellable) == G_FILE_TYPE_DIRECTORY)
4637 return g_file_monitor_directory (file, flags, cancellable, error);
4639 return g_file_monitor_file (file, flags, cancellable, error);
4642 /********************************************
4643 * Default implementation of async ops *
4644 ********************************************/
4648 GFileQueryInfoFlags flags;
4650 } QueryInfoAsyncData;
4653 query_info_data_free (QueryInfoAsyncData *data)
4656 g_object_unref (data->info);
4657 g_free (data->attributes);
4662 query_info_async_thread (GSimpleAsyncResult *res,
4664 GCancellable *cancellable)
4666 GError *error = NULL;
4667 QueryInfoAsyncData *data;
4670 data = g_simple_async_result_get_op_res_gpointer (res);
4672 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
4676 g_simple_async_result_set_from_error (res, error);
4677 g_error_free (error);
4684 g_file_real_query_info_async (GFile *file,
4685 const char *attributes,
4686 GFileQueryInfoFlags flags,
4688 GCancellable *cancellable,
4689 GAsyncReadyCallback callback,
4692 GSimpleAsyncResult *res;
4693 QueryInfoAsyncData *data;
4695 data = g_new0 (QueryInfoAsyncData, 1);
4696 data->attributes = g_strdup (attributes);
4697 data->flags = flags;
4699 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_info_async);
4700 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
4702 g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
4703 g_object_unref (res);
4707 g_file_real_query_info_finish (GFile *file,
4711 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4712 QueryInfoAsyncData *data;
4714 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_info_async);
4716 data = g_simple_async_result_get_op_res_gpointer (simple);
4718 return g_object_ref (data->info);
4726 } QueryFilesystemInfoAsyncData;
4729 query_filesystem_info_data_free (QueryFilesystemInfoAsyncData *data)
4732 g_object_unref (data->info);
4733 g_free (data->attributes);
4738 query_filesystem_info_async_thread (GSimpleAsyncResult *res,
4740 GCancellable *cancellable)
4742 GError *error = NULL;
4743 QueryFilesystemInfoAsyncData *data;
4746 data = g_simple_async_result_get_op_res_gpointer (res);
4748 info = g_file_query_filesystem_info (G_FILE (object), data->attributes, cancellable, &error);
4752 g_simple_async_result_set_from_error (res, error);
4753 g_error_free (error);
4760 g_file_real_query_filesystem_info_async (GFile *file,
4761 const char *attributes,
4763 GCancellable *cancellable,
4764 GAsyncReadyCallback callback,
4767 GSimpleAsyncResult *res;
4768 QueryFilesystemInfoAsyncData *data;
4770 data = g_new0 (QueryFilesystemInfoAsyncData, 1);
4771 data->attributes = g_strdup (attributes);
4773 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_filesystem_info_async);
4774 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_filesystem_info_data_free);
4776 g_simple_async_result_run_in_thread (res, query_filesystem_info_async_thread, io_priority, cancellable);
4777 g_object_unref (res);
4781 g_file_real_query_filesystem_info_finish (GFile *file,
4785 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4786 QueryFilesystemInfoAsyncData *data;
4788 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_filesystem_info_async);
4790 data = g_simple_async_result_get_op_res_gpointer (simple);
4792 return g_object_ref (data->info);
4799 GFileQueryInfoFlags flags;
4800 GFileEnumerator *enumerator;
4801 } EnumerateChildrenAsyncData;
4804 enumerate_children_data_free (EnumerateChildrenAsyncData *data)
4806 if (data->enumerator)
4807 g_object_unref (data->enumerator);
4808 g_free (data->attributes);
4813 enumerate_children_async_thread (GSimpleAsyncResult *res,
4815 GCancellable *cancellable)
4817 GError *error = NULL;
4818 EnumerateChildrenAsyncData *data;
4819 GFileEnumerator *enumerator;
4821 data = g_simple_async_result_get_op_res_gpointer (res);
4823 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
4825 if (enumerator == NULL)
4827 g_simple_async_result_set_from_error (res, error);
4828 g_error_free (error);
4831 data->enumerator = enumerator;
4835 g_file_real_enumerate_children_async (GFile *file,
4836 const char *attributes,
4837 GFileQueryInfoFlags flags,
4839 GCancellable *cancellable,
4840 GAsyncReadyCallback callback,
4843 GSimpleAsyncResult *res;
4844 EnumerateChildrenAsyncData *data;
4846 data = g_new0 (EnumerateChildrenAsyncData, 1);
4847 data->attributes = g_strdup (attributes);
4848 data->flags = flags;
4850 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_enumerate_children_async);
4851 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)enumerate_children_data_free);
4853 g_simple_async_result_run_in_thread (res, enumerate_children_async_thread, io_priority, cancellable);
4854 g_object_unref (res);
4857 static GFileEnumerator *
4858 g_file_real_enumerate_children_finish (GFile *file,
4862 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4863 EnumerateChildrenAsyncData *data;
4865 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_enumerate_children_async);
4867 data = g_simple_async_result_get_op_res_gpointer (simple);
4868 if (data->enumerator)
4869 return g_object_ref (data->enumerator);
4875 open_read_async_thread (GSimpleAsyncResult *res,
4877 GCancellable *cancellable)
4880 GFileInputStream *stream;
4881 GError *error = NULL;
4883 iface = G_FILE_GET_IFACE (object);
4885 if (iface->read_fn == NULL)
4887 g_set_error_literal (&error, G_IO_ERROR,
4888 G_IO_ERROR_NOT_SUPPORTED,
4889 _("Operation not supported"));
4891 g_simple_async_result_set_from_error (res, error);
4892 g_error_free (error);
4897 stream = iface->read_fn (G_FILE (object), cancellable, &error);
4901 g_simple_async_result_set_from_error (res, error);
4902 g_error_free (error);
4905 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4909 g_file_real_read_async (GFile *file,
4911 GCancellable *cancellable,
4912 GAsyncReadyCallback callback,
4915 GSimpleAsyncResult *res;
4917 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_read_async);
4919 g_simple_async_result_run_in_thread (res, open_read_async_thread, io_priority, cancellable);
4920 g_object_unref (res);
4923 static GFileInputStream *
4924 g_file_real_read_finish (GFile *file,
4928 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4931 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_read_async);
4933 op = g_simple_async_result_get_op_res_gpointer (simple);
4935 return g_object_ref (op);
4941 append_to_async_thread (GSimpleAsyncResult *res,
4943 GCancellable *cancellable)
4946 GFileCreateFlags *data;
4947 GFileOutputStream *stream;
4948 GError *error = NULL;
4950 iface = G_FILE_GET_IFACE (object);
4952 data = g_simple_async_result_get_op_res_gpointer (res);
4954 stream = iface->append_to (G_FILE (object), *data, cancellable, &error);
4958 g_simple_async_result_set_from_error (res, error);
4959 g_error_free (error);
4962 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4966 g_file_real_append_to_async (GFile *file,
4967 GFileCreateFlags flags,
4969 GCancellable *cancellable,
4970 GAsyncReadyCallback callback,
4973 GFileCreateFlags *data;
4974 GSimpleAsyncResult *res;
4976 data = g_new0 (GFileCreateFlags, 1);
4979 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_append_to_async);
4980 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
4982 g_simple_async_result_run_in_thread (res, append_to_async_thread, io_priority, cancellable);
4983 g_object_unref (res);
4986 static GFileOutputStream *
4987 g_file_real_append_to_finish (GFile *file,
4991 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4994 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_append_to_async);
4996 op = g_simple_async_result_get_op_res_gpointer (simple);
4998 return g_object_ref (op);
5004 create_async_thread (GSimpleAsyncResult *res,
5006 GCancellable *cancellable)
5009 GFileCreateFlags *data;
5010 GFileOutputStream *stream;
5011 GError *error = NULL;
5013 iface = G_FILE_GET_IFACE (object);
5015 data = g_simple_async_result_get_op_res_gpointer (res);
5017 stream = iface->create (G_FILE (object), *data, cancellable, &error);
5021 g_simple_async_result_set_from_error (res, error);
5022 g_error_free (error);
5025 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
5029 g_file_real_create_async (GFile *file,
5030 GFileCreateFlags flags,
5032 GCancellable *cancellable,
5033 GAsyncReadyCallback callback,
5036 GFileCreateFlags *data;
5037 GSimpleAsyncResult *res;
5039 data = g_new0 (GFileCreateFlags, 1);
5042 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_create_async);
5043 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
5045 g_simple_async_result_run_in_thread (res, create_async_thread, io_priority, cancellable);
5046 g_object_unref (res);
5049 static GFileOutputStream *
5050 g_file_real_create_finish (GFile *file,
5054 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5057 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_create_async);
5059 op = g_simple_async_result_get_op_res_gpointer (simple);
5061 return g_object_ref (op);
5067 GFileOutputStream *stream;
5069 gboolean make_backup;
5070 GFileCreateFlags flags;
5074 replace_async_data_free (ReplaceAsyncData *data)
5077 g_object_unref (data->stream);
5078 g_free (data->etag);
5083 replace_async_thread (GSimpleAsyncResult *res,
5085 GCancellable *cancellable)
5088 GFileOutputStream *stream;
5089 GError *error = NULL;
5090 ReplaceAsyncData *data;
5092 iface = G_FILE_GET_IFACE (object);
5094 data = g_simple_async_result_get_op_res_gpointer (res);
5096 stream = iface->replace (G_FILE (object),
5105 g_simple_async_result_set_from_error (res, error);
5106 g_error_free (error);
5109 data->stream = stream;
5113 g_file_real_replace_async (GFile *file,
5115 gboolean make_backup,
5116 GFileCreateFlags flags,
5118 GCancellable *cancellable,
5119 GAsyncReadyCallback callback,
5122 GSimpleAsyncResult *res;
5123 ReplaceAsyncData *data;
5125 data = g_new0 (ReplaceAsyncData, 1);
5126 data->etag = g_strdup (etag);
5127 data->make_backup = make_backup;
5128 data->flags = flags;
5130 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_replace_async);
5131 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_async_data_free);
5133 g_simple_async_result_run_in_thread (res, replace_async_thread, io_priority, cancellable);
5134 g_object_unref (res);
5137 static GFileOutputStream *
5138 g_file_real_replace_finish (GFile *file,
5142 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5143 ReplaceAsyncData *data;
5145 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_replace_async);
5147 data = g_simple_async_result_get_op_res_gpointer (simple);
5149 return g_object_ref (data->stream);
5155 open_readwrite_async_thread (GSimpleAsyncResult *res,
5157 GCancellable *cancellable)
5160 GFileIOStream *stream;
5161 GError *error = NULL;
5163 iface = G_FILE_GET_IFACE (object);
5165 if (iface->open_readwrite == NULL)
5167 g_set_error_literal (&error, G_IO_ERROR,
5168 G_IO_ERROR_NOT_SUPPORTED,
5169 _("Operation not supported"));
5171 g_simple_async_result_set_from_error (res, error);
5172 g_error_free (error);
5177 stream = iface->open_readwrite (G_FILE (object), cancellable, &error);
5181 g_simple_async_result_set_from_error (res, error);
5182 g_error_free (error);
5185 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
5189 g_file_real_open_readwrite_async (GFile *file,
5191 GCancellable *cancellable,
5192 GAsyncReadyCallback callback,
5195 GSimpleAsyncResult *res;
5197 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_open_readwrite_async);
5199 g_simple_async_result_run_in_thread (res, open_readwrite_async_thread, io_priority, cancellable);
5200 g_object_unref (res);
5203 static GFileIOStream *
5204 g_file_real_open_readwrite_finish (GFile *file,
5208 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5211 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_open_readwrite_async);
5213 op = g_simple_async_result_get_op_res_gpointer (simple);
5215 return g_object_ref (op);
5221 create_readwrite_async_thread (GSimpleAsyncResult *res,
5223 GCancellable *cancellable)
5226 GFileCreateFlags *data;
5227 GFileIOStream *stream;
5228 GError *error = NULL;
5230 iface = G_FILE_GET_IFACE (object);
5232 data = g_simple_async_result_get_op_res_gpointer (res);
5234 if (iface->create_readwrite == NULL)
5236 g_set_error_literal (&error, G_IO_ERROR,
5237 G_IO_ERROR_NOT_SUPPORTED,
5238 _("Operation not supported"));
5240 g_simple_async_result_set_from_error (res, error);
5241 g_error_free (error);
5246 stream = iface->create_readwrite (G_FILE (object), *data, cancellable, &error);
5250 g_simple_async_result_set_from_error (res, error);
5251 g_error_free (error);
5254 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
5258 g_file_real_create_readwrite_async (GFile *file,
5259 GFileCreateFlags flags,
5261 GCancellable *cancellable,
5262 GAsyncReadyCallback callback,
5265 GFileCreateFlags *data;
5266 GSimpleAsyncResult *res;
5268 data = g_new0 (GFileCreateFlags, 1);
5271 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_create_readwrite_async);
5272 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
5274 g_simple_async_result_run_in_thread (res, create_readwrite_async_thread, io_priority, cancellable);
5275 g_object_unref (res);
5278 static GFileIOStream *
5279 g_file_real_create_readwrite_finish (GFile *file,
5283 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5286 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_create_readwrite_async);
5288 op = g_simple_async_result_get_op_res_gpointer (simple);
5290 return g_object_ref (op);
5296 GFileIOStream *stream;
5298 gboolean make_backup;
5299 GFileCreateFlags flags;
5300 } ReplaceRWAsyncData;
5303 replace_rw_async_data_free (ReplaceRWAsyncData *data)
5306 g_object_unref (data->stream);
5307 g_free (data->etag);
5312 replace_readwrite_async_thread (GSimpleAsyncResult *res,
5314 GCancellable *cancellable)
5317 GFileIOStream *stream;
5318 GError *error = NULL;
5319 ReplaceRWAsyncData *data;
5321 iface = G_FILE_GET_IFACE (object);
5323 data = g_simple_async_result_get_op_res_gpointer (res);
5325 stream = iface->replace_readwrite (G_FILE (object),
5334 g_simple_async_result_set_from_error (res, error);
5335 g_error_free (error);
5338 data->stream = stream;
5342 g_file_real_replace_readwrite_async (GFile *file,
5344 gboolean make_backup,
5345 GFileCreateFlags flags,
5347 GCancellable *cancellable,
5348 GAsyncReadyCallback callback,
5351 GSimpleAsyncResult *res;
5352 ReplaceRWAsyncData *data;
5354 data = g_new0 (ReplaceRWAsyncData, 1);
5355 data->etag = g_strdup (etag);
5356 data->make_backup = make_backup;
5357 data->flags = flags;
5359 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_replace_readwrite_async);
5360 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_rw_async_data_free);
5362 g_simple_async_result_run_in_thread (res, replace_readwrite_async_thread, io_priority, cancellable);
5363 g_object_unref (res);
5366 static GFileIOStream *
5367 g_file_real_replace_readwrite_finish (GFile *file,
5371 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5372 ReplaceRWAsyncData *data;
5374 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_replace_readwrite_async);
5376 data = g_simple_async_result_get_op_res_gpointer (simple);
5378 return g_object_ref (data->stream);
5386 } SetDisplayNameAsyncData;
5389 set_display_name_data_free (SetDisplayNameAsyncData *data)
5391 g_free (data->name);
5393 g_object_unref (data->file);
5398 set_display_name_async_thread (GSimpleAsyncResult *res,
5400 GCancellable *cancellable)
5402 GError *error = NULL;
5403 SetDisplayNameAsyncData *data;
5406 data = g_simple_async_result_get_op_res_gpointer (res);
5408 file = g_file_set_display_name (G_FILE (object), data->name, cancellable, &error);
5412 g_simple_async_result_set_from_error (res, error);
5413 g_error_free (error);
5420 g_file_real_set_display_name_async (GFile *file,
5421 const char *display_name,
5423 GCancellable *cancellable,
5424 GAsyncReadyCallback callback,
5427 GSimpleAsyncResult *res;
5428 SetDisplayNameAsyncData *data;
5430 data = g_new0 (SetDisplayNameAsyncData, 1);
5431 data->name = g_strdup (display_name);
5433 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_display_name_async);
5434 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_display_name_data_free);
5436 g_simple_async_result_run_in_thread (res, set_display_name_async_thread, io_priority, cancellable);
5437 g_object_unref (res);
5441 g_file_real_set_display_name_finish (GFile *file,
5445 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5446 SetDisplayNameAsyncData *data;
5448 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_display_name_async);
5450 data = g_simple_async_result_get_op_res_gpointer (simple);
5452 return g_object_ref (data->file);
5458 GFileQueryInfoFlags flags;
5465 set_info_data_free (SetInfoAsyncData *data)
5468 g_object_unref (data->info);
5470 g_error_free (data->error);
5475 set_info_async_thread (GSimpleAsyncResult *res,
5477 GCancellable *cancellable)
5479 SetInfoAsyncData *data;
5481 data = g_simple_async_result_get_op_res_gpointer (res);
5484 data->res = g_file_set_attributes_from_info (G_FILE (object),
5492 g_file_real_set_attributes_async (GFile *file,
5494 GFileQueryInfoFlags flags,
5496 GCancellable *cancellable,
5497 GAsyncReadyCallback callback,
5500 GSimpleAsyncResult *res;
5501 SetInfoAsyncData *data;
5503 data = g_new0 (SetInfoAsyncData, 1);
5504 data->info = g_file_info_dup (info);
5505 data->flags = flags;
5507 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_attributes_async);
5508 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_info_data_free);
5510 g_simple_async_result_run_in_thread (res, set_info_async_thread, io_priority, cancellable);
5511 g_object_unref (res);
5515 g_file_real_set_attributes_finish (GFile *file,
5520 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5521 SetInfoAsyncData *data;
5523 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_attributes_async);
5525 data = g_simple_async_result_get_op_res_gpointer (simple);
5528 *info = g_object_ref (data->info);
5530 if (error != NULL && data->error)
5531 *error = g_error_copy (data->error);
5537 find_enclosing_mount_async_thread (GSimpleAsyncResult *res,
5539 GCancellable *cancellable)
5541 GError *error = NULL;
5544 mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
5548 g_simple_async_result_set_from_error (res, error);
5549 g_error_free (error);
5552 g_simple_async_result_set_op_res_gpointer (res, mount, (GDestroyNotify)g_object_unref);
5556 g_file_real_find_enclosing_mount_async (GFile *file,
5558 GCancellable *cancellable,
5559 GAsyncReadyCallback callback,
5562 GSimpleAsyncResult *res;
5564 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_find_enclosing_mount_async);
5566 g_simple_async_result_run_in_thread (res, find_enclosing_mount_async_thread, io_priority, cancellable);
5567 g_object_unref (res);
5571 g_file_real_find_enclosing_mount_finish (GFile *file,
5575 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5578 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_find_enclosing_mount_async);
5580 mount = g_simple_async_result_get_op_res_gpointer (simple);
5581 return g_object_ref (mount);
5588 GFileCopyFlags flags;
5589 GFileProgressCallback progress_cb;
5590 gpointer progress_cb_data;
5591 GIOSchedulerJob *job;
5595 copy_async_data_free (CopyAsyncData *data)
5597 g_object_unref (data->source);
5598 g_object_unref (data->destination);
5603 CopyAsyncData *data;
5604 goffset current_num_bytes;
5605 goffset total_num_bytes;
5609 copy_async_progress_in_main (gpointer user_data)
5611 ProgressData *progress = user_data;
5612 CopyAsyncData *data = progress->data;
5614 data->progress_cb (progress->current_num_bytes,
5615 progress->total_num_bytes,
5616 data->progress_cb_data);
5622 mainloop_barrier (gpointer user_data)
5624 /* Does nothing, but ensures all queued idles before
5631 copy_async_progress_callback (goffset current_num_bytes,
5632 goffset total_num_bytes,
5635 CopyAsyncData *data = user_data;
5636 ProgressData *progress;
5638 progress = g_new (ProgressData, 1);
5639 progress->data = data;
5640 progress->current_num_bytes = current_num_bytes;
5641 progress->total_num_bytes = total_num_bytes;
5643 g_io_scheduler_job_send_to_mainloop_async (data->job,
5644 copy_async_progress_in_main,
5650 copy_async_thread (GIOSchedulerJob *job,
5651 GCancellable *cancellable,
5654 GSimpleAsyncResult *res;
5655 CopyAsyncData *data;
5660 data = g_simple_async_result_get_op_res_gpointer (res);
5664 result = g_file_copy (data->source,
5668 (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
5672 /* Ensure all progress callbacks are done running in main thread */
5673 if (data->progress_cb != NULL)
5674 g_io_scheduler_job_send_to_mainloop (job,
5680 g_simple_async_result_set_from_error (res, error);
5681 g_error_free (error);
5684 g_simple_async_result_complete_in_idle (res);
5690 g_file_real_copy_async (GFile *source,
5692 GFileCopyFlags flags,
5694 GCancellable *cancellable,
5695 GFileProgressCallback progress_callback,
5696 gpointer progress_callback_data,
5697 GAsyncReadyCallback callback,
5700 GSimpleAsyncResult *res;
5701 CopyAsyncData *data;
5703 data = g_new0 (CopyAsyncData, 1);
5704 data->source = g_object_ref (source);
5705 data->destination = g_object_ref (destination);
5706 data->flags = flags;
5707 data->progress_cb = progress_callback;
5708 data->progress_cb_data = progress_callback_data;
5710 res = g_simple_async_result_new (G_OBJECT (source), callback, user_data, g_file_real_copy_async);
5711 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)copy_async_data_free);
5713 g_io_scheduler_push_job (copy_async_thread, res, g_object_unref, io_priority, cancellable);
5717 g_file_real_copy_finish (GFile *file,
5721 /* Error handled in g_file_copy_finish() */
5726 /********************************************
5727 * Default VFS operations *
5728 ********************************************/
5731 * g_file_new_for_path:
5732 * @path: a string containing a relative or absolute path.
5734 * Constructs a #GFile for a given path. This operation never
5735 * fails, but the returned object might not support any I/O
5736 * operation if @path is malformed.
5738 * Returns: a new #GFile for the given @path.
5741 g_file_new_for_path (const char *path)
5743 g_return_val_if_fail (path != NULL, NULL);
5745 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
5749 * g_file_new_for_uri:
5750 * @uri: a string containing a URI.
5752 * Constructs a #GFile for a given URI. This operation never
5753 * fails, but the returned object might not support any I/O
5754 * operation if @uri is malformed or if the uri type is
5757 * Returns: a #GFile for the given @uri.
5760 g_file_new_for_uri (const char *uri)
5762 g_return_val_if_fail (uri != NULL, NULL);
5764 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
5768 * g_file_parse_name:
5769 * @parse_name: a file name or path to be parsed.
5771 * Constructs a #GFile with the given @parse_name (i.e. something given by g_file_get_parse_name()).
5772 * This operation never fails, but the returned object might not support any I/O
5773 * operation if the @parse_name cannot be parsed.
5775 * Returns: a new #GFile.
5778 g_file_parse_name (const char *parse_name)
5780 g_return_val_if_fail (parse_name != NULL, NULL);
5782 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
5786 is_valid_scheme_character (char c)
5788 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
5791 /* Following RFC 2396, valid schemes are built like:
5792 * scheme = alpha *( alpha | digit | "+" | "-" | "." )
5795 has_valid_scheme (const char *uri)
5801 if (!g_ascii_isalpha (*p))
5806 } while (is_valid_scheme_character (*p));
5812 * g_file_new_for_commandline_arg:
5813 * @arg: a command line string.
5815 * Creates a #GFile with the given argument from the command line. The value of
5816 * @arg can be either a URI, an absolute path or a relative path resolved
5817 * relative to the current working directory.
5818 * This operation never fails, but the returned object might not support any
5819 * I/O operation if @arg points to a malformed path.
5821 * Returns: a new #GFile.
5824 g_file_new_for_commandline_arg (const char *arg)
5830 g_return_val_if_fail (arg != NULL, NULL);
5832 if (g_path_is_absolute (arg))
5833 return g_file_new_for_path (arg);
5835 if (has_valid_scheme (arg))
5836 return g_file_new_for_uri (arg);
5838 current_dir = g_get_current_dir ();
5839 filename = g_build_filename (current_dir, arg, NULL);
5840 g_free (current_dir);
5842 file = g_file_new_for_path (filename);
5849 * g_file_mount_enclosing_volume:
5850 * @location: input #GFile.
5851 * @flags: flags affecting the operation
5852 * @mount_operation: a #GMountOperation or %NULL to avoid user interaction.
5853 * @cancellable: optional #GCancellable object, %NULL to ignore.
5854 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
5855 * @user_data: the data to pass to callback function
5857 * Starts a @mount_operation, mounting the volume that contains the file @location.
5859 * When this operation has completed, @callback will be called with
5860 * @user_user data, and the operation can be finalized with
5861 * g_file_mount_enclosing_volume_finish().
5863 * If @cancellable is not %NULL, then the operation can be cancelled by
5864 * triggering the cancellable object from another thread. If the operation
5865 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5868 g_file_mount_enclosing_volume (GFile *location,
5869 GMountMountFlags flags,
5870 GMountOperation *mount_operation,
5871 GCancellable *cancellable,
5872 GAsyncReadyCallback callback,
5877 g_return_if_fail (G_IS_FILE (location));
5879 iface = G_FILE_GET_IFACE (location);
5881 if (iface->mount_enclosing_volume == NULL)
5883 g_simple_async_report_error_in_idle (G_OBJECT (location),
5884 callback, user_data,
5885 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5886 _("volume doesn't implement mount"));
5891 (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
5896 * g_file_mount_enclosing_volume_finish:
5897 * @location: input #GFile.
5898 * @result: a #GAsyncResult.
5899 * @error: a #GError, or %NULL
5901 * Finishes a mount operation started by g_file_mount_enclosing_volume().
5903 * Returns: %TRUE if successful. If an error
5904 * has occurred, this function will return %FALSE and set @error
5905 * appropriately if present.
5908 g_file_mount_enclosing_volume_finish (GFile *location,
5909 GAsyncResult *result,
5914 g_return_val_if_fail (G_IS_FILE (location), FALSE);
5915 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5917 if (G_IS_SIMPLE_ASYNC_RESULT (result))
5919 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
5920 if (g_simple_async_result_propagate_error (simple, error))
5924 iface = G_FILE_GET_IFACE (location);
5926 return (* iface->mount_enclosing_volume_finish) (location, result, error);
5929 /********************************************
5930 * Utility functions *
5931 ********************************************/
5934 * g_file_query_default_handler:
5935 * @file: a #GFile to open.
5936 * @cancellable: optional #GCancellable object, %NULL to ignore.
5937 * @error: a #GError, or %NULL
5939 * Returns the #GAppInfo that is registered as the default
5940 * application to handle the file specified by @file.
5942 * If @cancellable is not %NULL, then the operation can be cancelled by
5943 * triggering the cancellable object from another thread. If the operation
5944 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5946 * Returns: a #GAppInfo if the handle was found, %NULL if there were errors.
5947 * When you are done with it, release it with g_object_unref()
5950 g_file_query_default_handler (GFile *file,
5951 GCancellable *cancellable,
5955 const char *content_type;
5960 uri_scheme = g_file_get_uri_scheme (file);
5961 if (uri_scheme && uri_scheme[0] != '\0')
5963 appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
5964 g_free (uri_scheme);
5966 if (appinfo != NULL)
5970 info = g_file_query_info (file,
5971 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
5980 content_type = g_file_info_get_content_type (info);
5983 /* Don't use is_native(), as we want to support fuse paths if availible */
5984 path = g_file_get_path (file);
5985 appinfo = g_app_info_get_default_for_type (content_type,
5990 g_object_unref (info);
5992 if (appinfo != NULL)
5995 g_set_error_literal (error, G_IO_ERROR,
5996 G_IO_ERROR_NOT_SUPPORTED,
5997 _("No application is registered as handling this file"));
6003 #define GET_CONTENT_BLOCK_SIZE 8192
6006 * g_file_load_contents:
6007 * @file: input #GFile.
6008 * @cancellable: optional #GCancellable object, %NULL to ignore.
6009 * @contents: a location to place the contents of the file.
6010 * @length: a location to place the length of the contents of the file,
6011 * or %NULL if the length is not needed
6012 * @etag_out: a location to place the current entity tag for the file,
6013 * or %NULL if the entity tag is not needed
6014 * @error: a #GError, or %NULL
6016 * Loads the content of the file into memory. The data is always
6017 * zero-terminated, but this is not included in the resultant @length.
6018 * The returned @content should be freed with g_free() when no longer
6021 * If @cancellable is not %NULL, then the operation can be cancelled by
6022 * triggering the cancellable object from another thread. If the operation
6023 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6025 * Returns: %TRUE if the @file's contents were successfully loaded.
6026 * %FALSE if there were errors.
6029 g_file_load_contents (GFile *file,
6030 GCancellable *cancellable,
6036 GFileInputStream *in;
6037 GByteArray *content;
6042 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6043 g_return_val_if_fail (contents != NULL, FALSE);
6045 in = g_file_read (file, cancellable, error);
6049 content = g_byte_array_new ();
6052 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6053 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
6054 content->data + pos,
6055 GET_CONTENT_BLOCK_SIZE,
6056 cancellable, error)) > 0)
6059 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6066 info = g_file_input_stream_query_info (in,
6067 G_FILE_ATTRIBUTE_ETAG_VALUE,
6072 *etag_out = g_strdup (g_file_info_get_etag (info));
6073 g_object_unref (info);
6077 /* Ignore errors on close */
6078 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
6079 g_object_unref (in);
6083 /* error is set already */
6084 g_byte_array_free (content, TRUE);
6091 /* Zero terminate (we got an extra byte allocated for this */
6092 content->data[pos] = 0;
6094 *contents = (char *)g_byte_array_free (content, FALSE);
6102 GCancellable *cancellable;
6103 GFileReadMoreCallback read_more_callback;
6104 GAsyncReadyCallback callback;
6106 GByteArray *content;
6113 load_contents_data_free (LoadContentsData *data)
6116 g_error_free (data->error);
6117 if (data->cancellable)
6118 g_object_unref (data->cancellable);
6120 g_byte_array_free (data->content, TRUE);
6121 g_free (data->etag);
6122 g_object_unref (data->file);
6127 load_contents_close_callback (GObject *obj,
6128 GAsyncResult *close_res,
6131 GInputStream *stream = G_INPUT_STREAM (obj);
6132 LoadContentsData *data = user_data;
6133 GSimpleAsyncResult *res;
6135 /* Ignore errors here, we're only reading anyway */
6136 g_input_stream_close_finish (stream, close_res, NULL);
6137 g_object_unref (stream);
6139 res = g_simple_async_result_new (G_OBJECT (data->file),
6142 g_file_load_contents_async);
6143 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)load_contents_data_free);
6144 g_simple_async_result_complete (res);
6145 g_object_unref (res);
6149 load_contents_fstat_callback (GObject *obj,
6150 GAsyncResult *stat_res,
6153 GInputStream *stream = G_INPUT_STREAM (obj);
6154 LoadContentsData *data = user_data;
6157 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
6161 data->etag = g_strdup (g_file_info_get_etag (info));
6162 g_object_unref (info);
6165 g_input_stream_close_async (stream, 0,
6167 load_contents_close_callback, data);
6171 load_contents_read_callback (GObject *obj,
6172 GAsyncResult *read_res,
6175 GInputStream *stream = G_INPUT_STREAM (obj);
6176 LoadContentsData *data = user_data;
6177 GError *error = NULL;
6180 read_size = g_input_stream_read_finish (stream, read_res, &error);
6184 /* Error or EOF, close the file */
6185 data->error = error;
6186 g_input_stream_close_async (stream, 0,
6188 load_contents_close_callback, data);
6190 else if (read_size == 0)
6192 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
6193 G_FILE_ATTRIBUTE_ETAG_VALUE,
6196 load_contents_fstat_callback,
6199 else if (read_size > 0)
6201 data->pos += read_size;
6203 g_byte_array_set_size (data->content,
6204 data->pos + GET_CONTENT_BLOCK_SIZE);
6207 if (data->read_more_callback &&
6208 !data->read_more_callback ((char *)data->content->data, data->pos, data->user_data))
6209 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
6210 G_FILE_ATTRIBUTE_ETAG_VALUE,
6213 load_contents_fstat_callback,
6216 g_input_stream_read_async (stream,
6217 data->content->data + data->pos,
6218 GET_CONTENT_BLOCK_SIZE,
6221 load_contents_read_callback,
6227 load_contents_open_callback (GObject *obj,
6228 GAsyncResult *open_res,
6231 GFile *file = G_FILE (obj);
6232 GFileInputStream *stream;
6233 LoadContentsData *data = user_data;
6234 GError *error = NULL;
6235 GSimpleAsyncResult *res;
6237 stream = g_file_read_finish (file, open_res, &error);
6241 g_byte_array_set_size (data->content,
6242 data->pos + GET_CONTENT_BLOCK_SIZE);
6243 g_input_stream_read_async (G_INPUT_STREAM (stream),
6244 data->content->data + data->pos,
6245 GET_CONTENT_BLOCK_SIZE,
6248 load_contents_read_callback,
6254 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
6258 g_simple_async_result_complete (res);
6259 g_error_free (error);
6260 load_contents_data_free (data);
6261 g_object_unref (res);
6266 * g_file_load_partial_contents_async:
6267 * @file: input #GFile.
6268 * @cancellable: optional #GCancellable object, %NULL to ignore.
6269 * @read_more_callback: a #GFileReadMoreCallback to receive partial data and to specify whether further data should be read.
6270 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6271 * @user_data: the data to pass to the callback functions.
6273 * Reads the partial contents of a file. A #GFileReadMoreCallback should be
6274 * used to stop reading from the file when appropriate, else this function
6275 * will behave exactly as g_file_load_contents_async(). This operation
6276 * can be finished by g_file_load_partial_contents_finish().
6278 * Users of this function should be aware that @user_data is passed to
6279 * both the @read_more_callback and the @callback.
6281 * If @cancellable is not %NULL, then the operation can be cancelled by
6282 * triggering the cancellable object from another thread. If the operation
6283 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6286 g_file_load_partial_contents_async (GFile *file,
6287 GCancellable *cancellable,
6288 GFileReadMoreCallback read_more_callback,
6289 GAsyncReadyCallback callback,
6292 LoadContentsData *data;
6294 g_return_if_fail (G_IS_FILE (file));
6296 data = g_new0 (LoadContentsData, 1);
6299 data->cancellable = g_object_ref (cancellable);
6300 data->read_more_callback = read_more_callback;
6301 data->callback = callback;
6302 data->user_data = user_data;
6303 data->content = g_byte_array_new ();
6304 data->file = g_object_ref (file);
6306 g_file_read_async (file,
6309 load_contents_open_callback,
6314 * g_file_load_partial_contents_finish:
6315 * @file: input #GFile.
6316 * @res: a #GAsyncResult.
6317 * @contents: a location to place the contents of the file.
6318 * @length: a location to place the length of the contents of the file,
6319 * or %NULL if the length is not needed
6320 * @etag_out: a location to place the current entity tag for the file,
6321 * or %NULL if the entity tag is not needed
6322 * @error: a #GError, or %NULL
6324 * Finishes an asynchronous partial load operation that was started
6325 * with g_file_load_partial_contents_async(). The data is always
6326 * zero-terminated, but this is not included in the resultant @length.
6327 * The returned @content should be freed with g_free() when no longer
6330 * Returns: %TRUE if the load was successful. If %FALSE and @error is
6331 * present, it will be set appropriately.
6334 g_file_load_partial_contents_finish (GFile *file,
6341 GSimpleAsyncResult *simple;
6342 LoadContentsData *data;
6344 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6345 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
6346 g_return_val_if_fail (contents != NULL, FALSE);
6348 simple = G_SIMPLE_ASYNC_RESULT (res);
6350 if (g_simple_async_result_propagate_error (simple, error))
6353 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_load_contents_async);
6355 data = g_simple_async_result_get_op_res_gpointer (simple);
6359 g_propagate_error (error, data->error);
6368 *length = data->pos;
6372 *etag_out = data->etag;
6376 /* Zero terminate */
6377 g_byte_array_set_size (data->content, data->pos + 1);
6378 data->content->data[data->pos] = 0;
6380 *contents = (char *)g_byte_array_free (data->content, FALSE);
6381 data->content = NULL;
6387 * g_file_load_contents_async:
6388 * @file: input #GFile.
6389 * @cancellable: optional #GCancellable object, %NULL to ignore.
6390 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6391 * @user_data: the data to pass to callback function
6393 * Starts an asynchronous load of the @file's contents.
6395 * For more details, see g_file_load_contents() which is
6396 * the synchronous version of this call.
6398 * When the load operation has completed, @callback will be called
6399 * with @user data. To finish the operation, call
6400 * g_file_load_contents_finish() with the #GAsyncResult returned by
6403 * If @cancellable is not %NULL, then the operation can be cancelled by
6404 * triggering the cancellable object from another thread. If the operation
6405 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6408 g_file_load_contents_async (GFile *file,
6409 GCancellable *cancellable,
6410 GAsyncReadyCallback callback,
6413 g_file_load_partial_contents_async (file,
6416 callback, user_data);
6420 * g_file_load_contents_finish:
6421 * @file: input #GFile.
6422 * @res: a #GAsyncResult.
6423 * @contents: a location to place the contents of the file.
6424 * @length: a location to place the length of the contents of the file,
6425 * or %NULL if the length is not needed
6426 * @etag_out: a location to place the current entity tag for the file,
6427 * or %NULL if the entity tag is not needed
6428 * @error: a #GError, or %NULL
6430 * Finishes an asynchronous load of the @file's contents.
6431 * The contents are placed in @contents, and @length is set to the
6432 * size of the @contents string. The @content should be freed with
6433 * g_free() when no longer needed. If @etag_out is present, it will be
6434 * set to the new entity tag for the @file.
6436 * Returns: %TRUE if the load was successful. If %FALSE and @error is
6437 * present, it will be set appropriately.
6440 g_file_load_contents_finish (GFile *file,
6447 return g_file_load_partial_contents_finish (file,
6456 * g_file_replace_contents:
6457 * @file: input #GFile.
6458 * @contents: a string containing the new contents for @file.
6459 * @length: the length of @contents in bytes.
6460 * @etag: the old <link linkend="gfile-etag">entity tag</link>
6461 * for the document, or %NULL
6462 * @make_backup: %TRUE if a backup should be created.
6463 * @flags: a set of #GFileCreateFlags.
6464 * @new_etag: a location to a new <link linkend="gfile-etag">entity tag</link>
6465 * for the document. This should be freed with g_free() when no longer
6467 * @cancellable: optional #GCancellable object, %NULL to ignore.
6468 * @error: a #GError, or %NULL
6470 * Replaces the contents of @file with @contents of @length bytes.
6472 * If @etag is specified (not %NULL) any existing file must have that etag, or
6473 * the error %G_IO_ERROR_WRONG_ETAG will be returned.
6475 * If @make_backup is %TRUE, this function will attempt to make a backup of @file.
6477 * If @cancellable is not %NULL, then the operation can be cancelled by
6478 * triggering the cancellable object from another thread. If the operation
6479 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6481 * The returned @new_etag can be used to verify that the file hasn't changed the
6482 * next time it is saved over.
6484 * Returns: %TRUE if successful. If an error
6485 * has occurred, this function will return %FALSE and set @error
6486 * appropriately if present.
6489 g_file_replace_contents (GFile *file,
6490 const char *contents,
6493 gboolean make_backup,
6494 GFileCreateFlags flags,
6496 GCancellable *cancellable,
6499 GFileOutputStream *out;
6500 gsize pos, remainder;
6504 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6505 g_return_val_if_fail (contents != NULL, FALSE);
6507 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
6513 while (remainder > 0 &&
6514 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
6516 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
6524 if (remainder > 0 && res < 0)
6526 /* Ignore errors on close */
6527 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
6528 g_object_unref (out);
6530 /* error is set already */
6534 ret = g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error);
6537 *new_etag = g_file_output_stream_get_etag (out);
6539 g_object_unref (out);
6547 GCancellable *cancellable;
6548 GAsyncReadyCallback callback;
6550 const char *content;
6554 } ReplaceContentsData;
6557 replace_contents_data_free (ReplaceContentsData *data)
6560 g_error_free (data->error);
6561 if (data->cancellable)
6562 g_object_unref (data->cancellable);
6563 g_object_unref (data->file);
6564 g_free (data->etag);
6569 replace_contents_close_callback (GObject *obj,
6570 GAsyncResult *close_res,
6573 GOutputStream *stream = G_OUTPUT_STREAM (obj);
6574 ReplaceContentsData *data = user_data;
6575 GSimpleAsyncResult *res;
6577 /* Ignore errors here, we're only reading anyway */
6578 g_output_stream_close_finish (stream, close_res, NULL);
6579 g_object_unref (stream);
6581 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
6583 res = g_simple_async_result_new (G_OBJECT (data->file),
6586 g_file_replace_contents_async);
6587 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_contents_data_free);
6588 g_simple_async_result_complete (res);
6589 g_object_unref (res);
6593 replace_contents_write_callback (GObject *obj,
6594 GAsyncResult *read_res,
6597 GOutputStream *stream = G_OUTPUT_STREAM (obj);
6598 ReplaceContentsData *data = user_data;
6599 GError *error = NULL;
6602 write_size = g_output_stream_write_finish (stream, read_res, &error);
6604 if (write_size <= 0)
6606 /* Error or EOF, close the file */
6608 data->error = error;
6609 g_output_stream_close_async (stream, 0,
6611 replace_contents_close_callback, data);
6613 else if (write_size > 0)
6615 data->pos += write_size;
6617 if (data->pos >= data->length)
6618 g_output_stream_close_async (stream, 0,
6620 replace_contents_close_callback, data);
6622 g_output_stream_write_async (stream,
6623 data->content + data->pos,
6624 data->length - data->pos,
6627 replace_contents_write_callback,
6633 replace_contents_open_callback (GObject *obj,
6634 GAsyncResult *open_res,
6637 GFile *file = G_FILE (obj);
6638 GFileOutputStream *stream;
6639 ReplaceContentsData *data = user_data;
6640 GError *error = NULL;
6641 GSimpleAsyncResult *res;
6643 stream = g_file_replace_finish (file, open_res, &error);
6647 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
6648 data->content + data->pos,
6649 data->length - data->pos,
6652 replace_contents_write_callback,
6658 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
6662 g_simple_async_result_complete (res);
6663 g_error_free (error);
6664 replace_contents_data_free (data);
6665 g_object_unref (res);
6670 * g_file_replace_contents_async:
6671 * @file: input #GFile.
6672 * @contents: string of contents to replace the file with.
6673 * @length: the length of @contents in bytes.
6674 * @etag: a new <link linkend="gfile-etag">entity tag</link> for the @file, or %NULL
6675 * @make_backup: %TRUE if a backup should be created.
6676 * @flags: a set of #GFileCreateFlags.
6677 * @cancellable: optional #GCancellable object, %NULL to ignore.
6678 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6679 * @user_data: the data to pass to callback function
6681 * Starts an asynchronous replacement of @file with the given
6682 * @contents of @length bytes. @etag will replace the document's
6683 * current entity tag.
6685 * When this operation has completed, @callback will be called with
6686 * @user_user data, and the operation can be finalized with
6687 * g_file_replace_contents_finish().
6689 * If @cancellable is not %NULL, then the operation can be cancelled by
6690 * triggering the cancellable object from another thread. If the operation
6691 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6693 * If @make_backup is %TRUE, this function will attempt to
6694 * make a backup of @file.
6697 g_file_replace_contents_async (GFile *file,
6698 const char *contents,
6701 gboolean make_backup,
6702 GFileCreateFlags flags,
6703 GCancellable *cancellable,
6704 GAsyncReadyCallback callback,
6707 ReplaceContentsData *data;
6709 g_return_if_fail (G_IS_FILE (file));
6710 g_return_if_fail (contents != NULL);
6712 data = g_new0 (ReplaceContentsData, 1);
6715 data->cancellable = g_object_ref (cancellable);
6716 data->callback = callback;
6717 data->user_data = user_data;
6718 data->content = contents;
6719 data->length = length;
6721 data->file = g_object_ref (file);
6723 g_file_replace_async (file,
6729 replace_contents_open_callback,
6734 * g_file_replace_contents_finish:
6735 * @file: input #GFile.
6736 * @res: a #GAsyncResult.
6737 * @new_etag: a location of a new <link linkend="gfile-etag">entity tag</link>
6738 * for the document. This should be freed with g_free() when it is no
6739 * longer needed, or %NULL
6740 * @error: a #GError, or %NULL
6742 * Finishes an asynchronous replace of the given @file. See
6743 * g_file_replace_contents_async(). Sets @new_etag to the new entity
6744 * tag for the document, if present.
6746 * Returns: %TRUE on success, %FALSE on failure.
6749 g_file_replace_contents_finish (GFile *file,
6754 GSimpleAsyncResult *simple;
6755 ReplaceContentsData *data;
6757 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6758 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
6760 simple = G_SIMPLE_ASYNC_RESULT (res);
6762 if (g_simple_async_result_propagate_error (simple, error))
6765 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_replace_contents_async);
6767 data = g_simple_async_result_get_op_res_gpointer (simple);
6771 g_propagate_error (error, data->error);
6779 *new_etag = data->etag;
6780 data->etag = NULL; /* Take ownership */
6787 * g_file_start_mountable:
6788 * @file: input #GFile.
6789 * @flags: flags affecting the operation
6790 * @start_operation: a #GMountOperation, or %NULL to avoid user interaction.
6791 * @cancellable: optional #GCancellable object, %NULL to ignore.
6792 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
6793 * @user_data: the data to pass to callback function
6795 * Starts a file of type G_FILE_TYPE_MOUNTABLE.
6796 * Using @start_operation, you can request callbacks when, for instance,
6797 * passwords are needed during authentication.
6799 * If @cancellable is not %NULL, then the operation can be cancelled by
6800 * triggering the cancellable object from another thread. If the operation
6801 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6803 * When the operation is finished, @callback will be called. You can then call
6804 * g_file_mount_mountable_finish() to get the result of the operation.
6809 g_file_start_mountable (GFile *file,
6810 GDriveStartFlags flags,
6811 GMountOperation *start_operation,
6812 GCancellable *cancellable,
6813 GAsyncReadyCallback callback,
6818 g_return_if_fail (G_IS_FILE (file));
6820 iface = G_FILE_GET_IFACE (file);
6822 if (iface->start_mountable == NULL)
6824 g_simple_async_report_error_in_idle (G_OBJECT (file),
6828 G_IO_ERROR_NOT_SUPPORTED,
6829 _("Operation not supported"));
6833 (* iface->start_mountable) (file,
6842 * g_file_start_mountable_finish:
6843 * @file: input #GFile.
6844 * @result: a #GAsyncResult.
6845 * @error: a #GError, or %NULL
6847 * Finishes a start operation. See g_file_start_mountable() for details.
6849 * Finish an asynchronous start operation that was started
6850 * with g_file_start_mountable().
6852 * Returns: %TRUE if the operation finished successfully. %FALSE
6858 g_file_start_mountable_finish (GFile *file,
6859 GAsyncResult *result,
6864 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6865 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
6867 if (G_IS_SIMPLE_ASYNC_RESULT (result))
6869 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
6870 if (g_simple_async_result_propagate_error (simple, error))
6874 iface = G_FILE_GET_IFACE (file);
6875 return (* iface->start_mountable_finish) (file, result, error);
6879 * g_file_stop_mountable:
6880 * @file: input #GFile.
6881 * @flags: flags affecting the operation
6882 * @mount_operation: a #GMountOperation, or %NULL to avoid user interaction.
6883 * @cancellable: optional #GCancellable object, %NULL to ignore.
6884 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
6885 * @user_data: the data to pass to callback function
6887 * Stops a file of type G_FILE_TYPE_MOUNTABLE.
6889 * If @cancellable is not %NULL, then the operation can be cancelled by
6890 * triggering the cancellable object from another thread. If the operation
6891 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6893 * When the operation is finished, @callback will be called. You can then call
6894 * g_file_stop_mountable_finish() to get the result of the operation.
6899 g_file_stop_mountable (GFile *file,
6900 GMountUnmountFlags flags,
6901 GMountOperation *mount_operation,
6902 GCancellable *cancellable,
6903 GAsyncReadyCallback callback,
6908 g_return_if_fail (G_IS_FILE (file));
6910 iface = G_FILE_GET_IFACE (file);
6912 if (iface->stop_mountable == NULL)
6914 g_simple_async_report_error_in_idle (G_OBJECT (file),
6918 G_IO_ERROR_NOT_SUPPORTED,
6919 _("Operation not supported"));
6923 (* iface->stop_mountable) (file,
6932 * g_file_stop_mountable_finish:
6933 * @file: input #GFile.
6934 * @result: a #GAsyncResult.
6935 * @error: a #GError, or %NULL
6937 * Finishes an stop operation, see g_file_stop_mountable() for details.
6939 * Finish an asynchronous stop operation that was started
6940 * with g_file_stop_mountable().
6942 * Returns: %TRUE if the operation finished successfully. %FALSE
6948 g_file_stop_mountable_finish (GFile *file,
6949 GAsyncResult *result,
6954 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6955 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
6957 if (G_IS_SIMPLE_ASYNC_RESULT (result))
6959 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
6960 if (g_simple_async_result_propagate_error (simple, error))
6964 iface = G_FILE_GET_IFACE (file);
6965 return (* iface->stop_mountable_finish) (file, result, error);
6969 * g_file_poll_mountable:
6970 * @file: input #GFile.
6971 * @cancellable: optional #GCancellable object, %NULL to ignore.
6972 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
6973 * @user_data: the data to pass to callback function
6975 * Polls a file of type G_FILE_TYPE_MOUNTABLE.
6977 * If @cancellable is not %NULL, then the operation can be cancelled by
6978 * triggering the cancellable object from another thread. If the operation
6979 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6981 * When the operation is finished, @callback will be called. You can then call
6982 * g_file_mount_mountable_finish() to get the result of the operation.
6987 g_file_poll_mountable (GFile *file,
6988 GCancellable *cancellable,
6989 GAsyncReadyCallback callback,
6994 g_return_if_fail (G_IS_FILE (file));
6996 iface = G_FILE_GET_IFACE (file);
6998 if (iface->poll_mountable == NULL)
7000 g_simple_async_report_error_in_idle (G_OBJECT (file),
7004 G_IO_ERROR_NOT_SUPPORTED,
7005 _("Operation not supported"));
7009 (* iface->poll_mountable) (file,
7016 * g_file_poll_mountable_finish:
7017 * @file: input #GFile.
7018 * @result: a #GAsyncResult.
7019 * @error: a #GError, or %NULL
7021 * Finishes a poll operation. See g_file_poll_mountable() for details.
7023 * Finish an asynchronous poll operation that was polled
7024 * with g_file_poll_mountable().
7026 * Returns: %TRUE if the operation finished successfully. %FALSE
7032 g_file_poll_mountable_finish (GFile *file,
7033 GAsyncResult *result,
7038 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7039 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7041 if (G_IS_SIMPLE_ASYNC_RESULT (result))
7043 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
7044 if (g_simple_async_result_propagate_error (simple, error))
7048 iface = G_FILE_GET_IFACE (file);
7049 return (* iface->poll_mountable_finish) (file, result, error);
7053 * g_file_supports_thread_contexts:
7056 * Checks if @file supports <link
7057 * linkend="g-main-context-push-thread-default-context">thread-default
7058 * contexts</link>. If this returns %FALSE, you cannot perform
7059 * asynchronous operations on @file in a thread that has a
7060 * thread-default context.
7062 * Returns: Whether or not @file supports thread-default contexts.
7067 g_file_supports_thread_contexts (GFile *file)
7071 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7073 iface = G_FILE_GET_IFACE (file);
7074 return iface->supports_thread_contexts;
7077 #define __G_FILE_C__
7078 #include "gioaliasdef.c"