1#![allow(dead_code)]
2
3use std::error::Error as StdError;
4use std::fmt::{self, Display};
5use std::io::{self, Read};
6use std::path::{Path, PathBuf};
7
8pub(crate) type Result<T> = std::result::Result<T, Error>;
9
10#[derive(Debug)]
11pub(crate) struct Error {
12 source: Option<io::Error>,
13 message: String,
14}
15
16impl Error {
17 pub(crate) fn kind(&self) -> io::ErrorKind {
18 match &self.source {
19 Some(io_error) => io_error.kind(),
20 None => io::ErrorKind::Other,
21 }
22 }
23}
24
25impl Display for Error {
26 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
27 formatter.write_str(&self.message)
28 }
29}
30
31impl StdError for Error {
32 fn source(&self) -> Option<&(dyn StdError + 'static)> {
33 let source = self.source.as_ref()?;
34 Some(source)
35 }
36}
37
38macro_rules! err {
39 ($io_error:expr, $fmt:expr $(, $path:expr)* $(,)?) => {
40 Err(Error {
41 source: Option::from($io_error),
42 message: format!($fmt $(, $path.display())*),
43 })
44 }
45}
46
47pub(crate) fn copy(from: impl AsRef<Path>, to: impl AsRef<Path>) -> Result<u64> {
48 let from = from.as_ref();
49 let to = to.as_ref();
50 match std::fs::copy(from, to) {
51 Ok(n) => Ok(n),
52 Err(e) => err!(e, "Failed to copy `{}` -> `{}`", from, to),
53 }
54}
55
56pub(crate) fn create_dir_all(path: impl AsRef<Path>) -> Result<()> {
57 let path = path.as_ref();
58 match std::fs::create_dir_all(path) {
59 Ok(()) => Ok(()),
60 Err(e) => err!(e, "Failed to create directory `{}`", path),
61 }
62}
63
64pub(crate) fn current_dir() -> Result<PathBuf> {
65 match std::env::current_dir() {
66 Ok(dir) => Ok(dir),
67 Err(e) => err!(e, "Failed to determine current directory"),
68 }
69}
70
71pub(crate) fn exists(path: impl AsRef<Path>) -> bool {
72 let path = path.as_ref();
73 std::fs::symlink_metadata(path).is_ok()
76}
77
78pub(crate) fn read(path: impl AsRef<Path>) -> Result<Vec<u8>> {
79 let path = path.as_ref();
80 match std::fs::read(path) {
81 Ok(string) => Ok(string),
82 Err(e) => err!(e, "Failed to read file `{}`", path),
83 }
84}
85
86pub(crate) fn read_stdin() -> Result<Vec<u8>> {
87 let mut bytes = Vec::new();
88 match io::stdin().read_to_end(&mut bytes) {
89 Ok(_len) => Ok(bytes),
90 Err(e) => err!(e, "Failed to read input from stdin"),
91 }
92}
93
94pub(crate) fn remove_file(path: impl AsRef<Path>) -> Result<()> {
95 let path = path.as_ref();
96 match std::fs::remove_file(path) {
97 Ok(()) => Ok(()),
98 Err(e) => err!(e, "Failed to remove file `{}`", path),
99 }
100}
101
102pub(crate) fn remove_dir(path: impl AsRef<Path>) -> Result<()> {
103 let path = path.as_ref();
104 match std::fs::remove_dir(path) {
105 Ok(()) => Ok(()),
106 Err(e) => err!(e, "Failed to remove directory `{}`", path),
107 }
108}
109
110fn symlink<'a>(
111 original: &'a Path,
112 link: &'a Path,
113 fun: fn(&'a Path, &'a Path) -> io::Result<()>,
114) -> Result<()> {
115 match fun(original, link) {
116 Ok(()) => Ok(()),
117 Err(e) => err!(
118 e,
119 "Failed to create symlink `{}` pointing to `{}`",
120 link,
121 original,
122 ),
123 }
124}
125
126pub(crate) fn symlink_fail(original: impl AsRef<Path>, link: impl AsRef<Path>) -> Result<()> {
127 err!(
128 None,
129 "Failed to create symlink `{}` pointing to `{}`",
130 link.as_ref(),
131 original.as_ref(),
132 )
133}
134
135#[cfg(unix)]
136#[allow(unused_imports)]
137pub(crate) use self::symlink_file as symlink_dir;
138
139#[cfg(not(any(unix, windows)))]
140#[allow(unused_imports)]
141pub(crate) use self::symlink_fail as symlink_dir;
142
143#[cfg(unix)]
144pub(crate) fn symlink_file(original: impl AsRef<Path>, link: impl AsRef<Path>) -> Result<()> {
145 symlink(original.as_ref(), link.as_ref(), std::os::unix::fs::symlink)
146}
147
148#[cfg(windows)]
149pub(crate) fn symlink_file(original: impl AsRef<Path>, link: impl AsRef<Path>) -> Result<()> {
150 symlink(
151 original.as_ref(),
152 link.as_ref(),
153 std::os::windows::fs::symlink_file,
154 )
155}
156
157#[cfg(windows)]
158pub(crate) fn symlink_dir(original: impl AsRef<Path>, link: impl AsRef<Path>) -> Result<()> {
159 symlink(
160 original.as_ref(),
161 link.as_ref(),
162 std::os::windows::fs::symlink_dir,
163 )
164}
165
166pub(crate) fn write(path: impl AsRef<Path>, contents: impl AsRef<[u8]>) -> Result<()> {
167 let path = path.as_ref();
168 match std::fs::write(path, contents) {
169 Ok(()) => Ok(()),
170 Err(e) => err!(e, "Failed to write file `{}`", path),
171 }
172}