blob: fe15f868d4b40a63a6e0421bd8bbcff8977e1915 [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};
5use std::io;
6use std::path::{Path, PathBuf};
7
8type Result<T> = std::result::Result<T, Error>;
9
10#[derive(Debug)]
11pub(crate) struct Error {
12 source: io::Error,
13 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)> {
24 Some(&self.source)
25 }
26}
27
28macro_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 Tolnay0d85ccd2020-08-30 20:18:13 -070037pub(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
46pub(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
54pub(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 Tolnaydbff3c42020-08-31 00:41:53 -070061pub(crate) fn read(path: impl AsRef<Path>) -> Result<Vec<u8>> {
David Tolnay0d85ccd2020-08-30 20:18:13 -070062 let path = path.as_ref();
David Tolnaydbff3c42020-08-31 00:41:53 -070063 match std::fs::read(path) {
David Tolnay0d85ccd2020-08-30 20:18:13 -070064 Ok(string) => Ok(string),
65 Err(e) => err!(e, "Failed to read file `{}`", path),
66 }
67}
68
69pub(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 Tolnayb6614db2020-09-01 21:17:45 -070077fn 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 Tolnay0d85ccd2020-08-30 20:18:13 -070083 Ok(()) => Ok(()),
84 Err(e) => err!(
85 e,
86 "Failed to create symlink `{}` pointing to `{}`",
87 dst,
88 src,
89 ),
90 }
91}
92
David Tolnayb6614db2020-09-01 21:17:45 -070093#[cfg(unix)]
94#[allow(unused_imports)]
95pub(crate) use self::symlink_file as symlink_dir;
96
97#[cfg(unix)]
98pub(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 Tolnay0d85ccd2020-08-30 20:18:13 -0700102#[cfg(windows)]
103pub(crate) fn symlink_file(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> Result<()> {
David Tolnayb6614db2020-09-01 21:17:45 -0700104 symlink(
105 src.as_ref(),
106 dst.as_ref(),
107 std::os::windows::fs::symlink_file,
108 )
109}
110
111#[cfg(windows)]
112pub(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 Tolnay0d85ccd2020-08-30 20:18:13 -0700118}
119
120pub(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}