Sha256: e921129f8ccd4af143d3c95e9ced6401961970090b54971defb2c40efd80565c

Contents?: true

Size: 1.15 KB

Versions: 13

Compression:

Stored size: 1.15 KB

Contents

use crate::fs::{ImplMetadataExt, Metadata};
use rustix::fs::{statat, AtFlags};
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering::Relaxed;
use std::{fs, io};

/// Like `file.metadata()`, but works with `O_PATH` descriptors on old (pre
/// 3.6) versions of Linux too.
pub(super) fn file_metadata(file: &fs::File) -> io::Result<Metadata> {
    // Record whether we've seen an `EBADF` from an `fstat` on an `O_PATH`
    // file descriptor, meaning we're on a Linux that doesn't support it.
    static FSTAT_PATH_BADF: AtomicBool = AtomicBool::new(false);

    if !FSTAT_PATH_BADF.load(Relaxed) {
        match Metadata::from_file(file) {
            Ok(metadata) => return Ok(metadata),
            Err(err) => match rustix::io::Errno::from_io_error(&err) {
                // Before Linux 3.6, `fstat` with `O_PATH` returned `EBADF`.
                Some(rustix::io::Errno::BADF) => FSTAT_PATH_BADF.store(true, Relaxed),
                _ => return Err(err),
            },
        }
    }

    // If `fstat` with `O_PATH` isn't supported, use `statat` with `AT_EMPTY_PATH`.
    Ok(statat(file, "", AtFlags::EMPTY_PATH).map(ImplMetadataExt::from_rustix)?)
}

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/linux/fs/file_metadata.rs
wasmtime-28.0.0 ./ext/cargo-vendor/cap-primitives-3.4.2/src/rustix/linux/fs/file_metadata.rs
wasmtime-27.0.0 ./ext/cargo-vendor/cap-primitives-3.4.1/src/rustix/linux/fs/file_metadata.rs
wasmtime-26.0.0 ./ext/cargo-vendor/cap-primitives-3.3.0/src/rustix/linux/fs/file_metadata.rs
wasmtime-25.0.2 ./ext/cargo-vendor/cap-primitives-3.2.0/src/rustix/linux/fs/file_metadata.rs
wasmtime-25.0.1 ./ext/cargo-vendor/cap-primitives-3.2.0/src/rustix/linux/fs/file_metadata.rs
wasmtime-25.0.0 ./ext/cargo-vendor/cap-primitives-3.2.0/src/rustix/linux/fs/file_metadata.rs
wasmtime-24.0.0 ./ext/cargo-vendor/cap-primitives-3.2.0/src/rustix/linux/fs/file_metadata.rs
wasmtime-23.0.2 ./ext/cargo-vendor/cap-primitives-3.1.0/src/rustix/linux/fs/file_metadata.rs
wasmtime-22.0.0 ./ext/cargo-vendor/cap-primitives-3.1.0/src/rustix/linux/fs/file_metadata.rs
wasmtime-21.0.1 ./ext/cargo-vendor/cap-primitives-3.1.0/src/rustix/linux/fs/file_metadata.rs
wasmtime-20.0.2 ./ext/cargo-vendor/cap-primitives-3.1.0/src/rustix/linux/fs/file_metadata.rs
wasmtime-20.0.0 ./ext/cargo-vendor/cap-primitives-3.0.0/src/rustix/linux/fs/file_metadata.rs