module Gio::File
Overview
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 Gio::InputStream
and Gio::OutputStream
).
To construct a GFile
, you can use:
Gio::File#new_for_path
if you have a path.Gio::File#new_for_uri
if you have a URI.Gio::File#new_for_commandline_arg
orGio::File#new_for_commandline_arg_and_cwd
for a command line argument.Gio::File#new_tmp
to create a temporary file from a template.Gio::File#new_tmp_async
to asynchronously create a temporary file.Gio::File#new_tmp_dir_async
to asynchronously create a temporary directory.Gio::File#parse_name
from a UTF-8 string gotten fromGio::File#parse_name
.Gio::File#new_build_filename
orGio::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 Gio::File#parent
to get an identifier for the
parent directory, Gio::File#child
to get a child within a
directory, and 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 Gio::File#parent
on two different files.
All GFile
s have a basename (get with Gio::File#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
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 Gio::AsyncReadyCallback
which is then used to
finalize the operation, producing a 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 for more.
Some GFile
operations almost always take a noticeable amount of time, and
so do not have synchronous analogs. Notable cases include:
Gio::File#mount_mountable
to mount a mountable file.Gio::File#unmount_mountable_with_operation
to unmount a mountable file.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.
Defined in:
lib/gi-crystal/src/auto/gio-2.0/file.crConstructors
-
.cast(obj : GObject::Object) : self
Cast a
GObject::Object
toself
, throws aTypeCastError
if the cast can't be made.
Class Method Summary
- .cast?(obj : GObject::Object) : self | Nil
- .g_type : UInt64
- .new_build_filenamev(args : Enumerable(String)) : Gio::File
- .new_build_filenamev(*args : String)
- .new_for_commandline_arg(arg : String) : Gio::File
- .new_for_commandline_arg_and_cwd(arg : String, cwd : String) : Gio::File
- .new_for_path(path : String) : Gio::File
- .new_for_uri(uri : String) : Gio::File
- .new_tmp_async(tmpl : String | Nil, io_priority : Int32, cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil
- .new_tmp_dir_async(tmpl : String | Nil, io_priority : Int32, cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil
- .parse_name(parse_name : String) : Gio::File
Instance Method Summary
- #append_to(flags : Gio::FileCreateFlags, cancellable : Gio::Cancellable | Nil) : Gio::FileOutputStream
- #append_to_async(flags : Gio::FileCreateFlags, io_priority : Int32, cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil
- #append_to_finish(res : Gio::AsyncResult) : Gio::FileOutputStream
- #basename : Path | Nil
- #build_attribute_list_for_copy(flags : Gio::FileCopyFlags, cancellable : Gio::Cancellable | Nil) : String
- #child(name : String) : Gio::File
- #child_for_display_name(display_name : String) : Gio::File
- #copy(destination : Gio::File, flags : Gio::FileCopyFlags, cancellable : Gio::Cancellable | Nil, progress_callback : Gio::FileProgressCallback | Nil, progress_callback_data : Pointer(Void) | Nil) : Bool
- #copy_async(destination : Gio::File, flags : Gio::FileCopyFlags, io_priority : Int32, cancellable : Gio::Cancellable | Nil, progress_callback_closure : GObject::Closure | Nil, ready_callback_closure : GObject::Closure) : Nil
- #copy_attributes(destination : Gio::File, flags : Gio::FileCopyFlags, cancellable : Gio::Cancellable | Nil) : Bool
- #copy_finish(res : Gio::AsyncResult) : Bool
- #create(flags : Gio::FileCreateFlags, cancellable : Gio::Cancellable | Nil) : Gio::FileOutputStream
- #create_async(flags : Gio::FileCreateFlags, io_priority : Int32, cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil
- #create_finish(res : Gio::AsyncResult) : Gio::FileOutputStream
- #create_readwrite(flags : Gio::FileCreateFlags, cancellable : Gio::Cancellable | Nil) : Gio::FileIOStream
- #create_readwrite_async(flags : Gio::FileCreateFlags, io_priority : Int32, cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil
- #create_readwrite_finish(res : Gio::AsyncResult) : Gio::FileIOStream
- #delete(cancellable : Gio::Cancellable | Nil) : Bool
- #delete_async(io_priority : Int32, cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil
- #delete_finish(result : Gio::AsyncResult) : Bool
- #display_name_finish=(res : Gio::AsyncResult) : Gio::File
- #dup : Gio::File
-
#eject_mountable(flags : Gio::MountUnmountFlags, cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil
DEPRECATED
-
#eject_mountable_finish(result : Gio::AsyncResult) : Bool
DEPRECATED
- #eject_mountable_with_operation(flags : Gio::MountUnmountFlags, mount_operation : Gio::MountOperation | Nil, cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil
- #eject_mountable_with_operation_finish(result : Gio::AsyncResult) : Bool
- #enumerate_children(attributes : String, flags : Gio::FileQueryInfoFlags, cancellable : Gio::Cancellable | Nil) : Gio::FileEnumerator
- #enumerate_children_async(attributes : String, flags : Gio::FileQueryInfoFlags, io_priority : Int32, cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil
- #enumerate_children_finish(res : Gio::AsyncResult) : Gio::FileEnumerator
- #equal(file2 : Gio::File) : Bool
- #find_enclosing_mount(cancellable : Gio::Cancellable | Nil) : Gio::Mount
- #find_enclosing_mount_async(io_priority : Int32, cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil
- #find_enclosing_mount_finish(res : Gio::AsyncResult) : Gio::Mount
- #has_parent(parent : Gio::File | Nil) : Bool
- #has_prefix(prefix : Gio::File) : Bool
- #has_uri_scheme(uri_scheme : String) : Bool
- #hash : UInt32
- #is_native : Bool
- #load_bytes(cancellable : Gio::Cancellable | Nil) : GLib::Bytes
- #load_bytes_async(cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil
- #load_bytes_finish(result : Gio::AsyncResult) : GLib::Bytes
- #load_contents(cancellable : Gio::Cancellable | Nil, contents : Bytes) : Bool
- #load_contents_async(cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil
- #load_contents_finish(res : Gio::AsyncResult, contents : Bytes) : Bool
- #load_partial_contents_finish(res : Gio::AsyncResult, contents : Bytes) : Bool
- #make_directory(cancellable : Gio::Cancellable | Nil) : Bool
- #make_directory_async(io_priority : Int32, cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil
- #make_directory_finish(result : Gio::AsyncResult) : Bool
- #make_directory_with_parents(cancellable : Gio::Cancellable | Nil) : Bool
- #make_symbolic_link(symlink_value : String, cancellable : Gio::Cancellable | Nil) : Bool
- #make_symbolic_link_async(symlink_value : String, io_priority : Int32, cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil
- #make_symbolic_link_finish(result : Gio::AsyncResult) : Bool
- #measure_disk_usage(flags : Gio::FileMeasureFlags, cancellable : Gio::Cancellable | Nil, progress_callback : Gio::FileMeasureProgressCallback | Nil, progress_data : Pointer(Void) | Nil) : Bool
- #measure_disk_usage_finish(result : Gio::AsyncResult) : Bool
- #monitor(flags : Gio::FileMonitorFlags, cancellable : Gio::Cancellable | Nil) : Gio::FileMonitor
- #monitor_directory(flags : Gio::FileMonitorFlags, cancellable : Gio::Cancellable | Nil) : Gio::FileMonitor
- #monitor_file(flags : Gio::FileMonitorFlags, cancellable : Gio::Cancellable | Nil) : Gio::FileMonitor
- #mount_enclosing_volume(flags : Gio::MountMountFlags, mount_operation : Gio::MountOperation | Nil, cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil
- #mount_enclosing_volume_finish(result : Gio::AsyncResult) : Bool
- #mount_mountable(flags : Gio::MountMountFlags, mount_operation : Gio::MountOperation | Nil, cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil
- #mount_mountable_finish(result : Gio::AsyncResult) : Gio::File
- #move(destination : Gio::File, flags : Gio::FileCopyFlags, cancellable : Gio::Cancellable | Nil, progress_callback : Gio::FileProgressCallback | Nil, progress_callback_data : Pointer(Void) | Nil) : Bool
- #move_async(destination : Gio::File, flags : Gio::FileCopyFlags, io_priority : Int32, cancellable : Gio::Cancellable | Nil, progress_callback_closure : GObject::Closure | Nil, ready_callback_closure : GObject::Closure) : Nil
- #move_finish(result : Gio::AsyncResult) : Bool
- #new_tmp(tmpl : String | Nil, iostream : Gio::FileIOStream) : Gio::File
- #new_tmp_dir_finish(result : Gio::AsyncResult) : Gio::File
- #new_tmp_finish(result : Gio::AsyncResult, iostream : Gio::FileIOStream) : Gio::File
- #open_readwrite(cancellable : Gio::Cancellable | Nil) : Gio::FileIOStream
- #open_readwrite_async(io_priority : Int32, cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil
- #open_readwrite_finish(res : Gio::AsyncResult) : Gio::FileIOStream
- #parent : Gio::File | Nil
- #parse_name : String
- #path : Path | Nil
- #peek_path : Path | Nil
- #poll_mountable(cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil
- #poll_mountable_finish(result : Gio::AsyncResult) : Bool
- #query_default_handler(cancellable : Gio::Cancellable | Nil) : Gio::AppInfo
- #query_default_handler_async(io_priority : Int32, cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil
- #query_default_handler_finish(result : Gio::AsyncResult) : Gio::AppInfo
- #query_exists(cancellable : Gio::Cancellable | Nil) : Bool
- #query_file_type(flags : Gio::FileQueryInfoFlags, cancellable : Gio::Cancellable | Nil) : Gio::FileType
- #query_filesystem_info(attributes : String, cancellable : Gio::Cancellable | Nil) : Gio::FileInfo
- #query_filesystem_info_async(attributes : String, io_priority : Int32, cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil
- #query_filesystem_info_finish(res : Gio::AsyncResult) : Gio::FileInfo
- #query_info(attributes : String, flags : Gio::FileQueryInfoFlags, cancellable : Gio::Cancellable | Nil) : Gio::FileInfo
- #query_info_async(attributes : String, flags : Gio::FileQueryInfoFlags, io_priority : Int32, cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil
- #query_info_finish(res : Gio::AsyncResult) : Gio::FileInfo
- #query_settable_attributes(cancellable : Gio::Cancellable | Nil) : Gio::FileAttributeInfoList
- #query_writable_namespaces(cancellable : Gio::Cancellable | Nil) : Gio::FileAttributeInfoList
- #read(cancellable : Gio::Cancellable | Nil) : Gio::FileInputStream
- #read_async(io_priority : Int32, cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil
- #read_finish(res : Gio::AsyncResult) : Gio::FileInputStream
- #relative_path(descendant : Gio::File) : Path | Nil
- #replace(etag : String | Nil, make_backup : Bool, flags : Gio::FileCreateFlags, cancellable : Gio::Cancellable | Nil) : Gio::FileOutputStream
- #replace_async(etag : String | Nil, make_backup : Bool, flags : Gio::FileCreateFlags, io_priority : Int32, cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil
- #replace_contents(contents : Bytes, etag : String | Nil, make_backup : Bool, flags : Gio::FileCreateFlags, cancellable : Gio::Cancellable | Nil) : Bool
- #replace_contents_async(contents : Bytes, etag : String | Nil, make_backup : Bool, flags : Gio::FileCreateFlags, cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil
- #replace_contents_bytes_async(contents : GLib::Bytes, etag : String | Nil, make_backup : Bool, flags : Gio::FileCreateFlags, cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil
- #replace_contents_finish(res : Gio::AsyncResult) : Bool
- #replace_finish(res : Gio::AsyncResult) : Gio::FileOutputStream
- #replace_readwrite(etag : String | Nil, make_backup : Bool, flags : Gio::FileCreateFlags, cancellable : Gio::Cancellable | Nil) : Gio::FileIOStream
- #replace_readwrite_async(etag : String | Nil, make_backup : Bool, flags : Gio::FileCreateFlags, io_priority : Int32, cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil
- #replace_readwrite_finish(res : Gio::AsyncResult) : Gio::FileIOStream
- #resolve_relative_path(relative_path : String) : Gio::File
- #set_attribute(attribute : String, type : Gio::FileAttributeType, value_p : Pointer(Void) | Nil, flags : Gio::FileQueryInfoFlags, cancellable : Gio::Cancellable | Nil) : Bool
- #set_attribute_byte_string(attribute : String, value : String, flags : Gio::FileQueryInfoFlags, cancellable : Gio::Cancellable | Nil) : Bool
- #set_attribute_int32(attribute : String, value : Int32, flags : Gio::FileQueryInfoFlags, cancellable : Gio::Cancellable | Nil) : Bool
- #set_attribute_int64(attribute : String, value : Int64, flags : Gio::FileQueryInfoFlags, cancellable : Gio::Cancellable | Nil) : Bool
- #set_attribute_string(attribute : String, value : String, flags : Gio::FileQueryInfoFlags, cancellable : Gio::Cancellable | Nil) : Bool
- #set_attribute_uint32(attribute : String, value : UInt32, flags : Gio::FileQueryInfoFlags, cancellable : Gio::Cancellable | Nil) : Bool
- #set_attribute_uint64(attribute : String, value : UInt64, flags : Gio::FileQueryInfoFlags, cancellable : Gio::Cancellable | Nil) : Bool
- #set_attributes_async(info : Gio::FileInfo, flags : Gio::FileQueryInfoFlags, io_priority : Int32, cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil
- #set_attributes_finish(result : Gio::AsyncResult, info : Gio::FileInfo) : Bool
- #set_attributes_from_info(info : Gio::FileInfo, flags : Gio::FileQueryInfoFlags, cancellable : Gio::Cancellable | Nil) : Bool
- #set_display_name(display_name : String, cancellable : Gio::Cancellable | Nil) : Gio::File
- #set_display_name_async(display_name : String, io_priority : Int32, cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil
- #start_mountable(flags : Gio::DriveStartFlags, start_operation : Gio::MountOperation | Nil, cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil
- #start_mountable_finish(result : Gio::AsyncResult) : Bool
- #stop_mountable(flags : Gio::MountUnmountFlags, mount_operation : Gio::MountOperation | Nil, cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil
- #stop_mountable_finish(result : Gio::AsyncResult) : Bool
- #supports_thread_contexts : Bool
- #to_unsafe
- #trash(cancellable : Gio::Cancellable | Nil) : Bool
- #trash_async(io_priority : Int32, cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil
- #trash_finish(result : Gio::AsyncResult) : Bool
-
#unmount_mountable(flags : Gio::MountUnmountFlags, cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil
DEPRECATED
-
#unmount_mountable_finish(result : Gio::AsyncResult) : Bool
DEPRECATED
- #unmount_mountable_with_operation(flags : Gio::MountUnmountFlags, mount_operation : Gio::MountOperation | Nil, cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil
- #unmount_mountable_with_operation_finish(result : Gio::AsyncResult) : Bool
- #uri : String
- #uri_scheme : String | Nil
Constructor Detail
Class Method Detail
Instance Method Detail
DEPRECATED
DEPRECATED