1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
use crate::errors::ProtoError;
use crate::{color, Describable};
use log::{debug, trace};
use std::fs::{self, File};
use std::io::{self, Read};
use std::path::{Path, PathBuf};
use tar::Archive;
use zip::result::ZipError;
use zip::ZipArchive;
#[async_trait::async_trait]
pub trait Installable<'tool>: Send + Sync + Describable<'tool> {
fn get_archive_prefix(&self) -> Result<Option<String>, ProtoError> {
Ok(None)
}
fn get_install_dir(&self) -> Result<PathBuf, ProtoError>;
async fn install(&self, install_dir: &Path, download_path: &Path) -> Result<bool, ProtoError> {
if install_dir.exists() {
debug!(target: self.get_log_target(), "Tool already installed, continuing");
return Ok(false);
}
if !download_path.exists() {
return Err(ProtoError::InstallMissingDownload(self.get_name()));
}
let prefix = self.get_archive_prefix()?;
debug!(
target: self.get_log_target(),
"Attempting to install {} to {}",
color::path(download_path),
color::path(install_dir),
);
unpack(download_path, install_dir, prefix)?;
debug!(target: self.get_log_target(), "Successfully installed tool");
Ok(true)
}
async fn uninstall(&self, install_dir: &Path) -> Result<bool, ProtoError> {
if !install_dir.exists() {
debug!(target: self.get_log_target(), "Tool has not been installed, aborting");
return Ok(false);
}
debug!(
target: self.get_log_target(),
"Deleting install directory {}",
color::path(install_dir)
);
fs::remove_dir_all(install_dir)
.map_err(|e| ProtoError::Fs(install_dir.to_path_buf(), e.to_string()))?;
debug!(target: self.get_log_target(), "Successfully uninstalled tool");
Ok(true)
}
}
pub fn unpack<I: AsRef<Path>, O: AsRef<Path>>(
input_file: I,
output_dir: O,
remove_prefix: Option<String>,
) -> Result<(), ProtoError> {
let input_file = input_file.as_ref();
let ext = input_file.extension().unwrap_or_default().to_string_lossy();
match ext.as_ref() {
"zip" => unzip(input_file, output_dir, remove_prefix),
"tgz" | "gz" => untar_gzip(input_file, output_dir, remove_prefix),
"txz" | "xz" => untar_xzip(input_file, output_dir, remove_prefix),
_ => Err(ProtoError::UnsupportedArchiveFormat(
input_file.to_path_buf(),
ext.to_string(),
)),
}
}
pub fn untar<I: AsRef<Path>, O: AsRef<Path>, R: FnOnce(File) -> D, D: Read>(
input_file: I,
output_dir: O,
remove_prefix: Option<String>,
decoder: R,
) -> Result<(), ProtoError> {
let input_file = input_file.as_ref();
let output_dir = output_dir.as_ref();
let handle_input_error = |e: io::Error| ProtoError::Fs(input_file.to_path_buf(), e.to_string());
let handle_output_error =
|e: io::Error| ProtoError::Fs(output_dir.to_path_buf(), e.to_string());
trace!(
target: "proto:installer",
"Unpacking tar archive {} to {}",
color::path(input_file),
color::path(output_dir),
);
if !output_dir.exists() {
fs::create_dir_all(output_dir).map_err(handle_output_error)?;
}
let tar_gz = File::open(input_file).map_err(handle_input_error)?;
let tar = decoder(tar_gz);
let mut archive = Archive::new(tar);
for entry_result in archive.entries().map_err(handle_input_error)? {
let mut entry = entry_result.map_err(handle_input_error)?;
let mut path: PathBuf = entry.path().map_err(handle_input_error)?.into_owned();
if let Some(prefix) = &remove_prefix {
if path.starts_with(prefix) {
path = path.strip_prefix(prefix).unwrap().to_owned();
}
}
let output_path = output_dir.join(path);
if let Some(parent_dir) = output_path.parent() {
fs::create_dir_all(parent_dir)
.map_err(|e| ProtoError::Fs(parent_dir.to_path_buf(), e.to_string()))?;
}
entry
.unpack(&output_path)
.map_err(|e| ProtoError::Fs(output_path.to_path_buf(), e.to_string()))?;
}
Ok(())
}
pub fn untar_gzip<I: AsRef<Path>, O: AsRef<Path>>(
input_file: I,
output_dir: O,
remove_prefix: Option<String>,
) -> Result<(), ProtoError> {
untar(input_file, output_dir, remove_prefix, |file| {
flate2::read::GzDecoder::new(file)
})
}
pub fn untar_xzip<I: AsRef<Path>, O: AsRef<Path>>(
input_file: I,
output_dir: O,
remove_prefix: Option<String>,
) -> Result<(), ProtoError> {
untar(input_file, output_dir, remove_prefix, |file| {
xz2::read::XzDecoder::new(file)
})
}
pub fn unzip<I: AsRef<Path>, O: AsRef<Path>>(
input_file: I,
output_dir: O,
remove_prefix: Option<String>,
) -> Result<(), ProtoError> {
let input_file = input_file.as_ref();
let output_dir = output_dir.as_ref();
let handle_input_error = |e: io::Error| ProtoError::Fs(input_file.to_path_buf(), e.to_string());
let handle_output_error =
|e: io::Error| ProtoError::Fs(output_dir.to_path_buf(), e.to_string());
let handle_zip_error = |e: ZipError| ProtoError::Zip(e.to_string());
trace!(
target: "proto:installer",
"Unzipping zip archive {} to {}",
color::path(input_file),
color::path(output_dir),
);
if !output_dir.exists() {
fs::create_dir_all(output_dir).map_err(handle_output_error)?;
}
let zip = File::open(input_file).map_err(handle_input_error)?;
let mut archive = ZipArchive::new(zip).map_err(handle_zip_error)?;
for i in 0..archive.len() {
let mut file = archive.by_index(i).map_err(handle_zip_error)?;
let mut path = match file.enclosed_name() {
Some(path) => path.to_owned(),
None => continue,
};
if let Some(prefix) = &remove_prefix {
if path.starts_with(prefix) {
path = path.strip_prefix(prefix).unwrap().to_owned();
}
}
let output_path = output_dir.join(&path);
let handle_error = |e: io::Error| ProtoError::Fs(output_path.to_path_buf(), e.to_string());
if let Some(parent_dir) = &output_path.parent() {
fs::create_dir_all(parent_dir)
.map_err(|e| ProtoError::Fs(parent_dir.to_path_buf(), e.to_string()))?;
}
if file.is_dir() {
fs::create_dir_all(&output_path).map_err(handle_error)?;
}
if file.is_file() {
let mut out = File::create(&output_path).map_err(handle_error)?;
io::copy(&mut file, &mut out).map_err(handle_error)?;
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
if let Some(mode) = file.unix_mode() {
fs::set_permissions(&output_path, fs::Permissions::from_mode(mode))
.map_err(handle_error)?;
}
}
}
}
Ok(())
}