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
pub mod config;
pub use config::Config;
use anyhow::{ensure, Result};
use flate2::bufread::GzDecoder;
use log::info;
use sha2::{Digest, Sha256};
use std::{
env,
fs::{File, OpenOptions},
io::{self, prelude::*, BufReader, BufWriter},
path::{Path, PathBuf},
sync::Arc,
};
use tar::Archive;
pub(crate) fn load(config: Config) -> Result<PathBuf> {
let out_dir = config.out_dir.clone().unwrap_or_else(|| {
let var = env::var_os("OUT_DIR").expect("OUT_DIR is not set");
PathBuf::from(var)
});
let source_ready_file = out_dir.join("source_ready");
let src_dir = out_dir.join(&config.dir);
println!("cargo:rerun-if-changed={}", out_dir.to_str().unwrap());
skip_or_run(&source_ready_file, || {
prepare_tar_gz(&config.url, &config.sha256sum, &out_dir)?;
ensure!(src_dir.is_dir(), "'{}' does not exist", src_dir.display());
Ok(())
})?;
Ok(src_dir)
}
fn prepare_tar_gz(url: &str, sha256_digest: &[u8], dir: &Path) -> Result<()> {
let tarball_path = dir.join("source.tar.gz");
let is_source_ready = tarball_path.is_file() && sha256_digest == sha256sum(&tarball_path)?;
if is_source_ready {
info!("Source file is ready. Skip downloading.");
} else {
info!("Downloading file from '{}'", url);
let mut tarball_file = OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(&tarball_path)?;
let mut writer = BufWriter::new(&mut tarball_file);
let mut reader = ureq::AgentBuilder::new()
.tls_connector(Arc::new(native_tls::TlsConnector::new()?))
.build()
.get(url)
.call()?
.into_reader();
io::copy(&mut reader, &mut writer)?;
writer.flush()?;
}
{
let digest = sha256sum(&tarball_path)?;
ensure!(
sha256_digest == digest,
"Checksum mismatch: expect {:X?}, but get {:x?}",
sha256_digest,
digest
);
}
{
info!("Unpacking {}", tarball_path.display());
let mut tarball_file = BufReader::new(File::open(&tarball_path)?);
let mut reader = BufReader::new(&mut tarball_file);
let tar = GzDecoder::new(&mut reader);
let mut archive = Archive::new(tar);
archive.unpack(dir)?;
}
Ok(())
}
fn sha256sum(path: &Path) -> Result<[u8; 32]> {
let mut hasher = Sha256::new();
let mut reader = BufReader::new(File::open(path)?);
loop {
let mut buf = [0u8; 8192];
let len = reader.read(&mut buf)?;
if len == 0 {
break;
}
hasher.update(&buf[0..len]);
}
let hash = hasher.finalize();
Ok(hash.as_slice().try_into().unwrap())
}
fn touch(path: &Path) -> Result<()> {
OpenOptions::new().create(true).write(true).open(path)?;
Ok(())
}
fn skip_or_run<T, F>(target_path: &Path, callback: F) -> Result<Option<T>>
where
F: FnOnce() -> Result<T>,
{
with_target(target_path, || {
let output = (callback)()?;
touch(target_path)?;
Ok(output)
})
}
fn with_target<T, F>(target_path: &Path, callback: F) -> Result<Option<T>>
where
F: FnOnce() -> Result<T>,
{
with_targets(&[target_path], callback)
}
fn with_targets<T, F>(target_paths: &[&Path], callback: F) -> Result<Option<T>>
where
F: FnOnce() -> Result<T>,
{
let ok = target_paths.iter().all(|path| path.exists());
if ok {
return Ok(None);
}
(callback)().map(Some)
}