cap_primitives/fs/manually/
read_link_one.rs

1use crate::fs::{errors, read_link_unchecked, MAX_SYMLINK_EXPANSIONS};
2use std::ffi::OsStr;
3use std::path::{Path, PathBuf};
4use std::{fs, io};
5
6/// This is a wrapper around `read_link_unchecked` which performs a single
7/// symlink expansion on a single path component, and which enforces the
8/// recursion limit.
9pub(super) fn read_link_one(
10    base: &fs::File,
11    name: &OsStr,
12    symlink_count: &mut u8,
13    reuse: PathBuf,
14) -> io::Result<PathBuf> {
15    let name: &Path = name.as_ref();
16    assert!(
17        name.as_os_str().is_empty() || name.file_name().is_some(),
18        "read_link_one expects a single normal path component, got '{}'",
19        name.display()
20    );
21    assert!(
22        name.as_os_str().is_empty() || name.parent().unwrap().as_os_str().is_empty(),
23        "read_link_one expects a single normal path component, got '{}'",
24        name.display()
25    );
26
27    if *symlink_count == MAX_SYMLINK_EXPANSIONS {
28        return Err(errors::too_many_symlinks());
29    }
30
31    let destination = read_link_unchecked(base, name, reuse)?;
32
33    *symlink_count += 1;
34
35    Ok(destination)
36}