-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathartifact.rs
More file actions
60 lines (53 loc) · 1.85 KB
/
artifact.rs
File metadata and controls
60 lines (53 loc) · 1.85 KB
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
use std::{
fs::{self, OpenOptions},
io::{self, Write},
path::{Path, PathBuf},
};
/// An artifact (e.g., a DLL or shared library) whose content is embedded and needs to be written to disk.
pub struct Artifact {
pub name: &'static str,
pub content: &'static [u8],
pub hash: &'static str,
}
#[cfg(target_os = "macos")]
#[doc(hidden)]
#[macro_export]
macro_rules! artifact {
($name: literal) => {
$crate::artifact::Artifact::new(
$name,
::core::include_bytes!(::core::concat!(::core::env!("OUT_DIR"), "/", $name)),
::core::include_str!(::core::concat!(::core::env!("OUT_DIR"), "/", $name, ".hash")),
)
};
}
#[cfg(target_os = "macos")]
pub use artifact;
impl Artifact {
#[cfg(all(not(target_os = "android"), not(target_os = "linux")))]
pub const fn new(name: &'static str, content: &'static [u8], hash: &'static str) -> Self {
Self { name, content, hash }
}
pub fn write_to(&self, dir: impl AsRef<Path>, suffix: &str) -> io::Result<PathBuf> {
let dir = dir.as_ref();
let path = dir.join(format!("{}_{}{}", self.name, self.hash, suffix));
if fs::exists(&path)? {
return Ok(path);
}
let tmp_path = dir.join(format!("{:x}", rand::random::<u128>()));
let mut tmp_file_open_options = OpenOptions::new();
tmp_file_open_options.write(true).create_new(true);
#[cfg(unix)]
std::os::unix::fs::OpenOptionsExt::mode(&mut tmp_file_open_options, 0o755); // executable
let mut tmp_file = tmp_file_open_options.open(&tmp_path)?;
tmp_file.write_all(self.content)?;
drop(tmp_file);
if let Err(err) = fs::rename(&tmp_path, &path) {
if !fs::exists(&path)? {
return Err(err);
}
fs::remove_file(&tmp_path)?;
}
Ok(path)
}
}