`GFile` is a high level abstraction for manipulating files on a virtual file system.
`GFile`s 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 [class@Gio.InputStream] and [
class@Gio.OutputStream]).
To construct a `GFile`, you can use:
- [func@Gio.File.new_for_path] if you have a path.
- [func@Gio.File.new_for_uri] if you have a URI.
- [func@Gio.File.new_for_commandline_arg] or [func@Gio.File.new_for_commandline_arg_and_cwd] for a command line argument.
- [func@Gio.File.new_tmp] to create a temporary file from a template.
- [func@Gio.File.new_tmp_async] to asynchronously create a temporary file.
- [func@Gio.File.new_tmp_dir_async] to asynchronously create a temporary directory.
- [func@Gio.File.parse_name] from a UTF-8 string gotten from [method@Gio.File.get_parse_name].
- [func@Gio.File.new_build_filename] or [func@Gio.File.new_build_filenamev] 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
`GFile`s are extensible it could also be something else that corresponds to a pathname in a userspace implementation of a filesystem.
`GFile`s 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 [method@Gio.File.get_parent] to get an identifier for the parent directory, [method@Gio.File.get_child] to get a child within a
directory, and [method@Gio.File.resolve_relative_path] to resolve a relative path between two `GFile`s. There can be multiple hierarchies, so
you may not end up at the same root if you repeatedly call [method@Gio.File.get_parent] on two different files.
All `GFile`s have a basename (get with [method@Gio.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 [method@Gio.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 `GFile`s 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 `GFile`s 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 `GFile`s.
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 [callback@Gio.AsyncReadyCallback] which
is then used to finalize the operation, producing a [iface@Gio.AsyncResult] 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](overview.html#asynchronous-programming) for more.
Some `GFile` operations almost always take a noticeable amount of time, and so do not have synchronous analogs. Notable cases include:
- [method@Gio.File.mount_mountable] to mount a mountable file.
- [method@Gio.File.unmount_mountable_with_operation] to unmount a mountable file.
- [method@Gio.File.eject_mountable_with_operation] to eject a mountable file.
Entity Tags
One notable feature of `GFile`s 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 for HTTP `ETag` headers, which are a very
similar concept.
- public abstract bool @delete (Cancellable? cancellable = null) throws Error
Deletes a file.
- public abstract FileOutputStream append_to (FileCreateFlags flags, Cancellable? cancellable = null) throws Error
Gets an output stream for appending data to the file.
- public virtual async FileOutputStream append_to_async (FileCreateFlags flags, int io_priority = DEFAULT, Cancellable? cancellable = null) throws Error
Asynchronously opens this for appending.
- public string build_attribute_list_for_copy (FileCopyFlags flags, Cancellable? cancellable = null) throws Error
Prepares the file attribute query string for copying to
this.
- public abstract bool copy (File destination, FileCopyFlags flags, Cancellable? cancellable = null, FileProgressCallback? progress_callback = null) throws Error
Copies the file this to the location
specified by destination
.
- public virtual async bool copy_async (File destination, FileCopyFlags flags, int io_priority = DEFAULT, Cancellable? cancellable = null, FileProgressCallback? progress_callback = null) throws Error
Copies the file this to the location
specified by destination
asynchronously.
- public bool copy_attributes (File destination, FileCopyFlags flags, Cancellable? cancellable = null) throws Error
Copies the file attributes from this to
destination
.
- public abstract FileOutputStream create (FileCreateFlags flags, Cancellable? cancellable = null) throws Error
Creates a new file and returns an output stream for writing to it.
- public virtual async FileOutputStream create_async (FileCreateFlags flags, int io_priority = DEFAULT, Cancellable? cancellable = null) throws Error
Asynchronously creates a new file and returns an output stream for writing
to it.
- public abstract FileIOStream create_readwrite (FileCreateFlags flags, Cancellable? cancellable = null) throws Error
Creates a new file and returns a stream for reading and writing to it.
- public virtual async FileIOStream create_readwrite_async (FileCreateFlags flags, int io_priority = DEFAULT, Cancellable? cancellable = null) throws Error
Asynchronously creates a new file and returns a stream for reading and
writing to it.
- public virtual async bool delete_async (int io_priority = DEFAULT, Cancellable? cancellable = null) throws Error
Asynchronously delete a file.
- public abstract File dup ()
Duplicates a File handle.
- public abstract async bool eject_mountable (MountUnmountFlags flags, Cancellable? cancellable = null) throws Error
Starts an asynchronous eject on a mountable.
- public abstract async bool eject_mountable_with_operation (MountUnmountFlags flags, MountOperation? mount_operation, Cancellable? cancellable = null) throws Error
Starts an asynchronous eject on a mountable.
- public abstract FileEnumerator enumerate_children (string attributes, FileQueryInfoFlags flags, Cancellable? cancellable = null) throws Error
Gets the requested information about the files in a directory.
- public virtual async FileEnumerator enumerate_children_async (string attributes, FileQueryInfoFlags flags, int io_priority = DEFAULT, Cancellable? cancellable = null) throws Error
Asynchronously gets the requested information about the files in a
directory.
- public abstract bool equal (File file2)
Checks if the two given Files refer to the
same file.
- public abstract Mount find_enclosing_mount (Cancellable? cancellable = null) throws Error
Gets a Mount for
the File.
- public virtual async Mount find_enclosing_mount_async (int io_priority = DEFAULT, Cancellable? cancellable = null) throws Error
Asynchronously gets the mount for the file.
- public abstract string? get_basename ()
Gets the base name (the last component of the path) for a given
File.
- public File get_child (string name)
Gets a child of this with basename equal
to name
.
- public abstract File get_child_for_display_name (string display_name) throws Error
Gets the child of this for a given
display_name
(i.
- public abstract File? get_parent ()
Gets the parent directory for the this.
- public abstract string get_parse_name ()
Gets the parse name of the this.
- public abstract string? get_path ()
Gets the local pathname for File, if one
exists.
- public abstract string? get_relative_path (File descendant)
Gets the path for descendant
relative to
this.
- public abstract string get_uri ()
Gets the URI for the this.
- public abstract string? get_uri_scheme ()
Gets the URI scheme for a File.
- public bool has_parent (File? parent)
Checks if this has a parent, and
optionally, if it is parent
.
- public abstract bool has_prefix (File file)
Checks whether this has the prefix
specified by prefix
.
- public abstract bool has_uri_scheme (string uri_scheme)
Checks to see if a File has a given URI
scheme.
- public abstract uint hash ()
Creates a hash value for a File.
- public abstract bool is_native ()
Checks to see if a file is native to the platform.
- public Bytes load_bytes (Cancellable? cancellable = null, out string? etag_out = null) throws Error
Loads the contents of this and returns it
as Bytes.
- public async Bytes load_bytes_async (Cancellable? cancellable = null, out string? etag_out) throws Error
Asynchronously loads the contents of this
as Bytes.
- public bool load_contents (Cancellable? cancellable, out uint8[] contents, out string? etag_out) throws Error
Loads the content of the file into memory.
- public async bool load_contents_async (Cancellable? cancellable = null, out uint8[] contents, out string? etag_out) throws Error
Starts an asynchronous load of the this's
contents.
- public async bool load_partial_contents_async (Cancellable? cancellable = null, FileReadMoreCallback read_more_callback, out uint8[] contents, out string etag_out) throws Error
Reads the partial contents of a file.
- public abstract bool make_directory (Cancellable? cancellable = null) throws Error
Creates a directory.
- public virtual async bool make_directory_async (int io_priority = DEFAULT, Cancellable? cancellable = null) throws Error
Asynchronously creates a directory.
- public bool make_directory_with_parents (Cancellable? cancellable = null) throws Error
Creates a directory and any parent directories that may not exist similar to
'mkdir -p'.
- public abstract bool make_symbolic_link (string symlink_value, Cancellable? cancellable = null) throws Error
Creates a symbolic link named this which
contains the string symlink_value
.
- public virtual async bool make_symbolic_link_async (string symlink_value, int io_priority = DEFAULT, Cancellable? cancellable = null) throws Error
Asynchronously creates a symbolic link named this
which contains the string symlink_value
.
- public virtual bool measure_disk_usage (FileMeasureFlags flags, Cancellable? cancellable, FileMeasureProgressCallback? progress_callback, out uint64 disk_usage, out uint64 num_dirs, out uint64 num_files) throws Error
Recursively measures the disk usage of this
.
- public virtual async bool measure_disk_usage_async (FileMeasureFlags flags, int io_priority, Cancellable? cancellable, FileMeasureProgressCallback? progress_callback, out uint64 disk_usage, out uint64 num_dirs, out uint64 num_files) throws Error
Recursively measures the disk usage of this
.
- public FileMonitor monitor (FileMonitorFlags flags, Cancellable? cancellable = null) throws Error
Obtains a file or directory monitor for the given file, depending on the
type of the file.
- public abstract FileMonitor monitor_directory (FileMonitorFlags flags, Cancellable? cancellable = null) throws IOError
Obtains a directory monitor for the given file.
- public abstract FileMonitor monitor_file (FileMonitorFlags flags, Cancellable? cancellable = null) throws IOError
Obtains a file monitor for the given file.
- public abstract async bool mount_enclosing_volume (MountMountFlags flags, MountOperation? mount_operation, Cancellable? cancellable = null) throws Error
Starts a mount_operation
, mounting the volume that contains the
file this.
- public abstract async File mount_mountable (MountMountFlags flags, MountOperation? mount_operation, Cancellable? cancellable = null) throws Error
Mounts a file of type G_FILE_TYPE_MOUNTABLE.
- public abstract bool move (File destination, FileCopyFlags flags, Cancellable? cancellable = null, FileProgressCallback? progress_callback = null) throws Error
Tries to move the file or directory this
to the location specified by destination
.
- public virtual async bool move_async (File destination, FileCopyFlags flags, int io_priority = DEFAULT, Cancellable? cancellable = null, FileProgressCallback? progress_callback) throws Error
Asynchronously moves a file this to the
location of destination
.
- public abstract FileIOStream open_readwrite (Cancellable? cancellable = null) throws Error
Opens an existing file for reading and writing.
- public virtual async FileIOStream open_readwrite_async (int io_priority = DEFAULT, Cancellable? cancellable = null) throws Error
Asynchronously opens this for reading and
writing.
- public unowned string? peek_path ()
- public abstract async bool poll_mountable (Cancellable? cancellable = null) throws Error
Polls a file of type g_file_type_mountable
.
- public AppInfo query_default_handler (Cancellable? cancellable = null) throws Error
Returns the AppInfo
that is registered as the default application to handle the file specified by this.
- public async AppInfo query_default_handler_async (int io_priority = DEFAULT, Cancellable? cancellable = null) throws Error
- public bool query_exists (Cancellable? cancellable = null)
Utility function to check if a particular file exists.
- public FileType query_file_type (FileQueryInfoFlags flags, Cancellable? cancellable = null)
Utility function to inspect the
FileType of a file.
- public abstract FileInfo query_filesystem_info (string attributes, Cancellable? cancellable = null) throws Error
Similar to
query_info, but obtains information about the filesystem the
this is on, rather than the file itself.
- public virtual async FileInfo query_filesystem_info_async (string attributes, int io_priority = DEFAULT, Cancellable? cancellable = null) throws Error
Asynchronously gets the requested information about the filesystem that the
specified this is on.
- public abstract FileInfo query_info (string attributes, FileQueryInfoFlags flags, Cancellable? cancellable = null) throws Error
Gets the requested information about specified
this.
- public virtual async FileInfo query_info_async (string attributes, FileQueryInfoFlags flags, int io_priority = DEFAULT, Cancellable? cancellable = null) throws Error
Asynchronously gets the requested information about specified
this.
- public abstract FileAttributeInfoList query_settable_attributes (Cancellable? cancellable = null) throws Error
Obtain the list of settable attributes for the file.
- public abstract FileAttributeInfoList query_writable_namespaces (Cancellable? cancellable = null) throws Error
Obtain the list of attribute namespaces where new attributes can be created
by a user.
- public abstract FileInputStream read (Cancellable? cancellable = null) throws Error
Opens a file for reading.
- public virtual async FileInputStream read_async (int io_priority = DEFAULT, Cancellable? cancellable = null) throws Error
Asynchronously opens this for reading.
- public abstract FileOutputStream replace (string? etag, bool make_backup, FileCreateFlags flags, Cancellable? cancellable = null) throws Error
Returns an output stream for overwriting the file, possibly creating a
backup copy of the file first.
- public virtual async FileOutputStream replace_async (string? etag, bool make_backup, FileCreateFlags flags, int io_priority = DEFAULT, Cancellable? cancellable = null) throws Error
Asynchronously overwrites the file, replacing the contents, possibly
creating a backup copy of the file first.
- public bool replace_contents (uint8[] contents, string? etag, bool make_backup, FileCreateFlags flags, out string? new_etag, Cancellable? cancellable = null) throws Error
Replaces the contents of this with
contents
of contents.length
bytes.
- public async bool replace_contents_async (uint8[] contents, string? etag, bool make_backup, FileCreateFlags flags, Cancellable? cancellable = null, out string? new_etag) throws Error
Starts an asynchronous replacement of this
with the given contents
of contents.length
bytes.
- public async bool replace_contents_bytes_async (Bytes contents, string? etag, bool make_backup, FileCreateFlags flags, Cancellable? cancellable = null, out string? new_etag) throws Error
- public abstract FileIOStream replace_readwrite (string? etag, bool make_backup, FileCreateFlags flags, Cancellable? cancellable = null) throws Error
Returns an output stream for overwriting the file in readwrite mode,
possibly creating a backup copy of the file first.
- public virtual async FileIOStream replace_readwrite_async (string? etag, bool make_backup, FileCreateFlags flags, int io_priority = DEFAULT, Cancellable? cancellable = null) throws Error
Asynchronously overwrites the file in read-write mode, replacing the
contents, possibly creating a backup copy of the file first.
- public abstract File resolve_relative_path (string relative_path)
Resolves a relative path for this to an
absolute path.
- public abstract bool set_attribute (string attribute, FileAttributeType type, void* value_p, FileQueryInfoFlags flags, Cancellable? cancellable = null) throws Error
Sets an attribute in the file with attribute name attribute
to
value_p
.
- public bool set_attribute_byte_string (string attribute, string value, FileQueryInfoFlags flags, Cancellable? cancellable = null) throws Error
Sets attribute
of type
g_file_attribute_type_byte_string to value
.
- public bool set_attribute_int32 (string attribute, int32 value, FileQueryInfoFlags flags, Cancellable? cancellable = null) throws Error
Sets attribute
of type
g_file_attribute_type_int32 to value
.
- public bool set_attribute_int64 (string attribute, int64 value, FileQueryInfoFlags flags, Cancellable? cancellable = null) throws Error
Sets attribute
of type
g_file_attribute_type_int64 to value
.
- public bool set_attribute_string (string attribute, string value, FileQueryInfoFlags flags, Cancellable? cancellable = null) throws Error
Sets attribute
of type
g_file_attribute_type_string to value
.
- public bool set_attribute_uint32 (string attribute, uint32 value, FileQueryInfoFlags flags, Cancellable? cancellable = null) throws Error
Sets attribute
of type
g_file_attribute_type_uint32 to value
.
- public bool set_attribute_uint64 (string attribute, uint64 value, FileQueryInfoFlags flags, Cancellable? cancellable = null) throws Error
Sets attribute
of type
g_file_attribute_type_uint64 to value
.
- public virtual async bool set_attributes_async (FileInfo info, FileQueryInfoFlags flags, int io_priority = DEFAULT, Cancellable? cancellable = null, out FileInfo info_out) throws Error
Asynchronously sets the attributes of this
with info
.
- public abstract bool set_attributes_from_info (FileInfo info, FileQueryInfoFlags flags, Cancellable? cancellable = null) throws Error
Tries to set all attributes in the
FileInfo on the target values, not stopping on the first error.
- public abstract File set_display_name (string display_name, Cancellable? cancellable = null) throws Error
Renames this to the specified display
name.
- public virtual async File set_display_name_async (string display_name, int io_priority = DEFAULT, Cancellable? cancellable = null) throws Error
Asynchronously sets the display name for a given
File.
- public abstract async bool start_mountable (DriveStartFlags flags, MountOperation? start_operation, Cancellable? cancellable = null) throws Error
Starts a file of type g_file_type_mountable
.
- public abstract async bool stop_mountable (MountUnmountFlags flags, MountOperation? mount_operation, Cancellable? cancellable = null) throws Error
Stops a file of type g_file_type_mountable
.
- public bool supports_thread_contexts ()
Checks if this supports thread-default
contexts.
- public abstract bool trash (Cancellable? cancellable = null) throws Error
Sends this to the "Trashcan", if possible.
- public virtual async bool trash_async (int io_priority = DEFAULT, Cancellable? cancellable = null) throws Error
Asynchronously sends this to the Trash
location, if possible.
- public abstract async bool unmount_mountable (MountUnmountFlags flags, Cancellable? cancellable = null) throws Error
Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
- public abstract async bool unmount_mountable_with_operation (MountUnmountFlags flags, MountOperation? mount_operation, Cancellable? cancellable = null) throws Error
Unmounts a file of type g_file_type_mountable
.