gix_fs/
symlink.rs

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