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§

Open the file for reading.

Open the file with the given options.

Examples found in repository?
src/fs/dir_entry.rs (line 31)
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())
    }

Open the entry as a directory.

Removes the file from its filesystem.

Examples found in repository?
src/rustix/fs/remove_dir_all_impl.rs (line 37)
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(())
}

Removes the directory from its filesystem.

Examples found in repository?
src/rustix/fs/remove_dir_all_impl.rs (line 35)
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
Hide additional examples
src/rustix/fs/remove_open_dir_by_searching.rs (line 14)
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())
    }
}

Returns an iterator over the entries within the subdirectory.

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.

Returns the file type for the file that this entry points at.

This corresponds to std::fs::DirEntry::file_type.

Examples found in repository?
src/rustix/fs/remove_dir_all_impl.rs (line 33)
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(())
}

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?
src/fs/file_path_by_searching.rs (line 28)
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)
}

Trait Implementations§

Formats the value using the given formatter. Read more
Returns the underlying d_ino field in the contained dirent structure. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.