blob: 7053cc4477d168eb7f6f372d648d22889aaaa1a1 [file] [log] [blame]
David Tolnay0d85ccd2020-08-30 20:18:13 -07001#![allow(dead_code)]
2
3use std::error::Error as StdError;
4use std::fmt::{self, Display};
David Tolnayc0e07dc2020-09-02 15:39:28 -07005use std::io::{self, Read};
David Tolnay0d85ccd2020-08-30 20:18:13 -07006use std::path::{Path, PathBuf};
7
David Tolnayf486d562020-09-02 09:26:32 -07008pub(crate) type Result<T> = std::result::Result<T, Error>;
David Tolnay0d85ccd2020-08-30 20:18:13 -07009
10#[derive(Debug)]
11pub(crate) struct Error {
David Tolnayff21d912020-11-14 15:41:54 -080012 source: Option<io::Error>,
David Tolnay0d85ccd2020-08-30 20:18:13 -070013 message: String,
14}
15
16impl Display for Error {
17 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
18 formatter.write_str(&self.message)
19 }
20}
21
22impl StdError for Error {
23 fn source(&self) -> Option<&(dyn StdError + 'static)> {
David Tolnayff21d912020-11-14 15:41:54 -080024 let source = self.source.as_ref()?;
25 Some(source)
David Tolnay0d85ccd2020-08-30 20:18:13 -070026 }
27}
28
29macro_rules! err {
30 ($io_error:expr, $fmt:expr $(, $path:expr)* $(,)?) => {
31 Err(Error {
David Tolnayff21d912020-11-14 15:41:54 -080032 source: Option::from($io_error),
David Tolnay0d85ccd2020-08-30 20:18:13 -070033 message: format!($fmt $(, $path.display())*),
34 })
35 }
36}
37
David Tolnay0d85ccd2020-08-30 20:18:13 -070038pub(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
47pub(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
55pub(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 Tolnaydbff3c42020-08-31 00:41:53 -070062pub(crate) fn read(path: impl AsRef<Path>) -> Result<Vec<u8>> {
David Tolnay0d85ccd2020-08-30 20:18:13 -070063 let path = path.as_ref();
David Tolnaydbff3c42020-08-31 00:41:53 -070064 match std::fs::read(path) {
David Tolnay0d85ccd2020-08-30 20:18:13 -070065 Ok(string) => Ok(string),
66 Err(e) => err!(e, "Failed to read file `{}`", path),
67 }
68}
69
David Tolnayc0e07dc2020-09-02 15:39:28 -070070pub(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 Tolnay0d85ccd2020-08-30 20:18:13 -070078pub(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 Tolnaye61db0e2020-09-02 09:38:03 -070086pub(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 Tolnayb6614db2020-09-01 21:17:45 -070094fn symlink<'a>(
David Tolnay36731f82020-11-14 15:16:53 -080095 original: &'a Path,
96 link: &'a Path,
David Tolnayb6614db2020-09-01 21:17:45 -070097 fun: fn(&'a Path, &'a Path) -> io::Result<()>,
98) -> Result<()> {
David Tolnay36731f82020-11-14 15:16:53 -080099 match fun(original, link) {
David Tolnay0d85ccd2020-08-30 20:18:13 -0700100 Ok(()) => Ok(()),
101 Err(e) => err!(
102 e,
103 "Failed to create symlink `{}` pointing to `{}`",
David Tolnay36731f82020-11-14 15:16:53 -0800104 link,
105 original,
David Tolnay0d85ccd2020-08-30 20:18:13 -0700106 ),
107 }
108}
109
David Tolnayff21d912020-11-14 15:41:54 -0800110pub(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 Tolnayb6614db2020-09-01 21:17:45 -0700119#[cfg(unix)]
120#[allow(unused_imports)]
121pub(crate) use self::symlink_file as symlink_dir;
122
David Tolnayff21d912020-11-14 15:41:54 -0800123#[cfg(not(any(unix, windows)))]
124#[allow(unused_imports)]
125pub(crate) use self::symlink_fail as symlink_dir;
126
David Tolnayb6614db2020-09-01 21:17:45 -0700127#[cfg(unix)]
David Tolnay36731f82020-11-14 15:16:53 -0800128pub(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 Tolnayb6614db2020-09-01 21:17:45 -0700130}
131
David Tolnay0d85ccd2020-08-30 20:18:13 -0700132#[cfg(windows)]
David Tolnay36731f82020-11-14 15:16:53 -0800133pub(crate) fn symlink_file(original: impl AsRef<Path>, link: impl AsRef<Path>) -> Result<()> {
David Tolnayb6614db2020-09-01 21:17:45 -0700134 symlink(
David Tolnay36731f82020-11-14 15:16:53 -0800135 original.as_ref(),
136 link.as_ref(),
David Tolnayb6614db2020-09-01 21:17:45 -0700137 std::os::windows::fs::symlink_file,
138 )
139}
140
141#[cfg(windows)]
David Tolnay36731f82020-11-14 15:16:53 -0800142pub(crate) fn symlink_dir(original: impl AsRef<Path>, link: impl AsRef<Path>) -> Result<()> {
David Tolnayb6614db2020-09-01 21:17:45 -0700143 symlink(
David Tolnay36731f82020-11-14 15:16:53 -0800144 original.as_ref(),
145 link.as_ref(),
David Tolnayb6614db2020-09-01 21:17:45 -0700146 std::os::windows::fs::symlink_dir,
147 )
David Tolnay0d85ccd2020-08-30 20:18:13 -0700148}
149
150pub(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}