Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: do not skip backup in case of error + remove state.url as the last step #19

Merged
merged 4 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 33 additions & 19 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,22 +90,34 @@
}
}

fn backup_or_fail(file_path: &PathBuf) -> () {
if file_path.try_exists().unwrap_or(false) {
println!(
"Backing up file: {}",
file_path.file_name().unwrap().to_str().unwrap()
);
match backup_file(&file_path) {
Ok(b) => {
let backup_name = b.to_str().expect("Cannot get a path of backed up file");
println!("File backed up to: {}", backup_name);
}
Err(e) => {
eprintln!("Cannot create a backup file: {}", e);
process::exit(6);
fn backup_or_fail(file_path: PathBuf) {
match file_path.try_exists() {
Ok(true) => {
println!(
"Backing up file: {}",
file_path.file_name().unwrap().to_str().unwrap()
);
match backup_file(&file_path) {
brusherru marked this conversation as resolved.
Show resolved Hide resolved
Ok(b) => {
let backup_name = b.to_string_lossy();
println!("File backed up to: {}", backup_name);
}
Err(e) => {
eprintln!("Cannot create a backup file: {}", e);
process::exit(6);
}
}
}
Ok(false) => {
println!(
"Skip backup: file {} not found",
file_path.to_string_lossy()
);
}
Err(e) => {
eprintln!("Cannot create a backup file: {}", e);
process::exit(6);
}
}
}

Expand All @@ -129,7 +141,7 @@
i64::from(get_last_layer_from_db(&db_file_path).or_else(|err| {
eprintln!("{}", err);
println!("Cannot read database, trating it as empty database");
return Ok::<i32, anyhow::Error>(0);

Check warning on line 144 in src/main.rs

View workflow job for this annotation

GitHub Actions / clippy

unneeded `return` statement

warning: unneeded `return` statement --> src/main.rs:144:13 | 144 | return Ok::<i32, anyhow::Error>(0); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_return = note: `#[warn(clippy::needless_return)]` on by default help: remove `return` | 144 - return Ok::<i32, anyhow::Error>(0); 144 + Ok::<i32, anyhow::Error>(0) |
})?)
} else {
println!("Database file is not found");
Expand All @@ -144,7 +156,7 @@
let go_path_str = go_path
.to_str()
.expect("Cannot resolve path to go-spacemesh");
let go_version = get_version(&go_path_str)?;

Check warning on line 159 in src/main.rs

View workflow job for this annotation

GitHub Actions / clippy

this expression creates a reference which is immediately dereferenced by the compiler

warning: this expression creates a reference which is immediately dereferenced by the compiler --> src/main.rs:159:38 | 159 | let go_version = get_version(&go_path_str)?; | ^^^^^^^^^^^^ help: change this to: `go_path_str` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
let quicksync_layer = fetch_latest_available_layer(&download_url, &go_version)?;
println!("Latest layer in cloud: {}", quicksync_layer);
Ok(())
Expand Down Expand Up @@ -281,18 +293,20 @@
println!("Download URL is not found: skip DB checksum verification");
}

backup_or_fail(&final_file_path);
backup_or_fail(&wal_file_path);
backup_or_fail(final_file_path.clone());
backup_or_fail(wal_file_path);

std::fs::rename(&unpacked_file_path, &final_file_path)
.expect("Cannot rename downloaded file into state.sql");

if redirect_file_path.try_exists().unwrap_or(false) {
std::fs::remove_file(&redirect_file_path)?;
}
if archive_file_path.try_exists().unwrap_or(false) {
println!("Archive file is deleted.");
std::fs::remove_file(&archive_file_path)?;
}
if redirect_file_path.try_exists().unwrap_or(false) {
println!("URL file is deleted.");
std::fs::remove_file(&redirect_file_path)?;
}

println!("Done!");
println!("Now you can run go-spacemesh as usually.");
Expand Down
9 changes: 0 additions & 9 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
.build()?;

let path = format!("{}/state.zst", go_version);
let url = build_url(&download_url, &path);

Check warning on line 84 in src/utils.rs

View workflow job for this annotation

GitHub Actions / clippy

this expression creates a reference which is immediately dereferenced by the compiler

warning: this expression creates a reference which is immediately dereferenced by the compiler --> src/utils.rs:84:23 | 84 | let url = build_url(&download_url, &path); | ^^^^^^^^^^^^^ help: change this to: `download_url` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow = note: `#[warn(clippy::needless_borrow)]` on by default

let response = client.head(url).send()?;

Expand Down Expand Up @@ -114,13 +114,4 @@
let url = Url::parse("https://quicksync.spacemesh.network/state.zip").unwrap();
assert!(extract_number_from_url(&url).is_err());
}

#[test]
fn test_to_precision() {
assert_eq!(to_precision(23.57742, 3), 23.577);
assert_eq!(to_precision(23.57742, 2), 23.58);
assert_eq!(to_precision(55555.0, 3), 55555.0);
assert_eq!(to_precision(55555.0, 0), 55555.0);
assert_eq!(to_precision(123.456789, 5), 123.45679);
}
}
Loading