| @@ -1,16 +1,21 @@ |
| 1 | use std::{io, io::ErrorKind::AlreadyExists, path::Path}; | 1 | use std::{io, io::ErrorKind::AlreadyExists, path::Path}; |
| 2 | | 2 | |
| 3 | #[cfg(not(windows))] | 3 | #[cfg(not(windows))] |
| | 4 | /// Create a new symlink at `link` which points to `original`. |
| 4 | pub fn create(original: &Path, link: &Path) -> io::Result<()> { | 5 | pub fn create(original: &Path, link: &Path) -> io::Result<()> { |
| 5 | std::os::unix::fs::symlink(original, link) | 6 | std::os::unix::fs::symlink(original, link) |
| 6 | } | 7 | } |
| 7 | | 8 | |
| 8 | #[cfg(not(windows))] | 9 | #[cfg(not(windows))] |
| | 10 | /// Remove a symlink. |
| | 11 | /// |
| | 12 | /// Note that on only on windows this is special. |
| 9 | pub fn remove(path: &Path) -> io::Result<()> { | 13 | pub fn remove(path: &Path) -> io::Result<()> { |
| 10 | std::fs::remove_file(path) | 14 | std::fs::remove_file(path) |
| 11 | } | 15 | } |
| 12 | | 16 | |
| 13 | // TODO: use the `symlink` crate once it can delete directory symlinks | 17 | // TODO: use the `symlink` crate once it can delete directory symlinks |
| | 18 | /// Remove a symlink. |
| 14 | #[cfg(windows)] | 19 | #[cfg(windows)] |
| 15 | pub fn remove(path: &Path) -> io::Result<()> { | 20 | pub fn remove(path: &Path) -> io::Result<()> { |
| 16 | if let Ok(meta) = std::fs::metadata(path) { | 21 | if let Ok(meta) = std::fs::metadata(path) { |
| @@ -25,6 +30,7 @@ pub fn remove(path: &Path) -> io::Result<()> { · 8 unmodified lines |
| 25 | } | 30 | } |
| 26 | | 31 | |
| 27 | #[cfg(windows)] | 32 | #[cfg(windows)] |
| | 33 | /// Create a new symlink at `link` which points to `original`. |
| 28 | pub fn create(original: &Path, link: &Path) -> io::Result<()> { | 34 | pub fn create(original: &Path, link: &Path) -> io::Result<()> { |
| 29 | use std::os::windows::fs::{symlink_dir, symlink_file}; | 35 | use std::os::windows::fs::{symlink_dir, symlink_file}; |
| 30 | // TODO: figure out if links to links count as files or whatever they point at | 36 | // TODO: figure out if links to links count as files or whatever they point at |
| @@ -36,6 +42,8 @@ pub fn create(original: &Path, link: &Path) -> io::Result<()> { · 5 unmodified lines |
| 36 | } | 42 | } |
| 37 | | 43 | |
| 38 | #[cfg(not(windows))] | 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. |
| 39 | pub fn is_collision_error(err: &std::io::Error) -> bool { | 47 | pub fn is_collision_error(err: &std::io::Error) -> bool { |
| 40 | // TODO: use ::IsDirectory as well when stabilized instead of raw_os_error(), and ::FileSystemLoop respectively | 48 | // TODO: use ::IsDirectory as well when stabilized instead of raw_os_error(), and ::FileSystemLoop respectively |
| 41 | err.kind() == AlreadyExists | 49 | err.kind() == AlreadyExists |
| @@ -45,6 +53,8 @@ pub fn is_collision_error(err: &std::io::Error) -> bool { · 3 unmodified lines |
| 45 | } | 53 | } |
| 46 | | 54 | |
| 47 | #[cfg(windows)] | 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. |
| 48 | pub fn is_collision_error(err: &std::io::Error) -> bool { | 58 | pub fn is_collision_error(err: &std::io::Error) -> bool { |
| 49 | err.kind() == AlreadyExists || err.kind() == std::io::ErrorKind::PermissionDenied | 59 | err.kind() == AlreadyExists || err.kind() == std::io::ErrorKind::PermissionDenied |
| 50 | } | 60 | } |