forked from rust-lang/log
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bins.rs
258 lines (221 loc) · 7.11 KB
/
bins.rs
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
252
253
254
255
256
257
258
use std::{
borrow::Cow,
fs,
path::{Component, Path, PathBuf},
};
use cargo_toml::Product;
use compact_str::CompactString;
use log::debug;
use normalize_path::NormalizePath;
use serde::Serialize;
use tinytemplate::TinyTemplate;
use crate::{
errors::BinstallError,
fs::{atomic_install, atomic_symlink_file},
manifests::cargo_toml_binstall::{PkgFmt, PkgMeta},
};
/// Return true if the path does not look outside of current dir
///
/// * `path` - must be normalized before passing to this function
fn is_valid_path(path: &Path) -> bool {
!matches!(
path.components().next(),
// normalized path cannot have curdir or parentdir,
// so checking prefix/rootdir is enough.
Some(Component::Prefix(..) | Component::RootDir)
)
}
/// Must be called after the archive is downloaded and extracted.
/// This function might uses blocking I/O.
pub fn infer_bin_dir_template(data: &Data) -> Cow<'static, str> {
let name = &data.name;
let target = &data.target;
let version = &data.version;
// Make sure to update
// fetchers::gh_crate_meta::hosting::{FULL_FILENAMES,
// NOVERSION_FILENAMES} if you update this array.
let possible_dirs = [
format!("{name}-{target}-v{version}"),
format!("{name}-{target}-{version}"),
format!("{name}-{version}-{target}"),
format!("{name}-v{version}-{target}"),
format!("{name}-{target}"),
// Ignore the following when updating hosting::{FULL_FILENAMES, NOVERSION_FILENAMES}
format!("{name}-{version}"),
format!("{name}-v{version}"),
name.to_string(),
];
let default_bin_dir_template = Cow::Borrowed("{ bin }{ binary-ext }");
possible_dirs
.into_iter()
.find(|dirname| data.bin_path.join(dirname).is_dir())
.map(|mut dir| {
dir.reserve_exact(1 + default_bin_dir_template.len());
dir += "/";
dir += &default_bin_dir_template;
Cow::Owned(dir)
})
// Fallback to no dir
.unwrap_or(default_bin_dir_template)
}
pub struct BinFile {
pub base_name: CompactString,
pub source: PathBuf,
pub dest: PathBuf,
pub link: Option<PathBuf>,
}
impl BinFile {
pub fn from_product(
data: &Data<'_>,
product: &Product,
bin_dir: &str,
no_symlinks: bool,
) -> Result<Self, BinstallError> {
let base_name = product.name.as_deref().unwrap();
let binary_ext = if data.target.contains("windows") {
".exe"
} else {
""
};
let ctx = Context {
name: data.name,
repo: data.repo,
target: data.target,
version: data.version,
bin: base_name,
format: binary_ext,
binary_ext,
};
let source = if data.meta.pkg_fmt == Some(PkgFmt::Bin) {
data.bin_path.clone()
} else {
// Generate install paths
// Source path is the download dir + the generated binary path
let path = ctx.render(bin_dir)?;
let path_normalized = Path::new(&path).normalize();
if path_normalized.components().next().is_none() {
return Err(BinstallError::EmptySourceFilePath);
}
if !is_valid_path(&path_normalized) {
return Err(BinstallError::InvalidSourceFilePath {
path: path_normalized,
});
}
data.bin_path.join(&path_normalized)
};
// Destination at install dir + base-name{.extension}
let dest = data.install_path.join(ctx.render("{ bin }{ binary-ext }")?);
let (dest, link) = if no_symlinks {
(dest, None)
} else {
// Destination path is the install dir + base-name-version{.extension}
let dest_file_path_with_ver = ctx.render("{ bin }-v{ version }{ binary-ext }")?;
let dest_with_ver = data.install_path.join(dest_file_path_with_ver);
(dest_with_ver, Some(dest))
};
Ok(Self {
base_name: CompactString::from(base_name),
source,
dest,
link,
})
}
pub fn preview_bin(&self) -> String {
format!(
"{} ({} -> {})",
self.base_name,
self.source.file_name().unwrap().to_string_lossy(),
self.dest.display()
)
}
pub fn preview_link(&self) -> String {
if let Some(link) = &self.link {
format!(
"{} ({} -> {})",
self.base_name,
link.display(),
self.link_dest().display()
)
} else {
String::new()
}
}
/// Return `Ok` if the source exists, otherwise `Err`.
pub fn check_source_exists(&self) -> Result<(), BinstallError> {
if !self.source.try_exists()? {
Err(BinstallError::BinFileNotFound(self.source.clone()))
} else {
Ok(())
}
}
pub fn install_bin(&self) -> Result<(), BinstallError> {
self.check_source_exists()?;
debug!(
"Atomically install file from '{}' to '{}'",
self.source.display(),
self.dest.display()
);
#[cfg(unix)]
fs::set_permissions(
&self.source,
std::os::unix::fs::PermissionsExt::from_mode(0o755),
)?;
atomic_install(&self.source, &self.dest)?;
Ok(())
}
pub fn install_link(&self) -> Result<(), BinstallError> {
if let Some(link) = &self.link {
// Remove existing symlink
// TODO: check if existing symlink is correct
if link.exists() {
debug!("Remove link '{}'", link.display());
std::fs::remove_file(link)?;
}
let dest = self.link_dest();
debug!(
"Create link '{}' pointing to '{}'",
link.display(),
dest.display()
);
atomic_symlink_file(dest, link)?;
}
Ok(())
}
fn link_dest(&self) -> &Path {
if cfg!(target_family = "unix") {
Path::new(self.dest.file_name().unwrap())
} else {
&self.dest
}
}
}
/// Data required to get bin paths
pub struct Data<'a> {
pub name: &'a str,
pub target: &'a str,
pub version: &'a str,
pub repo: Option<&'a str>,
pub meta: PkgMeta,
pub bin_path: PathBuf,
pub install_path: PathBuf,
}
#[derive(Clone, Debug, Serialize)]
struct Context<'c> {
pub name: &'c str,
pub repo: Option<&'c str>,
pub target: &'c str,
pub version: &'c str,
pub bin: &'c str,
/// Soft-deprecated alias for binary-ext
pub format: &'c str,
/// Filename extension on the binary, i.e. .exe on Windows, nothing otherwise
#[serde(rename = "binary-ext")]
pub binary_ext: &'c str,
}
impl<'c> Context<'c> {
fn render(&self, template: &str) -> Result<String, BinstallError> {
let mut tt = TinyTemplate::new();
tt.add_template("path", template)?;
Ok(tt.render("path", self)?)
}
}