pub struct CanonicalPath(/* private fields */);
Expand description
A reference type for a canonical filesystem path
More specifically: it is at least guaranteed to be canonical at the time it is created. There are potential TOCTTOU problems if the underlying filesystem structure changes after path canonicalization.
Implementations§
Source§impl CanonicalPath
impl CanonicalPath
Sourcepub fn new<P>(path: &P) -> Result<&Self>
pub fn new<P>(path: &P) -> Result<&Self>
Create a canonical path, returning error if the supplied path is not canonical
Sourcepub unsafe fn from_path_unchecked<P>(path: &P) -> &Self
pub unsafe fn from_path_unchecked<P>(path: &P) -> &Self
Create a canonical path from a path, skipping the canonicalization check
This uses the same unsafe reference conversion tricks as std
to
convert from AsRef<Path>
to AsRef<CanonicalPath>
, i.e. &CanonicalPath
is a newtype for &Path
in the same way &Path
is a newtype for &OsStr
.
Sourcepub fn to_canonical_path_buf(&self) -> CanonicalPathBuf
pub fn to_canonical_path_buf(&self) -> CanonicalPathBuf
Convert a canonical path reference into an owned CanonicalPathBuf
Sourcepub fn parent(&self) -> Result<CanonicalPathBuf>
pub fn parent(&self) -> Result<CanonicalPathBuf>
Return a canonical parent path of this path, or io::Error
if the
path is the root directory or another canonicalization error occurs.
Sourcepub fn file_name(&self) -> Option<&OsStr>
pub fn file_name(&self) -> Option<&OsStr>
Returns the final component of the path, if there is one.
Sourcepub fn starts_with<P: AsRef<Path>>(&self, base: P) -> bool
pub fn starts_with<P: AsRef<Path>>(&self, base: P) -> bool
Determines whether base is a prefix of self.
Sourcepub fn ends_with<P: AsRef<Path>>(&self, child: P) -> bool
pub fn ends_with<P: AsRef<Path>>(&self, child: P) -> bool
Determines whether child is a suffix of self.
Sourcepub fn file_stem(&self) -> Option<&OsStr>
pub fn file_stem(&self) -> Option<&OsStr>
Extracts the stem (non-extension) portion of self.file_name
.
Sourcepub fn extension(&self) -> Option<&OsStr>
pub fn extension(&self) -> Option<&OsStr>
Extracts the extension of self.file_name
, if possible.
Sourcepub fn with_file_name<S: AsRef<OsStr>>(
&self,
file_name: S,
) -> Result<CanonicalPathBuf>
pub fn with_file_name<S: AsRef<OsStr>>( &self, file_name: S, ) -> Result<CanonicalPathBuf>
Creates an owned CanonicalPathBuf
like self but with the given file name.
Sourcepub fn with_extension<S: AsRef<OsStr>>(
&self,
extension: S,
) -> Result<CanonicalPathBuf>
pub fn with_extension<S: AsRef<OsStr>>( &self, extension: S, ) -> Result<CanonicalPathBuf>
Creates an owned CanonicalPathBuf
like self but with the given extension.
Sourcepub fn components(&self) -> Components<'_>
pub fn components(&self) -> Components<'_>
Produces an iterator over the Component
s of a path
Sourcepub fn iter(&self) -> Iter<'_>
pub fn iter(&self) -> Iter<'_>
Produces an iterator over the path’s components viewed as
OsStr
slices.
Sourcepub fn display(&self) -> Display<'_>
pub fn display(&self) -> Display<'_>
Returns an object that implements Display
for safely printing
paths that may contain non-Unicode data.
Sourcepub fn metadata(&self) -> Result<Metadata>
pub fn metadata(&self) -> Result<Metadata>
Queries the file system to get information about a file, directory, etc.
Unlike the std
version of this method, it will not follow symlinks,
since as a canonical path we should be symlink-free.
Sourcepub fn join<P: AsRef<Path>>(&self, path: P) -> Result<CanonicalPathBuf>
pub fn join<P: AsRef<Path>>(&self, path: P) -> Result<CanonicalPathBuf>
Join a path onto a canonical path, returning a CanonicalPathBuf
.