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 <rbradford@rivosinc.com>
This commit is contained in:
Rob Bradford 2024-07-05 11:08:20 +01:00 committed by Liu Wei
parent 3103526153
commit e97cee99ef

View File

@ -190,6 +190,22 @@ impl Response {
Ok(response)
}
pub fn ok_or_abandon<T>(
self,
fd: &mut T,
error: MigratableError,
) -> Result<Response, MigratableError>
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)