Struct gio::File [−][src]
pub struct File(_);
Expand description
File
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
File
objects do not represent files, merely an identifier for a
file. All file content I/O is implemented as streaming operations
(see InputStream
and OutputStream
).
To construct a File
, you can use:
for_path()
if you have a path.for_uri()
if you have a URI.for_commandline_arg()
for a command line argument.new_tmp()
to create a temporary file from a template.for_parse_name()
from a UTF-8 string gotten fromFileExt::parse_name()
.g_file_new_build_filename()
to create a file from path elements.
One way to think of a File
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
File
using FileExt::parent()
to get an identifier for the parent
directory, FileExt::child()
to get a child within a directory,
FileExt::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 FileExt::parent()
on two different
files.
All GFiles
have a basename (get with FileExt::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
FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME
attribute with FileExt::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 File
to use to actually
access the file, because there is no way to go from a display name to
the actual name.
Using File
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
FILE_ATTRIBUTE_ID_FILE
attribute. Note that File
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 File
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 File
operations almost always take a noticeable amount of time, and
so do not have synchronous analogs. Notable cases include:
FileExt::mount_mountable()
to mount a mountable file.FileExt::unmount_mountable_with_operation()
to unmount a mountable file.FileExt::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.
Implements
Implementations
Creates a File
with the given argument from the command line.
The value of arg
can be either a URI, an absolute path or a
relative path resolved relative to the current working directory.
This operation never fails, but the returned object might not
support any I/O operation if arg
points to a malformed path.
Note that on Windows, this function expects its argument to be in
UTF-8 – not the system code page. This means that you
should not use this function with string from argv as it is passed
to main()
. g_win32_get_command_line()
will return a UTF-8 version of
the commandline. Application
also uses UTF-8 but
ApplicationCommandLineExt::create_file_for_arg()
may be more useful
for you there. It is also always possible to use this function with
GOptionContext
arguments of type glib::OptionArg::Filename
.
arg
a command line string
Returns
a new File
.
Free the returned object with g_object_unref()
.
Creates a File
with the given argument from the command line.
This function is similar to for_commandline_arg()
except
that it allows for passing the current working directory as an
argument instead of using the current working directory of the
process.
This is useful if the commandline argument was given in a context other than the invocation of the current process.
See also ApplicationCommandLineExt::create_file_for_arg()
.
arg
a command line string
cwd
the current working directory of the commandline
Returns
a new File
Constructs a File
for a given path. This operation never
fails, but the returned object might not support any I/O
operation if path
is malformed.
path
a string containing a relative or absolute path. The string must be encoded in the glib filename encoding.
Returns
a new File
for the given path
.
Free the returned object with g_object_unref()
.
Constructs a File
for a given URI. This operation never
fails, but the returned object might not support any I/O
operation if uri
is malformed or if the uri type is
not supported.
uri
a UTF-8 string containing a URI
Returns
a new File
for the given uri
.
Free the returned object with g_object_unref()
.
Opens a file in the preferred directory for temporary files (as
returned by g_get_tmp_dir()
) and returns a File
and
FileIOStream
pointing to it.
tmpl
should be a string in the GLib file name encoding
containing a sequence of six ‘X’ characters, and containing no
directory components. If it is None
, a default template is used.
Unlike the other File
constructors, this will return None
if
a temporary file could not be created.
tmpl
Template for the file
name, as in g_file_open_tmp()
, or None
for a default template
Returns
a new File
.
Free the returned object with g_object_unref()
.
iostream
on return, a FileIOStream
for the created file
Constructs a File
with the given parse_name
(i.e. something
given by FileExt::parse_name()
). This operation never fails,
but the returned object might not support any I/O operation if
the parse_name
cannot be parsed.
parse_name
a file name or path to be parsed
Returns
a new File
.
Trait Implementations
This method returns an ordering between self
and other
values if one exists. Read more
This method tests less than (for self
and other
) and is used by the <
operator. Read more
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
Returns the type identifier of Self
.
Auto Trait Implementations
Blanket Implementations
Mutably borrows from an owned value. Read more
Upcasts an object to a superclass or interface T
. Read more
Upcasts an object to a reference of its superclass or interface T
. Read more
Tries to downcast to a subclass or interface implementor T
. Read more
Tries to downcast to a reference of its subclass or interface implementor T
. Read more
Tries to cast to an object of type T
. This handles upcasting, downcasting
and casting between interface and interface implementors. All checks are performed at
runtime, while downcast
and upcast
will do many checks at compile-time already. Read more
Tries to cast to reference to an object of type T
. This handles upcasting, downcasting
and casting between interface and interface implementors. All checks are performed at
runtime, while downcast
and upcast
will do many checks at compile-time already. Read more
Casts to T
unconditionally. Read more
Casts to &T
unconditionally. Read more
Returns true
if the object is an instance of (can be cast to) T
.
pub fn set_properties_from_value(
&self,
property_values: &[(&str, Value)]
) -> Result<(), BoolError>
pub fn set_property<'a, N, V>(
&self,
property_name: N,
value: V
) -> Result<(), BoolError> where
V: ToValue,
N: Into<&'a str>,
pub fn set_property_from_value<'a, N>(
&self,
property_name: N,
value: &Value
) -> Result<(), BoolError> where
N: Into<&'a str>,
Safety Read more
Safety Read more
Safety Read more
Safety Read more
pub fn connect_notify<F>(&self, name: Option<&str>, f: F) -> SignalHandlerId where
F: 'static + Fn(&T, &ParamSpec) + Send + Sync,
pub fn connect_notify_local<F>(
&self,
name: Option<&str>,
f: F
) -> SignalHandlerId where
F: 'static + Fn(&T, &ParamSpec),
pub unsafe fn connect_notify_unsafe<F>(
&self,
name: Option<&str>,
f: F
) -> SignalHandlerId where
F: Fn(&T, &ParamSpec),
pub fn has_property<'a, N>(&self, property_name: N, type_: Option<Type>) -> bool where
N: Into<&'a str>,
pub fn find_property<'a, N>(&self, property_name: N) -> Option<ParamSpec> where
N: Into<&'a str>,
pub fn connect<'a, N, F>(
&self,
signal_name: N,
after: bool,
callback: F
) -> Result<SignalHandlerId, BoolError> where
F: Fn(&[Value]) -> Option<Value> + Send + Sync + 'static,
N: Into<&'a str>,
Same as connect
but takes a SignalId
instead of a signal name.
pub fn connect_local<'a, N, F>(
&self,
signal_name: N,
after: bool,
callback: F
) -> Result<SignalHandlerId, BoolError> where
F: Fn(&[Value]) -> Option<Value> + 'static,
N: Into<&'a str>,
Same as connect_local
but takes a SignalId
instead of a signal name.
pub unsafe fn connect_unsafe<'a, N, F>(
&self,
signal_name: N,
after: bool,
callback: F
) -> Result<SignalHandlerId, BoolError> where
F: Fn(&[Value]) -> Option<Value>,
N: Into<&'a str>,
Same as connect_unsafe
but takes a SignalId
instead of a signal name.
Emit signal by signal id.
Emit signal with details by signal id.
Emit signal by it’s name.
pub fn bind_property<'a, O, N, M>(
&'a self,
source_property: N,
target: &'a O,
target_property: M
) -> BindingBuilder<'a> where
O: ObjectType,
N: Into<&'a str>,
M: Into<&'a str>,
Same as emit
but takes Value
for the arguments.
Same as emit_by_name
but takes Value
for the arguments.
Returns a SendValue
clone of self
.
impl<'a, T, C> FromValueOptional<'a> for T where
C: ValueTypeChecker<Error = ValueTypeMismatchOrNoneError>,
T: FromValue<'a, Checker = C>,