]> git.proxmox.com Git - rustc.git/blob - vendor/gix-fs/src/symlink.rs
New upstream version 1.71.1+dfsg1
[rustc.git] / vendor / gix-fs / src / symlink.rs
1 use std::{io, io::ErrorKind::AlreadyExists, path::Path};
2
3 #[cfg(not(windows))]
4 /// Create a new symlink at `link` which points to `original`.
5 pub fn create(original: &Path, link: &Path) -> io::Result<()> {
6 std::os::unix::fs::symlink(original, link)
7 }
8
9 #[cfg(not(windows))]
10 /// Remove a symlink.
11 ///
12 /// Note that on only on windows this is special.
13 pub fn remove(path: &Path) -> io::Result<()> {
14 std::fs::remove_file(path)
15 }
16
17 // TODO: use the `symlink` crate once it can delete directory symlinks
18 /// Remove a symlink.
19 #[cfg(windows)]
20 pub fn remove(path: &Path) -> io::Result<()> {
21 if let Ok(meta) = std::fs::metadata(path) {
22 if meta.is_file() {
23 std::fs::remove_file(path) // this removes the link itself
24 } else {
25 std::fs::remove_dir(path) // however, this sees the destination directory, which isn't the right thing actually
26 }
27 } else {
28 std::fs::remove_file(path).or_else(|_| std::fs::remove_dir(path))
29 }
30 }
31
32 #[cfg(windows)]
33 /// Create a new symlink at `link` which points to `original`.
34 pub fn create(original: &Path, link: &Path) -> io::Result<()> {
35 use std::os::windows::fs::{symlink_dir, symlink_file};
36 // TODO: figure out if links to links count as files or whatever they point at
37 if std::fs::metadata(link.parent().expect("dir for link").join(original))?.is_dir() {
38 symlink_dir(original, link)
39 } else {
40 symlink_file(original, link)
41 }
42 }
43
44 #[cfg(not(windows))]
45 /// Return true if `err` indicates that a file collision happened, i.e. a symlink couldn't be created as the `link`
46 /// already exists as filesystem object.
47 pub fn is_collision_error(err: &std::io::Error) -> bool {
48 // TODO: use ::IsDirectory as well when stabilized instead of raw_os_error(), and ::FileSystemLoop respectively
49 err.kind() == AlreadyExists
50 || err.raw_os_error() == Some(21)
51 || err.raw_os_error() == Some(62) // no-follow on symlnk on mac-os
52 || err.raw_os_error() == Some(40) // no-follow on symlnk on ubuntu
53 }
54
55 #[cfg(windows)]
56 /// Return true if `err` indicates that a file collision happened, i.e. a symlink couldn't be created as the `link`
57 /// already exists as filesystem object.
58 pub fn is_collision_error(err: &std::io::Error) -> bool {
59 err.kind() == AlreadyExists || err.kind() == std::io::ErrorKind::PermissionDenied
60 }