Skip to content

Commit

Permalink
Merge pull request #255 from MordechaiHadad/fix/checksums-file
Browse files Browse the repository at this point in the history
  • Loading branch information
MordechaiHadad authored Feb 3, 2025
2 parents cb0f7c9 + a06a498 commit 9d7d535
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "bob-nvim"
edition = "2021"
version = "4.0.2"
version = "4.0.3"
description = "A version manager for neovim"
readme = "README.md"
keywords = ["neovim", "version-manager"]
Expand Down
54 changes: 40 additions & 14 deletions src/handlers/install_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,13 @@ pub async fn start(
downloaded_checksum.file_name, downloaded_checksum.file_format
));

if !sha256cmp(&archive_path, &checksum_path)? {
let platform = helpers::get_platform_name_download(&version.semver);

if !sha256cmp(
&archive_path,
&checksum_path,
&format!("{}.{}", platform, downloaded_archive.file_format),
)? {
tokio::fs::remove_file(archive_path).await?;
tokio::fs::remove_file(checksum_path).await?;
return Err(anyhow!("Checksum mismatch!"));
Expand Down Expand Up @@ -360,7 +366,13 @@ async fn download_version(

let file_type = helpers::get_file_type();
let file_type = if get_sha256sum {
format!("{file_type}.sha256sum")
if version.version_type == VersionType::Nightly
|| version.semver.as_ref().unwrap() > &Version::new(0, 10, 4)
{
"shasum.txt".to_string()
} else {
format!("{file_type}.sha256sum")
}
} else {
file_type.to_owned()
};
Expand Down Expand Up @@ -396,10 +408,17 @@ async fn download_version(
semver: version.semver.clone(),
}))
} else {
Err(anyhow!(
"Please provide an existing neovim version, {}",
response.text().await?
))
let error_text = response.text().await?;
if error_text.contains("Not Found") {
Err(anyhow!(
"Version does not exist in Neovim releases. Please check available versions with 'bob list-remote'"
))
} else {
Err(anyhow!(
"Please provide an existing neovim version, {}",
error_text
))
}
}
}
Err(error) => Err(anyhow!(error)),
Expand Down Expand Up @@ -665,18 +684,25 @@ async fn send_request(
) -> Result<reqwest::Response, reqwest::Error> {
let platform = helpers::get_platform_name_download(&version.semver);
let file_type = helpers::get_file_type();
let file_type = if get_sha256sum {
format!("{file_type}.sha256sum")
} else {
file_type.to_owned()
};

let url = match &config.github_mirror {
Some(val) => val.to_string(),
None => "https://github.com".to_string(),
};
let version = &version.tag_name;
let request_url =
format!("{url}/neovim/neovim/releases/download/{version}/{platform}.{file_type}");
let version_tag = &version.tag_name;
let request_url = if get_sha256sum {
if version.version_type == VersionType::Nightly
|| version.semver.as_ref().unwrap() > &Version::new(0, 10, 4)
{
format!("{url}/neovim/neovim/releases/download/{version_tag}/shasum.txt")
} else {
format!(
"{url}/neovim/neovim/releases/download/{version_tag}/{platform}.{file_type}.sha256sum"
)
}
} else {
format!("{url}/neovim/neovim/releases/download/{version_tag}/{platform}.{file_type}")
};

client
.get(request_url)
Expand Down
11 changes: 8 additions & 3 deletions src/helpers/checksum.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use anyhow::anyhow;
use anyhow::Result;
use sha2::{Digest, Sha256};
use std::path::Path;
Expand All @@ -13,9 +14,13 @@ use std::{fs, io};
///
/// This function returns a `Result` that contains a `bool` indicating whether the checksum of the file at path 'a' matches the checksum saved in the file at path 'b'.
/// If there is an error opening or reading the files, the function returns `Err(error)`.
pub fn sha256cmp(a: &Path, b: &Path) -> Result<bool> {
let checksum = fs::read_to_string(b)?;
let checksum = checksum.split(' ').next().unwrap();
pub fn sha256cmp(a: &Path, b: &Path, filename: &str) -> Result<bool> {
let checksum_contents = fs::read_to_string(b)?;
let checksum = checksum_contents
.lines()
.find(|line| line.contains(filename))
.and_then(|line| line.split_whitespace().next())
.ok_or_else(|| anyhow!("Checksum not found for {}", filename))?;

let mut hasher = Sha256::new();
let mut file = fs::File::open(a)?;
Expand Down

0 comments on commit 9d7d535

Please sign in to comment.