Skip to content

Commit

Permalink
tugger-debian: honor Acquire-By-Hash: yes setting
Browse files Browse the repository at this point in the history
Modern Debian repositories set an `Acquire-By-Hash: yes` field
to trigger clients to fetch URLs containing the content digest of the
entity they are fetching. We had support for parsing this field but
weren't keying off of it.

This commit implements functionality for resolving the `by-hash` path
for a release file entry and automatically using it if the release
file says to use acquire-by-hash mode.

Pre-existing tests should now fetch teh by-hash URLs. The tests
pass, indicating this functionality apparently works.
  • Loading branch information
indygreg committed Nov 30, 2021
1 parent b343587 commit 475f536
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
9 changes: 7 additions & 2 deletions tugger-debian/src/repository/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,13 +280,18 @@ impl<'client> HttpReleaseClient<'client> {
is_installer,
)
.ok_or(HttpError::PackagesIndicesEntryNotFound)?;
let path = entry.entry.path;

let path = if self.release.acquire_by_hash().unwrap_or_default() {
entry.entry.by_hash_path()
} else {
entry.entry.path.to_string()
};

// TODO perform digest verification.
// TODO make this stream output.

let mut reader = ControlParagraphAsyncReader::new(futures::io::BufReader::new(
self.get_path_reader(path, entry.compression).await?,
self.get_path_reader(&path, entry.compression).await?,
));

let mut res = BinaryPackageList::default();
Expand Down
44 changes: 44 additions & 0 deletions tugger-debian/src/repository/release.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,15 @@ impl<'a> ReleaseFileDigest<'a> {
Self::Sha256(_) => ChecksumType::Sha256.field_name(),
}
}

/// Obtain the tracked digest value.
pub fn digest(&self) -> &'a str {
match self {
Self::Md5(v) => v,
Self::Sha1(v) => v,
Self::Sha256(v) => v,
}
}
}

/// An entry for a file in a parsed `Release` file.
Expand All @@ -136,6 +145,26 @@ pub struct ReleaseFileEntry<'a> {
pub size: usize,
}

impl<'a> ReleaseFileEntry<'a> {
/// Obtain the `by-hash` path variant for this entry.
pub fn by_hash_path(&self) -> String {
if let Some((prefix, _)) = self.path.rsplit_once('/') {
format!(
"{}/by-hash/{}/{}",
prefix,
self.digest.field_name(),
self.digest.digest()
)
} else {
format!(
"by-hash/{}/{}",
self.digest.field_name(),
self.digest.digest()
)
}
}
}

/// A type of [ReleaseFileEntry] that describes a `Contents` file.
#[derive(Clone, Debug, PartialEq)]
pub struct ContentsFileEntry<'a> {
Expand Down Expand Up @@ -608,6 +637,10 @@ mod test {
size: 738242,
}
);
assert_eq!(
entries[0].by_hash_path(),
"contrib/by-hash/MD5Sum/7fdf4db15250af5368cc52a91e8edbce"
);
assert_eq!(
entries[1],
ReleaseFileEntry {
Expand All @@ -616,6 +649,10 @@ mod test {
size: 57319,
}
);
assert_eq!(
entries[1].by_hash_path(),
"contrib/by-hash/MD5Sum/cbd7bc4d3eb517ac2b22f929dfc07b47"
);
assert_eq!(
entries[599],
ReleaseFileEntry {
Expand All @@ -624,6 +661,10 @@ mod test {
size: 80488,
}
);
assert_eq!(
entries[599].by_hash_path(),
"non-free/source/by-hash/MD5Sum/e3830f6fc5a946b5a5b46e8277e1d86f"
);

assert!(release.iter_index_files(ChecksumType::Sha1).is_none());

Expand All @@ -642,6 +683,7 @@ mod test {
size: 738242,
}
);
assert_eq!(entries[0].by_hash_path(), "contrib/by-hash/SHA256/3957f28db16e3f28c7b34ae84f1c929c567de6970f3f1b95dac9b498dd80fe63");
assert_eq!(
entries[1],
ReleaseFileEntry {
Expand All @@ -652,6 +694,7 @@ mod test {
size: 57319,
}
);
assert_eq!(entries[1].by_hash_path(), "contrib/by-hash/SHA256/3e9a121d599b56c08bc8f144e4830807c77c29d7114316d6984ba54695d3db7b");
assert_eq!(
entries[599],
ReleaseFileEntry {
Expand All @@ -662,6 +705,7 @@ mod test {
path: "non-free/source/Sources.xz",
}
);
assert_eq!(entries[599].by_hash_path(), "non-free/source/by-hash/SHA256/30f3f996941badb983141e3b29b2ed5941d28cf81f9b5f600bb48f782d386fc7");

let contents = release
.iter_components_indices(ChecksumType::Sha256)
Expand Down

0 comments on commit 475f536

Please sign in to comment.