Skip to content

Commit 1bdfc9d

Browse files
committed
Use null instead of empty string to represent missing checksum
1 parent 7620823 commit 1bdfc9d

File tree

8 files changed

+30
-23
lines changed

8 files changed

+30
-23
lines changed

crates/cargo-test-support/src/publish.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ pub(crate) fn create_index_line(
161161
name: serde_json::Value,
162162
vers: &str,
163163
deps: Vec<serde_json::Value>,
164-
cksum: &str,
164+
cksum: Option<&str>,
165165
features: crate::registry::FeatureMap,
166166
yanked: bool,
167167
links: Option<String>,

crates/cargo-test-support/src/registry.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,7 @@ impl HttpServer {
822822
serde_json::json!(new_crate.name),
823823
&new_crate.vers,
824824
deps,
825-
&cksum(file),
825+
Some(&cksum(file)),
826826
new_crate.features,
827827
false,
828828
new_crate.links,
@@ -1093,9 +1093,9 @@ impl Package {
10931093
.collect::<Vec<_>>();
10941094
let cksum = if self.generate_checksum {
10951095
let c = t!(fs::read(&self.archive_dst()));
1096-
cksum(&c)
1096+
Some(cksum(&c))
10971097
} else {
1098-
String::new()
1098+
None
10991099
};
11001100
let name = if self.invalid_json {
11011101
serde_json::json!(1)
@@ -1106,7 +1106,7 @@ impl Package {
11061106
name,
11071107
&self.vers,
11081108
deps,
1109-
&cksum,
1109+
cksum.as_deref(),
11101110
self.features.clone(),
11111111
self.yanked,
11121112
self.links.clone(),
@@ -1121,7 +1121,7 @@ impl Package {
11211121

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

1124-
cksum
1124+
cksum.unwrap_or_else(String::new)
11251125
}
11261126

11271127
fn make_archive(&self) {

src/cargo/core/resolver/mod.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,7 @@ pub fn resolve(
147147

148148
let mut cksums = HashMap::new();
149149
for (summary, _) in cx.activations.values() {
150-
let cksum = summary
151-
.checksum()
152-
.map(|s| s.to_string())
153-
.filter(|s| !s.is_empty());
150+
let cksum = summary.checksum().map(|s| s.to_string());
154151
cksums.insert(summary.package_id(), cksum);
155152
}
156153
let graph = cx.graph();

src/cargo/sources/registry/http_remote.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,8 @@ impl<'cfg> RegistryData for HttpRegistry<'cfg> {
556556
self.requested_update = true;
557557
}
558558

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

src/cargo/sources/registry/index.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,15 +265,18 @@ impl<'cfg> RegistryIndex<'cfg> {
265265
}
266266

267267
/// Returns the hash listed for a specified `PackageId`.
268-
pub fn hash(&mut self, pkg: PackageId, load: &mut dyn RegistryData) -> Poll<CargoResult<&str>> {
268+
pub fn hash(
269+
&mut self,
270+
pkg: PackageId,
271+
load: &mut dyn RegistryData,
272+
) -> Poll<CargoResult<Option<&str>>> {
269273
let req = OptVersionReq::exact(pkg.version());
270274
let summary = self.summaries(pkg.name(), &req, load)?;
271275
let summary = ready!(summary).next();
272276
Poll::Ready(Ok(summary
273277
.ok_or_else(|| internal(format!("no hash listed for {}", pkg)))?
274278
.summary
275-
.checksum()
276-
.ok_or_else(|| internal(format!("no hash listed for {}", pkg)))?))
279+
.checksum()))
277280
}
278281

279282
/// Load a list of summaries for `name` package in this registry which
@@ -855,7 +858,7 @@ impl IndexSummary {
855858
}
856859
}
857860
let mut summary = Summary::new(config, pkgid, deps, &features, links)?;
858-
summary.set_checksum(cksum);
861+
cksum.map(|cksum| summary.set_checksum(cksum));
859862
Ok(IndexSummary {
860863
summary,
861864
yanked: yanked.unwrap_or(false),

src/cargo/sources/registry/local.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ impl<'cfg> RegistryData for LocalRegistry<'cfg> {
108108
self.updated
109109
}
110110

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

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

136138
crate_file.seek(SeekFrom::Start(0))?;

src/cargo/sources/registry/mod.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ use crate::core::source::MaybePackage;
179179
use crate::core::{Package, PackageId, QueryKind, Source, SourceId, Summary};
180180
use crate::sources::PathSource;
181181
use crate::util::hex;
182+
use crate::util::internal;
182183
use crate::util::interning::InternedString;
183184
use crate::util::into_url::IntoUrl;
184185
use crate::util::network::PollExt;
@@ -271,7 +272,7 @@ pub struct RegistryPackage<'a> {
271272
/// will fail to load due to not being able to parse the new syntax, even
272273
/// with a `Cargo.lock` file.
273274
features2: Option<BTreeMap<InternedString, Vec<InternedString>>>,
274-
cksum: String,
275+
cksum: Option<String>,
275276
/// If `true`, Cargo will skip this version when resolving.
276277
///
277278
/// This was added in 2014. Everything in the crates.io index has this set
@@ -486,7 +487,7 @@ pub trait RegistryData {
486487
/// `finish_download`. For already downloaded `.crate` files, it does not
487488
/// validate the checksum, assuming the filesystem does not suffer from
488489
/// corruption or manipulation.
489-
fn download(&mut self, pkg: PackageId, checksum: &str) -> CargoResult<MaybeLock>;
490+
fn download(&mut self, pkg: PackageId, checksum: Option<&str>) -> CargoResult<MaybeLock>;
490491

491492
/// Finish a download by saving a `.crate` file to disk.
492493
///
@@ -794,7 +795,9 @@ impl<'cfg> Source for RegistrySource<'cfg> {
794795
Poll::Pending => self.block_until_ready()?,
795796
Poll::Ready(hash) => break hash,
796797
}
797-
};
798+
}
799+
.ok_or_else(|| internal(format!("no hash listed for {}", package)))?;
800+
798801
let file = self.ops.finish_download(package, hash, &data)?;
799802
self.get_pkg(package, &file)
800803
}

src/cargo/sources/registry/remote.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::sources::registry::MaybeLock;
55
use crate::sources::registry::{LoadResponse, RegistryConfig, RegistryData};
66
use crate::util::errors::CargoResult;
77
use crate::util::interning::InternedString;
8-
use crate::util::{Config, Filesystem};
8+
use crate::util::{internal, Config, Filesystem};
99
use anyhow::Context as _;
1010
use cargo_util::paths;
1111
use lazycell::LazyCell;
@@ -310,7 +310,8 @@ impl<'cfg> RegistryData for RemoteRegistry<'cfg> {
310310
self.updated
311311
}
312312

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

0 commit comments

Comments
 (0)