Sha256: c6d1600afa85c87492313fedb259c6be964044ef76577c607ec625715097ea6c

Contents?: true

Size: 1.89 KB

Versions: 25

Compression:

Stored size: 1.89 KB

Contents

use crate::fs::Permissions;
use rustix::fs::RawMode;
use std::fs;

#[derive(Debug, Clone, Eq, PartialEq)]
pub(crate) struct PermissionsExt {
    #[cfg(not(target_os = "wasi"))]
    mode: RawMode,
}

#[cfg(not(target_os = "wasi"))]
impl PermissionsExt {
    /// Constructs a new instance of `Self` from the given
    /// [`std::fs::Permissions`].
    #[inline]
    pub(crate) fn from_std(std: fs::Permissions) -> Self {
        use std::os::unix::fs::PermissionsExt;
        Self {
            mode: std.mode() as RawMode,
        }
    }

    /// Constructs a new instance of `Permissions` from the given
    /// `RawMode`.
    #[inline]
    pub(crate) const fn from_raw_mode(mode: RawMode) -> Permissions {
        Permissions {
            readonly: Self::readonly(mode),
            ext: Self { mode },
        }
    }

    /// Test whether the given `RawMode` lacks write permissions.
    #[inline]
    pub(crate) const fn readonly(mode: RawMode) -> bool {
        mode & 0o222 == 0
    }

    /// Test whether the given `RawMode` lacks write permissions.
    #[inline]
    pub(crate) fn set_readonly(&mut self, readonly: bool) {
        if readonly {
            // remove write permission for all classes; equivalent to `chmod a-w <file>`
            self.mode &= !0o222;
        } else {
            // add write permission for all classes; equivalent to `chmod a+w <file>`
            self.mode |= 0o222;
        }
    }
}

#[cfg(not(target_os = "wasi"))]
impl std::os::unix::fs::PermissionsExt for PermissionsExt {
    fn mode(&self) -> u32 {
        self.mode as u32
    }

    fn set_mode(&mut self, mode: u32) {
        self.mode = mode as RawMode & 0o7777;
    }

    fn from_mode(mode: u32) -> Self {
        Self {
            mode: mode as RawMode & 0o7777,
        }
    }
}

#[cfg(target_os = "wasi")]
impl PermissionsExt {
    pub(crate) fn default() -> Permissions {
        Permissions { readonly: false }
    }
}

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
wasmtime-18.0.3 ./ext/cargo-vendor/cap-primitives-2.0.1/src/rustix/fs/permissions_ext.rs
wasmtime-17.0.1 ./ext/cargo-vendor/cap-primitives-2.0.1/src/rustix/fs/permissions_ext.rs
wasmtime-17.0.0 ./ext/cargo-vendor/cap-primitives-2.0.1/src/rustix/fs/permissions_ext.rs
wasmtime-16.0.0 ./ext/cargo-vendor/cap-primitives-2.0.1/src/rustix/fs/permissions_ext.rs
wasmtime-15.0.1 ./ext/cargo-vendor/cap-primitives-2.0.1/src/rustix/fs/permissions_ext.rs
wasmtime-15.0.0 ./ext/cargo-vendor/cap-primitives-2.0.1/src/rustix/fs/permissions_ext.rs
wasmtime-14.0.4 ./ext/cargo-vendor/cap-primitives-2.0.0/src/rustix/fs/permissions_ext.rs
wasmtime-14.0.3 ./ext/cargo-vendor/cap-primitives-2.0.0/src/rustix/fs/permissions_ext.rs
wasmtime-14.0.1 ./ext/cargo-vendor/cap-primitives-2.0.0/src/rustix/fs/permissions_ext.rs
wasmtime-14.0.0 ./ext/cargo-vendor/cap-primitives-2.0.0/src/rustix/fs/permissions_ext.rs
wasmtime-13.0.0 ./ext/cargo-vendor/cap-primitives-2.0.0/src/rustix/fs/permissions_ext.rs
wasmtime-12.0.1 ./ext/cargo-vendor/cap-primitives-2.0.0/src/rustix/fs/permissions_ext.rs
wasmtime-12.0.0 ./ext/cargo-vendor/cap-primitives-2.0.0/src/rustix/fs/permissions_ext.rs
wasmtime-11.0.0 ./ext/cargo-vendor/cap-primitives-1.0.15/src/rustix/fs/permissions_ext.rs
wasmtime-10.0.1 ./ext/cargo-vendor/cap-primitives-1.0.15/src/rustix/fs/permissions_ext.rs
wasmtime-10.0.0 ./ext/cargo-vendor/cap-primitives-1.0.15/src/rustix/fs/permissions_ext.rs
wasmtime-9.0.4 ./ext/cargo-vendor/cap-primitives-1.0.15/src/rustix/fs/permissions_ext.rs
wasmtime-9.0.1 ./ext/cargo-vendor/cap-primitives-1.0.15/src/rustix/fs/permissions_ext.rs
wasmtime-8.0.0 ./ext/cargo-vendor/cap-primitives-1.0.14/src/rustix/fs/permissions_ext.rs
wasmtime-7.0.0 ./ext/cargo-vendor/cap-primitives-1.0.9/src/rustix/fs/permissions_ext.rs