Skip to main content

eryx_vfs/
perms.rs

1//! Permission bits for VFS descriptors.
2//!
3//! These were re-exported from `wasmtime_wasi` up to wasmtime 47. wasmtime 48
4//! collapsed them into a coarse `FsPerms { ReadOnly, ReadWrite }`, which cannot
5//! express the distinctions the VFS host actually enforces — a directory that
6//! is readable but not mutable, or a file opened write-only. They are vendored
7//! here so the eryx public API and the host's permission checks are decoupled
8//! from wasmtime's own preopen configuration type, which the VFS shadows
9//! anyway. `FsPerms` is used only at the `preopened_dir` boundary.
10//!
11//! The flag values are unchanged from wasmtime-wasi 47, so serialized or
12//! bit-compared values carry over.
13
14bitflags::bitflags! {
15    /// Permission bits for operating on a file.
16    #[derive(Copy, Clone, Debug, PartialEq, Eq)]
17    pub struct FilePerms: usize {
18        /// This file can be read from.
19        const READ = 0b1;
20
21        /// This file can be written to.
22        const WRITE = 0b10;
23    }
24}
25
26bitflags::bitflags! {
27    /// Permission bits for operating on a directory.
28    ///
29    /// Directories can be limited to being readonly. This will restrict what
30    /// can be done with them, for example preventing creation of new files.
31    #[derive(Copy, Clone, Debug, PartialEq, Eq)]
32    pub struct DirPerms: usize {
33        /// This directory can be read, for example its entries can be iterated
34        /// over and files can be opened.
35        const READ = 0b1;
36
37        /// This directory can be mutated, for example by creating new files
38        /// within it.
39        const MUTATE = 0b10;
40    }
41}
42
43impl DirPerms {
44    /// The [`wasmtime_wasi::FsPerms`] that most closely covers these bits.
45    ///
46    /// wasmtime's preopen type only distinguishes readonly from read-write, so
47    /// [`Self::MUTATE`] widens the preopen to `ReadWrite` and the finer-grained
48    /// bits continue to be enforced by the VFS host on each operation.
49    pub fn to_fs_perms(self) -> wasmtime_wasi::FsPerms {
50        if self.contains(Self::MUTATE) {
51            wasmtime_wasi::FsPerms::ReadWrite
52        } else {
53            wasmtime_wasi::FsPerms::ReadOnly
54        }
55    }
56}