From e97cee99efb801e2353ad54fb739c1c040be9cc2 Mon Sep 17 00:00:00 2001 From: Rob Bradford Date: Fri, 5 Jul 2024 11:08:20 +0100 Subject: [PATCH] vm-migration: Introduce Response::ok_or_abandon() This method will return the existing Response if the status is successful (Status::Ok) otherwise issue a command to abandon the migration and return the desired error. Signed-off-by: Rob Bradford --- vm-migration/src/protocol.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/vm-migration/src/protocol.rs b/vm-migration/src/protocol.rs index ad453b178..4789bdf74 100644 --- a/vm-migration/src/protocol.rs +++ b/vm-migration/src/protocol.rs @@ -190,6 +190,22 @@ impl Response { Ok(response) } + pub fn ok_or_abandon( + self, + fd: &mut T, + error: MigratableError, + ) -> Result + where + T: Read + Write, + { + if self.status != Status::Ok { + Request::abandon().write_to(fd)?; + Response::read_from(fd)?; + return Err(error); + } + Ok(self) + } + pub fn write_to(&self, fd: &mut dyn Write) -> Result<(), MigratableError> { fd.write_all(Self::as_slice(self)) .map_err(MigratableError::MigrateSocket)