Sha256: 758e01bf503b343174dd3a03352fc5e477deaccde6afe91e137e415a7d92d9f1

Contents?: true

Size: 1.43 KB

Versions: 20

Compression:

Stored size: 1.43 KB

Contents

use crate::fs::{errors, is_root_dir, read_dir_unchecked, FollowSymlinks, Metadata};
use std::path::Component;
use std::{fs, io};

/// Delete the directory referenced by the given handle by searching for it in
/// its `..`. This requires search permission in `..`, but that's usually
/// available.
pub(crate) fn remove_open_dir_by_searching(dir: fs::File) -> io::Result<()> {
    let metadata = Metadata::from_file(&dir)?;
    let mut iter = read_dir_unchecked(&dir, Component::ParentDir.as_ref(), FollowSymlinks::No)?;
    while let Some(child) = iter.next() {
        let child = child?;

        // Test if the child we found by iteration matches the directory we're
        // looking for. Ignore `NotFound` errors, which can happen if another
        // process removes a different directory in the same parent.
        let same = match child.is_same_file(&metadata) {
            Ok(same) => same,
            Err(err) if err.kind() == std::io::ErrorKind::NotFound => false,
            Err(err) => Err(err)?,
        };

        if same {
            return child.remove_dir();
        }
    }

    // We didn't find the directory among its parent's children. Check for the
    // root directory and handle it specially -- removal will probably fail, so
    // we'll get the appropriate error code.
    if is_root_dir(&dir, &iter)? {
        fs::remove_dir(Component::RootDir.as_os_str())
    } else {
        Err(errors::no_such_file_or_directory())
    }
}

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
wasmtime-30.0.2 ./ext/cargo-vendor/cap-primitives-3.4.2/src/rustix/fs/remove_open_dir_by_searching.rs
wasmtime-29.0.0 ./ext/cargo-vendor/cap-primitives-3.4.2/src/rustix/fs/remove_open_dir_by_searching.rs
wasmtime-28.0.0 ./ext/cargo-vendor/cap-primitives-3.4.2/src/rustix/fs/remove_open_dir_by_searching.rs
wasmtime-27.0.0 ./ext/cargo-vendor/cap-primitives-3.4.1/src/rustix/fs/remove_open_dir_by_searching.rs
wasmtime-26.0.0 ./ext/cargo-vendor/cap-primitives-3.3.0/src/rustix/fs/remove_open_dir_by_searching.rs
wasmtime-25.0.2 ./ext/cargo-vendor/cap-primitives-3.2.0/src/rustix/fs/remove_open_dir_by_searching.rs
wasmtime-25.0.1 ./ext/cargo-vendor/cap-primitives-3.2.0/src/rustix/fs/remove_open_dir_by_searching.rs
wasmtime-25.0.0 ./ext/cargo-vendor/cap-primitives-3.2.0/src/rustix/fs/remove_open_dir_by_searching.rs
wasmtime-24.0.0 ./ext/cargo-vendor/cap-primitives-3.2.0/src/rustix/fs/remove_open_dir_by_searching.rs
wasmtime-23.0.2 ./ext/cargo-vendor/cap-primitives-3.1.0/src/rustix/fs/remove_open_dir_by_searching.rs
wasmtime-22.0.0 ./ext/cargo-vendor/cap-primitives-3.1.0/src/rustix/fs/remove_open_dir_by_searching.rs
wasmtime-21.0.1 ./ext/cargo-vendor/cap-primitives-3.1.0/src/rustix/fs/remove_open_dir_by_searching.rs
wasmtime-20.0.2 ./ext/cargo-vendor/cap-primitives-3.1.0/src/rustix/fs/remove_open_dir_by_searching.rs
wasmtime-20.0.0 ./ext/cargo-vendor/cap-primitives-3.0.0/src/rustix/fs/remove_open_dir_by_searching.rs
wasmtime-18.0.3 ./ext/cargo-vendor/cap-primitives-2.0.1/src/rustix/fs/remove_open_dir_by_searching.rs
wasmtime-17.0.1 ./ext/cargo-vendor/cap-primitives-2.0.1/src/rustix/fs/remove_open_dir_by_searching.rs
wasmtime-17.0.0 ./ext/cargo-vendor/cap-primitives-2.0.1/src/rustix/fs/remove_open_dir_by_searching.rs
wasmtime-16.0.0 ./ext/cargo-vendor/cap-primitives-2.0.1/src/rustix/fs/remove_open_dir_by_searching.rs
wasmtime-15.0.1 ./ext/cargo-vendor/cap-primitives-2.0.1/src/rustix/fs/remove_open_dir_by_searching.rs
wasmtime-15.0.0 ./ext/cargo-vendor/cap-primitives-2.0.1/src/rustix/fs/remove_open_dir_by_searching.rs