From: Ian Jackson Date: Fri, 14 May 2021 23:56:17 +0000 (+0100) Subject: bundles: Pass size through X-Git-Tag: otter-0.6.0~304 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=4c9f4cc79d02492d2d2bbb8c8c4c40f605abf4e1;p=otter.git bundles: Pass size through The progress report wants it, and it's probably good to send it in the metadata. Signed-off-by: Ian Jackson --- diff --git a/daemon/cmdlistener.rs b/daemon/cmdlistener.rs index 4436a8d5..d4afb9f1 100644 --- a/daemon/cmdlistener.rs +++ b/daemon/cmdlistener.rs @@ -221,7 +221,7 @@ fn execute_and_respond(cs: &mut CommandStreamData, cmd: MgmtCommand, resp } - MC::UploadBundle { game, hash, kind } => { + MC::UploadBundle { game, size, hash, kind } => { let (upload, auth) = { let ag = AccountsGuard::lock(); let gref = Instance::lookup_by_name_unauth(&game)?; @@ -233,7 +233,8 @@ fn execute_and_respond(cs: &mut CommandStreamData, cmd: MgmtCommand, let upload = bundles.start_upload(ig, kind)?; (upload, auth) }; - let uploaded = upload.bulk(&mut bulk_upload, &hash, &mut for_response)?; + let uploaded = upload.bulk(&mut bulk_upload, size, + &hash, &mut for_response)?; { let gref = Instance::lookup_by_name(&game, auth)?; let mut bundles = gref.lock_bundles(); diff --git a/src/bin/otter.rs b/src/bin/otter.rs index b978437e..8257fd8b 100644 --- a/src/bin/otter.rs +++ b/src/bin/otter.rs @@ -1340,12 +1340,17 @@ mod upload_bundle { let f = File::open(&args.bundle_file) .with_context(|| args.bundle_file.clone()) .context("open bundle file")?; + let size = f + .metadata().context("fstat bundle file")? + .len() + .try_into().map_err(|_| anyhow!("bundle file far too large"))?; let mut f = BufReader::new(f); let hash = bundles::DigestWrite::of(&mut f) .context("read bundle file (for hash)")?; let kind = bundles::Kind::only(); f.rewind().context("rewind bundle file")?; let cmd = MC::UploadBundle { + size, game: instance_name.clone(), hash: bundles::Hash(hash.into()), kind, }; diff --git a/src/bundles.rs b/src/bundles.rs index 71e307c3..6518f077 100644 --- a/src/bundles.rs +++ b/src/bundles.rs @@ -825,7 +825,7 @@ impl InstanceBundles { impl Uploading { #[throws(MgmtError)] - pub fn bulk(self, data: &mut R, expected: &Hash, + pub fn bulk(self, data: &mut R, size: usize, expected: &Hash, for_progress: &mut ResponseWriter) -> Uploaded where R: Read, PW: Write { @@ -837,12 +837,14 @@ impl Uploading { let Uploading { id, mut file, instance } = self; let tmp = id.path_tmp(&instance); - io::copy(data, &mut file) + let copied_size = io::copy(data, &mut file) .with_context(|| tmp.clone()) .context("copy").map_err(IE::from)?; let (hash, file) = file.finish(); + if copied_size != size as u64 { throw!(ME::UploadTruncated) } + let mut file = file.into_inner().map_err(|e| e.into_error()) .with_context(|| tmp.clone()).context("flush").map_err(IE::from)?; if hash.as_slice() != &expected.0[..] { throw!(ME::UploadCorrupted) } diff --git a/src/commands.rs b/src/commands.rs index e18a4bad..8ca26f14 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -43,6 +43,7 @@ pub enum MgmtCommand { },*/ UploadBundle { game: InstanceName, + size: usize, hash: bundles::Hash, kind: bundles::Kind, }, @@ -230,6 +231,7 @@ pub enum MgmtError { TomlSyntaxError(String), TomlStructureError(String), RngIsReal, + UploadTruncated, UploadCorrupted, TooManyBundles, BadBundle(String),