| 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}; |
| David Tolnay | c0e07dc | 2020-09-02 15:39:28 -0700 | [diff] [blame] | 5 | use std::io::{self, Read}; |
| David Tolnay | 0d85ccd | 2020-08-30 20:18:13 -0700 | [diff] [blame] | 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 { |
| David Tolnay | ff21d91 | 2020-11-14 15:41:54 -0800 | [diff] [blame] | 12 | source: Option<io::Error>, |
| David Tolnay | 0d85ccd | 2020-08-30 20:18:13 -0700 | [diff] [blame] | 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)> { |
| David Tolnay | ff21d91 | 2020-11-14 15:41:54 -0800 | [diff] [blame] | 24 | let source = self.source.as_ref()?; |
| 25 | Some(source) |
| David Tolnay | 0d85ccd | 2020-08-30 20:18:13 -0700 | [diff] [blame] | 26 | } |
| 27 | } |
| 28 | |
| 29 | macro_rules! err { |
| 30 | ($io_error:expr, $fmt:expr $(, $path:expr)* $(,)?) => { |
| 31 | Err(Error { |
| David Tolnay | ff21d91 | 2020-11-14 15:41:54 -0800 | [diff] [blame] | 32 | source: Option::from($io_error), |
| David Tolnay | 0d85ccd | 2020-08-30 20:18:13 -0700 | [diff] [blame] | 33 | message: format!($fmt $(, $path.display())*), |
| 34 | }) |
| 35 | } |
| 36 | } |
| 37 | |
| David Tolnay | 0d85ccd | 2020-08-30 20:18:13 -0700 | [diff] [blame] | 38 | pub(crate) fn copy(from: impl AsRef<Path>, to: impl AsRef<Path>) -> Result<u64> { |
| 39 | let from = from.as_ref(); |
| 40 | let to = to.as_ref(); |
| 41 | match std::fs::copy(from, to) { |
| 42 | Ok(n) => Ok(n), |
| 43 | Err(e) => err!(e, "Failed to copy `{}` -> `{}`", from, to), |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | pub(crate) fn create_dir_all(path: impl AsRef<Path>) -> Result<()> { |
| 48 | let path = path.as_ref(); |
| 49 | match std::fs::create_dir_all(path) { |
| 50 | Ok(()) => Ok(()), |
| 51 | Err(e) => err!(e, "Failed to create directory `{}`", path), |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | pub(crate) fn current_dir() -> Result<PathBuf> { |
| 56 | match std::env::current_dir() { |
| 57 | Ok(dir) => Ok(dir), |
| 58 | Err(e) => err!(e, "Failed to determine current directory"), |
| 59 | } |
| 60 | } |
| 61 | |
| David Tolnay | dbff3c4 | 2020-08-31 00:41:53 -0700 | [diff] [blame] | 62 | pub(crate) fn read(path: impl AsRef<Path>) -> Result<Vec<u8>> { |
| David Tolnay | 0d85ccd | 2020-08-30 20:18:13 -0700 | [diff] [blame] | 63 | let path = path.as_ref(); |
| David Tolnay | dbff3c4 | 2020-08-31 00:41:53 -0700 | [diff] [blame] | 64 | match std::fs::read(path) { |
| David Tolnay | 0d85ccd | 2020-08-30 20:18:13 -0700 | [diff] [blame] | 65 | Ok(string) => Ok(string), |
| 66 | Err(e) => err!(e, "Failed to read file `{}`", path), |
| 67 | } |
| 68 | } |
| 69 | |
| David Tolnay | c0e07dc | 2020-09-02 15:39:28 -0700 | [diff] [blame] | 70 | pub(crate) fn read_stdin() -> Result<Vec<u8>> { |
| 71 | let mut bytes = Vec::new(); |
| 72 | match io::stdin().read_to_end(&mut bytes) { |
| 73 | Ok(_len) => Ok(bytes), |
| 74 | Err(e) => err!(e, "Failed to read input from stdin"), |
| 75 | } |
| 76 | } |
| 77 | |
| David Tolnay | 0d85ccd | 2020-08-30 20:18:13 -0700 | [diff] [blame] | 78 | pub(crate) fn remove_file(path: impl AsRef<Path>) -> Result<()> { |
| 79 | let path = path.as_ref(); |
| 80 | match std::fs::remove_file(path) { |
| 81 | Ok(()) => Ok(()), |
| 82 | Err(e) => err!(e, "Failed to remove file `{}`", path), |
| 83 | } |
| 84 | } |
| 85 | |
| David Tolnay | e61db0e | 2020-09-02 09:38:03 -0700 | [diff] [blame] | 86 | pub(crate) fn remove_dir(path: impl AsRef<Path>) -> Result<()> { |
| 87 | let path = path.as_ref(); |
| 88 | match std::fs::remove_dir(path) { |
| 89 | Ok(()) => Ok(()), |
| 90 | Err(e) => err!(e, "Failed to remove directory `{}`", path), |
| 91 | } |
| 92 | } |
| 93 | |
| David Tolnay | b6614db | 2020-09-01 21:17:45 -0700 | [diff] [blame] | 94 | fn symlink<'a>( |
| David Tolnay | 36731f8 | 2020-11-14 15:16:53 -0800 | [diff] [blame] | 95 | original: &'a Path, |
| 96 | link: &'a Path, |
| David Tolnay | b6614db | 2020-09-01 21:17:45 -0700 | [diff] [blame] | 97 | fun: fn(&'a Path, &'a Path) -> io::Result<()>, |
| 98 | ) -> Result<()> { |
| David Tolnay | 36731f8 | 2020-11-14 15:16:53 -0800 | [diff] [blame] | 99 | match fun(original, link) { |
| David Tolnay | 0d85ccd | 2020-08-30 20:18:13 -0700 | [diff] [blame] | 100 | Ok(()) => Ok(()), |
| 101 | Err(e) => err!( |
| 102 | e, |
| 103 | "Failed to create symlink `{}` pointing to `{}`", |
| David Tolnay | 36731f8 | 2020-11-14 15:16:53 -0800 | [diff] [blame] | 104 | link, |
| 105 | original, |
| David Tolnay | 0d85ccd | 2020-08-30 20:18:13 -0700 | [diff] [blame] | 106 | ), |
| 107 | } |
| 108 | } |
| 109 | |
| David Tolnay | ff21d91 | 2020-11-14 15:41:54 -0800 | [diff] [blame] | 110 | pub(crate) fn symlink_fail(original: impl AsRef<Path>, link: impl AsRef<Path>) -> Result<()> { |
| 111 | err!( |
| 112 | None, |
| 113 | "Failed to create symlink `{}` pointing to `{}`", |
| 114 | link.as_ref(), |
| 115 | original.as_ref(), |
| 116 | ) |
| 117 | } |
| 118 | |
| David Tolnay | b6614db | 2020-09-01 21:17:45 -0700 | [diff] [blame] | 119 | #[cfg(unix)] |
| 120 | #[allow(unused_imports)] |
| 121 | pub(crate) use self::symlink_file as symlink_dir; |
| 122 | |
| David Tolnay | ff21d91 | 2020-11-14 15:41:54 -0800 | [diff] [blame] | 123 | #[cfg(not(any(unix, windows)))] |
| 124 | #[allow(unused_imports)] |
| 125 | pub(crate) use self::symlink_fail as symlink_dir; |
| 126 | |
| David Tolnay | b6614db | 2020-09-01 21:17:45 -0700 | [diff] [blame] | 127 | #[cfg(unix)] |
| David Tolnay | 36731f8 | 2020-11-14 15:16:53 -0800 | [diff] [blame] | 128 | pub(crate) fn symlink_file(original: impl AsRef<Path>, link: impl AsRef<Path>) -> Result<()> { |
| 129 | symlink(original.as_ref(), link.as_ref(), std::os::unix::fs::symlink) |
| David Tolnay | b6614db | 2020-09-01 21:17:45 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| David Tolnay | 0d85ccd | 2020-08-30 20:18:13 -0700 | [diff] [blame] | 132 | #[cfg(windows)] |
| David Tolnay | 36731f8 | 2020-11-14 15:16:53 -0800 | [diff] [blame] | 133 | pub(crate) fn symlink_file(original: impl AsRef<Path>, link: impl AsRef<Path>) -> Result<()> { |
| David Tolnay | b6614db | 2020-09-01 21:17:45 -0700 | [diff] [blame] | 134 | symlink( |
| David Tolnay | 36731f8 | 2020-11-14 15:16:53 -0800 | [diff] [blame] | 135 | original.as_ref(), |
| 136 | link.as_ref(), |
| David Tolnay | b6614db | 2020-09-01 21:17:45 -0700 | [diff] [blame] | 137 | std::os::windows::fs::symlink_file, |
| 138 | ) |
| 139 | } |
| 140 | |
| 141 | #[cfg(windows)] |
| David Tolnay | 36731f8 | 2020-11-14 15:16:53 -0800 | [diff] [blame] | 142 | pub(crate) fn symlink_dir(original: impl AsRef<Path>, link: impl AsRef<Path>) -> Result<()> { |
| David Tolnay | b6614db | 2020-09-01 21:17:45 -0700 | [diff] [blame] | 143 | symlink( |
| David Tolnay | 36731f8 | 2020-11-14 15:16:53 -0800 | [diff] [blame] | 144 | original.as_ref(), |
| 145 | link.as_ref(), |
| David Tolnay | b6614db | 2020-09-01 21:17:45 -0700 | [diff] [blame] | 146 | std::os::windows::fs::symlink_dir, |
| 147 | ) |
| David Tolnay | 0d85ccd | 2020-08-30 20:18:13 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | pub(crate) fn write(path: impl AsRef<Path>, contents: impl AsRef<[u8]>) -> Result<()> { |
| 151 | let path = path.as_ref(); |
| 152 | match std::fs::write(path, contents) { |
| 153 | Ok(()) => Ok(()), |
| 154 | Err(e) => err!(e, "Failed to write file `{}`", path), |
| 155 | } |
| 156 | } |