| David Tolnay | 0d85ccd | 2020-08-30 20:18:13 -0700 | [diff] [blame] | 1 | #![allow(dead_code)] |
| 2 | |
| 3 | use std::error::Error as StdError; |
| 4 | use std::fmt::{self, Display}; |
| 5 | use std::io; |
| 6 | use std::path::{Path, PathBuf}; |
| 7 | |
| David Tolnay | f486d56 | 2020-09-02 09:26:32 -0700 | [diff] [blame^] | 8 | pub(crate) type Result<T> = std::result::Result<T, Error>; |
| David Tolnay | 0d85ccd | 2020-08-30 20:18:13 -0700 | [diff] [blame] | 9 | |
| 10 | #[derive(Debug)] |
| 11 | pub(crate) struct Error { |
| 12 | source: io::Error, |
| 13 | message: String, |
| 14 | } |
| 15 | |
| 16 | impl Display for Error { |
| 17 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 18 | formatter.write_str(&self.message) |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | impl StdError for Error { |
| 23 | fn source(&self) -> Option<&(dyn StdError + 'static)> { |
| 24 | Some(&self.source) |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | macro_rules! err { |
| 29 | ($io_error:expr, $fmt:expr $(, $path:expr)* $(,)?) => { |
| 30 | Err(Error { |
| 31 | source: $io_error, |
| 32 | message: format!($fmt $(, $path.display())*), |
| 33 | }) |
| 34 | } |
| 35 | } |
| 36 | |
| David Tolnay | 0d85ccd | 2020-08-30 20:18:13 -0700 | [diff] [blame] | 37 | pub(crate) fn copy(from: impl AsRef<Path>, to: impl AsRef<Path>) -> Result<u64> { |
| 38 | let from = from.as_ref(); |
| 39 | let to = to.as_ref(); |
| 40 | match std::fs::copy(from, to) { |
| 41 | Ok(n) => Ok(n), |
| 42 | Err(e) => err!(e, "Failed to copy `{}` -> `{}`", from, to), |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | pub(crate) fn create_dir_all(path: impl AsRef<Path>) -> Result<()> { |
| 47 | let path = path.as_ref(); |
| 48 | match std::fs::create_dir_all(path) { |
| 49 | Ok(()) => Ok(()), |
| 50 | Err(e) => err!(e, "Failed to create directory `{}`", path), |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | pub(crate) fn current_dir() -> Result<PathBuf> { |
| 55 | match std::env::current_dir() { |
| 56 | Ok(dir) => Ok(dir), |
| 57 | Err(e) => err!(e, "Failed to determine current directory"), |
| 58 | } |
| 59 | } |
| 60 | |
| David Tolnay | dbff3c4 | 2020-08-31 00:41:53 -0700 | [diff] [blame] | 61 | pub(crate) fn read(path: impl AsRef<Path>) -> Result<Vec<u8>> { |
| David Tolnay | 0d85ccd | 2020-08-30 20:18:13 -0700 | [diff] [blame] | 62 | let path = path.as_ref(); |
| David Tolnay | dbff3c4 | 2020-08-31 00:41:53 -0700 | [diff] [blame] | 63 | match std::fs::read(path) { |
| David Tolnay | 0d85ccd | 2020-08-30 20:18:13 -0700 | [diff] [blame] | 64 | Ok(string) => Ok(string), |
| 65 | Err(e) => err!(e, "Failed to read file `{}`", path), |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | pub(crate) fn remove_file(path: impl AsRef<Path>) -> Result<()> { |
| 70 | let path = path.as_ref(); |
| 71 | match std::fs::remove_file(path) { |
| 72 | Ok(()) => Ok(()), |
| 73 | Err(e) => err!(e, "Failed to remove file `{}`", path), |
| 74 | } |
| 75 | } |
| 76 | |
| David Tolnay | b6614db | 2020-09-01 21:17:45 -0700 | [diff] [blame] | 77 | fn symlink<'a>( |
| 78 | src: &'a Path, |
| 79 | dst: &'a Path, |
| 80 | fun: fn(&'a Path, &'a Path) -> io::Result<()>, |
| 81 | ) -> Result<()> { |
| 82 | match fun(src, dst) { |
| David Tolnay | 0d85ccd | 2020-08-30 20:18:13 -0700 | [diff] [blame] | 83 | Ok(()) => Ok(()), |
| 84 | Err(e) => err!( |
| 85 | e, |
| 86 | "Failed to create symlink `{}` pointing to `{}`", |
| 87 | dst, |
| 88 | src, |
| 89 | ), |
| 90 | } |
| 91 | } |
| 92 | |
| David Tolnay | b6614db | 2020-09-01 21:17:45 -0700 | [diff] [blame] | 93 | #[cfg(unix)] |
| 94 | #[allow(unused_imports)] |
| 95 | pub(crate) use self::symlink_file as symlink_dir; |
| 96 | |
| 97 | #[cfg(unix)] |
| 98 | pub(crate) fn symlink_file(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> Result<()> { |
| 99 | symlink(src.as_ref(), dst.as_ref(), std::os::unix::fs::symlink) |
| 100 | } |
| 101 | |
| David Tolnay | 0d85ccd | 2020-08-30 20:18:13 -0700 | [diff] [blame] | 102 | #[cfg(windows)] |
| 103 | pub(crate) fn symlink_file(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> Result<()> { |
| David Tolnay | b6614db | 2020-09-01 21:17:45 -0700 | [diff] [blame] | 104 | symlink( |
| 105 | src.as_ref(), |
| 106 | dst.as_ref(), |
| 107 | std::os::windows::fs::symlink_file, |
| 108 | ) |
| 109 | } |
| 110 | |
| 111 | #[cfg(windows)] |
| 112 | pub(crate) fn symlink_dir(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> Result<()> { |
| 113 | symlink( |
| 114 | src.as_ref(), |
| 115 | dst.as_ref(), |
| 116 | std::os::windows::fs::symlink_dir, |
| 117 | ) |
| David Tolnay | 0d85ccd | 2020-08-30 20:18:13 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | pub(crate) fn write(path: impl AsRef<Path>, contents: impl AsRef<[u8]>) -> Result<()> { |
| 121 | let path = path.as_ref(); |
| 122 | match std::fs::write(path, contents) { |
| 123 | Ok(()) => Ok(()), |
| 124 | Err(e) => err!(e, "Failed to write file `{}`", path), |
| 125 | } |
| 126 | } |