Struct cap_primitives::fs::DirEntry
source · pub struct DirEntry { /* private fields */ }
Expand description
Entries returned by the ReadDir
iterator.
This corresponds to std::fs::DirEntry
.
Unlike std::fs::DirEntry
, this API has no DirEntry::path
, because
absolute paths don’t interoperate well with the capability model.
There is a file_name
function, however there are also open
,
open_with
, open_dir
, remove_file
, and remove_dir
functions for
opening or removing the entry directly, which can be more efficient and
convenient.
There is no from_std
method, as std::fs::DirEntry
doesn’t provide a way
to construct a DirEntry
without opening directories by ambient paths.
Implementations§
source§impl DirEntry
impl DirEntry
sourcepub fn open_with(&self, options: &OpenOptions) -> Result<File>
pub fn open_with(&self, options: &OpenOptions) -> Result<File>
Open the file with the given options.
Examples found in repository?
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
pub fn open(&self) -> io::Result<fs::File> {
self.open_with(OpenOptions::new().read(true))
}
/// Open the file with the given options.
#[inline]
pub fn open_with(&self, options: &OpenOptions) -> io::Result<fs::File> {
self.inner.open(options)
}
/// Open the entry as a directory.
#[inline]
pub fn open_dir(&self) -> io::Result<fs::File> {
self.open_with(&dir_options())
}
sourcepub fn remove_file(&self) -> Result<()>
pub fn remove_file(&self) -> Result<()>
Removes the file from its filesystem.
Examples found in repository?
30 31 32 33 34 35 36 37 38 39 40 41
fn remove_dir_all_recursive(children: ReadDir) -> io::Result<()> {
for child in children {
let child = child?;
if child.file_type()?.is_dir() {
remove_dir_all_recursive(child.inner.read_dir(FollowSymlinks::No)?)?;
child.remove_dir()?;
} else {
child.remove_file()?;
}
}
Ok(())
}
sourcepub fn remove_dir(&self) -> Result<()>
pub fn remove_dir(&self) -> Result<()>
Removes the directory from its filesystem.
Examples found in repository?
30 31 32 33 34 35 36 37 38 39 40 41
fn remove_dir_all_recursive(children: ReadDir) -> io::Result<()> {
for child in children {
let child = child?;
if child.file_type()?.is_dir() {
remove_dir_all_recursive(child.inner.read_dir(FollowSymlinks::No)?)?;
child.remove_dir()?;
} else {
child.remove_file()?;
}
}
Ok(())
}
More examples
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
pub(crate) fn remove_open_dir_by_searching(dir: fs::File) -> io::Result<()> {
let metadata = Metadata::from_file(&dir)?;
let mut iter = read_dir_unchecked(&dir, Component::ParentDir.as_ref(), FollowSymlinks::No)?;
while let Some(child) = iter.next() {
let child = child?;
if child.is_same_file(&metadata)? {
return child.remove_dir();
}
}
// We didn't find the directory among its parent's children. Check for the
// root directory and handle it specially -- removal will probably fail, so
// we'll get the appropriate error code.
if is_root_dir(&dir, &iter)? {
fs::remove_dir(Component::RootDir.as_os_str())
} else {
Err(errors::no_such_file_or_directory())
}
}
sourcepub fn read_dir(&self) -> Result<ReadDir>
pub fn read_dir(&self) -> Result<ReadDir>
Returns an iterator over the entries within the subdirectory.
sourcepub fn metadata(&self) -> Result<Metadata>
pub fn metadata(&self) -> Result<Metadata>
Returns the metadata for the file that this entry points at.
This corresponds to std::fs::DirEntry::metadata
.
Platform-specific behavior
On Windows, this produces a Metadata
object which does not contain
the optional values returned by MetadataExt
. Use
cap_fs_ext::DirEntryExt::full_metadata
to obtain a Metadata
with
the values filled in.
sourcepub fn file_type(&self) -> Result<FileType>
pub fn file_type(&self) -> Result<FileType>
Returns the file type for the file that this entry points at.
This corresponds to std::fs::DirEntry::file_type
.
Examples found in repository?
30 31 32 33 34 35 36 37 38 39 40 41
fn remove_dir_all_recursive(children: ReadDir) -> io::Result<()> {
for child in children {
let child = child?;
if child.file_type()?.is_dir() {
remove_dir_all_recursive(child.inner.read_dir(FollowSymlinks::No)?)?;
child.remove_dir()?;
} else {
child.remove_file()?;
}
}
Ok(())
}
sourcepub fn file_name(&self) -> OsString
pub fn file_name(&self) -> OsString
Returns the bare file name of this directory entry without any other leading path component.
This corresponds to std::fs::DirEntry::file_name
.