cap_primitives/rustix/fs/
access_unchecked.rs

1use crate::fs::{AccessType, FollowSymlinks};
2use rustix::fs::{Access, AtFlags};
3use std::path::Path;
4use std::{fs, io};
5
6/// *Unsandboxed* function similar to `access`, but which does not perform
7/// sandboxing.
8pub(crate) fn access_unchecked(
9    start: &fs::File,
10    path: &Path,
11    type_: AccessType,
12    follow: FollowSymlinks,
13) -> io::Result<()> {
14    let mut access_type = Access::empty();
15    match type_ {
16        AccessType::Exists => access_type |= Access::EXISTS,
17        AccessType::Access(modes) => {
18            if modes.readable {
19                access_type |= Access::READ_OK;
20            }
21            if modes.writable {
22                access_type |= Access::WRITE_OK;
23            }
24            if modes.executable {
25                access_type |= Access::EXEC_OK;
26            }
27        }
28    }
29
30    let atflags = match follow {
31        FollowSymlinks::Yes => AtFlags::empty(),
32        FollowSymlinks::No => AtFlags::SYMLINK_NOFOLLOW,
33    };
34
35    rustix::fs::accessat(start, path, access_type, atflags)?;
36
37    Ok(())
38}