blob: a5709a4cc8074e4597fd287270bfacac6ad81731 [file] [log] [blame]
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
/* GIO - GLib Input, Output and Streaming Library
*
* Copyright (C) 2006-2007 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
*
* Author: Alexander Larsson <alexl@redhat.com>
*/
#include "config.h"
#ifdef __linux__
#include <sys/ioctl.h>
#include <errno.h>
/* See linux.git/fs/btrfs/ioctl.h */
#define BTRFS_IOCTL_MAGIC 0x94
#define BTRFS_IOC_CLONE _IOW(BTRFS_IOCTL_MAGIC, 9, int)
#endif
#ifdef HAVE_SPLICE
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#endif
#include <string.h>
#include <sys/types.h>
#include "gfile.h"
#include "glib/gstdio.h"
#ifdef G_OS_UNIX
#include "glib-unix.h"
#endif
#include "gvfs.h"
#include "gtask.h"
#include "gfileattribute-priv.h"
#include "gfiledescriptorbased.h"
#include "gpollfilemonitor.h"
#include "gappinfo.h"
#include "gfileinputstream.h"
#include "gfileoutputstream.h"
#include "glocalfileoutputstream.h"
#include "glocalfileiostream.h"
#include "glocalfile.h"
#include "gcancellable.h"
#include "gasyncresult.h"
#include "gioerror.h"
#include "glibintl.h"
/**
* SECTION:gfile
* @short_description: File and Directory Handling
* @include: gio/gio.h
* @see_also: #GFileInfo, #GFileEnumerator
*
* #GFile is a high level abstraction for manipulating files on a
* virtual file system. #GFiles are lightweight, immutable objects
* that do no I/O upon creation. It is necessary to understand that
* #GFile objects do not represent files, merely an identifier for a
* file. All file content I/O is implemented as streaming operations
* (see #GInputStream and #GOutputStream).
*
* To construct a #GFile, you can use:
* - g_file_new_for_path() if you have a path.
* - g_file_new_for_uri() if you have a URI.
* - g_file_new_for_commandline_arg() for a command line argument.
* - g_file_new_tmp() to create a temporary file from a template.
* - g_file_parse_name() from a UTF-8 string gotten from g_file_get_parse_name().
* - g_file_new_build_filename() to create a file from path elements.
*
* One way to think of a #GFile is as an abstraction of a pathname. For
* normal files the system pathname is what is stored internally, but as
* #GFiles are extensible it could also be something else that corresponds
* to a pathname in a userspace implementation of a filesystem.
*
* #GFiles make up hierarchies of directories and files that correspond to
* the files on a filesystem. You can move through the file system with
* #GFile using g_file_get_parent() to get an identifier for the parent
* directory, g_file_get_child() to get a child within a directory,
* g_file_resolve_relative_path() to resolve a relative path between two
* #GFiles. There can be multiple hierarchies, so you may not end up at
* the same root if you repeatedly call g_file_get_parent() on two different
* files.
*
* All #GFiles have a basename (get with g_file_get_basename()). These names
* are byte strings that are used to identify the file on the filesystem
* (relative to its parent directory) and there is no guarantees that they
* have any particular charset encoding or even make any sense at all. If
* you want to use filenames in a user interface you should use the display
* name that you can get by requesting the
* %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME attribute with g_file_query_info().
* This is guaranteed to be in UTF-8 and can be used in a user interface.
* But always store the real basename or the #GFile to use to actually
* access the file, because there is no way to go from a display name to
* the actual name.
*
* Using #GFile as an identifier has the same weaknesses as using a path
* in that there may be multiple aliases for the same file. For instance,
* hard or soft links may cause two different #GFiles to refer to the same
* file. Other possible causes for aliases are: case insensitive filesystems,
* short and long names on FAT/NTFS, or bind mounts in Linux. If you want to
* check if two #GFiles point to the same file you can query for the
* %G_FILE_ATTRIBUTE_ID_FILE attribute. Note that #GFile does some trivial
* canonicalization of pathnames passed in, so that trivial differences in
* the path string used at creation (duplicated slashes, slash at end of
* path, "." or ".." path segments, etc) does not create different #GFiles.
*
* Many #GFile operations have both synchronous and asynchronous versions
* to suit your application. Asynchronous versions of synchronous functions
* simply have _async() appended to their function names. The asynchronous
* I/O functions call a #GAsyncReadyCallback which is then used to finalize
* the operation, producing a GAsyncResult which is then passed to the
* function's matching _finish() operation.
*
* It is highly recommended to use asynchronous calls when running within a
* shared main loop, such as in the main thread of an application. This avoids
* I/O operations blocking other sources on the main loop from being dispatched.
* Synchronous I/O operations should be performed from worker threads. See the
* [introduction to asynchronous programming section][async-programming] for
* more.
*
* Some #GFile operations almost always take a noticeable amount of time, and
* so do not have synchronous analogs. Notable cases include:
* - g_file_mount_mountable() to mount a mountable file.
* - g_file_unmount_mountable_with_operation() to unmount a mountable file.
* - g_file_eject_mountable_with_operation() to eject a mountable file.
*
* ## Entity Tags # {#gfile-etag}
*
* One notable feature of #GFiles are entity tags, or "etags" for
* short. Entity tags are somewhat like a more abstract version of the
* traditional mtime, and can be used to quickly determine if the file
* has been modified from the version on the file system. See the
* HTTP 1.1
* [specification](http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html)
* for HTTP Etag headers, which are a very similar concept.
*/
static void g_file_real_query_info_async (GFile *file,
const char *attributes,
GFileQueryInfoFlags flags,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);
static GFileInfo * g_file_real_query_info_finish (GFile *file,
GAsyncResult *res,
GError **error);
static void g_file_real_query_filesystem_info_async (GFile *file,
const char *attributes,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);
static GFileInfo * g_file_real_query_filesystem_info_finish (GFile *file,
GAsyncResult *res,
GError **error);
static void g_file_real_enumerate_children_async (GFile *file,
const char *attributes,
GFileQueryInfoFlags flags,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);
static GFileEnumerator * g_file_real_enumerate_children_finish (GFile *file,
GAsyncResult *res,
GError **error);
static void g_file_real_read_async (GFile *file,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);
static GFileInputStream * g_file_real_read_finish (GFile *file,
GAsyncResult *res,
GError **error);
static void g_file_real_append_to_async (GFile *file,
GFileCreateFlags flags,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);
static GFileOutputStream *g_file_real_append_to_finish (GFile *file,
GAsyncResult *res,
GError **error);
static void g_file_real_create_async (GFile *file,
GFileCreateFlags flags,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);
static GFileOutputStream *g_file_real_create_finish (GFile *file,
GAsyncResult *res,
GError **error);
static void g_file_real_replace_async (GFile *file,
const char *etag,
gboolean make_backup,
GFileCreateFlags flags,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);
static GFileOutputStream *g_file_real_replace_finish (GFile *file,
GAsyncResult *res,
GError **error);
static void g_file_real_delete_async (GFile *file,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);
static gboolean g_file_real_delete_finish (GFile *file,
GAsyncResult *res,
GError **error);
static void g_file_real_trash_async (GFile *file,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);
static gboolean g_file_real_trash_finish (GFile *file,
GAsyncResult *res,
GError **error);
static void g_file_real_make_directory_async (GFile *file,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);
static gboolean g_file_real_make_directory_finish (GFile *file,
GAsyncResult *res,
GError **error);
static void g_file_real_open_readwrite_async (GFile *file,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);
static GFileIOStream * g_file_real_open_readwrite_finish (GFile *file,
GAsyncResult *res,
GError **error);
static void g_file_real_create_readwrite_async (GFile *file,
GFileCreateFlags flags,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);
static GFileIOStream * g_file_real_create_readwrite_finish (GFile *file,
GAsyncResult *res,
GError **error);
static void g_file_real_replace_readwrite_async (GFile *file,
const char *etag,
gboolean make_backup,
GFileCreateFlags flags,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);
static GFileIOStream * g_file_real_replace_readwrite_finish (GFile *file,
GAsyncResult *res,
GError **error);
static gboolean g_file_real_set_attributes_from_info (GFile *file,
GFileInfo *info,
GFileQueryInfoFlags flags,
GCancellable *cancellable,
GError **error);
static void g_file_real_set_display_name_async (GFile *file,
const char *display_name,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);
static GFile * g_file_real_set_display_name_finish (GFile *file,
GAsyncResult *res,
GError **error);
static void g_file_real_set_attributes_async (GFile *file,
GFileInfo *info,
GFileQueryInfoFlags flags,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);
static gboolean g_file_real_set_attributes_finish (GFile *file,
GAsyncResult *res,
GFileInfo **info,
GError **error);
static void g_file_real_find_enclosing_mount_async (GFile *file,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);
static GMount * g_file_real_find_enclosing_mount_finish (GFile *file,
GAsyncResult *res,
GError **error);
static void g_file_real_copy_async (GFile *source,
GFile *destination,
GFileCopyFlags flags,
int io_priority,
GCancellable *cancellable,
GFileProgressCallback progress_callback,
gpointer progress_callback_data,
GAsyncReadyCallback callback,
gpointer user_data);
static gboolean g_file_real_copy_finish (GFile *file,
GAsyncResult *res,
GError **error);
static gboolean g_file_real_measure_disk_usage (GFile *file,
GFileMeasureFlags flags,
GCancellable *cancellable,
GFileMeasureProgressCallback progress_callback,
gpointer progress_data,
guint64 *disk_usage,
guint64 *num_dirs,
guint64 *num_files,
GError **error);
static void g_file_real_measure_disk_usage_async (GFile *file,
GFileMeasureFlags flags,
gint io_priority,
GCancellable *cancellable,
GFileMeasureProgressCallback progress_callback,
gpointer progress_data,
GAsyncReadyCallback callback,
gpointer user_data);
static gboolean g_file_real_measure_disk_usage_finish (GFile *file,
GAsyncResult *result,
guint64 *disk_usage,
guint64 *num_dirs,
guint64 *num_files,
GError **error);
typedef GFileIface GFileInterface;
G_DEFINE_INTERFACE (GFile, g_file, G_TYPE_OBJECT)
static void
g_file_default_init (GFileIface *iface)
{
iface->enumerate_children_async = g_file_real_enumerate_children_async;
iface->enumerate_children_finish = g_file_real_enumerate_children_finish;
iface->set_display_name_async = g_file_real_set_display_name_async;
iface->set_display_name_finish = g_file_real_set_display_name_finish;
iface->query_info_async = g_file_real_query_info_async;
iface->query_info_finish = g_file_real_query_info_finish;
iface->query_filesystem_info_async = g_file_real_query_filesystem_info_async;
iface->query_filesystem_info_finish = g_file_real_query_filesystem_info_finish;
iface->set_attributes_async = g_file_real_set_attributes_async;
iface->set_attributes_finish = g_file_real_set_attributes_finish;
iface->read_async = g_file_real_read_async;
iface->read_finish = g_file_real_read_finish;
iface->append_to_async = g_file_real_append_to_async;
iface->append_to_finish = g_file_real_append_to_finish;
iface->create_async = g_file_real_create_async;
iface->create_finish = g_file_real_create_finish;
iface->replace_async = g_file_real_replace_async;
iface->replace_finish = g_file_real_replace_finish;
iface->delete_file_async = g_file_real_delete_async;
iface->delete_file_finish = g_file_real_delete_finish;
iface->trash_async = g_file_real_trash_async;
iface->trash_finish = g_file_real_trash_finish;
iface->make_directory_async = g_file_real_make_directory_async;
iface->make_directory_finish = g_file_real_make_directory_finish;
iface->open_readwrite_async = g_file_real_open_readwrite_async;
iface->open_readwrite_finish = g_file_real_open_readwrite_finish;
iface->create_readwrite_async = g_file_real_create_readwrite_async;
iface->create_readwrite_finish = g_file_real_create_readwrite_finish;
iface->replace_readwrite_async = g_file_real_replace_readwrite_async;
iface->replace_readwrite_finish = g_file_real_replace_readwrite_finish;
iface->find_enclosing_mount_async = g_file_real_find_enclosing_mount_async;
iface->find_enclosing_mount_finish = g_file_real_find_enclosing_mount_finish;
iface->set_attributes_from_info = g_file_real_set_attributes_from_info;
iface->copy_async = g_file_real_copy_async;
iface->copy_finish = g_file_real_copy_finish;
iface->measure_disk_usage = g_file_real_measure_disk_usage;
iface->measure_disk_usage_async = g_file_real_measure_disk_usage_async;
iface->measure_disk_usage_finish = g_file_real_measure_disk_usage_finish;
}
/**
* g_file_is_native:
* @file: input #GFile
*
* Checks to see if a file is native to the platform.
*
* A native file is one expressed in the platform-native filename format,
* e.g. "C:\Windows" or "/usr/bin/". This does not mean the file is local,
* as it might be on a locally mounted remote filesystem.
*
* On some systems non-native files may be available using the native
* filesystem via a userspace filesystem (FUSE), in these cases this call
* will return %FALSE, but g_file_get_path() will still return a native path.
*
* This call does no blocking I/O.
*
* Returns: %TRUE if @file is native
*/
gboolean
g_file_is_native (GFile *file)
{
GFileIface *iface;
g_return_val_if_fail (G_IS_FILE (file), FALSE);
iface = G_FILE_GET_IFACE (file);
return (* iface->is_native) (file);
}
/**
* g_file_has_uri_scheme:
* @file: input #GFile
* @uri_scheme: a string containing a URI scheme
*
* Checks to see if a #GFile has a given URI scheme.
*
* This call does no blocking I/O.
*
* Returns: %TRUE if #GFile's backend supports the
* given URI scheme, %FALSE if URI scheme is %NULL,
* not supported, or #GFile is invalid.
*/
gboolean
g_file_has_uri_scheme (GFile *file,
const char *uri_scheme)
{
GFileIface *iface;
g_return_val_if_fail (G_IS_FILE (file), FALSE);
g_return_val_if_fail (uri_scheme != NULL, FALSE);
iface = G_FILE_GET_IFACE (file);
return (* iface->has_uri_scheme) (file, uri_scheme);
}
/**
* g_file_get_uri_scheme:
* @file: input #GFile
*
* Gets the URI scheme for a #GFile.
* RFC 3986 decodes the scheme as:
* |[
* URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
* ]|
* Common schemes include "file", "http", "ftp", etc.
*
* This call does no blocking I/O.
*
* Returns: a string containing the URI scheme for the given
* #GFile. The returned string should be freed with g_free()
* when no longer needed.
*/
char *
g_file_get_uri_scheme (GFile *file)
{
GFileIface *iface;
g_return_val_if_fail (G_IS_FILE (file), NULL);
iface = G_FILE_GET_IFACE (file);
return (* iface->get_uri_scheme) (file);
}
/**
* g_file_get_basename:
* @file: input #GFile
*
* Gets the base name (the last component of the path) for a given #GFile.
*
* If called for the top level of a system (such as the filesystem root
* or a uri like sftp://host/) it will return a single directory separator
* (and on Windows, possibly a drive letter).
*
* The base name is a byte string (not UTF-8). It has no defined encoding
* or rules other than it may not contain zero bytes. If you want to use
* filenames in a user interface you should use the display name that you
* can get by requesting the %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME
* attribute with g_file_query_info().
*
* This call does no blocking I/O.
*
* Returns: (type filename) (nullable): string containing the #GFile's
* base name, or %NULL if given #GFile is invalid. The returned string
* should be freed with g_free() when no longer needed.
*/
char *
g_file_get_basename (GFile *file)
{
GFileIface *iface;
g_return_val_if_fail (G_IS_FILE (file), NULL);
iface = G_FILE_GET_IFACE (file);
return (* iface->get_basename) (file);
}
/**
* g_file_get_path:
* @file: input #GFile
*
* Gets the local pathname for #GFile, if one exists. If non-%NULL, this is
* guaranteed to be an absolute, canonical path. It might contain symlinks.
*
* This call does no blocking I/O.
*
* Returns: (type filename) (nullable): string containing the #GFile's path,
* or %NULL if no such path exists. The returned string should be freed
* with g_free() when no longer needed.
*/
char *
g_file_get_path (GFile *file)
{
GFileIface *iface;
g_return_val_if_fail (G_IS_FILE (file), NULL);
iface = G_FILE_GET_IFACE (file);
return (* iface->get_path) (file);
}
/* Original commit introducing this in libgsystem:
*
* fileutil: Handle recent: and trash: URIs
*
* The gs_file_get_path_cached() was rather brittle in its handling
* of URIs. It would assert() when a GFile didn't have a backing path
* (such as when handling trash: or recent: URIs), and didn't know
* how to get the target URI for those items either.
*
* Make sure that we do not assert() when a backing path cannot be
* found, and handle recent: and trash: URIs.
*
* https://bugzilla.gnome.org/show_bug.cgi?id=708435
*/
static char *
file_get_target_path (GFile *file)
{
GFileInfo *info;
const char *target;
char *path;
info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TARGET_URI, G_FILE_QUERY_INFO_NONE, NULL, NULL);
if (info == NULL)
return NULL;
target = g_file_info_get_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_TARGET_URI);
path = g_filename_from_uri (target, NULL, NULL);
g_object_unref (info);
return path;
}
static const char *
file_peek_path_generic (GFile *file)
{
const char *path;
static GQuark _file_path_quark = 0;
if (G_UNLIKELY (_file_path_quark) == 0)
_file_path_quark = g_quark_from_static_string ("gio-file-path");
/* We need to be careful about threading, as two threads calling
* g_file_peek_path() on the same file could race: both would see
* (g_object_get_qdata(…) == NULL) to begin with, both would generate and add
* the path, but the second thread to add it would end up freeing the path
* set by the first thread. The first thread would still return the pointer
* to that freed path, though, resulting an a read-after-free. Handle that
* with a compare-and-swap loop. The g_object_*_qdata() functions are atomic. */
while (TRUE)
{
gchar *new_path = NULL;
path = g_object_get_qdata ((GObject*)file, _file_path_quark);
if (path != NULL)
break;
if (g_file_has_uri_scheme (file, "trash") ||
g_file_has_uri_scheme (file, "recent"))
new_path = file_get_target_path (file);
else
new_path = g_file_get_path (file);
if (new_path == NULL)
return NULL;
/* By passing NULL here, we ensure we never replace existing data: */
if (g_object_replace_qdata ((GObject *) file, _file_path_quark,
NULL, (gpointer) new_path,
(GDestroyNotify) g_free, NULL))
break;
else
g_free (new_path);
}
return path;
}
/**
* g_file_peek_path:
* @file: input #GFile
*
* Exactly like g_file_get_path(), but caches the result via
* g_object_set_qdata_full(). This is useful for example in C
* applications which mix `g_file_*` APIs with native ones. It
* also avoids an extra duplicated string when possible, so will be
* generally more efficient.
*
* This call does no blocking I/O.
*
* Returns: (type filename) (nullable): string containing the #GFile's path,
* or %NULL if no such path exists. The returned string is owned by @file.
* Since: 2.56
*/
const char *
g_file_peek_path (GFile *file)
{
if (G_IS_LOCAL_FILE (file))
return _g_local_file_get_filename ((GLocalFile *) file);
return file_peek_path_generic (file);
}
/**
* g_file_get_uri:
* @file: input #GFile
*
* Gets the URI for the @file.
*
* This call does no blocking I/O.
*
* Returns: a string containing the #GFile's URI.
* The returned string should be freed with g_free()
* when no longer needed.
*/
char *
g_file_get_uri (GFile *file)
{
GFileIface *iface;
g_return_val_if_fail (G_IS_FILE (file), NULL);
iface = G_FILE_GET_IFACE (file);
return (* iface->get_uri) (file);
}
/**
* g_file_get_parse_name:
* @file: input #GFile
*
* Gets the parse name of the @file.
* A parse name is a UTF-8 string that describes the
* file such that one can get the #GFile back using
* g_file_parse_name().
*
* This is generally used to show the #GFile as a nice
* full-pathname kind of string in a user interface,
* like in a location entry.
*
* For local files with names that can safely be converted
* to UTF-8 the pathname is used, otherwise the IRI is used
* (a form of URI that allows UTF-8 characters unescaped).
*
* This call does no blocking I/O.
*
* Returns: a string containing the #GFile's parse name.
* The returned string should be freed with g_free()
* when no longer needed.
*/
char *
g_file_get_parse_name (GFile *file)
{
GFileIface *iface;
g_return_val_if_fail (G_IS_FILE (file), NULL);
iface = G_FILE_GET_IFACE (file);
return (* iface->get_parse_name) (file);
}
/**
* g_file_dup:
* @file: input #GFile
*
* Duplicates a #GFile handle. This operation does not duplicate
* the actual file or directory represented by the #GFile; see
* g_file_copy() if attempting to copy a file.
*
* g_file_dup() is useful when a second handle is needed to the same underlying
* file, for use in a separate thread (#GFile is not thread-safe). For use
* within the same thread, use g_object_ref() to increment the existing object’s
* reference count.
*
* This call does no blocking I/O.
*
* Returns: (transfer full): a new #GFile that is a duplicate
* of the given #GFile.
*/
GFile *
g_file_dup (GFile *file)
{
GFileIface *iface;
g_return_val_if_fail (G_IS_FILE (file), NULL);
iface = G_FILE_GET_IFACE (file);
return (* iface->dup) (file);
}
/**
* g_file_hash:
* @file: (type GFile): #gconstpointer to a #GFile
*
* Creates a hash value for a #GFile.
*
* This call does no blocking I/O.
*
* Virtual: hash
* Returns: 0 if @file is not a valid #GFile, otherwise an
* integer that can be used as hash value for the #GFile.
* This function is intended for easily hashing a #GFile to
* add to a #GHashTable or similar data structure.
*/
guint
g_file_hash (gconstpointer file)
{
GFileIface *iface;
g_return_val_if_fail (G_IS_FILE (file), 0);
iface = G_FILE_GET_IFACE (file);
return (* iface->hash) ((GFile *)file);
}
/**
* g_file_equal:
* @file1: the first #GFile
* @file2: the second #GFile
*
* Checks if the two given #GFiles refer to the same file.
*
* Note that two #GFiles that differ can still refer to the same
* file on the filesystem due to various forms of filename
* aliasing.
*
* This call does no blocking I/O.
*
* Returns: %TRUE if @file1 and @file2 are equal.
*/
gboolean
g_file_equal (GFile *file1,
GFile *file2)
{
GFileIface *iface;
g_return_val_if_fail (G_IS_FILE (file1), FALSE);
g_return_val_if_fail (G_IS_FILE (file2), FALSE);
if (file1 == file2)
return TRUE;
if (G_TYPE_FROM_INSTANCE (file1) != G_TYPE_FROM_INSTANCE (file2))
return FALSE;
iface = G_FILE_GET_IFACE (file1);
return (* iface->equal) (file1, file2);
}
/**
* g_file_get_parent:
* @file: input #GFile
*
* Gets the parent directory for the @file.
* If the @file represents the root directory of the
* file system, then %NULL will be returned.
*
* This call does no blocking I/O.
*
* Returns: (nullable) (transfer full): a #GFile structure to the
* parent of the given #GFile or %NULL if there is no parent. Free
* the returned object with g_object_unref().
*/
GFile *
g_file_get_parent (GFile *file)
{
GFileIface *iface;
g_return_val_if_fail (G_IS_FILE (file), NULL);
iface = G_FILE_GET_IFACE (file);
return (* iface->get_parent) (file);
}
/**
* g_file_has_parent:
* @file: input #GFile
* @parent: (nullable): the parent to check for, or %NULL
*
* Checks if @file has a parent, and optionally, if it is @parent.
*
* If @parent is %NULL then this function returns %TRUE if @file has any
* parent at all. If @parent is non-%NULL then %TRUE is only returned
* if @file is an immediate child of @parent.
*
* Returns: %TRUE if @file is an immediate child of @parent (or any parent in
* the case that @parent is %NULL).
*
* Since: 2.24
*/
gboolean
g_file_has_parent (GFile *file,
GFile *parent)
{
GFile *actual_parent;
gboolean result;
g_return_val_if_fail (G_IS_FILE (file), FALSE);
g_return_val_if_fail (parent == NULL || G_IS_FILE (parent), FALSE);
actual_parent = g_file_get_parent (file);
if (actual_parent != NULL)
{
if (parent != NULL)
result = g_file_equal (parent, actual_parent);
else
result = TRUE;
g_object_unref (actual_parent);
}
else
result = FALSE;
return result;
}
/**
* g_file_get_child:
* @file: input #GFile
* @name: (type filename): string containing the child's basename
*
* Gets a child of @file with basename equal to @name.
*
* Note that the file with that specific name might not exist, but
* you can still have a #GFile that points to it. You can use this
* for instance to create that file.
*
* This call does no blocking I/O.
*
* Returns: (transfer full): a #GFile to a child specified by @name.
* Free the returned object with g_object_unref().
*/
GFile *
g_file_get_child (GFile *file,
const char *name)
{
g_return_val_if_fail (G_IS_FILE (file), NULL);
g_return_val_if_fail (name != NULL, NULL);
return g_file_resolve_relative_path (file, name);
}
/**
* g_file_get_child_for_display_name:
* @file: input #GFile
* @display_name: string to a possible child
* @error: return location for an error
*
* Gets the child of @file for a given @display_name (i.e. a UTF-8
* version of the name). If this function fails, it returns %NULL
* and @error will be set. This is very useful when constructing a
* #GFile for a new file and the user entered the filename in the
* user interface, for instance when you select a directory and
* type a filename in the file selector.
*
* This call does no blocking I/O.
*
* Returns: (transfer full): a #GFile to the specified child, or
* %NULL if the display name couldn't be converted.
* Free the returned object with g_object_unref().
*/
GFile *
g_file_get_child_for_display_name (GFile *file,
const char *display_name,
GError **error)
{
GFileIface *iface;
g_return_val_if_fail (G_IS_FILE (file), NULL);
g_return_val_if_fail (display_name != NULL, NULL);
iface = G_FILE_GET_IFACE (file);
return (* iface->get_child_for_display_name) (file, display_name, error);
}
/**
* g_file_has_prefix:
* @file: input #GFile
* @prefix: input #GFile
*
* Checks whether @file has the prefix specified by @prefix.
*
* In other words, if the names of initial elements of @file's
* pathname match @prefix. Only full pathname elements are matched,
* so a path like /foo is not considered a prefix of /foobar, only
* of /foo/bar.
*
* A #GFile is not a prefix of itself. If you want to check for
* equality, use g_file_equal().
*
* This call does no I/O, as it works purely on names. As such it can
* sometimes return %FALSE even if @file is inside a @prefix (from a
* filesystem point of view), because the prefix of @file is an alias
* of @prefix.
*
* Virtual: prefix_matches
* Returns: %TRUE if the @files's parent, grandparent, etc is @prefix,
* %FALSE otherwise.
*/
gboolean
g_file_has_prefix (GFile *file,
GFile *prefix)
{
GFileIface *iface;
g_return_val_if_fail (G_IS_FILE (file), FALSE);
g_return_val_if_fail (G_IS_FILE (prefix), FALSE);
if (G_TYPE_FROM_INSTANCE (file) != G_TYPE_FROM_INSTANCE (prefix))
return FALSE;
iface = G_FILE_GET_IFACE (file);
/* The vtable function differs in arg order since
* we're using the old contains_file call
*/
return (* iface->prefix_matches) (prefix, file);
}
/**
* g_file_get_relative_path:
* @parent: input #GFile
* @descendant: input #GFile
*
* Gets the path for @descendant relative to @parent.
*
* This call does no blocking I/O.
*
* Returns: (type filename) (nullable): string with the relative path from
* @descendant to @parent, or %NULL if @descendant doesn't have @parent as
* prefix. The returned string should be freed with g_free() when
* no longer needed.
*/
char *
g_file_get_relative_path (GFile *parent,
GFile *descendant)
{
GFileIface *iface;
g_return_val_if_fail (G_IS_FILE (parent), NULL);
g_return_val_if_fail (G_IS_FILE (descendant), NULL);
if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
return NULL;
iface = G_FILE_GET_IFACE (parent);
return (* iface->get_relative_path) (parent, descendant);
}
/**
* g_file_resolve_relative_path:
* @file: input #GFile
* @relative_path: (type filename): a given relative path string
*
* Resolves a relative path for @file to an absolute path.
*
* This call does no blocking I/O.
*
* Returns: (transfer full): #GFile to the resolved path.
* %NULL if @relative_path is %NULL or if @file is invalid.
* Free the returned object with g_object_unref().
*/
GFile *
g_file_resolve_relative_path (GFile *file,
const char *relative_path)
{
GFileIface *iface;
g_return_val_if_fail (G_IS_FILE (file), NULL);
g_return_val_if_fail (relative_path != NULL, NULL);
iface = G_FILE_GET_IFACE (file);
return (* iface->resolve_relative_path) (file, relative_path);
}
/**
* g_file_enumerate_children:
* @file: input #GFile
* @attributes: an attribute query string
* @flags: a set of #GFileQueryInfoFlags
* @cancellable: (nullable): optional #GCancellable object,
* %NULL to ignore
* @error: #GError for error reporting
*
* Gets the requested information about the files in a directory.
* The result is a #GFileEnumerator object that will give out
* #GFileInfo objects for all the files in the directory.
*
* The @attributes value is a string that specifies the file
* attributes that should be gathered. It is not an error if
* it's not possible to read a particular requested attribute
* from a file - it just won't be set. @attributes should
* be a comma-separated list of attributes or attribute wildcards.
* The wildcard "*" means all attributes, and a wildcard like
* "standard::*" means all attributes in the standard namespace.
* An example attribute query be "standard::*,owner::user".
* The standard attributes are available as defines, like
* #G_FILE_ATTRIBUTE_STANDARD_NAME.
*
* If @cancellable is not %NULL, then the operation can be cancelled
* by triggering the cancellable object from another thread. If the
* operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
* returned.
*
* If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
* be returned. If the file is not a directory, the %G_IO_ERROR_NOT_DIRECTORY
* error will be returned. Other errors are possible too.
*
* Returns: (transfer full): A #GFileEnumerator if successful,
* %NULL on error. Free the returned object with g_object_unref().
*/
GFileEnumerator *
g_file_enumerate_children (GFile *file,
const char *attributes,
GFileQueryInfoFlags flags,
GCancellable *cancellable,
GError **error)
{
GFileIface *iface;
g_return_val_if_fail (G_IS_FILE (file), NULL);
if (g_cancellable_set_error_if_cancelled (cancellable, error))
return NULL;
iface = G_FILE_GET_IFACE (file);
if (iface->enumerate_children == NULL)
{
g_set_error_literal (error, G_IO_ERROR,
G_IO_ERROR_NOT_SUPPORTED,
_("Operation not supported"));
return NULL;
}
return (* iface->enumerate_children) (file, attributes, flags,
cancellable, error);
}
/**
* g_file_enumerate_children_async:
* @file: input #GFile
* @attributes: an attribute query string
* @flags: a set of #GFileQueryInfoFlags
* @io_priority: the [I/O priority][io-priority] of the request
* @cancellable: (nullable): optional #GCancellable object,
* %NULL to ignore
* @callback: (scope async): a #GAsyncReadyCallback to call when the
* request is satisfied
* @user_data: (closure): the data to pass to callback function
*
* Asynchronously gets the requested information about the files
* in a directory. The result is a #GFileEnumerator object that will
* give out #GFileInfo objects for all the files in the directory.
*
* For more details, see g_file_enumerate_children() which is
* the synchronous version of this call.
*
* When the operation is finished, @callback will be called. You can
* then call g_file_enumerate_children_finish() to get the result of
* the operation.
*/
void
g_file_enumerate_children_async (GFile *file,
const char *attributes,
GFileQueryInfoFlags flags,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
GFileIface *iface;
g_return_if_fail (G_IS_FILE (file));
iface = G_FILE_GET_IFACE (file);
(* iface->enumerate_children_async) (file,
attributes,
flags,
io_priority,
cancellable,
callback,
user_data);
}
/**
* g_file_enumerate_children_finish:
* @file: input #GFile
* @res: a #GAsyncResult
* @error: a #GError
*
* Finishes an async enumerate children operation.
* See g_file_enumerate_children_async().
*
* Returns: (transfer full): a #GFileEnumerator or %NULL
* if an error occurred.
* Free the returned object with g_object_unref().
*/
GFileEnumerator *
g_file_enumerate_children_finish (GFile *file,
GAsyncResult *res,
GError **error)
{
GFileIface *iface;
g_return_val_if_fail (G_IS_FILE (file), NULL);
g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
if (g_async_result_legacy_propagate_error (res, error))
return NULL;
iface = G_FILE_GET_IFACE (file);
return (* iface->enumerate_children_finish) (file, res, error);
}
/**
* g_file_query_exists:
* @file: input #GFile
* @cancellable: (nullable): optional #GCancellable object,
* %NULL to ignore
*
* Utility function to check if a particular file exists. This is
* implemented using g_file_query_info() and as such does blocking I/O.
*
* Note that in many cases it is [racy to first check for file existence](https://en.wikipedia.org/wiki/Time_of_check_to_time_of_use)
* and then execute something based on the outcome of that, because the
* file might have been created or removed in between the operations. The
* general approach to handling that is to not check, but just do the
* operation and handle the errors as they come.
*
* As an example of race-free checking, take the case of reading a file,
* and if it doesn't exist, creating it. There are two racy versions: read
* it, and on error create it; and: check if it exists, if not create it.
* These can both result in two processes creating the file (with perhaps
* a partially written file as the result). The correct approach is to
* always try to create the file with g_file_create() which will either
* atomically create the file or fail with a %G_IO_ERROR_EXISTS error.
*
* However, in many cases an existence check is useful in a user interface,
* for instance to make a menu item sensitive/insensitive, so that you don't
* have to fool users that something is possible and then just show an error
* dialog. If you do this, you should make sure to also handle the errors
* that can happen due to races when you execute the operation.
*
* Returns: %TRUE if the file exists (and can be detected without error),
* %FALSE otherwise (or if cancelled).
*/
gboolean
g_file_query_exists (GFile *file,
GCancellable *cancellable)
{
GFileInfo *info;
g_return_val_if_fail (G_IS_FILE(file), FALSE);
info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE,
G_FILE_QUERY_INFO_NONE, cancellable, NULL);
if (info != NULL)
{
g_object_unref (info);
return TRUE;
}
return FALSE;
}
/**
* g_file_query_file_type:
* @file: input #GFile
* @flags: a set of #GFileQueryInfoFlags passed to g_file_query_info()
* @cancellable: (nullable): optional #GCancellable object,
* %NULL to ignore
*
* Utility function to inspect the #GFileType of a file. This is
* implemented using g_file_query_info() and as such does blocking I/O.
*
* The primary use case of this method is to check if a file is
* a regular file, directory, or symlink.
*
* Returns: The #GFileType of the file and #G_FILE_TYPE_UNKNOWN
* if the file does not exist
*
* Since: 2.18
*/
GFileType
g_file_query_file_type (GFile *file,
GFileQueryInfoFlags flags,
GCancellable *cancellable)
{
GFileInfo *info;
GFileType file_type;
g_return_val_if_fail (G_IS_FILE(file), G_FILE_TYPE_UNKNOWN);
info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE, flags,
cancellable, NULL);
if (info != NULL)
{
file_type = g_file_info_get_file_type (info);
g_object_unref (info);
}
else
file_type = G_FILE_TYPE_UNKNOWN;
return file_type;
}
/**
* g_file_query_info:
* @file: input #GFile
* @attributes: an attribute query string
* @flags: a set of #GFileQueryInfoFlags
* @cancellable: (nullable): optional #GCancellable object,
* %NULL to ignore
* @error: a #GError
*
* Gets the requested information about specified @file.
* The result is a #GFileInfo object that contains key-value
* attributes (such as the type or size of the file).
*
* The @attributes value is a string that specifies the file
* attributes that should be gathered. It is not an error if
* it's not possible to read a particular requested attribute
* from a file - it just won't be set. @attributes should be a
* comma-separated list of attributes or attribute wildcards.
* The wildcard "*" means all attributes, and a wildcard like
* "standard::*" means all attributes in the standard namespace.
* An example attribute query be "standard::*,owner::user".
* The standard attributes are available as defines, like
* #G_FILE_ATTRIBUTE_STANDARD_NAME.
*
* If @cancellable is not %NULL, then the operation can be cancelled
* by triggering the cancellable object from another thread. If the
* operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
* returned.
*
* For symlinks, normally the information about the target of the
* symlink is returned, rather than information about the symlink
* itself. However if you pass #G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS
* in @flags the information about the symlink itself will be returned.
* Also, for symlinks that point to non-existing files the information
* about the symlink itself will be returned.
*
* If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will be
* returned. Other errors are possible too, and depend on what kind of
* filesystem the file is on.
*
* Returns: (transfer full): a #GFileInfo for the given @file, or %NULL
* on error. Free the returned object with g_object_unref().
*/
GFileInfo *
g_file_query_info (GFile *file,
const char *attributes,
GFileQueryInfoFlags flags,
GCancellable *cancellable,
GError **error)
{
GFileIface *iface;
g_return_val_if_fail (G_IS_FILE (file), NULL);
if (g_cancellable_set_error_if_cancelled (cancellable, error))
return NULL;
iface = G_FILE_GET_IFACE (file);
if (iface->query_info == NULL)
{
g_set_error_literal (error, G_IO_ERROR,
G_IO_ERROR_NOT_SUPPORTED,
_("Operation not supported"));
return NULL;
}
return (* iface->query_info) (file, attributes, flags, cancellable, error);
}
/**
* g_file_query_info_async:
* @file: input #GFile
* @attributes: an attribute query string
* @flags: a set of #GFileQueryInfoFlags
* @io_priority: the [I/O priority][io-priority] of the request
* @cancellable: (nullable): optional #GCancellable object,
* %NULL to ignore
* @callback: (scope async): a #GAsyncReadyCallback to call when the
* request is satisfied
* @user_data: (closure): the data to pass to callback function
*
* Asynchronously gets the requested information about specified @file.
* The result is a #GFileInfo object that contains key-value attributes
* (such as type or size for the file).
*
* For more details, see g_file_query_info() which is the synchronous
* version of this call.
*
* When the operation is finished, @callback will be called. You can
* then call g_file_query_info_finish() to get the result of the operation.
*/
void
g_file_query_info_async (GFile *file,
const char *attributes,
GFileQueryInfoFlags flags,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
GFileIface *iface;
g_return_if_fail (G_IS_FILE (file));
iface = G_FILE_GET_IFACE (file);
(* iface->query_info_async) (file,
attributes,
flags,
io_priority,
cancellable,
callback,
user_data);
}
/**
* g_file_query_info_finish:
* @file: input #GFile
* @res: a #GAsyncResult
* @error: a #GError
*
* Finishes an asynchronous file info query.
* See g_file_query_info_async().
*
* Returns: (transfer full): #GFileInfo for given @file
* or %NULL on error. Free the returned object with
* g_object_unref().
*/
GFileInfo *
g_file_query_info_finish (GFile *file,
GAsyncResult *res,
GError **error)
{
GFileIface *iface;
g_return_val_if_fail (G_IS_FILE (file), NULL);
g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
if (g_async_result_legacy_propagate_error (res, error))
return NULL;
iface = G_FILE_GET_IFACE (file);
return (* iface->query_info_finish) (file, res, error);
}
/**
* g_file_query_filesystem_info:
* @file: input #GFile
* @attributes: an attribute query string
* @cancellable: (nullable): optional #GCancellable object,
* %NULL to ignore
* @error: a #GError
*
* Similar to g_file_query_info(), but obtains information
* about the filesystem the @file is on, rather than the file itself.
* For instance the amount of space available and the type of
* the filesystem.
*
* The @attributes value is a string that specifies the attributes
* that should be gathered. It is not an error if it's not possible
* to read a particular requested attribute from a file - it just
* won't be set. @attributes should be a comma-separated list of
* attributes or attribute wildcards. The wildcard "*" means all
* attributes, and a wildcard like "filesystem::*" means all attributes
* in the filesystem namespace. The standard namespace for filesystem
* attributes is "filesystem". Common attributes of interest are
* #G_FILE_ATTRIBUTE_FILESYSTEM_SIZE (the total size of the filesystem
* in bytes), #G_FILE_ATTRIBUTE_FILESYSTEM_FREE (number of bytes available),
* and #G_FILE_ATTRIBUTE_FILESYSTEM_TYPE (type of the filesystem).
*
* If @cancellable is not %NULL, then the operation can be cancelled
* by triggering the cancellable object from another thread. If the
* operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
* returned.
*
* If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
* be returned. Other errors are possible too, and depend on what
* kind of filesystem the file is on.
*
* Returns: (transfer full): a #GFileInfo or %NULL if there was an error.
* Free the returned object with g_object_unref().
*/
GFileInfo *
g_file_query_filesystem_info (GFile *file,
const char *attributes,
GCancellable *cancellable,
GError **error)
{
GFileIface *iface;
g_return_val_if_fail (G_IS_FILE (file), NULL);
if (g_cancellable_set_error_if_cancelled (cancellable, error))
return NULL;
iface = G_FILE_GET_IFACE (file);
if (iface->query_filesystem_info == NULL)
{
g_set_error_literal (error, G_IO_ERROR,
G_IO_ERROR_NOT_SUPPORTED,
_("Operation not supported"));
return NULL;
}
return (* iface->query_filesystem_info) (file, attributes, cancellable, error);
}
/**
* g_file_query_filesystem_info_async:
* @file: input #GFile
* @attributes: an attribute query string
* @io_priority: the [I/O priority][io-priority] of the request
* @cancellable: (nullable): optional #GCancellable object,
* %NULL to ignore
* @callback: (scope async): a #GAsyncReadyCallback to call
* when the request is satisfied
* @user_data: (closure): the data to pass to callback function
*
* Asynchronously gets the requested information about the filesystem
* that the specified @file is on. The result is a #GFileInfo object
* that contains key-value attributes (such as type or size for the
* file).
*
* For more details, see g_file_query_filesystem_info() which is the
* synchronous version of this call.
*
* When the operation is finished, @callback will be called. You can
* then call g_file_query_info_finish() to get the result of the
* operation.
*/
void
g_file_query_filesystem_info_async (GFile *file,
const char *attributes,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
GFileIface *iface;
g_return_if_fail (G_IS_FILE (file));
iface = G_FILE_GET_IFACE (file);
(* iface->query_filesystem_info_async) (file,
attributes,
io_priority,
cancellable,
callback,
user_data);
}
/**
* g_file_query_filesystem_info_finish:
* @file: input #GFile
* @res: a #GAsyncResult
* @error: a #GError
*
* Finishes an asynchronous filesystem info query.
* See g_file_query_filesystem_info_async().
*
* Returns: (transfer full): #GFileInfo for given @file
* or %NULL on error.
* Free the returned object with g_object_unref().
*/
GFileInfo *
g_file_query_filesystem_info_finish (GFile *file,
GAsyncResult *res,
GError **error)
{
GFileIface *iface;
g_return_val_if_fail (G_IS_FILE (file), NULL);
g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
if (g_async_result_legacy_propagate_error (res, error))
return NULL;
iface = G_FILE_GET_IFACE (file);
return (* iface->query_filesystem_info_finish) (file, res, error);
}
/**
* g_file_find_enclosing_mount:
* @file: input #GFile
* @cancellable: (nullable): optional #GCancellable object,
* %NULL to ignore
* @error: a #GError
*
* Gets a #GMount for the #GFile.
*
* If the #GFileIface for @file does not have a mount (e.g.
* possibly a remote share), @error will be set to %G_IO_ERROR_NOT_FOUND
* and %NULL will be returned.
*
* If @cancellable is not %NULL, then the operation can be cancelled by
* triggering the cancellable object from another thread. If the operation
* was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
*
* Returns: (transfer full): a #GMount where the @file is located
* or %NULL on error.
* Free the returned object with g_object_unref().
*/
GMount *
g_file_find_enclosing_mount (GFile *file,
GCancellable *cancellable,
GError **error)
{
GFileIface *iface;
g_return_val_if_fail (G_IS_FILE (file), NULL);
if (g_cancellable_set_error_if_cancelled (cancellable, error))
return NULL;
iface = G_FILE_GET_IFACE (file);
if (iface->find_enclosing_mount == NULL)
{
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
/* Translators: This is an error message when
* trying to find the enclosing (user visible)
* mount of a file, but none exists.
*/
_("Containing mount does not exist"));
return NULL;
}
return (* iface->find_enclosing_mount) (file, cancellable, error);
}
/**
* g_file_find_enclosing_mount_async:
* @file: a #GFile
* @io_priority: the [I/O priority][io-priority] of the request
* @cancellable: (nullable): optional #GCancellable object,
* %NULL to ignore
* @callback: (scope async): a #GAsyncReadyCallback to call
* when the request is satisfied
* @user_data: (closure): the data to pass to callback function
*
* Asynchronously gets the mount for the file.
*
* For more details, see g_file_find_enclosing_mount() which is
* the synchronous version of this call.
*
* When the operation is finished, @callback will be called.
* You can then call g_file_find_enclosing_mount_finish() to
* get the result of the operation.
*/
void
g_file_find_enclosing_mount_async (GFile *file,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
GFileIface *iface;
g_return_if_fail (G_IS_FILE (file));
iface = G_FILE_GET_IFACE (file);
(* iface->find_enclosing_mount_async) (file,
io_priority,
cancellable,
callback,
user_data);
}
/**
* g_file_find_enclosing_mount_finish:
* @file: a #GFile
* @res: a #GAsyncResult
* @error: a #GError
*
* Finishes an asynchronous find mount request.
* See g_file_find_enclosing_mount_async().
*
* Returns: (transfer full): #GMount for given @file or %NULL on error.
* Free the returned object with g_object_unref().
*/
GMount *
g_file_find_enclosing_mount_finish (GFile *file,
GAsyncResult *res,
GError **error)
{
GFileIface *iface;
g_return_val_if_fail (G_IS_FILE (file), NULL);
g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
if (g_async_result_legacy_propagate_error (res, error))
return NULL;
iface = G_FILE_GET_IFACE (file);
return (* iface->find_enclosing_mount_finish) (file, res, error);
}
/**
* g_file_read:
* @file: #GFile to read
* @cancellable: (nullable): a #GCancellable
* @error: a #GError, or %NULL
*
* Opens a file for reading. The result is a #GFileInputStream that
* can be used to read the contents of the file.
*
* If @cancellable is not %NULL, then the operation can be cancelled by
* triggering the cancellable object from another thread. If the operation
* was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
*
* If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will be
* returned. If the file is a directory, the %G_IO_ERROR_IS_DIRECTORY
* error will be returned. Other errors are possible too, and depend
* on what kind of filesystem the file is on.
*
* Virtual: read_fn
* Returns: (transfer full): #GFileInputStream or %NULL on error.
* Free the returned object with g_object_unref().
*/
GFileInputStream *
g_file_read (GFile *file,
GCancellable *cancellable,
GError **error)
{
GFileIface *iface;
g_return_val_if_fail (G_IS_FILE (file), NULL);
if (g_cancellable_set_error_if_cancelled (cancellable, error))
return NULL;
iface = G_FILE_GET_IFACE (file);
if (iface->read_fn == NULL)
{
g_set_error_literal (error, G_IO_ERROR,
G_IO_ERROR_NOT_SUPPORTED,
_("Operation not supported"));
return NULL;
}
return (* iface->read_fn) (file, cancellable, error);
}
/**
* g_file_append_to:
* @file: input #GFile
* @flags: a set of #GFileCreateFlags
* @cancellable: (nullable): optional #GCancellable object,
* %NULL to ignore
* @error: a #GError, or %NULL
*
* Gets an output stream for appending data to the file.
* If the file doesn't already exist it is created.
*
* By default files created are generally readable by everyone,
* but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
* will be made readable only to the current user, to the level that
* is supported on the target filesystem.
*
* If @cancellable is not %NULL, then the operation can be cancelled
* by triggering the cancellable object from another thread. If the
* operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
* returned.
*
* Some file systems don't allow all file names, and may return an
* %G_IO_ERROR_INVALID_FILENAME error. If the file is a directory the
* %G_IO_ERROR_IS_DIRECTORY error will be returned. Other errors are
* possible too, and depend on what kind of filesystem the file is on.
*
* Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
* Free the returned object with g_object_unref().
*/
GFileOutputStream *
g_file_append_to (GFile *file,
GFileCreateFlags flags,
GCancellable *cancellable,
GError **error)
{
GFileIface *iface;
g_return_val_if_fail (G_IS_FILE (file), NULL);
if (g_cancellable_set_error_if_cancelled (cancellable, error))
return NULL;
iface = G_FILE_GET_IFACE (file);
if (iface->append_to == NULL)
{
g_set_error_literal (error, G_IO_ERROR,
G_IO_ERROR_NOT_SUPPORTED,
_("Operation not supported"));
return NULL;
}
return (* iface->append_to) (file, flags, cancellable, error);
}
/**
* g_file_create:
* @file: input #GFile
* @flags: a set of #GFileCreateFlags
* @cancellable: (nullable): optional #GCancellable object,
* %NULL to ignore
* @error: a #GError, or %NULL
*
* Creates a new file and returns an output stream for writing to it.
* The file must not already exist.
*
* By default files created are generally readable by everyone,
* but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
* will be made readable only to the current user, to the level
* that is supported on the target filesystem.
*
* If @cancellable is not %NULL, then the operation can be cancelled
* by triggering the cancellable object from another thread. If the
* operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
* returned.
*
* If a file or directory with this name already exists the
* %G_IO_ERROR_EXISTS error will be returned. Some file systems don't
* allow all file names, and may return an %G_IO_ERROR_INVALID_FILENAME
* error, and if the name is to long %G_IO_ERROR_FILENAME_TOO_LONG will
* be returned. Other errors are possible too, and depend on what kind
* of filesystem the file is on.
*
* Returns: (transfer full): a #GFileOutputStream for the newly created
* file, or %NULL on error.
* Free the returned object with g_object_unref().
*/
GFileOutputStream *
g_file_create (GFile *file,
GFileCreateFlags flags,
GCancellable *cancellable,
GError **error)
{
GFileIface *iface;
g_return_val_if_fail (G_IS_FILE (file), NULL);
if (g_cancellable_set_error_if_cancelled (cancellable, error))
return NULL;
iface = G_FILE_GET_IFACE (file);
if (iface->create == NULL)
{
g_set_error_literal (error, G_IO_ERROR,
G_IO_ERROR_NOT_SUPPORTED,
_("Operation not supported"));
return NULL;
}
return (* iface->create) (file, flags, cancellable, error);
}
/**
* g_file_replace:
* @file: input #GFile
* @etag: (nullable): an optional [entity tag][gfile-etag]
* for the current #GFile, or #NULL to ignore
* @make_backup: %TRUE if a backup should be created
* @flags: a set of #GFileCreateFlags
* @cancellable: (nullable): optional #GCancellable object,
* %NULL to ignore
* @error: a #GError, or %NULL
*
* Returns an output stream for overwriting the file, possibly
* creating a backup copy of the file first. If the file doesn't exist,
* it will be created.
*
* This will try to replace the file in the safest way possible so
* that any errors during the writing will not affect an already
* existing copy of the file. For instance, for local files it
* may write to a temporary file and then atomically rename over
* the destination when the stream is closed.
*
* By default files created are generally readable by everyone,
* but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
* will be made readable only to the current user, to the level that
* is supported on the target filesystem.
*
* If @cancellable is not %NULL, then the operation can be cancelled
* by triggering the cancellable object from another thread. If the
* operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
* returned.
*
* If you pass in a non-%NULL @etag value and @file already exists, then
* this value is compared to the current entity tag of the file, and if
* they differ an %G_IO_ERROR_WRONG_ETAG error is returned. This
* generally means that the file has been changed since you last read
* it. You can get the new etag from g_file_output_stream_get_etag()
* after you've finished writing and closed the #GFileOutputStream. When
* you load a new file you can use g_file_input_stream_query_info() to
* get the etag of the file.
*
* If @make_backup is %TRUE, this function will attempt to make a
* backup of the current file before overwriting it. If this fails
* a %G_IO_ERROR_CANT_CREATE_BACKUP error will be returned. If you
* want to replace anyway, try again with @make_backup set to %FALSE.
*
* If the file is a directory the %G_IO_ERROR_IS_DIRECTORY error will
* be returned, and if the file is some other form of non-regular file
* then a %G_IO_ERROR_NOT_REGULAR_FILE error will be returned. Some
* file systems don't allow all file names, and may return an
* %G_IO_ERROR_INVALID_FILENAME error, and if the name is to long
* %G_IO_ERROR_FILENAME_TOO_LONG will be returned. Other errors are
* possible too, and depend on what kind of filesystem the file is on.
*
* Returns: (transfer full): a #GFileOutputStream or %NULL on error.
* Free the returned object with g_object_unref().
*/
GFileOutputStream *
g_file_replace (GFile *file,
const char *etag,
gboolean make_backup,
GFileCreateFlags flags,
GCancellable *cancellable,
GError **error)
{
GFileIface *iface;
g_return_val_if_fail (G_IS_FILE (file), NULL);
if (g_cancellable_set_error_if_cancelled (cancellable, error))
return NULL;
iface = G_FILE_GET_IFACE (file);
if (iface->replace == NULL)
{
g_set_error_literal (error, G_IO_ERROR,
G_IO_ERROR_NOT_SUPPORTED,
_("Operation not supported"));
return NULL;
}
/* Handle empty tag string as NULL in consistent way. */
if (etag && *etag == 0)
etag = NULL;
return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
}
/**
* g_file_open_readwrite:
* @file: #GFile to open
* @cancellable: (nullable): a #GCancellable
* @error: a #GError, or %NULL
*
* Opens an existing file for reading and writing. The result is
* a #GFileIOStream that can be used to read and write the contents
* of the file.
*
* If @cancellable is not %NULL, then the operation can be cancelled
* by triggering the cancellable object from another thread. If the
* operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
* returned.
*
* If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
* be returned. If the file is a directory, the %G_IO_ERROR_IS_DIRECTORY
* error will be returned. Other errors are possible too, and depend on
* what kind of filesystem the file is on. Note that in many non-local
* file cases read and write streams are not supported, so make sure you
* really need to do read and write streaming, rather than just opening
* for reading or writing.
*
* Returns: (transfer full): #GFileIOStream or %NULL on error.
* Free the returned object with g_object_unref().
*
* Since: 2.22
*/
GFileIOStream *
g_file_open_readwrite (GFile *file,
GCancellable *cancellable,
GError **error)
{
GFileIface *iface;
g_return_val_if_fail (G_IS_FILE (file), NULL);
if (g_cancellable_set_error_if_cancelled (cancellable, error))
return NULL;
iface = G_FILE_GET_IFACE (file);
if (iface->open_readwrite == NULL)
{
g_set_error_literal (error, G_IO_ERROR,
G_IO_ERROR_NOT_SUPPORTED,
_("Operation not supported"));
return NULL;
}
return (* iface->open_readwrite) (file, cancellable, error);
}
/**
* g_file_create_readwrite:
* @file: a #GFile
* @flags: a set of #GFileCreateFlags
* @cancellable: (nullable): optional #GCancellable object,
* %NULL to ignore
* @error: return location for a #GError, or %NULL
*
* Creates a new file and returns a stream for reading and
* writing to it. The file must not already exist.
*
* By default files created are generally readable by everyone,
* but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
* will be made readable only to the current user, to the level
* that is supported on the target filesystem.
*
* If @cancellable is not %NULL, then the operation can be cancelled
* by triggering the cancellable object from another thread. If the
* operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
* returned.
*
* If a file or directory with this name already exists, the
* %G_IO_ERROR_EXISTS error will be returned. Some file systems don't
* allow all file names, and may return an %G_IO_ERROR_INVALID_FILENAME
* error, and if the name is too long, %G_IO_ERROR_FILENAME_TOO_LONG
* will be returned. Other errors are possible too, and depend on what
* kind of filesystem the file is on.
*
* Note that in many non-local file cases read and write streams are
* not supported, so make sure you really need to do read and write
* streaming, rather than just opening for reading or writing.
*
* Returns: (transfer full): a #GFileIOStream for the newly created
* file, or %NULL on error.
* Free the returned object with g_object_unref().
*
* Since: 2.22
*/
GFileIOStream *
g_file_create_readwrite (GFile *file,
GFileCreateFlags flags,
GCancellable *cancellable,
GError **error)
{
GFileIface *iface;
g_return_val_if_fail (G_IS_FILE (file), NULL);
if (g_cancellable_set_error_if_cancelled (cancellable, error))
return NULL;
iface = G_FILE_GET_IFACE (file);
if (iface->create_readwrite == NULL)
{
g_set_error_literal (error, G_IO_ERROR,
G_IO_ERROR_NOT_SUPPORTED,
_("Operation not supported"));
return NULL;
}
return (* iface->create_readwrite) (file, flags, cancellable, error);
}
/**
* g_file_replace_readwrite:
* @file: a #GFile
* @etag: (nullable): an optional [entity tag][gfile-etag]
* for the current #GFile, or #NULL to ignore
* @make_backup: %TRUE if a backup should be created
* @flags: a set of #GFileCreateFlags
* @cancellable: (nullable): optional #GCancellable object,
* %NULL to ignore
* @error: return location for a #GError, or %NULL
*
* Returns an output stream for overwriting the file in readwrite mode,
* possibly creating a backup copy of the file first. If the file doesn't
* exist, it will be created.
*
* For details about the behaviour, see g_file_replace() which does the
* same thing but returns an output stream only.
*
* Note that in many non-local file cases read and write streams are not
* supported, so make sure you really need to do read and write streaming,
* rather than just opening for reading or writing.
*
* Returns: (transfer full): a #GFileIOStream or %NULL on error.
* Free the returned object with g_object_unref().
*
* Since: 2.22
*/
GFileIOStream *
g_file_replace_readwrite (GFile *file,
const char *etag,
gboolean make_backup,
GFileCreateFlags flags,
GCancellable *cancellable,
GError **error)
{
GFileIface *iface;
g_return_val_if_fail (G_IS_FILE (file), NULL);
if (g_cancellable_set_error_if_cancelled (cancellable, error))
return NULL;
iface = G_FILE_GET_IFACE (file);
if (iface->replace_readwrite == NULL)
{
g_set_error_literal (error, G_IO_ERROR,
G_IO_ERROR_NOT_SUPPORTED,
_("Operation not supported"));
return NULL;
}
return (* iface->replace_readwrite) (file, etag, make_backup, flags, cancellable, error);
}
/**
* g_file_read_async:
* @file: input #GFile
* @io_priority: the [I/O priority][io-priority] of the request
* @cancellable: (nullable): optional #GCancellable object,
* %NULL to ignore
* @callback: (scope async): a #GAsyncReadyCallback to call
* when the request is satisfied
* @user_data: (closure): the data to pass to callback function
*
* Asynchronously opens @file for reading.
*
* For more details, see g_file_read() which is
* the synchronous version of this call.
*
* When the operation is finished, @callback will be called.
* You can then call g_file_read_finish() to get the result
* of the operation.
*/
void
g_file_read_async (GFile *file,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
GFileIface *iface;
g_return_if_fail (G_IS_FILE (file));
iface = G_FILE_GET_IFACE (file);
(* iface->read_async) (file,
io_priority,
cancellable,
callback,
user_data);
}
/**
* g_file_read_finish:
* @file: input #GFile
* @res: a #GAsyncResult
* @error: a #GError, or %NULL
*
* Finishes an asynchronous file read operation started with
* g_file_read_async().
*
* Returns: (transfer full): a #GFileInputStream or %NULL on error.
* Free the returned object with g_object_unref().
*/
GFileInputStream *
g_file_read_finish (GFile *file,
GAsyncResult *res,
GError **error)
{
GFileIface *iface;
g_return_val_if_fail (G_IS_FILE (file), NULL);
g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
if (g_async_result_legacy_propagate_error (res, error))
return NULL;
iface = G_FILE_GET_IFACE (file);
return (* iface->read_finish) (file, res, error);
}
/**
* g_file_append_to_async:
* @file: input #GFile
* @flags: a set of #GFileCreateFlags
* @io_priority: the [I/O priority][io-priority] of the request
* @cancellable: (nullable): optional #GCancellable object,
* %NULL to ignore
* @callback: (scope async): a #GAsyncReadyCallback to call
* when the request is satisfied
* @user_data: (closure): the data to pass to callback function
*
* Asynchronously opens @file for appending.
*
* For more details, see g_file_append_to() which is
* the synchronous version of this call.
*
* When the operation is finished, @callback will be called.
* You can then call g_file_append_to_finish() to get the result
* of the operation.
*/
void
g_file_append_to_async (GFile *file,
GFileCreateFlags flags,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
GFileIface *iface;
g_return_if_fail (G_IS_FILE (file));
iface = G_FILE_GET_IFACE (file);
(* iface->append_to_async) (file,
flags,
io_priority,
cancellable,
callback,
user_data);
}
/**
* g_file_append_to_finish:
* @file: input #GFile
* @res: #GAsyncResult
* @error: a #GError, or %NULL
*
* Finishes an asynchronous file append operation started with
* g_file_append_to_async().
*
* Returns: (transfer full): a valid #GFileOutputStream
* or %NULL on error.
* Free the returned object with g_object_unref().
*/
GFileOutputStream *
g_file_append_to_finish (GFile *file,
GAsyncResult *res,
GError **error)
{
GFileIface *iface;
g_return_val_if_fail (G_IS_FILE (file), NULL);
g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
if (g_async_result_legacy_propagate_error (res, error))
return NULL;
iface = G_FILE_GET_IFACE (file);
return (* iface->append_to_finish) (file, res, error);
}
/**
* g_file_create_async:
* @file: input #GFile
* @flags: a set of #GFileCreateFlags
* @io_priority: the [I/O priority][io-priority] of the request
* @cancellable: (nullable): optional #GCancellable object,
* %NULL to ignore
* @callback: (scope async): a #GAsyncReadyCallback to call
* when the request is satisfied
* @user_data: (closure): the data to pass to callback function
*
* Asynchronously creates a new file and returns an output stream
* for writing to it. The file must not already exist.
*
* For more details, see g_file_create() which is
* the synchronous version of this call.
*
* When the operation is finished, @callback will be called.
* You can then call g_file_create_finish() to get the result
* of the operation.
*/
void
g_file_create_async (GFile *file,
GFileCreateFlags flags,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
GFileIface *iface;
g_return_if_fail (G_IS_FILE (file));
iface = G_FILE_GET_IFACE (file);
(* iface->create_async) (file,
flags,
io_priority,
cancellable,
callback,
user_data);
}
/**
* g_file_create_finish:
* @file: input #GFile
* @res: a #GAsyncResult
* @error: a #GError, or %NULL
*
* Finishes an asynchronous file create operation started with
* g_file_create_async().
*
* Returns: (transfer full): a #GFileOutputStream or %NULL on error.
* Free the returned object with g_object_unref().
*/
GFileOutputStream *
g_file_create_finish (GFile *file,
GAsyncResult *res,
GError **error)
{
GFileIface *iface;
g_return_val_if_fail (G_IS_FILE (file), NULL);
g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
if (g_async_result_legacy_propagate_error (res, error))
return NULL;
iface = G_FILE_GET_IFACE (file);
return (* iface->create_finish) (file, res, error);
}
/**
* g_file_replace_async:
* @file: input #GFile
* @etag: (nullable): an [entity tag][gfile-etag] for the current #GFile,
* or %NULL to ignore
* @make_backup: %TRUE if a backup should be created
* @flags: a set of #GFileCreateFlags
* @io_priority: the [I/O priority][io-priority] of the request
* @cancellable: (nullable): optional #GCancellable object,
* %NULL to ignore
* @callback: (scope async): a #GAsyncReadyCallback to call
* when the request is satisfied
* @user_data: (closure): the data to pass to callback function
*
* Asynchronously overwrites the file, replacing the contents,
* possibly creating a backup copy of the file first.
*
* For more details, see g_file_replace() which is
* the synchronous version of this call.
*
* When the operation is finished, @callback will be called.
* You can then call g_file_replace_finish() to get the result
* of the operation.
*/
void
g_file_replace_async (GFile *file,
const char *etag,
gboolean make_backup,
GFileCreateFlags flags,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
GFileIface *iface;
g_return_if_fail (G_IS_FILE (file));
iface = G_FILE_GET_IFACE (file);
(* iface->replace_async) (file,
etag,
make_backup,
flags,
io_priority,
cancellable,
callback,
user_data);
}
/**
* g_file_replace_finish:
* @file: input #GFile
* @res: a #GAsyncResult
* @error: a #GError, or %NULL
*
* Finishes an asynchronous file replace operation started with
* g_file_replace_async().
*
* Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
* Free the returned object with g_object_unref().
*/
GFileOutputStream *
g_file_replace_finish (GFile *file,
GAsyncResult *res,
GError **error)
{
GFileIface *iface;
g_return_val_if_fail (G_IS_FILE (file), NULL);
g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
if (g_async_result_legacy_propagate_error (res, error))
return NULL;
iface = G_FILE_GET_IFACE (file);
return (* iface->replace_finish) (file, res, error);
}
/**
* g_file_open_readwrite_async
* @file: input #GFile
* @io_priority: the [I/O priority][io-priority] of the request
* @cancellable: (nullable): optional #GCancellable object,
* %NULL to ignore
* @callback: (scope async): a #GAsyncReadyCallback to call
* when the request is satisfied
* @user_data: (closure): the data to pass to callback function
*
* Asynchronously opens @file for reading and writing.
*
* For more details, see g_file_open_readwrite() which is
* the synchronous version of this call.
*
* When the operation is finished, @callback will be called.
* You can then call g_file_open_readwrite_finish() to get
* the result of the operation.
*
* Since: 2.22
*/
void
g_file_open_readwrite_async (GFile *file,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
GFileIface *iface;
g_return_if_fail (G_IS_FILE (file));
iface = G_FILE_GET_IFACE (file);
(* iface->open_readwrite_async) (file,
io_priority,
cancellable,
callback,
user_data);
}
/**
* g_file_open_readwrite_finish:
* @file: input #GFile
* @res: a #GAsyncResult
* @error: a #GError, or %NULL
*
* Finishes an asynchronous file read operation started with
* g_file_open_readwrite_async().
*
* Returns: (transfer full): a #GFileIOStream or %NULL on error.
* Free the returned object with g_object_unref().
*
* Since: 2.22
*/
GFileIOStream *
g_file_open_readwrite_finish (GFile *file,
GAsyncResult *res,
GError **error)
{
GFileIface *iface;
g_return_val_if_fail (G_IS_FILE (file), NULL);
g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
if (g_async_result_legacy_propagate_error (res, error))
return NULL;
iface = G_FILE_GET_IFACE (file);
return (* iface->open_readwrite_finish) (file, res, error);
}
/**
* g_file_create_readwrite_async:
* @file: input #GFile
* @flags: a set of #GFileCreateFlags
* @io_priority: the [I/O priority][io-priority] of the request
* @cancellable: (nullable): optional #GCancellable object,
* %NULL to ignore
* @callback: (scope async): a #GAsyncReadyCallback to call
* when the request is satisfied
* @user_data: (closure): the data to pass to callback function
*
* Asynchronously creates a new file and returns a stream
* for reading and writing to it. The file must not already exist.
*
* For more details, see g_file_create_readwrite() which is
* the synchronous version of this call.
*
* When the operation is finished, @callback will be called.
* You can then call g_file_create_readwrite_finish() to get
* the result of the operation.
*
* Since: 2.22
*/
void
g_file_create_readwrite_async (GFile *file,
GFileCreateFlags flags,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
GFileIface *iface;
g_return_if_fail (G_IS_FILE (file));
iface = G_FILE_GET_IFACE (file);
(* iface->create_readwrite_async) (file,
flags,
io_priority,
cancellable,
callback,
user_data);
}
/**
* g_file_create_readwrite_finish:
* @file: input #GFile
* @res: a #GAsyncResult
* @error: a #GError, or %NULL
*
* Finishes an asynchronous file create operation started with
* g_file_create_readwrite_async().
*
* Returns: (transfer full): a #GFileIOStream or %NULL on error.
* Free the returned object with g_object_unref().
*
* Since: 2.22
*/
GFileIOStream *
g_file_create_readwrite_finish (GFile *file,
GAsyncResult *res,
GError **error)
{
GFileIface *iface;
g_return_val_if_fail (G_IS_FILE (file), NULL);
g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
if (g_async_result_legacy_propagate_error (res, error))
return NULL;
iface = G_FILE_GET_IFACE (file);
return (* iface->create_readwrite_finish) (file, res, error);
}
/**
* g_file_replace_readwrite_async:
* @file: input #GFile
* @etag: (nullable): an [entity tag][gfile-etag] for the current #GFile,
* or %NULL to ignore
* @make_backup: %TRUE if a backup should be created
* @flags: a set of #GFileCreateFlags
* @io_priority: the [I/O priority][io-priority] of the request
* @cancellable: (nullable): optional #GCancellable object,
* %NULL to ignore
* @callback: (scope async): a #GAsyncReadyCallback to call
* when the request is satisfied
* @user_data: (closure): the data to pass to callback function
*
* Asynchronously overwrites the file in read-write mode,
* replacing the contents, possibly creating a backup copy
* of the file first.
*
* For more details, see g_file_replace_readwrite() which is
* the synchronous version of this call.
*
* When the operation is finished, @callback will be called.
* You can then call g_file_replace_readwrite_finish() to get
* the result of the operation.
*
* Since: 2.22
*/
void
g_file_replace_readwrite_async (GFile *file,
const char *etag,
gboolean make_backup,
GFileCreateFlags flags,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
GFileIface *iface;
g_return_if_fail (G_IS_FILE (file));
iface = G_FILE_GET_IFACE (file);
(* iface->replace_readwrite_async) (file,
etag,
make_backup,
flags,
io_priority,
cancellable,
callback,
user_data);
}
/**
* g_file_replace_readwrite_finish:
* @file: input #GFile
* @res: a #GAsyncResult
* @error: a #GError, or %NULL
*
* Finishes an asynchronous file replace operation started with
* g_file_replace_readwrite_async().
*
* Returns: (transfer full): a #GFileIOStream, or %NULL on error.
* Free the returned object with g_object_unref().
*
* Since: 2.22
*/
GFileIOStream *
g_file_replace_readwrite_finish (GFile *file,
GAsyncResult *res,
GError **error)
{
GFileIface *iface;
g_return_val_if_fail (G_IS_FILE (file), NULL);
g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
if (g_async_result_legacy_propagate_error (res, error))
return NULL;
iface = G_FILE_GET_IFACE (file);
return (* iface->replace_readwrite_finish) (file, res, error);
}
static gboolean
copy_symlink (GFile *destination,
GFileCopyFlags flags,
GCancellable *cancellable,
const char *target,
GError **error)
{
GError *my_error;
gboolean tried_delete;
GFileInfo *info;
GFileType file_type;
tried_delete = FALSE;
retry:
my_error = NULL;
if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
{
/* Maybe it already existed, and we want to overwrite? */
if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) &&
my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
{
g_clear_error (&my_error);
/* Don't overwrite if the destination is a directory */
info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
cancellable, &my_error);
if (info != NULL)
{
file_type = g_file_info_get_file_type (info);
g_object_unref (info);
if (file_type == G_FILE_TYPE_DIRECTORY)
{
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
_("Can’t copy over directory"));
return FALSE;
}
}
if (!g_file_delete (destination, cancellable, error))
return FALSE;
tried_delete = TRUE;
goto retry;
}
/* Nah, fail */
g_propagate_error (error, my_error);
return FALSE;
}
return TRUE;
}
static GFileInputStream *
open_source_for_copy (GFile *source,
GFile *destination,
GFileCopyFlags flags,
GCancellable *cancellable,
GError **error)
{
GError *my_error;
GFileInputStream *ret;
GFileInfo *info;
GFileType file_type;
my_error = NULL;
ret = g_file_read (source, cancellable, &my_error);
if (ret != NULL)
return ret;
/* There was an error opening the source, try to set a good error for it: */
if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
{
/* The source is a directory, don't fail with WOULD_RECURSE immediately,
* as that is less useful to the app. Better check for errors on the
* target instead.
*/
g_error_free (my_error);
my_error = NULL;
info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
cancellable, &my_error);
if (info != NULL &&
g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_TYPE))
{
file_type = g_file_info_get_file_type (info);
g_object_unref (info);
if (flags & G_FILE_COPY_OVERWRITE)
{
if (file_type == G_FILE_TYPE_DIRECTORY)
{
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
_("Can’t copy directory over directory"));
return NULL;
}
/* continue to would_recurse error */
}
else
{
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
_("Target file exists"));
return NULL;
}
}
else
{
/* Error getting info from target, return that error
* (except for NOT_FOUND, which is no error here)
*/
g_clear_object (&info);
if (my_error != NULL && !g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
{
g_propagate_error (error, my_error);
return NULL;
}
g_clear_error (&my_error);
}
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
_("Can’t recursively copy directory"));
return NULL;
}
g_propagate_error (error, my_error);
return NULL;
}
static gboolean
should_copy (GFileAttributeInfo *info,
gboolean copy_all_attributes,
gboolean skip_perms)
{
if (skip_perms && strcmp(info->name, "unix::mode") == 0)
return FALSE;
if (copy_all_attributes)
return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
}
static gboolean
build_attribute_list_for_copy (GFile *file,
GFileCopyFlags flags,
char **out_attributes,
GCancellable *cancellable,
GError **error)
{
gboolean ret = FALSE;
GFileAttributeInfoList *attributes = NULL, *namespaces = NULL;
GString *s = NULL;
gboolean first;
int i;
gboolean copy_all_attributes;
gboolean skip_perms;
copy_all_attributes = flags & G_FILE_COPY_ALL_METADATA;
skip_perms = (flags & G_FILE_COPY_TARGET_DEFAULT_PERMS) != 0;
/* Ignore errors here, if the target supports no attributes there is
* nothing to copy. We still honor the cancellable though.
*/
attributes = g_file_query_settable_attributes (file, cancellable, NULL);
if (g_cancellable_set_error_if_cancelled (cancellable, error))
goto out;
namespaces = g_file_query_writable_namespaces (file, cancellable, NULL);
if (g_cancellable_set_error_if_cancelled (cancellable, error))
goto out;
if (attributes == NULL && namespaces == NULL)
goto out;
first = TRUE;
s = g_string_new ("");
if (attributes)
{
for (i = 0; i < attributes->n_infos; i++)
{
if (should_copy (&attributes->infos[i], copy_all_attributes, skip_perms))
{
if (first)
first = FALSE;
else
g_string_append_c (s, ',');
g_string_append (s, attributes->infos[i].name);
}
}
}
if (namespaces)
{
for (i = 0; i < namespaces->n_infos; i++)
{
if (should_copy (&namespaces->infos[i], copy_all_attributes, FALSE))
{
if (first)
first = FALSE;
else
g_string_append_c (s, ',');
g_string_append (s, namespaces->infos[i].name);
g_string_append (s, "::*");
}
}
}
ret = TRUE;
*out_attributes = g_string_free (s, FALSE);
s = NULL;
out:
if (s)
g_string_free (s, TRUE);
if (attributes)
g_file_attribute_info_list_unref (attributes);
if (namespaces)
g_file_attribute_info_list_unref (namespaces);
return ret;
}
/**
* g_file_copy_attributes:
* @source: a #GFile with attributes
* @destination: a #GFile to copy attributes to
* @flags: a set of #GFileCopyFlags
* @cancellable: (nullable): optional #GCancellable object,
* %NULL to ignore
* @error: a #GError, %NULL to ignore
*
* Copies the file attributes from @source to @destination.
*
* Normally only a subset of the file attributes are copied,
* those that are copies in a normal file copy operation
* (which for instance does not include e.g. owner). However
* if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
* all the metadata that is possible to copy is copied. This
* is useful when implementing move by copy + delete source.
*
* Returns: %TRUE if the attributes were copied successfully,
* %FALSE otherwise.
*/
gboolean
g_file_copy_attributes (GFile *source,
GFile *destination,
GFileCopyFlags flags,
GCancellable *cancellable,
GError **error)
{
char *attrs_to_read;
gboolean res;
GFileInfo *info;
gboolean source_nofollow_symlinks;
if (!build_attribute_list_for_copy (destination, flags, &attrs_to_read,
cancellable, error))
return FALSE;
source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
/* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
* we just don't copy it.
*/
info = g_file_query_info (source, attrs_to_read,
source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
cancellable,
NULL);
g_free (attrs_to_read);
res = TRUE;
if (info)
{
res = g_file_set_attributes_from_info (destination,
info,
G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
cancellable,
error);
g_object_unref (info);
}
return res;
}
/* 256k minus malloc overhead */
#define STREAM_BUFFER_SIZE (1024*256 - 2 *sizeof(gpointer))
static gboolean
copy_stream_with_progress (GInputStream *in,
GOutputStream *out,
GFile *source,
GCancellable *cancellable,
GFileProgressCallback progress_callback,
gpointer progress_callback_data,
GError **error)
{
gssize n_read;
gsize n_written;
goffset current_size;
char *buffer;
gboolean res;
goffset total_size;
GFileInfo *info;
total_size = -1;
/* avoid performance impact of querying total size when it's not needed */
if (progress_callback)
{
info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
G_FILE_ATTRIBUTE_STANDARD_SIZE,
cancellable, NULL);
if (info)
{
if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
total_size = g_file_info_get_size (info);
g_object_unref (info);
}
if (total_size == -1)
{
info = g_file_query_info (source,
G_FILE_ATTRIBUTE_STANDARD_SIZE,
G_FILE_QUERY_INFO_NONE,
cancellable, NULL);
if (info)
{
if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
total_size = g_file_info_get_size (info);
g_object_unref (info);
}
}
}
if (total_size == -1)
total_size = 0;
buffer = g_malloc0 (STREAM_BUFFER_SIZE);
current_size = 0;
res = TRUE;
while (TRUE)
{
n_read = g_input_stream_read (in, buffer, STREAM_BUFFER_SIZE, cancellable, error);
if (n_read == -1)
{
res = FALSE;
break;
}
if (n_read == 0)
break;
current_size += n_read;
res = g_output_stream_write_all (out, buffer, n_read, &n_written, cancellable, error);
if (!res)
break;
if (progress_callback)
progress_callback (current_size, total_size, progress_callback_data);
}
g_free (buffer);
/* Make sure we send full copied size */
if (progress_callback)
progress_callback (current_size, total_size, progress_callback_data);
return res;
}
#ifdef HAVE_SPLICE
static gboolean
do_splice (int fd_in,
loff_t *off_in,
int fd_out,
loff_t *off_out,
size_t len,
long *bytes_transferd,
GError **error)
{
long result;
retry:
result = splice (fd_in, off_in, fd_out, off_out, len, SPLICE_F_MORE);
if (result == -1)
{
int errsv = errno;
if (errsv == EINTR)
goto retry;
else if (errsv == ENOSYS || errsv == EINVAL || errsv == EOPNOTSUPP)
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
_("Splice not supported"));
else
g_set_error (error, G_IO_ERROR,
g_io_error_from_errno (errsv),
_("Error splicing file: %s"),
g_strerror (errsv));
return FALSE;
}
*bytes_transferd = result;
return TRUE;
}
static gboolean
splice_stream_with_progress (GInputStream *in,
GOutputStream *out,
GCancellable *cancellable,
GFileProgressCallback progress_callback,
gpointer progress_callback_data,
GError **error)
{
int buffer[2] = { -1, -1 };
int buffer_size;
gboolean res;
goffset total_size;
loff_t offset_in;
loff_t offset_out;
int fd_in, fd_out;
fd_in = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in));
fd_out = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out));
if (!g_unix_open_pipe (buffer, FD_CLOEXEC, error))
return FALSE;
#if defined(F_SETPIPE_SZ) && defined(F_GETPIPE_SZ)
/* Try a 1MiB buffer for improved throughput. If that fails, use the default
* pipe size. See: https://bugzilla.gnome.org/791457 */
buffer_size = fcntl (buffer[1], F_SETPIPE_SZ, 1024 * 1024);
if (buffer_size <= 0)
{
int errsv;
buffer_size = fcntl (buffer[1], F_GETPIPE_SZ);
errsv = errno;
if (buffer_size <= 0)
{
g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
_("Error splicing file: %s"), g_strerror (errsv));
res = FALSE;
goto out;
}
}
#else
/* If #F_GETPIPE_SZ isn’t available, assume we’re on Linux < 2.6.35,
* but ≥ 2.6.11, meaning the pipe capacity is 64KiB. Ignore the possibility of
* running on Linux < 2.6.11 (where the capacity was the system page size,
* typically 4KiB) because it’s ancient. See pipe(7). */
buffer_size = 1024 * 64;
#endif
g_assert (buffer_size > 0);
total_size = -1;
/* avoid performance impact of querying total size when it's not needed */
if (progress_callback)
{
struct stat sbuf;
if (fstat (fd_in, &sbuf) == 0)
total_size = sbuf.st_size;
}
if (total_size == -1)
total_size = 0;
offset_in = offset_out = 0;
res = FALSE;
while (TRUE)
{
long n_read;
long n_written;
if (g_cancellable_set_error_if_cancelled (cancellable, error))
break;
if (!do_splice (fd_in, &offset_in, buffer[1], NULL, buffer_size, &n_read, error))
break;
if (n_read == 0)
{
res = TRUE;
break;
}
while (n_read > 0)
{
if (g_cancellable_set_error_if_cancelled (cancellable, error))
goto out;
if (!do_splice (buffer[0], NULL, fd_out, &offset_out, n_read, &n_written, error))
goto out;
n_read -= n_written;
}
if (progress_callback)
progress_callback (offset_in, total_size, progress_callback_data);
}
/* Make sure we send full copied size */
if (progress_callback)
progress_callback (offset_in, total_size, progress_callback_data);
if (!g_close (buffer[0], error))
goto out;
buffer[0] = -1;
if (!g_close (buffer[1], error))
goto out;
buffer[1] = -1;
out:
if (buffer[0] != -1)
(void) g_close (buffer[0], NULL);
if (buffer[1] != -1)
(void) g_close (buffer[1], NULL);
return res;
}
#endif
#ifdef __linux__
static gboolean
btrfs_reflink_with_progress (GInputStream *in,
GOutputStream *out,
GFileInfo *info,
GCancellable *cancellable,
GFileProgressCallback progress_callback,
gpointer progress_callback_data,
GError **error)
{
goffset source_size;
int fd_in, fd_out;
int ret, errsv;
fd_in = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in));
fd_out = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out));
if (progress_callback)
source_size = g_file_info_get_size (info);
/* Btrfs clone ioctl properties:
* - Works at the inode level
* - Doesn't work with directories
* - Always follows symlinks (source and destination)
*
* By the time we get here, *in and *out are both regular files */
ret = ioctl (fd_out, BTRFS_IOC_CLONE, fd_in);
errsv = errno;
if (ret < 0)
{
if (errsv == EXDEV)
g_set_error_literal (error, G_IO_ERROR,
G_IO_ERROR_NOT_SUPPORTED,
_("Copy (reflink/clone) between mounts is not supported"));
else if (errsv == EINVAL)
g_set_error_literal (error, G_IO_ERROR,
G_IO_ERROR_NOT_SUPPORTED,
_("Copy (reflink/clone) is not supported or invalid"));
else
/* Most probably something odd happened; retry with fallback */
g_set_error_literal (error, G_IO_ERROR,
G_IO_ERROR_NOT_SUPPORTED,
_("Copy (reflink/clone) is not supported or didn’t work"));
/* We retry with fallback for all error cases because Btrfs is currently
* unstable, and so we can't trust it to do clone properly.
* In addition, any hard errors here would cause the same failure in the
* fallback manual copy as well. */
return FALSE;
}
/* Make sure we send full copied size */
if (progress_callback)
progress_callback (source_size, source_size, progress_callback_data);
return TRUE;
}
#endif
static gboolean
file_copy_fallback (GFile *source,
GFile *destination,
GFileCopyFlags flags,
GCancellable *cancellable,
GFileProgressCallback progress_callback,
gpointer progress_callback_data,
GError **error)
{
gboolean ret = FALSE;
GFileInputStream *file_in = NULL;
GInputStream *in = NULL;
GOutputStream *out = NULL;
GFileInfo *info = NULL;
const char *target;
char *attrs_to_read;
gboolean do_set_attributes = FALSE;
/* need to know the file type */
info = g_file_query_info (source,
G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
cancellable,
error);
if (!info)
goto out;
/* Maybe copy the symlink? */
if ((flags & G_FILE_COPY_NOFOLLOW_SYMLINKS) &&
g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK)
{
target = g_file_info_get_symlink_target (info);
if (target)
{
if (!copy_symlink (destination, flags, cancellable, target, error))
goto out;
ret = TRUE;
goto out;
}
/* ... else fall back on a regular file copy */
}
/* Handle "special" files (pipes, device nodes, ...)? */
else if (g_file_info_get_file_type (info) == G_FILE_TYPE_SPECIAL)
{
/* FIXME: could try to recreate device nodes and others? */
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
_("Can’t copy special file"));
goto out;
}
/* Everything else should just fall back on a regular copy. */
file_in = open_source_for_copy (source, destination, flags, cancellable, error);
if (!file_in)
goto out;
in = G_INPUT_STREAM (file_in);
if (!build_attribute_list_for_copy (destination, flags, &attrs_to_read,
cancellable, error))
goto out;
if (attrs_to_read != NULL)
{
GError *tmp_error = NULL;
/* Ok, ditch the previous lightweight info (on Unix we just
* called lstat()); at this point we gather all the information
* we need about the source from the opened file descriptor.
*/
g_object_unref (info);
info = g_file_input_stream_query_info (file_in, attrs_to_read,
cancellable, &tmp_error);
if (!info)
{
/* Not all gvfs backends implement query_info_on_read(), we
* can just fall back to the pathname again.
* https://bugzilla.gnome.org/706254
*/
if (g_error_matches (tmp_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
{
g_clear_error (&tmp_error);
info = g_file_query_info (source, attrs_to_read, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
cancellable, error);
}
else
{
g_free (attrs_to_read);
g_propagate_error (error, tmp_error);
goto out;
}
}
g_free (attrs_to_read);
if (!info)
goto out;
do_set_attributes = TRUE;
}
/* In the local file path, we pass down the source info which
* includes things like unix::mode, to ensure that the target file
* is not created with different permissions from the source file.
*
* If a future API like g_file_replace_with_info() is added, switch
* this code to use that.
*/
if (G_IS_LOCAL_FILE (destination))
{
if (flags & G_FILE_COPY_OVERWRITE)
out = (GOutputStream*)_g_local_file_output_stream_replace (_g_local_file_get_filename (G_LOCAL_FILE (destination)),
FALSE, NULL,
flags & G_FILE_COPY_BACKUP,
G_FILE_CREATE_REPLACE_DESTINATION,
info,
cancellable, error);
else
out = (GOutputStream*)_g_local_file_output_stream_create (_g_local_file_get_filename (G_LOCAL_FILE (destination)),
FALSE, 0, info,
cancellable, error);
}
else if (flags & G_FILE_COPY_OVERWRITE)
{
out = (GOutputStream *)g_file_replace (destination,
NULL,
flags & G_FILE_COPY_BACKUP,
G_FILE_CREATE_REPLACE_DESTINATION,
cancellable, error);
}
else
{
out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
}
if (!out)
goto out;
#ifdef __linux__
if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
{
GError *reflink_err = NULL;
if (!btrfs_reflink_with_progress (in, out, info, cancellable,
progress_callback, progress_callback_data,
&reflink_err))
{
if (g_error_matches (reflink_err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
{
g_clear_error (&reflink_err);
}
else
{
g_propagate_error (error, reflink_err);
goto out;
}
}
else
{
ret = TRUE;
goto out;
}
}
#endif
#ifdef HAVE_SPLICE
if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
{
GError *splice_err = NULL;
if (!splice_stream_with_progress (in, out, cancellable,
progress_callback, progress_callback_data,
&splice_err))
{
if (g_error_matches (splice_err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
{
g_clear_error (&splice_err);
}
else
{
g_propagate_error (error, splice_err);
goto out;
}
}
else
{
ret = TRUE;
goto out;
}
}
#endif
/* A plain read/write loop */
if (!copy_stream_with_progress (in, out, source, cancellable,
progress_callback, progress_callback_data,
error))
goto out;
ret = TRUE;
out:
if (in)
{
/* Don't care about errors in source here */
(void) g_input_stream_close (in, cancellable, NULL);
g_object_unref (in);
}
if (out)
{
/* But write errors on close are bad! */
if (!g_output_stream_close (out, cancellable, ret ? error : NULL))
ret = FALSE;
g_object_unref (out);
}
/* Ignore errors here. Failure to copy metadata is not a hard error */
/* TODO: set these attributes /before/ we do the rename() on Unix */
if (ret && do_set_attributes)
{
g_file_set_attributes_from_info (destination,
info,
G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
cancellable,
NULL);
}
g_clear_object (&info);
return ret;
}
/**
* g_file_copy:
* @source: input #GFile
* @destination: destination #GFile
* @flags: set of #GFileCopyFlags
* @cancellable: (nullable): optional #GCancellable object,
* %NULL to ignore
* @progress_callback: (nullable) (scope call): function to callback with
* progress information, or %NULL if progress information is not needed
* @progress_callback_data: (closure): user data to pass to @progress_callback
* @error: #GError to set on error, or %NULL
*
* Copies the file @source to the location specified by @destination.
* Can not handle recursive copies of directories.
*
* If the flag #G_FILE_COPY_OVERWRITE is specified an already
* existing @destination file is overwritten.
*
* If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
* will be copied as symlinks, otherwise the target of the
* @source symlink will be copied.
*
* If the flag #G_FILE_COPY_ALL_METADATA is specified then all the metadata
* that is possible to copy is copied, not just the default subset (which,
* for instance, does not include the owner, see #GFileInfo).
*
* If @cancellable is not %NULL, then the operation can be cancelled by
* triggering the cancellable object from another thread. If the operation
* was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
*
* If @progress_callback is not %NULL, then the operation can be monitored
* by setting this to a #GFileProgressCallback function.
* @progress_callback_data will be passed to this function. It is guaranteed
* that this callback will be called after all data has been transferred with
* the total number of bytes copied during the operation.
*
* If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND error
* is returned, independent on the status of the @destination.
*
* If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then
* the error %G_IO_ERROR_EXISTS is returned.
*
* If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
* error is returned. If trying to overwrite a directory with a directory the
* %G_IO_ERROR_WOULD_MERGE error is returned.
*
* If the source is a directory and the target does not exist, or
* #G_FILE_COPY_OVERWRITE is specified and the target is a file, then the
* %G_IO_ERROR_WOULD_RECURSE error is returned.
*
* If you are interested in copying the #GFile object itself (not the on-disk
* file), see g_file_dup().
*
* Returns: %TRUE on success, %FALSE otherwise.
*/
gboolean
g_file_copy (GFile *source,
GFile *destination,
GFileCopyFlags flags,
GCancellable *cancellable,
GFileProgressCallback progress_callback,
gpointer progress_callback_data,
GError **error)
{
GFileIface *iface;
GError *my_error;
gboolean res;
g_return_val_if_fail (G_IS_FILE (source), FALSE);
g_return_val_if_fail (G_IS_FILE (destination), FALSE);
if (g_cancellable_set_error_if_cancelled (cancellable, error))
return FALSE;
iface = G_FILE_GET_IFACE (destination);
if (iface->copy)
{
my_error = NULL;
res = (* iface->copy) (source, destination,
flags, cancellable,