module Gio::File

Overview

#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:

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:

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 for HTTP Etag headers, which are a very similar concept.

Defined in:

lib/gi-crystal/src/auto/gio-2.0/file.cr

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.cast(obj : GObject::Object) : self #

Cast a GObject::Object to self, throws a TypeCastError if the cast can't be made.


[View source]

Class Method Detail

def self.cast?(obj : GObject::Object) : self | Nil #

[View source]
def self.g_type : UInt64 #

[View source]
def self.new_for_commandline_arg(arg : String) : Gio::File #

[View source]
def self.new_for_commandline_arg_and_cwd(arg : String, cwd : String) : Gio::File #

[View source]
def self.new_for_path(path : String) : Gio::File #

[View source]
def self.new_for_uri(uri : String) : Gio::File #

[View source]
def self.parse_name(parse_name : String) : Gio::File #

[View source]

Instance Method Detail

def append_to(flags : Gio::FileCreateFlags, cancellable : Gio::Cancellable | Nil) : Gio::FileOutputStream #

[View source]
def append_to_async(flags : Gio::FileCreateFlags, io_priority : Int32, cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil #

[View source]
def append_to_finish(res : Gio::AsyncResult) : Gio::FileOutputStream #

[View source]
def basename : Path | Nil #

[View source]
def build_attribute_list_for_copy(flags : Gio::FileCopyFlags, cancellable : Gio::Cancellable | Nil) : String #

[View source]
def child(name : String) : Gio::File #

[View source]
def child_for_display_name(display_name : String) : Gio::File #

[View source]
def copy(destination : Gio::File, flags : Gio::FileCopyFlags, cancellable : Gio::Cancellable | Nil, progress_callback : Gio::FileProgressCallback | Nil, progress_callback_data : Pointer(Void) | Nil) : Bool #

[View source]
def copy_async(destination : Gio::File, flags : Gio::FileCopyFlags, io_priority : Int32, cancellable : Gio::Cancellable | Nil, progress_callback : Gio::FileProgressCallback | Nil, progress_callback_data : Pointer(Void) | Nil, &callback : Gio::AsyncReadyCallback) : Nil #

[View source]
def copy_attributes(destination : Gio::File, flags : Gio::FileCopyFlags, cancellable : Gio::Cancellable | Nil) : Bool #

[View source]
def copy_finish(res : Gio::AsyncResult) : Bool #

[View source]
def create(flags : Gio::FileCreateFlags, cancellable : Gio::Cancellable | Nil) : Gio::FileOutputStream #

[View source]
def create_async(flags : Gio::FileCreateFlags, io_priority : Int32, cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil #

[View source]
def create_finish(res : Gio::AsyncResult) : Gio::FileOutputStream #

[View source]
def create_readwrite(flags : Gio::FileCreateFlags, cancellable : Gio::Cancellable | Nil) : Gio::FileIOStream #

[View source]
def create_readwrite_async(flags : Gio::FileCreateFlags, io_priority : Int32, cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil #

[View source]
def create_readwrite_finish(res : Gio::AsyncResult) : Gio::FileIOStream #

[View source]
def delete(cancellable : Gio::Cancellable | Nil) : Bool #

[View source]
def delete_async(io_priority : Int32, cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil #

[View source]
def delete_finish(result : Gio::AsyncResult) : Bool #

[View source]
def display_name_finish=(res : Gio::AsyncResult) : Gio::File #

[View source]
def dup : Gio::File #

[View source]
def eject_mountable(flags : Gio::MountUnmountFlags, cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil #

DEPRECATED


[View source]
def eject_mountable_finish(result : Gio::AsyncResult) : Bool #

DEPRECATED


[View source]
def eject_mountable_with_operation(flags : Gio::MountUnmountFlags, mount_operation : Gio::MountOperation | Nil, cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil #

[View source]
def eject_mountable_with_operation_finish(result : Gio::AsyncResult) : Bool #

[View source]
def enumerate_children(attributes : String, flags : Gio::FileQueryInfoFlags, cancellable : Gio::Cancellable | Nil) : Gio::FileEnumerator #

[View source]
def enumerate_children_async(attributes : String, flags : Gio::FileQueryInfoFlags, io_priority : Int32, cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil #

[View source]
def enumerate_children_finish(res : Gio::AsyncResult) : Gio::FileEnumerator #

[View source]
def equal(file2 : Gio::File) : Bool #

[View source]
def find_enclosing_mount(cancellable : Gio::Cancellable | Nil) : Gio::Mount #

[View source]
def find_enclosing_mount_async(io_priority : Int32, cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil #

[View source]
def find_enclosing_mount_finish(res : Gio::AsyncResult) : Gio::Mount #

[View source]
def has_parent(parent : Gio::File | Nil) : Bool #

[View source]
def has_prefix(prefix : Gio::File) : Bool #

[View source]
def has_uri_scheme(uri_scheme : String) : Bool #

[View source]
def hash : UInt32 #

[View source]
def is_native : Bool #

[View source]
def load_bytes(cancellable : Gio::Cancellable | Nil) : GLib::Bytes #

[View source]
def load_bytes_async(cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil #

[View source]
def load_bytes_finish(result : Gio::AsyncResult) : GLib::Bytes #

[View source]
def load_contents(cancellable : Gio::Cancellable | Nil, contents : Bytes) : Bool #

[View source]
def load_contents_async(cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil #

[View source]
def load_contents_finish(res : Gio::AsyncResult, contents : Bytes) : Bool #

[View source]
def load_partial_contents_finish(res : Gio::AsyncResult, contents : Bytes) : Bool #

[View source]
def make_directory(cancellable : Gio::Cancellable | Nil) : Bool #

[View source]
def make_directory_async(io_priority : Int32, cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil #

[View source]
def make_directory_finish(result : Gio::AsyncResult) : Bool #

[View source]
def make_directory_with_parents(cancellable : Gio::Cancellable | Nil) : Bool #

[View source]
def make_symbolic_link(symlink_value : String, cancellable : Gio::Cancellable | Nil) : Bool #

[View source]
def measure_disk_usage_finish(result : Gio::AsyncResult) : Bool #

[View source]
def monitor(flags : Gio::FileMonitorFlags, cancellable : Gio::Cancellable | Nil) : Gio::FileMonitor #

[View source]
def monitor_directory(flags : Gio::FileMonitorFlags, cancellable : Gio::Cancellable | Nil) : Gio::FileMonitor #

[View source]
def monitor_file(flags : Gio::FileMonitorFlags, cancellable : Gio::Cancellable | Nil) : Gio::FileMonitor #

[View source]
def mount_enclosing_volume(flags : Gio::MountMountFlags, mount_operation : Gio::MountOperation | Nil, cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil #

[View source]
def mount_enclosing_volume_finish(result : Gio::AsyncResult) : Bool #

[View source]
def mount_mountable(flags : Gio::MountMountFlags, mount_operation : Gio::MountOperation | Nil, cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil #

[View source]
def mount_mountable_finish(result : Gio::AsyncResult) : Gio::File #

[View source]
def move(destination : Gio::File, flags : Gio::FileCopyFlags, cancellable : Gio::Cancellable | Nil, progress_callback : Gio::FileProgressCallback | Nil, progress_callback_data : Pointer(Void) | Nil) : Bool #

[View source]
def move_async(destination : Gio::File, flags : Gio::FileCopyFlags, io_priority : Int32, cancellable : Gio::Cancellable | Nil, progress_callback : Gio::FileProgressCallback | Nil, progress_callback_data : Pointer(Void) | Nil, &callback : Gio::AsyncReadyCallback) : Nil #

[View source]
def move_finish(result : Gio::AsyncResult) : Bool #

[View source]
def new_tmp(tmpl : String | Nil, iostream : Gio::FileIOStream) : Gio::File #

[View source]
def open_readwrite(cancellable : Gio::Cancellable | Nil) : Gio::FileIOStream #

[View source]
def open_readwrite_async(io_priority : Int32, cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil #

[View source]
def open_readwrite_finish(res : Gio::AsyncResult) : Gio::FileIOStream #

[View source]
def parent : Gio::File | Nil #

[View source]
def parse_name : String #

[View source]
def path : Path | Nil #

[View source]
def peek_path : Path | Nil #

[View source]
def poll_mountable(cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil #

[View source]
def poll_mountable_finish(result : Gio::AsyncResult) : Bool #

[View source]
def query_default_handler(cancellable : Gio::Cancellable | Nil) : Gio::AppInfo #

[View source]
def query_default_handler_async(io_priority : Int32, cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil #

[View source]
def query_default_handler_finish(result : Gio::AsyncResult) : Gio::AppInfo #

[View source]
def query_exists(cancellable : Gio::Cancellable | Nil) : Bool #

[View source]
def query_file_type(flags : Gio::FileQueryInfoFlags, cancellable : Gio::Cancellable | Nil) : Gio::FileType #

[View source]
def query_filesystem_info(attributes : String, cancellable : Gio::Cancellable | Nil) : Gio::FileInfo #

[View source]
def query_filesystem_info_async(attributes : String, io_priority : Int32, cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil #

[View source]
def query_filesystem_info_finish(res : Gio::AsyncResult) : Gio::FileInfo #

[View source]
def query_info(attributes : String, flags : Gio::FileQueryInfoFlags, cancellable : Gio::Cancellable | Nil) : Gio::FileInfo #

[View source]
def query_info_async(attributes : String, flags : Gio::FileQueryInfoFlags, io_priority : Int32, cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil #

[View source]
def query_info_finish(res : Gio::AsyncResult) : Gio::FileInfo #

[View source]
def query_settable_attributes(cancellable : Gio::Cancellable | Nil) : Gio::FileAttributeInfoList #

[View source]
def query_writable_namespaces(cancellable : Gio::Cancellable | Nil) : Gio::FileAttributeInfoList #

[View source]
def read(cancellable : Gio::Cancellable | Nil) : Gio::FileInputStream #

[View source]
def read_async(io_priority : Int32, cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil #

[View source]
def read_finish(res : Gio::AsyncResult) : Gio::FileInputStream #

[View source]
def relative_path(descendant : Gio::File) : Path | Nil #

[View source]
def replace(etag : String | Nil, make_backup : Bool, flags : Gio::FileCreateFlags, cancellable : Gio::Cancellable | Nil) : Gio::FileOutputStream #

[View source]
def replace_async(etag : String | Nil, make_backup : Bool, flags : Gio::FileCreateFlags, io_priority : Int32, cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil #

[View source]
def replace_contents(contents : Bytes, etag : String | Nil, make_backup : Bool, flags : Gio::FileCreateFlags, cancellable : Gio::Cancellable | Nil) : Bool #

[View source]
def replace_contents_async(contents : Bytes, etag : String | Nil, make_backup : Bool, flags : Gio::FileCreateFlags, cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil #

[View source]
def replace_contents_bytes_async(contents : GLib::Bytes, etag : String | Nil, make_backup : Bool, flags : Gio::FileCreateFlags, cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil #

[View source]
def replace_contents_finish(res : Gio::AsyncResult) : Bool #

[View source]
def replace_finish(res : Gio::AsyncResult) : Gio::FileOutputStream #

[View source]
def replace_readwrite(etag : String | Nil, make_backup : Bool, flags : Gio::FileCreateFlags, cancellable : Gio::Cancellable | Nil) : Gio::FileIOStream #

[View source]
def replace_readwrite_async(etag : String | Nil, make_backup : Bool, flags : Gio::FileCreateFlags, io_priority : Int32, cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil #

[View source]
def replace_readwrite_finish(res : Gio::AsyncResult) : Gio::FileIOStream #

[View source]
def resolve_relative_path(relative_path : String) : Gio::File #

[View source]
def set_attribute(attribute : String, type : Gio::FileAttributeType, value_p : Pointer(Void) | Nil, flags : Gio::FileQueryInfoFlags, cancellable : Gio::Cancellable | Nil) : Bool #

[View source]
def set_attribute_byte_string(attribute : String, value : String, flags : Gio::FileQueryInfoFlags, cancellable : Gio::Cancellable | Nil) : Bool #

[View source]
def set_attribute_int32(attribute : String, value : Int32, flags : Gio::FileQueryInfoFlags, cancellable : Gio::Cancellable | Nil) : Bool #

[View source]
def set_attribute_int64(attribute : String, value : Int64, flags : Gio::FileQueryInfoFlags, cancellable : Gio::Cancellable | Nil) : Bool #

[View source]
def set_attribute_string(attribute : String, value : String, flags : Gio::FileQueryInfoFlags, cancellable : Gio::Cancellable | Nil) : Bool #

[View source]
def set_attribute_uint32(attribute : String, value : UInt32, flags : Gio::FileQueryInfoFlags, cancellable : Gio::Cancellable | Nil) : Bool #

[View source]
def set_attribute_uint64(attribute : String, value : UInt64, flags : Gio::FileQueryInfoFlags, cancellable : Gio::Cancellable | Nil) : Bool #

[View source]
def set_attributes_async(info : Gio::FileInfo, flags : Gio::FileQueryInfoFlags, io_priority : Int32, cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil #

[View source]
def set_attributes_finish(result : Gio::AsyncResult, info : Gio::FileInfo) : Bool #

[View source]
def set_attributes_from_info(info : Gio::FileInfo, flags : Gio::FileQueryInfoFlags, cancellable : Gio::Cancellable | Nil) : Bool #

[View source]
def set_display_name(display_name : String, cancellable : Gio::Cancellable | Nil) : Gio::File #

[View source]
def set_display_name_async(display_name : String, io_priority : Int32, cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil #

[View source]
def start_mountable(flags : Gio::DriveStartFlags, start_operation : Gio::MountOperation | Nil, cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil #

[View source]
def start_mountable_finish(result : Gio::AsyncResult) : Bool #

[View source]
def stop_mountable(flags : Gio::MountUnmountFlags, mount_operation : Gio::MountOperation | Nil, cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil #

[View source]
def stop_mountable_finish(result : Gio::AsyncResult) : Bool #

[View source]
def supports_thread_contexts : Bool #

[View source]
abstract def to_unsafe #

[View source]
def trash(cancellable : Gio::Cancellable | Nil) : Bool #

[View source]
def trash_async(io_priority : Int32, cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil #

[View source]
def trash_finish(result : Gio::AsyncResult) : Bool #

[View source]
def unmount_mountable(flags : Gio::MountUnmountFlags, cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil #

DEPRECATED


[View source]
def unmount_mountable_finish(result : Gio::AsyncResult) : Bool #

DEPRECATED


[View source]
def unmount_mountable_with_operation(flags : Gio::MountUnmountFlags, mount_operation : Gio::MountOperation | Nil, cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil #

[View source]
def unmount_mountable_with_operation_finish(result : Gio::AsyncResult) : Bool #

[View source]
def uri : String #

[View source]
def uri_scheme : String | Nil #

[View source]