From 3e1aa698f307f7fc9e80779ad543fb3741ef3f39 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sat, 15 May 2021 16:21:08 +0100 Subject: [PATCH] cmdlistener: Break out modify_bundles We're going to want to reuse this for ClearBundles Signed-off-by: Ian Jackson --- daemon/cmdlistener.rs | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/daemon/cmdlistener.rs b/daemon/cmdlistener.rs index 705bc347..e6dbeb3c 100644 --- a/daemon/cmdlistener.rs +++ b/daemon/cmdlistener.rs @@ -103,6 +103,27 @@ fn execute_and_respond(cs: &mut CommandStreamData, cmd: MgmtCommand, ) } + #[throws(MgmtError)] + fn modify_bundles<'ig,F,R>( + cs: &mut CommandStreamData, + ag: &AccountsGuard, + gref: &'ig Unauthorised, + perms: &[TablePermission], + f: &mut F, + ) -> (R, Authorisation) + where F: FnMut( + &mut InstanceGuard<'ig>, + MutexGuard<'ig, InstanceBundles>, + ) -> Result + { + let bundles = gref.lock_bundles(); + let mut igu = gref.lock()?; + let (mut ig, auth) = cs.check_acl(&ag, &mut igu, PCH::Instance, perms)?; + let bundles = bundles.by(auth); + let r = f(&mut ig, bundles)?; + (r, auth) + } + let resp = (|| Ok::<_,MgmtError>(match cmd { MC::Noop => Fine, @@ -234,13 +255,12 @@ fn execute_and_respond(cs: &mut CommandStreamData, cmd: MgmtCommand, MC::UploadBundle { game, size, hash, kind } => { let (upload, auth) = { let (ag, gref) = start_modify_game(&game)?; - let bundles = gref.lock_bundles(); - let mut igu = gref.lock()?; - let (ig, auth) = cs.check_acl(&ag, &mut igu, PCH::Instance, - &[TP::UploadBundles])?; - let mut bundles = bundles.by(auth); - let upload = bundles.start_upload(ig, kind)?; - (upload, auth) + modify_bundles( + cs, &ag, &gref, &[TP::UploadBundles], + &mut |mut ig, mut bundles: MutexGuard<'_, InstanceBundles>| { + bundles.start_upload(&mut ig, kind) + } + )? }; let uploaded = upload.bulk(bulk_upload, size, &hash, &mut for_response)?; -- 2.30.2