Sha256: b8f009bcd9c7fe544d4aeacde78e7bd74d03b4ab69c285e1b878c120d299fe34

Contents?: true

Size: 1.9 KB

Versions: 13

Compression:

Stored size: 1.9 KB

Contents

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

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

#[cfg(not(target_os = "wasi"))]
impl ImplPermissionsExt {
    /// 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 crate::fs::PermissionsExt for ImplPermissionsExt {
    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 ImplPermissionsExt {
    pub(crate) fn default() -> Permissions {
        Permissions { readonly: false }
    }
}

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
wasmtime-29.0.0 ./ext/cargo-vendor/cap-primitives-3.4.2/src/rustix/fs/permissions_ext.rs
wasmtime-28.0.0 ./ext/cargo-vendor/cap-primitives-3.4.2/src/rustix/fs/permissions_ext.rs
wasmtime-27.0.0 ./ext/cargo-vendor/cap-primitives-3.4.1/src/rustix/fs/permissions_ext.rs
wasmtime-26.0.0 ./ext/cargo-vendor/cap-primitives-3.3.0/src/rustix/fs/permissions_ext.rs
wasmtime-25.0.2 ./ext/cargo-vendor/cap-primitives-3.2.0/src/rustix/fs/permissions_ext.rs
wasmtime-25.0.1 ./ext/cargo-vendor/cap-primitives-3.2.0/src/rustix/fs/permissions_ext.rs
wasmtime-25.0.0 ./ext/cargo-vendor/cap-primitives-3.2.0/src/rustix/fs/permissions_ext.rs
wasmtime-24.0.0 ./ext/cargo-vendor/cap-primitives-3.2.0/src/rustix/fs/permissions_ext.rs
wasmtime-23.0.2 ./ext/cargo-vendor/cap-primitives-3.1.0/src/rustix/fs/permissions_ext.rs
wasmtime-22.0.0 ./ext/cargo-vendor/cap-primitives-3.1.0/src/rustix/fs/permissions_ext.rs
wasmtime-21.0.1 ./ext/cargo-vendor/cap-primitives-3.1.0/src/rustix/fs/permissions_ext.rs
wasmtime-20.0.2 ./ext/cargo-vendor/cap-primitives-3.1.0/src/rustix/fs/permissions_ext.rs
wasmtime-20.0.0 ./ext/cargo-vendor/cap-primitives-3.0.0/src/rustix/fs/permissions_ext.rs