mirror of
https://github.com/mimblewimble/grin.git
synced 2025-01-21 11:31:08 +03:00
* remove (almost) all unwrap calls * replace with ? operator * build script will now work offline and in many other scenarios where the web wallet may not be downloaded
This commit is contained in:
parent
901c665ba7
commit
ed9c1acec1
1 changed files with 45 additions and 41 deletions
|
@ -59,59 +59,61 @@ fn main() {
|
||||||
format!("{}{}", env::var("OUT_DIR").unwrap(), "/built.rs"),
|
format!("{}{}", env::var("OUT_DIR").unwrap(), "/built.rs"),
|
||||||
);
|
);
|
||||||
|
|
||||||
install_web_wallet();
|
let web_wallet_install = install_web_wallet();
|
||||||
|
match web_wallet_install {
|
||||||
|
Ok(true) => {}
|
||||||
|
_ => println!(
|
||||||
|
"WARNING : Web wallet could not be installed due to {:?}",
|
||||||
|
web_wallet_install
|
||||||
|
),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn download_and_decompress(target_file: &str) {
|
fn download_and_decompress(target_file: &str) -> Result<bool, Box<std::error::Error>> {
|
||||||
let req_path = format!("https://github.com/mimblewimble/grin-web-wallet/releases/download/{}/grin-web-wallet.tar.gz", WEB_WALLET_TAG);
|
let req_path = format!("https://github.com/mimblewimble/grin-web-wallet/releases/download/{}/grin-web-wallet.tar.gz", WEB_WALLET_TAG);
|
||||||
let resp = reqwest::get(&req_path);
|
let mut resp = reqwest::get(&req_path)?;
|
||||||
|
|
||||||
// don't interfere if this doesn't work
|
if !resp.status().is_success() {
|
||||||
if resp.is_err() {
|
return Ok(false);
|
||||||
println!("Warning: Failed to download grin-web-wallet. Web wallet will not be available");
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut resp = resp.unwrap();
|
// read response
|
||||||
if resp.status().is_success() {
|
let mut out: Vec<u8> = vec![];
|
||||||
// read response
|
resp.read_to_end(&mut out)?;
|
||||||
let mut out: Vec<u8> = vec![];
|
|
||||||
let r2 = resp.read_to_end(&mut out);
|
|
||||||
if r2.is_err() {
|
|
||||||
println!(
|
|
||||||
"Warning: Failed to download grin-web-wallet. Web wallet will not be available"
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Gunzip
|
// Gunzip
|
||||||
let mut d = GzDecoder::new(&out[..]);
|
let mut d = GzDecoder::new(&out[..]);
|
||||||
let mut decomp: Vec<u8> = vec![];
|
let mut decomp: Vec<u8> = vec![];
|
||||||
d.read_to_end(&mut decomp).unwrap();
|
d.read_to_end(&mut decomp)?;
|
||||||
|
|
||||||
// write temp file
|
// write temp file
|
||||||
let mut buffer = File::create(target_file.clone()).unwrap();
|
let mut buffer = File::create(target_file.clone())?;
|
||||||
buffer.write_all(&decomp).unwrap();
|
buffer.write_all(&decomp)?;
|
||||||
buffer.flush().unwrap();
|
buffer.flush()?;
|
||||||
}
|
|
||||||
|
Ok(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Download and unzip tagged web-wallet build
|
/// Download and unzip tagged web-wallet build
|
||||||
fn install_web_wallet() {
|
fn install_web_wallet() -> Result<bool, Box<std::error::Error>> {
|
||||||
let target_file = format!(
|
let target_file = format!(
|
||||||
"{}/grin-web-wallet-{}.tar",
|
"{}/grin-web-wallet-{}.tar",
|
||||||
env::var("OUT_DIR").unwrap(),
|
env::var("OUT_DIR")?,
|
||||||
WEB_WALLET_TAG
|
WEB_WALLET_TAG
|
||||||
);
|
);
|
||||||
let out_dir = env::var("OUT_DIR").unwrap();
|
let out_dir = env::var("OUT_DIR")?;
|
||||||
let mut out_path = PathBuf::from(&out_dir);
|
let mut out_path = PathBuf::from(&out_dir);
|
||||||
out_path.pop();
|
out_path.pop();
|
||||||
out_path.pop();
|
out_path.pop();
|
||||||
out_path.pop();
|
out_path.pop();
|
||||||
|
|
||||||
// only re-download if needed
|
// only re-download if needed
|
||||||
|
println!("{}", target_file);
|
||||||
if !Path::new(&target_file).is_file() {
|
if !Path::new(&target_file).is_file() {
|
||||||
download_and_decompress(&target_file);
|
let success = download_and_decompress(&target_file)?;
|
||||||
|
if !success {
|
||||||
|
return Ok(false); // could not download and decompress
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// remove old version
|
// remove old version
|
||||||
|
@ -120,26 +122,28 @@ fn install_web_wallet() {
|
||||||
let _ = fs::remove_dir_all(remove_path);
|
let _ = fs::remove_dir_all(remove_path);
|
||||||
|
|
||||||
// Untar
|
// Untar
|
||||||
let file = File::open(target_file).unwrap();
|
let file = File::open(target_file)?;
|
||||||
let mut a = Archive::new(file);
|
let mut a = Archive::new(file);
|
||||||
|
|
||||||
for file in a.entries().unwrap() {
|
for file in a.entries()? {
|
||||||
let mut file = file.unwrap();
|
let mut file = file?;
|
||||||
let h = file.header().clone();
|
let h = file.header().clone();
|
||||||
let path = h.path().unwrap().clone().into_owned();
|
let path = h.path()?.clone().into_owned();
|
||||||
let is_dir = path.to_str().unwrap().ends_with(path::MAIN_SEPARATOR);
|
let is_dir = path.to_str().unwrap().ends_with(path::MAIN_SEPARATOR);
|
||||||
let path = path.strip_prefix("dist").unwrap();
|
let path = path.strip_prefix("dist")?;
|
||||||
let mut final_path = out_path.clone();
|
let mut final_path = out_path.clone();
|
||||||
final_path.push(path);
|
final_path.push(path);
|
||||||
|
|
||||||
let mut tmp: Vec<u8> = vec![];
|
let mut tmp: Vec<u8> = vec![];
|
||||||
file.read_to_end(&mut tmp).unwrap();
|
file.read_to_end(&mut tmp)?;
|
||||||
if is_dir {
|
if is_dir {
|
||||||
fs::create_dir_all(final_path).unwrap();
|
fs::create_dir_all(final_path)?;
|
||||||
} else {
|
} else {
|
||||||
let mut buffer = File::create(final_path).unwrap();
|
let mut buffer = File::create(final_path)?;
|
||||||
buffer.write_all(&tmp).unwrap();
|
buffer.write_all(&tmp)?;
|
||||||
buffer.flush().unwrap();
|
buffer.flush()?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Ok(true)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue