Skip to content

Commit

Permalink
Use null instead of empty string to represent missing checksum
Browse files Browse the repository at this point in the history
  • Loading branch information
jarhodes314 committed Oct 7, 2022
1 parent 994b580 commit bfca5c7
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 24 deletions.
2 changes: 1 addition & 1 deletion crates/cargo-test-support/src/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ pub(crate) fn create_index_line(
name: serde_json::Value,
vers: &str,
deps: Vec<serde_json::Value>,
cksum: &str,
cksum: Option<&str>,
features: crate::registry::FeatureMap,
yanked: bool,
links: Option<String>,
Expand Down
10 changes: 5 additions & 5 deletions crates/cargo-test-support/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -809,7 +809,7 @@ impl HttpServer {
serde_json::json!(new_crate.name),
&new_crate.vers,
deps,
&cksum(file),
Some(&cksum(file)),
new_crate.features,
false,
new_crate.links,
Expand Down Expand Up @@ -1080,9 +1080,9 @@ impl Package {
.collect::<Vec<_>>();
let cksum = if self.generate_checksum {
let c = t!(fs::read(&self.archive_dst()));
cksum(&c)
Some(cksum(&c))
} else {
String::new()
None
};
let name = if self.invalid_json {
serde_json::json!(1)
Expand All @@ -1093,7 +1093,7 @@ impl Package {
name,
&self.vers,
deps,
&cksum,
cksum.as_deref(),
self.features.clone(),
self.yanked,
self.links.clone(),
Expand All @@ -1108,7 +1108,7 @@ impl Package {

write_to_index(&registry_path, &self.name, line, self.local);

cksum
cksum.unwrap_or_else(String::new)
}

fn make_archive(&self) {
Expand Down
5 changes: 1 addition & 4 deletions src/cargo/core/resolver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,7 @@ pub fn resolve(

let mut cksums = HashMap::new();
for (summary, _) in cx.activations.values() {
let cksum = summary
.checksum()
.map(|s| s.to_string())
.filter(|s| !s.is_empty());
let cksum = summary.checksum().map(|s| s.to_string());
cksums.insert(summary.package_id(), cksum);
}
let graph = cx.graph();
Expand Down
5 changes: 3 additions & 2 deletions src/cargo/sources/registry/http_remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::sources::registry::download;
use crate::sources::registry::MaybeLock;
use crate::sources::registry::{LoadResponse, RegistryConfig, RegistryData};
use crate::util::errors::CargoResult;
use crate::util::{Config, Filesystem, IntoUrl, Progress, ProgressStyle};
use crate::util::{internal, Config, Filesystem, IntoUrl, Progress, ProgressStyle};
use anyhow::Context;
use cargo_util::paths;
use curl::easy::{HttpVersion, List};
Expand Down Expand Up @@ -543,7 +543,8 @@ impl<'cfg> RegistryData for HttpRegistry<'cfg> {
self.requested_update = true;
}

fn download(&mut self, pkg: PackageId, checksum: &str) -> CargoResult<MaybeLock> {
fn download(&mut self, pkg: PackageId, checksum: Option<&str>) -> CargoResult<MaybeLock> {
let checksum = checksum.ok_or_else(|| internal(format!("no hash listed for {}", pkg)))?;
let registry_config = loop {
match self.config()? {
Poll::Pending => self.block_until_ready()?,
Expand Down
11 changes: 7 additions & 4 deletions src/cargo/sources/registry/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,15 +265,18 @@ impl<'cfg> RegistryIndex<'cfg> {
}

/// Returns the hash listed for a specified `PackageId`.
pub fn hash(&mut self, pkg: PackageId, load: &mut dyn RegistryData) -> Poll<CargoResult<&str>> {
pub fn hash(
&mut self,
pkg: PackageId,
load: &mut dyn RegistryData,
) -> Poll<CargoResult<Option<&str>>> {
let req = OptVersionReq::exact(pkg.version());
let summary = self.summaries(pkg.name(), &req, load)?;
let summary = ready!(summary).next();
Poll::Ready(Ok(summary
.ok_or_else(|| internal(format!("no hash listed for {}", pkg)))?
.summary
.checksum()
.ok_or_else(|| internal(format!("no hash listed for {}", pkg)))?))
.checksum()))
}

/// Load a list of summaries for `name` package in this registry which
Expand Down Expand Up @@ -855,7 +858,7 @@ impl IndexSummary {
}
}
let mut summary = Summary::new(config, pkgid, deps, &features, links)?;
summary.set_checksum(cksum);
cksum.map(|cksum| summary.set_checksum(cksum));
Ok(IndexSummary {
summary,
yanked: yanked.unwrap_or(false),
Expand Down
8 changes: 5 additions & 3 deletions src/cargo/sources/registry/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl<'cfg> RegistryData for LocalRegistry<'cfg> {
self.updated
}

fn download(&mut self, pkg: PackageId, checksum: &str) -> CargoResult<MaybeLock> {
fn download(&mut self, pkg: PackageId, checksum: Option<&str>) -> CargoResult<MaybeLock> {
let crate_file = format!("{}-{}.crate", pkg.name(), pkg.version());

// Note that the usage of `into_path_unlocked` here is because the local
Expand All @@ -129,8 +129,10 @@ impl<'cfg> RegistryData for LocalRegistry<'cfg> {
// We don't actually need to download anything per-se, we just need to
// verify the checksum matches the .crate file itself.
let actual = Sha256::new().update_file(&crate_file)?.finish_hex();
if actual != checksum && !checksum.is_empty() {
anyhow::bail!("failed to verify the checksum of `{}`", pkg)
if let Some(checksum) = checksum {
if actual != checksum {
anyhow::bail!("failed to verify the checksum of `{}`", pkg)
}
}

crate_file.seek(SeekFrom::Start(0))?;
Expand Down
9 changes: 6 additions & 3 deletions src/cargo/sources/registry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ use crate::core::source::MaybePackage;
use crate::core::{Package, PackageId, QueryKind, Source, SourceId, Summary};
use crate::sources::PathSource;
use crate::util::hex;
use crate::util::internal;
use crate::util::interning::InternedString;
use crate::util::into_url::IntoUrl;
use crate::util::network::PollExt;
Expand Down Expand Up @@ -271,7 +272,7 @@ pub struct RegistryPackage<'a> {
/// will fail to load due to not being able to parse the new syntax, even
/// with a `Cargo.lock` file.
features2: Option<BTreeMap<InternedString, Vec<InternedString>>>,
cksum: String,
cksum: Option<String>,
/// If `true`, Cargo will skip this version when resolving.
///
/// This was added in 2014. Everything in the crates.io index has this set
Expand Down Expand Up @@ -486,7 +487,7 @@ pub trait RegistryData {
/// `finish_download`. For already downloaded `.crate` files, it does not
/// validate the checksum, assuming the filesystem does not suffer from
/// corruption or manipulation.
fn download(&mut self, pkg: PackageId, checksum: &str) -> CargoResult<MaybeLock>;
fn download(&mut self, pkg: PackageId, checksum: Option<&str>) -> CargoResult<MaybeLock>;

/// Finish a download by saving a `.crate` file to disk.
///
Expand Down Expand Up @@ -794,7 +795,9 @@ impl<'cfg> Source for RegistrySource<'cfg> {
Poll::Pending => self.block_until_ready()?,
Poll::Ready(hash) => break hash,
}
};
}
.ok_or_else(|| internal(format!("no hash listed for {}", package)))?;

let file = self.ops.finish_download(package, hash, &data)?;
self.get_pkg(package, &file)
}
Expand Down
5 changes: 3 additions & 2 deletions src/cargo/sources/registry/remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::sources::registry::MaybeLock;
use crate::sources::registry::{LoadResponse, RegistryConfig, RegistryData};
use crate::util::errors::CargoResult;
use crate::util::interning::InternedString;
use crate::util::{Config, Filesystem};
use crate::util::{internal, Config, Filesystem};
use anyhow::Context as _;
use cargo_util::paths;
use lazycell::LazyCell;
Expand Down Expand Up @@ -310,7 +310,8 @@ impl<'cfg> RegistryData for RemoteRegistry<'cfg> {
self.updated
}

fn download(&mut self, pkg: PackageId, checksum: &str) -> CargoResult<MaybeLock> {
fn download(&mut self, pkg: PackageId, checksum: Option<&str>) -> CargoResult<MaybeLock> {
let checksum = checksum.ok_or_else(|| internal(format!("no hash listed for {}", pkg)))?;
let registry_config = loop {
match self.config()? {
Poll::Pending => self.block_until_ready()?,
Expand Down

0 comments on commit bfca5c7

Please sign in to comment.