Struct camino_tempfile::Utf8TempPath
source · pub struct Utf8TempPath { /* private fields */ }
Expand description
A path to a named temporary file without an open file handle.
This is useful when the temporary file needs to be used by a child process, for example.
When dropped, the temporary file is deleted.
Implementations§
source§impl Utf8TempPath
impl Utf8TempPath
sourcepub fn close(self) -> Result<()>
pub fn close(self) -> Result<()>
Close and remove the temporary file.
Use this if you want to detect errors in deleting the file.
Errors
If the file cannot be deleted, Err
is returned.
Examples
use camino_tempfile::NamedUtf8TempFile;
let file = NamedUtf8TempFile::new()?;
// Close the file, but keep the path to it around.
let path = file.into_temp_path();
// By closing the `Utf8TempPath` explicitly, we can check that it has
// been deleted successfully. If we don't close it explicitly, the
// file will still be deleted when `file` goes out of scope, but we
// won't know whether deleting the file succeeded.
path.close()?;
sourcepub fn persist<P: AsRef<Path>>(
self,
new_path: P
) -> Result<(), Utf8PathPersistError>
pub fn persist<P: AsRef<Path>>( self, new_path: P ) -> Result<(), Utf8PathPersistError>
Persist the temporary file at the target path.
If a file exists at the target path, persist will atomically replace it. If this method
fails, it will return self
in the resulting Utf8PathPersistError
.
Notes
- This method accepts
AsRef<Path>
rather thanAsRef<Utf8Path>
, because in the success case it does not return anything. - Temporary files cannot be persisted across filesystems.
- Neither the file contents nor the containing directory are synchronized, so the update may
not yet have reached the disk when
persist
returns.
Security
Only use this method if you’re positive that a temporary file cleaner won’t have deleted your file. Otherwise, you might end up persisting an attacker controlled file.
Errors
If the file cannot be moved to the new location, Err
is returned.
Examples
use camino_tempfile::NamedUtf8TempFile;
let mut file = NamedUtf8TempFile::new()?;
writeln!(file, "Brian was here. Briefly.")?;
let path = file.into_temp_path();
path.persist("./saved_file.txt")?;
sourcepub fn persist_noclobber<P: AsRef<Path>>(
self,
new_path: P
) -> Result<(), Utf8PathPersistError>
pub fn persist_noclobber<P: AsRef<Path>>( self, new_path: P ) -> Result<(), Utf8PathPersistError>
Persist the temporary file at the target path if and only if no file exists there.
If a file exists at the target path, fail. If this method fails, it will return self
in
the resulting Utf8PathPersistError
.
Notes
- This method accepts
AsRef<Path>
rather thanAsRef<Utf8Path>
, because in the success case it does not return anything. - Temporary files cannot be persisted across filesystems.
- This method is not atomic. It can leave the original link to the temporary file behind.
Security
Only use this method if you’re positive that a temporary file cleaner won’t have deleted your file. Otherwise, you might end up persisting an attacker controlled file.
Errors
If the file cannot be moved to the new location or a file already exists there, Err
is
returned.
Examples
use camino_tempfile::NamedUtf8TempFile;
let mut file = NamedUtf8TempFile::new()?;
writeln!(file, "Brian was here. Briefly.")?;
let path = file.into_temp_path();
path.persist_noclobber("./saved_file.txt")?;
sourcepub fn keep(self) -> Result<Utf8PathBuf, Utf8PathPersistError>
pub fn keep(self) -> Result<Utf8PathBuf, Utf8PathPersistError>
Keep the temporary file from being deleted. This function will turn the temporary file into a non-temporary file without moving it.
Errors
On some platforms (e.g., Windows), we need to mark the file as non-temporary. This operation could fail.
Examples
use camino_tempfile::NamedUtf8TempFile;
let mut file = NamedUtf8TempFile::new()?;
writeln!(file, "Brian was here. Briefly.")?;
let path = file.into_temp_path();
let path = path.keep()?;
sourcepub fn from_path(path: impl Into<Utf8PathBuf>) -> Self
pub fn from_path(path: impl Into<Utf8PathBuf>) -> Self
Create a new Utf8TempPath
from an existing path. This can be done even if no file exists
at the given path.
This is mostly useful for interacting with libraries and external components that provide files to be consumed or expect a path with no existing file to be given.
Methods from Deref<Target = Utf8Path>§
sourcepub fn as_std_path(&self) -> &Path
pub fn as_std_path(&self) -> &Path
Converts a Utf8Path
to a Path
.
This is equivalent to the AsRef<&Path> for &Utf8Path
impl, but may aid in type inference.
Examples
use camino::Utf8Path;
use std::path::Path;
let utf8_path = Utf8Path::new("foo.txt");
let std_path: &Path = utf8_path.as_std_path();
assert_eq!(std_path.to_str(), Some("foo.txt"));
// Convert back to a Utf8Path.
let new_utf8_path = Utf8Path::from_path(std_path).unwrap();
assert_eq!(new_utf8_path, "foo.txt");
sourcepub fn as_str(&self) -> &str
pub fn as_str(&self) -> &str
Yields the underlying str
slice.
Unlike Path::to_str
, this always returns a slice because the contents of a Utf8Path
are guaranteed to be valid UTF-8.
Examples
use camino::Utf8Path;
let s = Utf8Path::new("foo.txt").as_str();
assert_eq!(s, "foo.txt");
sourcepub fn to_path_buf(&self) -> Utf8PathBuf
pub fn to_path_buf(&self) -> Utf8PathBuf
Converts a Utf8Path
to an owned Utf8PathBuf
.
Examples
use camino::{Utf8Path, Utf8PathBuf};
let path_buf = Utf8Path::new("foo.txt").to_path_buf();
assert_eq!(path_buf, Utf8PathBuf::from("foo.txt"));
sourcepub fn is_absolute(&self) -> bool
pub fn is_absolute(&self) -> bool
Returns true
if the Utf8Path
is absolute, i.e., if it is independent of
the current directory.
-
On Unix, a path is absolute if it starts with the root, so
is_absolute
andhas_root
are equivalent. -
On Windows, a path is absolute if it has a prefix and starts with the root:
c:\windows
is absolute, whilec:temp
and\temp
are not.
Examples
use camino::Utf8Path;
assert!(!Utf8Path::new("foo.txt").is_absolute());
sourcepub fn is_relative(&self) -> bool
pub fn is_relative(&self) -> bool
Returns true
if the Utf8Path
is relative, i.e., not absolute.
See is_absolute
’s documentation for more details.
Examples
use camino::Utf8Path;
assert!(Utf8Path::new("foo.txt").is_relative());
sourcepub fn has_root(&self) -> bool
pub fn has_root(&self) -> bool
Returns true
if the Utf8Path
has a root.
-
On Unix, a path has a root if it begins with
/
. -
On Windows, a path has a root if it:
- has no prefix and begins with a separator, e.g.,
\windows
- has a prefix followed by a separator, e.g.,
c:\windows
but notc:windows
- has any non-disk prefix, e.g.,
\\server\share
- has no prefix and begins with a separator, e.g.,
Examples
use camino::Utf8Path;
assert!(Utf8Path::new("/etc/passwd").has_root());
sourcepub fn parent(&self) -> Option<&Utf8Path>
pub fn parent(&self) -> Option<&Utf8Path>
Returns the Path
without its final component, if there is one.
Returns None
if the path terminates in a root or prefix.
Examples
use camino::Utf8Path;
let path = Utf8Path::new("/foo/bar");
let parent = path.parent().unwrap();
assert_eq!(parent, Utf8Path::new("/foo"));
let grand_parent = parent.parent().unwrap();
assert_eq!(grand_parent, Utf8Path::new("/"));
assert_eq!(grand_parent.parent(), None);
sourcepub fn ancestors(&self) -> Utf8Ancestors<'_>
pub fn ancestors(&self) -> Utf8Ancestors<'_>
Produces an iterator over Utf8Path
and its ancestors.
The iterator will yield the Utf8Path
that is returned if the parent
method is used zero
or more times. That means, the iterator will yield &self
, &self.parent().unwrap()
,
&self.parent().unwrap().parent().unwrap()
and so on. If the parent
method returns
None
, the iterator will do likewise. The iterator will always yield at least one value,
namely &self
.
Examples
use camino::Utf8Path;
let mut ancestors = Utf8Path::new("/foo/bar").ancestors();
assert_eq!(ancestors.next(), Some(Utf8Path::new("/foo/bar")));
assert_eq!(ancestors.next(), Some(Utf8Path::new("/foo")));
assert_eq!(ancestors.next(), Some(Utf8Path::new("/")));
assert_eq!(ancestors.next(), None);
let mut ancestors = Utf8Path::new("../foo/bar").ancestors();
assert_eq!(ancestors.next(), Some(Utf8Path::new("../foo/bar")));
assert_eq!(ancestors.next(), Some(Utf8Path::new("../foo")));
assert_eq!(ancestors.next(), Some(Utf8Path::new("..")));
assert_eq!(ancestors.next(), Some(Utf8Path::new("")));
assert_eq!(ancestors.next(), None);
sourcepub fn file_name(&self) -> Option<&str>
pub fn file_name(&self) -> Option<&str>
Returns the final component of the Utf8Path
, if there is one.
If the path is a normal file, this is the file name. If it’s the path of a directory, this is the directory name.
Returns None
if the path terminates in ..
.
Examples
use camino::Utf8Path;
assert_eq!(Some("bin"), Utf8Path::new("/usr/bin/").file_name());
assert_eq!(Some("foo.txt"), Utf8Path::new("tmp/foo.txt").file_name());
assert_eq!(Some("foo.txt"), Utf8Path::new("foo.txt/.").file_name());
assert_eq!(Some("foo.txt"), Utf8Path::new("foo.txt/.//").file_name());
assert_eq!(None, Utf8Path::new("foo.txt/..").file_name());
assert_eq!(None, Utf8Path::new("/").file_name());
sourcepub fn strip_prefix(
&self,
base: impl AsRef<Path>
) -> Result<&Utf8Path, StripPrefixError>
pub fn strip_prefix( &self, base: impl AsRef<Path> ) -> Result<&Utf8Path, StripPrefixError>
Returns a path that, when joined onto base
, yields self
.
Errors
If base
is not a prefix of self
(i.e., starts_with
returns false
), returns Err
.
Examples
use camino::{Utf8Path, Utf8PathBuf};
let path = Utf8Path::new("/test/haha/foo.txt");
assert_eq!(path.strip_prefix("/"), Ok(Utf8Path::new("test/haha/foo.txt")));
assert_eq!(path.strip_prefix("/test"), Ok(Utf8Path::new("haha/foo.txt")));
assert_eq!(path.strip_prefix("/test/"), Ok(Utf8Path::new("haha/foo.txt")));
assert_eq!(path.strip_prefix("/test/haha/foo.txt"), Ok(Utf8Path::new("")));
assert_eq!(path.strip_prefix("/test/haha/foo.txt/"), Ok(Utf8Path::new("")));
assert!(path.strip_prefix("test").is_err());
assert!(path.strip_prefix("/haha").is_err());
let prefix = Utf8PathBuf::from("/test/");
assert_eq!(path.strip_prefix(prefix), Ok(Utf8Path::new("haha/foo.txt")));
sourcepub fn starts_with(&self, base: impl AsRef<Path>) -> bool
pub fn starts_with(&self, base: impl AsRef<Path>) -> bool
Determines whether base
is a prefix of self
.
Only considers whole path components to match.
Examples
use camino::Utf8Path;
let path = Utf8Path::new("/etc/passwd");
assert!(path.starts_with("/etc"));
assert!(path.starts_with("/etc/"));
assert!(path.starts_with("/etc/passwd"));
assert!(path.starts_with("/etc/passwd/")); // extra slash is okay
assert!(path.starts_with("/etc/passwd///")); // multiple extra slashes are okay
assert!(!path.starts_with("/e"));
assert!(!path.starts_with("/etc/passwd.txt"));
assert!(!Utf8Path::new("/etc/foo.rs").starts_with("/etc/foo"));
sourcepub fn ends_with(&self, base: impl AsRef<Path>) -> bool
pub fn ends_with(&self, base: impl AsRef<Path>) -> bool
Determines whether child
is a suffix of self
.
Only considers whole path components to match.
Examples
use camino::Utf8Path;
let path = Utf8Path::new("/etc/resolv.conf");
assert!(path.ends_with("resolv.conf"));
assert!(path.ends_with("etc/resolv.conf"));
assert!(path.ends_with("/etc/resolv.conf"));
assert!(!path.ends_with("/resolv.conf"));
assert!(!path.ends_with("conf")); // use .extension() instead
sourcepub fn file_stem(&self) -> Option<&str>
pub fn file_stem(&self) -> Option<&str>
Extracts the stem (non-extension) portion of self.file_name
.
The stem is:
None
, if there is no file name;- The entire file name if there is no embedded
.
; - The entire file name if the file name begins with
.
and has no other.
s within; - Otherwise, the portion of the file name before the final
.
Examples
use camino::Utf8Path;
assert_eq!("foo", Utf8Path::new("foo.rs").file_stem().unwrap());
assert_eq!("foo.tar", Utf8Path::new("foo.tar.gz").file_stem().unwrap());
sourcepub fn extension(&self) -> Option<&str>
pub fn extension(&self) -> Option<&str>
Extracts the extension of self.file_name
, if possible.
The extension is:
None
, if there is no file name;None
, if there is no embedded.
;None
, if the file name begins with.
and has no other.
s within;- Otherwise, the portion of the file name after the final
.
Examples
use camino::Utf8Path;
assert_eq!("rs", Utf8Path::new("foo.rs").extension().unwrap());
assert_eq!("gz", Utf8Path::new("foo.tar.gz").extension().unwrap());
sourcepub fn join(&self, path: impl AsRef<Utf8Path>) -> Utf8PathBuf
pub fn join(&self, path: impl AsRef<Utf8Path>) -> Utf8PathBuf
Creates an owned Utf8PathBuf
with path
adjoined to self
.
See Utf8PathBuf::push
for more details on what it means to adjoin a path.
Examples
use camino::{Utf8Path, Utf8PathBuf};
assert_eq!(Utf8Path::new("/etc").join("passwd"), Utf8PathBuf::from("/etc/passwd"));
sourcepub fn join_os(&self, path: impl AsRef<Path>) -> PathBuf
pub fn join_os(&self, path: impl AsRef<Path>) -> PathBuf
Creates an owned PathBuf
with path
adjoined to self
.
See PathBuf::push
for more details on what it means to adjoin a path.
Examples
use camino::Utf8Path;
use std::path::PathBuf;
assert_eq!(Utf8Path::new("/etc").join_os("passwd"), PathBuf::from("/etc/passwd"));
sourcepub fn with_file_name(&self, file_name: impl AsRef<str>) -> Utf8PathBuf
pub fn with_file_name(&self, file_name: impl AsRef<str>) -> Utf8PathBuf
Creates an owned Utf8PathBuf
like self
but with the given file name.
See Utf8PathBuf::set_file_name
for more details.
Examples
use camino::{Utf8Path, Utf8PathBuf};
let path = Utf8Path::new("/tmp/foo.txt");
assert_eq!(path.with_file_name("bar.txt"), Utf8PathBuf::from("/tmp/bar.txt"));
let path = Utf8Path::new("/tmp");
assert_eq!(path.with_file_name("var"), Utf8PathBuf::from("/var"));
sourcepub fn with_extension(&self, extension: impl AsRef<str>) -> Utf8PathBuf
pub fn with_extension(&self, extension: impl AsRef<str>) -> Utf8PathBuf
Creates an owned Utf8PathBuf
like self
but with the given extension.
See Utf8PathBuf::set_extension
for more details.
Examples
use camino::{Utf8Path, Utf8PathBuf};
let path = Utf8Path::new("foo.rs");
assert_eq!(path.with_extension("txt"), Utf8PathBuf::from("foo.txt"));
let path = Utf8Path::new("foo.tar.gz");
assert_eq!(path.with_extension(""), Utf8PathBuf::from("foo.tar"));
assert_eq!(path.with_extension("xz"), Utf8PathBuf::from("foo.tar.xz"));
assert_eq!(path.with_extension("").with_extension("txt"), Utf8PathBuf::from("foo.txt"));
sourcepub fn components(&self) -> Utf8Components<'_>
pub fn components(&self) -> Utf8Components<'_>
Produces an iterator over the Utf8Component
s of the path.
When parsing the path, there is a small amount of normalization:
-
Repeated separators are ignored, so
a/b
anda//b
both havea
andb
as components. -
Occurrences of
.
are normalized away, except if they are at the beginning of the path. For example,a/./b
,a/b/
,a/b/.
anda/b
all havea
andb
as components, but./a/b
starts with an additionalCurDir
component. -
A trailing slash is normalized away,
/a/b
and/a/b/
are equivalent.
Note that no other normalization takes place; in particular, a/c
and a/b/../c
are distinct, to account for the possibility that b
is a symbolic link (so its parent isn’t a
).
Examples
use camino::{Utf8Component, Utf8Path};
let mut components = Utf8Path::new("/tmp/foo.txt").components();
assert_eq!(components.next(), Some(Utf8Component::RootDir));
assert_eq!(components.next(), Some(Utf8Component::Normal("tmp")));
assert_eq!(components.next(), Some(Utf8Component::Normal("foo.txt")));
assert_eq!(components.next(), None)
sourcepub fn iter(&self) -> Iter<'_>
pub fn iter(&self) -> Iter<'_>
Produces an iterator over the path’s components viewed as str
slices.
For more information about the particulars of how the path is separated
into components, see components
.
Examples
use camino::Utf8Path;
let mut it = Utf8Path::new("/tmp/foo.txt").iter();
assert_eq!(it.next(), Some(std::path::MAIN_SEPARATOR.to_string().as_str()));
assert_eq!(it.next(), Some("tmp"));
assert_eq!(it.next(), Some("foo.txt"));
assert_eq!(it.next(), None)
sourcepub fn metadata(&self) -> Result<Metadata, Error>
pub fn metadata(&self) -> Result<Metadata, Error>
Queries the file system to get information about a file, directory, etc.
This function will traverse symbolic links to query information about the destination file.
This is an alias to fs::metadata
.
Examples
use camino::Utf8Path;
let path = Utf8Path::new("/Minas/tirith");
let metadata = path.metadata().expect("metadata call failed");
println!("{:?}", metadata.file_type());
sourcepub fn symlink_metadata(&self) -> Result<Metadata, Error>
pub fn symlink_metadata(&self) -> Result<Metadata, Error>
Queries the metadata about a file without following symlinks.
This is an alias to fs::symlink_metadata
.
Examples
use camino::Utf8Path;
let path = Utf8Path::new("/Minas/tirith");
let metadata = path.symlink_metadata().expect("symlink_metadata call failed");
println!("{:?}", metadata.file_type());
sourcepub fn canonicalize(&self) -> Result<PathBuf, Error>
pub fn canonicalize(&self) -> Result<PathBuf, Error>
Returns the canonical, absolute form of the path with all intermediate components normalized and symbolic links resolved.
This returns a PathBuf
because even if a symlink is valid Unicode, its target may not
be. For a version that returns a Utf8PathBuf
, see
canonicalize_utf8
.
This is an alias to fs::canonicalize
.
Examples
use camino::Utf8Path;
use std::path::PathBuf;
let path = Utf8Path::new("/foo/test/../test/bar.rs");
assert_eq!(path.canonicalize().unwrap(), PathBuf::from("/foo/test/bar.rs"));
sourcepub fn canonicalize_utf8(&self) -> Result<Utf8PathBuf, Error>
pub fn canonicalize_utf8(&self) -> Result<Utf8PathBuf, Error>
Returns the canonical, absolute form of the path with all intermediate components normalized and symbolic links resolved.
This method attempts to convert the resulting PathBuf
into a Utf8PathBuf
. For a
version that does not attempt to do this conversion, see
canonicalize
.
Errors
The I/O operation may return an error: see the fs::canonicalize
documentation for more.
If the resulting path is not UTF-8, an io::Error
is returned with the
ErrorKind
set to InvalidData
and the payload set to a
FromPathBufError
.
Examples
use camino::{Utf8Path, Utf8PathBuf};
let path = Utf8Path::new("/foo/test/../test/bar.rs");
assert_eq!(path.canonicalize_utf8().unwrap(), Utf8PathBuf::from("/foo/test/bar.rs"));
sourcepub fn read_link(&self) -> Result<PathBuf, Error>
pub fn read_link(&self) -> Result<PathBuf, Error>
Reads a symbolic link, returning the file that the link points to.
This returns a PathBuf
because even if a symlink is valid Unicode, its target may not
be. For a version that returns a Utf8PathBuf
, see
read_link_utf8
.
This is an alias to fs::read_link
.
Examples
use camino::Utf8Path;
let path = Utf8Path::new("/laputa/sky_castle.rs");
let path_link = path.read_link().expect("read_link call failed");
sourcepub fn read_link_utf8(&self) -> Result<Utf8PathBuf, Error>
pub fn read_link_utf8(&self) -> Result<Utf8PathBuf, Error>
Reads a symbolic link, returning the file that the link points to.
This method attempts to convert the resulting PathBuf
into a Utf8PathBuf
. For a
version that does not attempt to do this conversion, see read_link
.
Errors
The I/O operation may return an error: see the fs::read_link
documentation for more.
If the resulting path is not UTF-8, an io::Error
is returned with the
ErrorKind
set to InvalidData
and the payload set to a
FromPathBufError
.
Examples
use camino::Utf8Path;
let path = Utf8Path::new("/laputa/sky_castle.rs");
let path_link = path.read_link_utf8().expect("read_link call failed");
sourcepub fn read_dir(&self) -> Result<ReadDir, Error>
pub fn read_dir(&self) -> Result<ReadDir, Error>
Returns an iterator over the entries within a directory.
The iterator will yield instances of io::Result
<
fs::DirEntry
>
. New
errors may be encountered after an iterator is initially constructed.
This is an alias to fs::read_dir
.
Examples
use camino::Utf8Path;
let path = Utf8Path::new("/laputa");
for entry in path.read_dir().expect("read_dir call failed") {
if let Ok(entry) = entry {
println!("{:?}", entry.path());
}
}
sourcepub fn read_dir_utf8(&self) -> Result<ReadDirUtf8, Error>
pub fn read_dir_utf8(&self) -> Result<ReadDirUtf8, Error>
Returns an iterator over the entries within a directory.
The iterator will yield instances of io::Result
<
Utf8DirEntry
>
. New
errors may be encountered after an iterator is initially constructed.
Errors
The I/O operation may return an error: see the fs::read_dir
documentation for more.
If a directory entry is not UTF-8, an io::Error
is returned with the
ErrorKind
set to InvalidData
and the payload set to a
FromPathBufError
.
Examples
use camino::Utf8Path;
let path = Utf8Path::new("/laputa");
for entry in path.read_dir_utf8().expect("read_dir call failed") {
if let Ok(entry) = entry {
println!("{}", entry.path());
}
}
sourcepub fn exists(&self) -> bool
pub fn exists(&self) -> bool
Returns true
if the path points at an existing entity.
Warning: this method may be error-prone, consider using try_exists()
instead!
It also has a risk of introducing time-of-check to time-of-use (TOCTOU) bugs.
This function will traverse symbolic links to query information about the
destination file. In case of broken symbolic links this will return false
.
If you cannot access the directory containing the file, e.g., because of a
permission error, this will return false
.
Examples
use camino::Utf8Path;
assert!(!Utf8Path::new("does_not_exist.txt").exists());
See Also
This is a convenience function that coerces errors to false. If you want to
check errors, call fs::metadata
.
sourcepub fn try_exists(&self) -> Result<bool, Error>
pub fn try_exists(&self) -> Result<bool, Error>
Returns Ok(true)
if the path points at an existing entity.
This function will traverse symbolic links to query information about the
destination file. In case of broken symbolic links this will return Ok(false)
.
As opposed to the exists()
method, this one doesn’t silently ignore errors
unrelated to the path not existing. (E.g. it will return Err(_)
in case of permission
denied on some of the parent directories.)
Note that while this avoids some pitfalls of the exists()
method, it still can not
prevent time-of-check to time-of-use (TOCTOU) bugs. You should only use it in scenarios
where those bugs are not an issue.
Examples
use camino::Utf8Path;
assert!(!Utf8Path::new("does_not_exist.txt").try_exists().expect("Can't check existence of file does_not_exist.txt"));
assert!(Utf8Path::new("/root/secret_file.txt").try_exists().is_err());
sourcepub fn is_file(&self) -> bool
pub fn is_file(&self) -> bool
Returns true
if the path exists on disk and is pointing at a regular file.
This function will traverse symbolic links to query information about the
destination file. In case of broken symbolic links this will return false
.
If you cannot access the directory containing the file, e.g., because of a
permission error, this will return false
.
Examples
use camino::Utf8Path;
assert_eq!(Utf8Path::new("./is_a_directory/").is_file(), false);
assert_eq!(Utf8Path::new("a_file.txt").is_file(), true);
See Also
This is a convenience function that coerces errors to false. If you want to
check errors, call fs::metadata
and handle its Result
. Then call
fs::Metadata::is_file
if it was Ok
.
When the goal is simply to read from (or write to) the source, the most
reliable way to test the source can be read (or written to) is to open
it. Only using is_file
can break workflows like diff <( prog_a )
on
a Unix-like system for example. See fs::File::open
or
fs::OpenOptions::open
for more information.
sourcepub fn is_dir(&self) -> bool
pub fn is_dir(&self) -> bool
Returns true
if the path exists on disk and is pointing at a directory.
This function will traverse symbolic links to query information about the
destination file. In case of broken symbolic links this will return false
.
If you cannot access the directory containing the file, e.g., because of a
permission error, this will return false
.
Examples
use camino::Utf8Path;
assert_eq!(Utf8Path::new("./is_a_directory/").is_dir(), true);
assert_eq!(Utf8Path::new("a_file.txt").is_dir(), false);
See Also
This is a convenience function that coerces errors to false. If you want to
check errors, call fs::metadata
and handle its Result
. Then call
fs::Metadata::is_dir
if it was Ok
.
sourcepub fn is_symlink(&self) -> bool
pub fn is_symlink(&self) -> bool
Returns true
if the path exists on disk and is pointing at a symbolic link.
This function will not traverse symbolic links. In case of a broken symbolic link this will also return true.
If you cannot access the directory containing the file, e.g., because of a permission error, this will return false.
Examples
use camino::Utf8Path;
use std::os::unix::fs::symlink;
let link_path = Utf8Path::new("link");
symlink("/origin_does_not_exist/", link_path).unwrap();
assert_eq!(link_path.is_symlink(), true);
assert_eq!(link_path.exists(), false);
See Also
This is a convenience function that coerces errors to false. If you want to
check errors, call Utf8Path::symlink_metadata
and handle its Result
. Then call
fs::Metadata::is_symlink
if it was Ok
.