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
.
Examples found in repository?
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
pub(crate) fn file_path_by_searching(file: &fs::File) -> Option<PathBuf> {
// Use the `_noassert` functions because the asserts depend on `file_path`,
// which is what we're implementing here.
let mut base = MaybeOwnedFile::borrowed_noassert(file);
let mut components = Vec::new();
// Iterate with `..` until we reach the root directory.
'next_component: loop {
// Open `..`.
let mut iter =
read_dir_unchecked(&base, Component::ParentDir.as_ref(), FollowSymlinks::No).ok()?;
let metadata = Metadata::from_file(&*base).ok()?;
// Search the children until we find one with matching metadata, and
// then record its name.
while let Some(child) = iter.next() {
let child = child.ok()?;
if child.is_same_file(&metadata).ok()? {
// Found a match. Record the name and continue to the next component.
components.push(child.file_name());
base = MaybeOwnedFile::owned_noassert(
open_dir_unchecked(&base, Component::ParentDir.as_ref()).ok()?,
);
continue 'next_component;
}
}
// We didn't find the directory among its parent's children. If we're at
// the root directory, we're done.
if is_root_dir(&base, &iter).ok()? {
break;
}
// Otherwise, something went wrong and we can't determine the path.
return None;
}
let mut path = PathBuf::new();
path.push(Component::RootDir);
for component in components.iter().rev() {
path.push(component);
}
Some(path)
}