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:
- 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_new_tmp_async() to asynchronously create a temporary file.
- g_file_new_tmp_dir_async() to asynchronously create a temporary directory.
- 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 for HTTP Etag headers, which are a very similar concept.
Defined in:
lib/gi-crystal/src/auto/gio-2.0/file.crClass Method Summary
- .g_type : UInt64
- .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?, io_priority : Int32, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil
- .new_tmp_dir_async(tmpl : String?, io_priority : Int32, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil
- .parse_name(parse_name : String) : Gio::File
Instance Method Summary
- #append_to(flags : Gio::FileCreateFlags, cancellable : Gio::Cancellable?) : Gio::FileOutputStream
- #append_to_async(flags : Gio::FileCreateFlags, io_priority : Int32, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil
- #append_to_finish(res : Gio::AsyncResult) : Gio::FileOutputStream
- #basename : Path?
- #build_attribute_list_for_copy(flags : Gio::FileCopyFlags, cancellable : Gio::Cancellable?) : 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?, progress_callback : Gio::FileProgressCallback?, progress_callback_data : Pointer(Void)?) : Bool
- #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
- #copy_attributes(destination : Gio::File, flags : Gio::FileCopyFlags, cancellable : Gio::Cancellable?) : Bool
- #copy_finish(res : Gio::AsyncResult) : Bool
- #create(flags : Gio::FileCreateFlags, cancellable : Gio::Cancellable?) : Gio::FileOutputStream
- #create_async(flags : Gio::FileCreateFlags, io_priority : Int32, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil
- #create_finish(res : Gio::AsyncResult) : Gio::FileOutputStream
- #create_readwrite(flags : Gio::FileCreateFlags, cancellable : Gio::Cancellable?) : Gio::FileIOStream
- #create_readwrite_async(flags : Gio::FileCreateFlags, io_priority : Int32, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil
- #create_readwrite_finish(res : Gio::AsyncResult) : Gio::FileIOStream
- #delete(cancellable : Gio::Cancellable?) : Bool
- #delete_async(io_priority : Int32, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : 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?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil
- #eject_mountable_finish(result : Gio::AsyncResult) : Bool
- #eject_mountable_with_operation(flags : Gio::MountUnmountFlags, mount_operation : Gio::MountOperation?, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil
- #eject_mountable_with_operation_finish(result : Gio::AsyncResult) : Bool
- #enumerate_children(attributes : String, flags : Gio::FileQueryInfoFlags, cancellable : Gio::Cancellable?) : Gio::FileEnumerator
- #enumerate_children_async(attributes : String, flags : Gio::FileQueryInfoFlags, io_priority : Int32, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil
- #enumerate_children_finish(res : Gio::AsyncResult) : Gio::FileEnumerator
- #equal(file2 : Gio::File) : Bool
- #find_enclosing_mount(cancellable : Gio::Cancellable?) : Gio::Mount
- #find_enclosing_mount_async(io_priority : Int32, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil
- #find_enclosing_mount_finish(res : Gio::AsyncResult) : Gio::Mount
- #has_parent(parent : Gio::File?) : Bool
- #has_prefix(prefix : Gio::File) : Bool
- #has_uri_scheme(uri_scheme : String) : Bool
- #hash : UInt32
- #is_native : Bool
- #load_bytes(cancellable : Gio::Cancellable?) : GLib::Bytes
- #load_bytes_async(cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil
- #load_bytes_finish(result : Gio::AsyncResult) : GLib::Bytes
- #load_contents(cancellable : Gio::Cancellable?, contents : Enumerable(UInt8)) : Bool
- #load_contents_async(cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil
- #load_contents_finish(res : Gio::AsyncResult, contents : Enumerable(UInt8)) : Bool
- #load_partial_contents_finish(res : Gio::AsyncResult, contents : Enumerable(UInt8)) : Bool
- #make_directory(cancellable : Gio::Cancellable?) : Bool
- #make_directory_async(io_priority : Int32, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil
- #make_directory_finish(result : Gio::AsyncResult) : Bool
- #make_directory_with_parents(cancellable : Gio::Cancellable?) : Bool
- #make_symbolic_link(symlink_value : String, cancellable : Gio::Cancellable?) : Bool
- #make_symbolic_link_async(symlink_value : String, io_priority : Int32, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil
- #make_symbolic_link_finish(result : Gio::AsyncResult) : Bool
- #measure_disk_usage_finish(result : Gio::AsyncResult) : Bool
- #monitor(flags : Gio::FileMonitorFlags, cancellable : Gio::Cancellable?) : Gio::FileMonitor
- #monitor_directory(flags : Gio::FileMonitorFlags, cancellable : Gio::Cancellable?) : Gio::FileMonitor
- #monitor_file(flags : Gio::FileMonitorFlags, cancellable : Gio::Cancellable?) : Gio::FileMonitor
- #mount_enclosing_volume(flags : Gio::MountMountFlags, mount_operation : Gio::MountOperation?, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil
- #mount_enclosing_volume_finish(result : Gio::AsyncResult) : Bool
- #mount_mountable(flags : Gio::MountMountFlags, mount_operation : Gio::MountOperation?, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil
- #mount_mountable_finish(result : Gio::AsyncResult) : Gio::File
- #move(destination : Gio::File, flags : Gio::FileCopyFlags, cancellable : Gio::Cancellable?, progress_callback : Gio::FileProgressCallback?, progress_callback_data : Pointer(Void)?) : Bool
- #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
- #move_finish(result : Gio::AsyncResult) : Bool
- #new_tmp(tmpl : String?, 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?) : Gio::FileIOStream
- #open_readwrite_async(io_priority : Int32, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil
- #open_readwrite_finish(res : Gio::AsyncResult) : Gio::FileIOStream
- #parent : Gio::File?
- #parse_name : String
- #path : Path?
- #peek_path : Path?
- #poll_mountable(cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil
- #poll_mountable_finish(result : Gio::AsyncResult) : Bool
- #query_default_handler(cancellable : Gio::Cancellable?) : Gio::AppInfo
- #query_default_handler_async(io_priority : Int32, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil
- #query_default_handler_finish(result : Gio::AsyncResult) : Gio::AppInfo
- #query_exists(cancellable : Gio::Cancellable?) : Bool
- #query_file_type(flags : Gio::FileQueryInfoFlags, cancellable : Gio::Cancellable?) : Gio::FileType
- #query_filesystem_info(attributes : String, cancellable : Gio::Cancellable?) : Gio::FileInfo
- #query_filesystem_info_async(attributes : String, io_priority : Int32, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil
- #query_filesystem_info_finish(res : Gio::AsyncResult) : Gio::FileInfo
- #query_info(attributes : String, flags : Gio::FileQueryInfoFlags, cancellable : Gio::Cancellable?) : Gio::FileInfo
- #query_info_async(attributes : String, flags : Gio::FileQueryInfoFlags, io_priority : Int32, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil
- #query_info_finish(res : Gio::AsyncResult) : Gio::FileInfo
- #query_settable_attributes(cancellable : Gio::Cancellable?) : Gio::FileAttributeInfoList
- #query_writable_namespaces(cancellable : Gio::Cancellable?) : Gio::FileAttributeInfoList
- #read(cancellable : Gio::Cancellable?) : Gio::FileInputStream
- #read_async(io_priority : Int32, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil
- #read_finish(res : Gio::AsyncResult) : Gio::FileInputStream
- #relative_path(descendant : Gio::File) : Path?
- #replace(etag : String?, make_backup : Bool, flags : Gio::FileCreateFlags, cancellable : Gio::Cancellable?) : Gio::FileOutputStream
- #replace_async(etag : String?, make_backup : Bool, flags : Gio::FileCreateFlags, io_priority : Int32, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil
- #replace_contents(contents : Enumerable(UInt8), etag : String?, make_backup : Bool, flags : Gio::FileCreateFlags, cancellable : Gio::Cancellable?) : Bool
- #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
- #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
- #replace_contents_finish(res : Gio::AsyncResult) : Bool
- #replace_finish(res : Gio::AsyncResult) : Gio::FileOutputStream
- #replace_readwrite(etag : String?, make_backup : Bool, flags : Gio::FileCreateFlags, cancellable : Gio::Cancellable?) : Gio::FileIOStream
- #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
- #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)?, flags : Gio::FileQueryInfoFlags, cancellable : Gio::Cancellable?) : Bool
- #set_attribute_byte_string(attribute : String, value : String, flags : Gio::FileQueryInfoFlags, cancellable : Gio::Cancellable?) : Bool
- #set_attribute_int32(attribute : String, value : Int32, flags : Gio::FileQueryInfoFlags, cancellable : Gio::Cancellable?) : Bool
- #set_attribute_int64(attribute : String, value : Int64, flags : Gio::FileQueryInfoFlags, cancellable : Gio::Cancellable?) : Bool
- #set_attribute_string(attribute : String, value : String, flags : Gio::FileQueryInfoFlags, cancellable : Gio::Cancellable?) : Bool
- #set_attribute_uint32(attribute : String, value : UInt32, flags : Gio::FileQueryInfoFlags, cancellable : Gio::Cancellable?) : Bool
- #set_attribute_uint64(attribute : String, value : UInt64, flags : Gio::FileQueryInfoFlags, cancellable : Gio::Cancellable?) : Bool
- #set_attributes_async(info : Gio::FileInfo, flags : Gio::FileQueryInfoFlags, io_priority : Int32, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil
- #set_attributes_finish(result : Gio::AsyncResult, info : Gio::FileInfo) : Bool
- #set_attributes_from_info(info : Gio::FileInfo, flags : Gio::FileQueryInfoFlags, cancellable : Gio::Cancellable?) : Bool
- #set_display_name(display_name : String, cancellable : Gio::Cancellable?) : Gio::File
- #set_display_name_async(display_name : String, io_priority : Int32, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil
- #start_mountable(flags : Gio::DriveStartFlags, start_operation : Gio::MountOperation?, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil
- #start_mountable_finish(result : Gio::AsyncResult) : Bool
- #stop_mountable(flags : Gio::MountUnmountFlags, mount_operation : Gio::MountOperation?, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil
- #stop_mountable_finish(result : Gio::AsyncResult) : Bool
- #supports_thread_contexts : Bool
- #to_unsafe
- #trash(cancellable : Gio::Cancellable?) : Bool
- #trash_async(io_priority : Int32, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil
- #trash_finish(result : Gio::AsyncResult) : Bool
- #unmount_mountable(flags : Gio::MountUnmountFlags, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil
- #unmount_mountable_finish(result : Gio::AsyncResult) : Bool
- #unmount_mountable_with_operation(flags : Gio::MountUnmountFlags, mount_operation : Gio::MountOperation?, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil
- #unmount_mountable_with_operation_finish(result : Gio::AsyncResult) : Bool
- #uri : String
- #uri_scheme : String?