Skip to content

Commit bcfd893

Browse files
committed
Auto merge of #9801 - arlosi:checksum, r=alexcrichton
Allow crate download by checksum The `dl` key in `config.json` currently allows the following substitutions: {crate}, {version}, {prefix}, {lowerprefix}. This change adds a {checksum} placeholder for the crate's sha256 checksum. Does not change any existing behavior. Allowing downloads by checksum makes it possible for crate files to be placed in a content addressable store.
2 parents 8b5771f + c890be2 commit bcfd893

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

src/cargo/sources/registry/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ const CRATE_TEMPLATE: &str = "{crate}";
189189
const VERSION_TEMPLATE: &str = "{version}";
190190
const PREFIX_TEMPLATE: &str = "{prefix}";
191191
const LOWER_PREFIX_TEMPLATE: &str = "{lowerprefix}";
192+
const CHECKSUM_TEMPLATE: &str = "{sha256-checksum}";
192193

193194
/// A "source" for a local (see `local::LocalRegistry`) or remote (see
194195
/// `remote::RemoteRegistry`) registry.
@@ -236,7 +237,8 @@ pub struct RegistryConfig {
236237
/// respectively. The substring `{prefix}` will be replaced with the
237238
/// crate's prefix directory name, and the substring `{lowerprefix}` will
238239
/// be replaced with the crate's prefix directory name converted to
239-
/// lowercase.
240+
/// lowercase. The substring `{sha256-checksum}` will be replaced with the
241+
/// crate's sha256 checksum.
240242
///
241243
/// For backwards compatibility, if the string does not contain any
242244
/// markers (`{crate}`, `{version}`, `{prefix}`, or ``{lowerprefix}`), it

src/cargo/sources/registry/remote.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use crate::core::{GitReference, PackageId, SourceId};
22
use crate::sources::git;
33
use crate::sources::registry::MaybeLock;
44
use crate::sources::registry::{
5-
RegistryConfig, RegistryData, CRATE_TEMPLATE, LOWER_PREFIX_TEMPLATE, PREFIX_TEMPLATE,
6-
VERSION_TEMPLATE,
5+
RegistryConfig, RegistryData, CHECKSUM_TEMPLATE, CRATE_TEMPLATE, LOWER_PREFIX_TEMPLATE,
6+
PREFIX_TEMPLATE, VERSION_TEMPLATE,
77
};
88
use crate::util::errors::CargoResult;
99
use crate::util::interning::InternedString;
@@ -243,7 +243,7 @@ impl<'cfg> RegistryData for RemoteRegistry<'cfg> {
243243
Ok(())
244244
}
245245

246-
fn download(&mut self, pkg: PackageId, _checksum: &str) -> CargoResult<MaybeLock> {
246+
fn download(&mut self, pkg: PackageId, checksum: &str) -> CargoResult<MaybeLock> {
247247
let filename = self.filename(pkg);
248248

249249
// Attempt to open an read-only copy first to avoid an exclusive write
@@ -267,6 +267,7 @@ impl<'cfg> RegistryData for RemoteRegistry<'cfg> {
267267
&& !url.contains(VERSION_TEMPLATE)
268268
&& !url.contains(PREFIX_TEMPLATE)
269269
&& !url.contains(LOWER_PREFIX_TEMPLATE)
270+
&& !url.contains(CHECKSUM_TEMPLATE)
270271
{
271272
write!(url, "/{}/{}/download", CRATE_TEMPLATE, VERSION_TEMPLATE).unwrap();
272273
}
@@ -275,7 +276,8 @@ impl<'cfg> RegistryData for RemoteRegistry<'cfg> {
275276
.replace(CRATE_TEMPLATE, &*pkg.name())
276277
.replace(VERSION_TEMPLATE, &pkg.version().to_string())
277278
.replace(PREFIX_TEMPLATE, &prefix)
278-
.replace(LOWER_PREFIX_TEMPLATE, &prefix.to_lowercase());
279+
.replace(LOWER_PREFIX_TEMPLATE, &prefix.to_lowercase())
280+
.replace(CHECKSUM_TEMPLATE, checksum);
279281

280282
Ok(MaybeLock::Download {
281283
url,

src/doc/src/reference/registries.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ The keys are:
135135
- `{prefix}`: A directory prefix computed from the crate name. For example,
136136
a crate named `cargo` has a prefix of `ca/rg`. See below for details.
137137
- `{lowerprefix}`: Lowercase variant of `{prefix}`.
138+
- `{sha256-checksum}`: The crate's sha256 checksum.
138139

139140
If none of the markers are present, then the value
140141
`/{crate}/{version}/download` is appended to the end.

0 commit comments

Comments
 (0)