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

Class Method Summary

Instance Method Summary

Class Method Detail

def self.g_type : UInt64 #

def self.new_for_commandline_arg(arg : String) : Gio::File #

def self.new_for_commandline_arg_and_cwd(arg : String, cwd : String) : Gio::File #

def self.new_for_path(path : String) : Gio::File #

def self.new_for_uri(uri : String) : Gio::File #

def self.parse_name(parse_name : String) : Gio::File #

Instance Method Detail

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

def append_to_async(flags : Gio::FileCreateFlags, io_priority : Int32, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil #

def append_to_finish(res : Gio::AsyncResult) : Gio::FileOutputStream #

def basename : Path? #

def build_attribute_list_for_copy(flags : Gio::FileCopyFlags, cancellable : Gio::Cancellable?) : String #

def child(name : String) : Gio::File #

def child_for_display_name(display_name : String) : Gio::File #

def copy(destination : Gio::File, flags : Gio::FileCopyFlags, cancellable : Gio::Cancellable?, progress_callback : Gio::FileProgressCallback?, progress_callback_data : Pointer(Void)?) : Bool #

def copy_async(destination : Gio::File, flags : Gio::FileCopyFlags, io_priority : Int32, cancellable : Gio::Cancellable?, progress_callback : Gio::FileProgressCallback?, progress_callback_data : Pointer(Void)?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil #

def copy_attributes(destination : Gio::File, flags : Gio::FileCopyFlags, cancellable : Gio::Cancellable?) : Bool #

def copy_finish(res : Gio::AsyncResult) : Bool #

def create(flags : Gio::FileCreateFlags, cancellable : Gio::Cancellable?) : Gio::FileOutputStream #

def create_async(flags : Gio::FileCreateFlags, io_priority : Int32, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil #

def create_finish(res : Gio::AsyncResult) : Gio::FileOutputStream #

def create_readwrite(flags : Gio::FileCreateFlags, cancellable : Gio::Cancellable?) : Gio::FileIOStream #

def create_readwrite_async(flags : Gio::FileCreateFlags, io_priority : Int32, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil #

def create_readwrite_finish(res : Gio::AsyncResult) : Gio::FileIOStream #

def delete(cancellable : Gio::Cancellable?) : Bool #

def delete_async(io_priority : Int32, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil #

def delete_finish(result : Gio::AsyncResult) : Bool #

def display_name_finish=(res : Gio::AsyncResult) : Gio::File #

def dup : Gio::File #

def eject_mountable(flags : Gio::MountUnmountFlags, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil #

def eject_mountable_finish(result : Gio::AsyncResult) : Bool #

def eject_mountable_with_operation(flags : Gio::MountUnmountFlags, mount_operation : Gio::MountOperation?, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil #

def eject_mountable_with_operation_finish(result : Gio::AsyncResult) : Bool #

def enumerate_children(attributes : String, flags : Gio::FileQueryInfoFlags, cancellable : Gio::Cancellable?) : Gio::FileEnumerator #

def enumerate_children_async(attributes : String, flags : Gio::FileQueryInfoFlags, io_priority : Int32, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil #

def enumerate_children_finish(res : Gio::AsyncResult) : Gio::FileEnumerator #

def equal(file2 : Gio::File) : Bool #

def find_enclosing_mount(cancellable : Gio::Cancellable?) : Gio::Mount #

def find_enclosing_mount_async(io_priority : Int32, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil #

def find_enclosing_mount_finish(res : Gio::AsyncResult) : Gio::Mount #

def has_parent(parent : Gio::File?) : Bool #

def has_prefix(prefix : Gio::File) : Bool #

def has_uri_scheme(uri_scheme : String) : Bool #

def hash : UInt32 #

def is_native : Bool #

def load_bytes(cancellable : Gio::Cancellable?) : GLib::Bytes #

def load_bytes_async(cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil #

def load_bytes_finish(result : Gio::AsyncResult) : GLib::Bytes #

def load_contents(cancellable : Gio::Cancellable?, contents : Enumerable(UInt8)) : Bool #

def load_contents_async(cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil #

def load_contents_finish(res : Gio::AsyncResult, contents : Enumerable(UInt8)) : Bool #

def load_partial_contents_finish(res : Gio::AsyncResult, contents : Enumerable(UInt8)) : Bool #

def make_directory(cancellable : Gio::Cancellable?) : Bool #

def make_directory_async(io_priority : Int32, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil #

def make_directory_finish(result : Gio::AsyncResult) : Bool #

def make_directory_with_parents(cancellable : Gio::Cancellable?) : Bool #

def make_symbolic_link(symlink_value : String, cancellable : Gio::Cancellable?) : Bool #

def measure_disk_usage_finish(result : Gio::AsyncResult) : Bool #

def monitor(flags : Gio::FileMonitorFlags, cancellable : Gio::Cancellable?) : Gio::FileMonitor #

def monitor_directory(flags : Gio::FileMonitorFlags, cancellable : Gio::Cancellable?) : Gio::FileMonitor #

def monitor_file(flags : Gio::FileMonitorFlags, cancellable : Gio::Cancellable?) : Gio::FileMonitor #

def mount_enclosing_volume(flags : Gio::MountMountFlags, mount_operation : Gio::MountOperation?, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil #

def mount_enclosing_volume_finish(result : Gio::AsyncResult) : Bool #

def mount_mountable(flags : Gio::MountMountFlags, mount_operation : Gio::MountOperation?, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil #

def mount_mountable_finish(result : Gio::AsyncResult) : Gio::File #

def move(destination : Gio::File, flags : Gio::FileCopyFlags, cancellable : Gio::Cancellable?, progress_callback : Gio::FileProgressCallback?, progress_callback_data : Pointer(Void)?) : Bool #

def move_async(destination : Gio::File, flags : Gio::FileCopyFlags, io_priority : Int32, cancellable : Gio::Cancellable?, progress_callback : Gio::FileProgressCallback?, progress_callback_data : Pointer(Void)?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil #

def move_finish(result : Gio::AsyncResult) : Bool #

def new_tmp(tmpl : String?, iostream : Gio::FileIOStream) : Gio::File #

def open_readwrite(cancellable : Gio::Cancellable?) : Gio::FileIOStream #

def open_readwrite_async(io_priority : Int32, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil #

def open_readwrite_finish(res : Gio::AsyncResult) : Gio::FileIOStream #

def parent : Gio::File? #

def parse_name : String #

def path : Path? #

def peek_path : Path? #

def poll_mountable(cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil #

def poll_mountable_finish(result : Gio::AsyncResult) : Bool #

def query_default_handler(cancellable : Gio::Cancellable?) : Gio::AppInfo #

def query_default_handler_async(io_priority : Int32, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil #

def query_default_handler_finish(result : Gio::AsyncResult) : Gio::AppInfo #

def query_exists(cancellable : Gio::Cancellable?) : Bool #

def query_file_type(flags : Gio::FileQueryInfoFlags, cancellable : Gio::Cancellable?) : Gio::FileType #

def query_filesystem_info(attributes : String, cancellable : Gio::Cancellable?) : Gio::FileInfo #

def query_filesystem_info_async(attributes : String, io_priority : Int32, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil #

def query_filesystem_info_finish(res : Gio::AsyncResult) : Gio::FileInfo #

def query_info(attributes : String, flags : Gio::FileQueryInfoFlags, cancellable : Gio::Cancellable?) : Gio::FileInfo #

def query_info_async(attributes : String, flags : Gio::FileQueryInfoFlags, io_priority : Int32, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil #

def query_info_finish(res : Gio::AsyncResult) : Gio::FileInfo #

def query_settable_attributes(cancellable : Gio::Cancellable?) : Gio::FileAttributeInfoList #

def query_writable_namespaces(cancellable : Gio::Cancellable?) : Gio::FileAttributeInfoList #

def read(cancellable : Gio::Cancellable?) : Gio::FileInputStream #

def read_async(io_priority : Int32, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil #

def read_finish(res : Gio::AsyncResult) : Gio::FileInputStream #

def relative_path(descendant : Gio::File) : Path? #

def replace(etag : String?, make_backup : Bool, flags : Gio::FileCreateFlags, cancellable : Gio::Cancellable?) : Gio::FileOutputStream #

def replace_async(etag : String?, make_backup : Bool, flags : Gio::FileCreateFlags, io_priority : Int32, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil #

def replace_contents(contents : Enumerable(UInt8), etag : String?, make_backup : Bool, flags : Gio::FileCreateFlags, cancellable : Gio::Cancellable?) : Bool #

def replace_contents_async(contents : Enumerable(UInt8), etag : String?, make_backup : Bool, flags : Gio::FileCreateFlags, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil #

def replace_contents_bytes_async(contents : GLib::Bytes, etag : String?, make_backup : Bool, flags : Gio::FileCreateFlags, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil #

def replace_contents_finish(res : Gio::AsyncResult) : Bool #

def replace_finish(res : Gio::AsyncResult) : Gio::FileOutputStream #

def replace_readwrite(etag : String?, make_backup : Bool, flags : Gio::FileCreateFlags, cancellable : Gio::Cancellable?) : Gio::FileIOStream #

def replace_readwrite_async(etag : String?, make_backup : Bool, flags : Gio::FileCreateFlags, io_priority : Int32, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil #

def replace_readwrite_finish(res : Gio::AsyncResult) : Gio::FileIOStream #

def resolve_relative_path(relative_path : String) : Gio::File #

def set_attribute(attribute : String, type : Gio::FileAttributeType, value_p : Pointer(Void)?, flags : Gio::FileQueryInfoFlags, cancellable : Gio::Cancellable?) : Bool #

def set_attribute_byte_string(attribute : String, value : String, flags : Gio::FileQueryInfoFlags, cancellable : Gio::Cancellable?) : Bool #

def set_attribute_int32(attribute : String, value : Int32, flags : Gio::FileQueryInfoFlags, cancellable : Gio::Cancellable?) : Bool #

def set_attribute_int64(attribute : String, value : Int64, flags : Gio::FileQueryInfoFlags, cancellable : Gio::Cancellable?) : Bool #

def set_attribute_string(attribute : String, value : String, flags : Gio::FileQueryInfoFlags, cancellable : Gio::Cancellable?) : Bool #

def set_attribute_uint32(attribute : String, value : UInt32, flags : Gio::FileQueryInfoFlags, cancellable : Gio::Cancellable?) : Bool #

def set_attribute_uint64(attribute : String, value : UInt64, flags : Gio::FileQueryInfoFlags, cancellable : Gio::Cancellable?) : Bool #

def set_attributes_async(info : Gio::FileInfo, flags : Gio::FileQueryInfoFlags, io_priority : Int32, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil #

def set_attributes_finish(result : Gio::AsyncResult, info : Gio::FileInfo) : Bool #

def set_attributes_from_info(info : Gio::FileInfo, flags : Gio::FileQueryInfoFlags, cancellable : Gio::Cancellable?) : Bool #

def set_display_name(display_name : String, cancellable : Gio::Cancellable?) : Gio::File #

def set_display_name_async(display_name : String, io_priority : Int32, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil #

def start_mountable(flags : Gio::DriveStartFlags, start_operation : Gio::MountOperation?, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil #

def start_mountable_finish(result : Gio::AsyncResult) : Bool #

def stop_mountable(flags : Gio::MountUnmountFlags, mount_operation : Gio::MountOperation?, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil #

def stop_mountable_finish(result : Gio::AsyncResult) : Bool #

def supports_thread_contexts : Bool #

abstract def to_unsafe #

def trash(cancellable : Gio::Cancellable?) : Bool #

def trash_async(io_priority : Int32, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil #

def trash_finish(result : Gio::AsyncResult) : Bool #

def unmount_mountable(flags : Gio::MountUnmountFlags, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil #

def unmount_mountable_finish(result : Gio::AsyncResult) : Bool #

def unmount_mountable_with_operation(flags : Gio::MountUnmountFlags, mount_operation : Gio::MountOperation?, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil #

def unmount_mountable_with_operation_finish(result : Gio::AsyncResult) : Bool #

def uri : String #

def uri_scheme : String? #