1 use std
::{io, io::ErrorKind::AlreadyExists, path::Path}
;
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
)
12 /// Note that on only on windows this is special.
13 pub fn remove(path
: &Path
) -> io
::Result
<()> {
14 std
::fs
::remove_file(path
)
17 // TODO: use the `symlink` crate once it can delete directory symlinks
20 pub fn remove(path
: &Path
) -> io
::Result
<()> {
21 if let Ok(meta
) = std
::fs
::metadata(path
) {
23 std
::fs
::remove_file(path
) // this removes the link itself
25 std
::fs
::remove_dir(path
) // however, this sees the destination directory, which isn't the right thing actually
28 std
::fs
::remove_file(path
).or_else(|_
| std
::fs
::remove_dir(path
))
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
)
40 symlink_file(original
, link
)
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
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