cap_primitives/fs/via_parent/
symlink.rs

1use super::open_parent;
2use crate::fs::MaybeOwnedFile;
3use std::path::Path;
4use std::{fs, io};
5
6/// Implement `symlink` by `open`ing up the parent component of the path and
7/// then calling `symlink_unchecked` on the last component.
8#[cfg(not(windows))]
9pub(crate) fn symlink(old_path: &Path, new_start: &fs::File, new_path: &Path) -> io::Result<()> {
10    use crate::fs::symlink_unchecked;
11    let new_start = MaybeOwnedFile::borrowed(new_start);
12
13    let (new_dir, new_basename) = open_parent(new_start, new_path)?;
14
15    symlink_unchecked(old_path, &new_dir, new_basename.as_ref())
16}
17
18/// Implement `symlink_file` by `open`ing up the parent component of the path
19/// and then calling `symlink_file` on the last component.
20#[cfg(windows)]
21pub(crate) fn symlink_file(
22    old_path: &Path,
23    new_start: &fs::File,
24    new_path: &Path,
25) -> io::Result<()> {
26    use crate::fs::symlink_file_unchecked;
27    let new_start = MaybeOwnedFile::borrowed(new_start);
28
29    let (new_dir, new_basename) = open_parent(new_start, new_path)?;
30
31    symlink_file_unchecked(old_path, &new_dir, new_basename.as_ref())
32}
33
34/// Implement `symlink_dir` by `open`ing up the parent component of the path
35/// and then calling `symlink_dir` on the last component.
36#[cfg(windows)]
37pub(crate) fn symlink_dir(
38    old_path: &Path,
39    new_start: &fs::File,
40    new_path: &Path,
41) -> io::Result<()> {
42    use crate::fs::symlink_dir_unchecked;
43    let new_start = MaybeOwnedFile::borrowed(new_start);
44
45    let (new_dir, new_basename) = open_parent(new_start, new_path)?;
46
47    symlink_dir_unchecked(old_path, &new_dir, new_basename.as_ref())
48}