Sha256: b1d0604d5c58e447df2e3a09d00554d49410e5f72afa30cca4145fcdafc08bb7

Contents?: true

Size: 1.42 KB

Versions: 6

Compression:

Stored size: 1.42 KB

Contents

//! Linux has an `O_PATH` flag which allows opening a file without necessary
//! having read or write access to it; we can use that with `openat2` and
//! `fstat` to perform a fast sandboxed `stat`.

use super::file_metadata;
use crate::fs::{manually, open_beneath, FollowSymlinks, Metadata, OpenOptions};
use rustix::fs::OFlags;
use std::path::Path;
use std::{fs, io};

/// Use `openat2` with `O_PATH` and `fstat`. If that's not available, fallback
/// to `manually::stat`.
pub(crate) fn stat_impl(
    start: &fs::File,
    path: &Path,
    follow: FollowSymlinks,
) -> io::Result<Metadata> {
    use std::os::unix::fs::OpenOptionsExt;

    // Open the path with `O_PATH`. Use `read(true)` even though we don't need
    // `read` permissions, because Rust's libstd requires an access mode, and
    // Linux ignores `O_RDONLY` with `O_PATH`.
    let result = open_beneath(
        start,
        path,
        OpenOptions::new()
            .read(true)
            .follow(follow)
            .custom_flags(OFlags::PATH.bits() as i32),
    );

    // If that worked, call `fstat`.
    match result {
        Ok(file) => file_metadata(&file),
        Err(err) => match rustix::io::Errno::from_io_error(&err) {
            // `ENOSYS` from `open_beneath` means `openat2` is unavailable
            // and we should use a fallback.
            Some(rustix::io::Errno::NOSYS) => manually::stat(start, path, follow),
            _ => Err(err),
        },
    }
}

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
wasmtime-7.0.0 ./ext/cargo-vendor/cap-primitives-1.0.9/src/rustix/linux/fs/stat_impl.rs
wasmtime-6.0.1 ./ext/cargo-vendor/cap-primitives-1.0.5/src/rustix/linux/fs/stat_impl.rs
wasmtime-6.0.0 ./ext/cargo-vendor/cap-primitives-1.0.5/src/rustix/linux/fs/stat_impl.rs
wasmtime-5.0.0 ./ext/cargo-vendor/cap-primitives-1.0.4/src/rustix/linux/fs/stat_impl.rs
wasmtime-0.4.1 ./ext/cargo-vendor/cap-primitives-1.0.3/src/rustix/linux/fs/stat_impl.rs
wasmtime-0.4.0 ./ext/cargo-vendor/cap-primitives-1.0.2/src/rustix/linux/fs/stat_impl.rs