diff options
| author | David Lönnhager <david.l@mullvad.net> | 2025-03-03 16:47:17 +0100 |
|---|---|---|
| committer | David Lönnhager <david.l@mullvad.net> | 2025-03-05 23:32:50 +0100 |
| commit | 8ae3b64a9b09aa762bc9cecc950949f1e7a423de (patch) | |
| tree | 4e85bdb3535aa37f4ecd0fd622788a9ce9a4af3f | |
| parent | 58575478f331b4082adfaeb0731245b9a89ecead (diff) | |
| download | mullvadvpn-8ae3b64a9b09aa762bc9cecc950949f1e7a423de.tar.xz mullvadvpn-8ae3b64a9b09aa762bc9cecc950949f1e7a423de.zip | |
Move size hint check to method
| -rw-r--r-- | mullvad-update/src/client/fetch.rs | 36 |
1 files changed, 19 insertions, 17 deletions
diff --git a/mullvad-update/src/client/fetch.rs b/mullvad-update/src/client/fetch.rs index 2a66fc68bc..d9de50a55d 100644 --- a/mullvad-update/src/client/fetch.rs +++ b/mullvad-update/src/client/fetch.rs @@ -35,6 +35,24 @@ pub enum SizeHint { Maximum(usize), } +impl SizeHint { + /// This function succeeds if `actual` is allowed according to the [SizeHint]. Otherwise, it + /// returns an error. + fn check_size(&self, actual: usize) -> anyhow::Result<()> { + match *self { + SizeHint::Exact(expected) if actual != expected => { + anyhow::bail!("File size mismatch: expected {expected} bytes, served {actual}") + } + SizeHint::Maximum(limit) if actual > limit => { + anyhow::bail!( + "File size exceeds limit: expected at most {limit} bytes, served {actual}" + ) + } + _ => Ok(()), + } + } +} + /// Download `url` to `file`. If the file already exists, this appends to it, as long /// as the file pointed to by `url` is larger than it. /// @@ -84,7 +102,7 @@ pub async fn get_to_writer( .get(CONTENT_LENGTH) .context("Missing file size")?; let total_size: usize = total_size.to_str()?.parse().context("invalid size")?; - check_size_hint(size_hint, total_size)?; + size_hint.check_size(total_size)?; let already_fetched_bytes = writer .stream_position() @@ -145,22 +163,6 @@ pub async fn get_to_writer( Ok(()) } -/// This function succeeds if `actual` is allowed according to the [SizeHint]. Otherwise, it -/// returns an error. -fn check_size_hint(hint: SizeHint, actual: usize) -> anyhow::Result<()> { - match hint { - SizeHint::Exact(expected) if actual != expected => { - anyhow::bail!("File size mismatch: expected {expected} bytes, served {actual}") - } - SizeHint::Maximum(limit) if actual > limit => { - anyhow::bail!( - "File size exceeds limit: expected at most {limit} bytes, served {actual}" - ) - } - _ => Ok(()), - } -} - /// If a file exists, append to it. Otherwise, create a new file async fn create_or_append(path: impl AsRef<Path>) -> io::Result<File> { match fs::File::create_new(&path).await { |
