agent: replace if let Err with or_else

Fixes #934

Signed-off-by: Tim Zhang <tim@hyper.sh>
This commit is contained in:
Tim Zhang 2020-10-13 14:35:22 +08:00
parent 6ffa8283f0
commit a3c64e5ce5
3 changed files with 18 additions and 10 deletions

View File

@ -487,23 +487,26 @@ fn do_init_child(cwfd: RawFd) -> Result<()> {
}
log_child!(cfd_log, "join namespace {:?}", s);
if let Err(e) = sched::setns(fd, s) {
sched::setns(fd, s).or_else(|e| {
if s == CloneFlags::CLONE_NEWUSER {
if e.as_errno().unwrap() != Errno::EINVAL {
check!(
write_sync(cwfd, SYNC_FAILED, format!("{:?}", e).as_str()),
"write_sync for CLONE_NEWUSER"
);
return Err(e.into());
return Err(e);
}
Ok(())
} else {
check!(
write_sync(cwfd, SYNC_FAILED, format!("{:?}", e).as_str()),
"write_sync for sched::setns"
);
return Err(e.into());
Err(e)
}
}
})?;
unistd::close(fd)?;
if s == CloneFlags::CLONE_NEWUSER {

View File

@ -479,15 +479,18 @@ fn mount_to_rootfs(logger: &Logger, m: &INIT_MOUNT) -> Result<()> {
fs::create_dir_all(Path::new(m.dest)).context("could not create directory")?;
if let Err(err) = bare_mount.mount() {
bare_mount.mount().or_else(|e| {
if m.src != "dev" {
return Err(err.into());
return Err(e);
}
error!(
logger,
"Could not mount filesystem from {} to {}", m.src, m.dest
);
}
Ok(())
})?;
Ok(())
}

View File

@ -1610,11 +1610,13 @@ fn do_copy_file(req: &CopyFileRequest) -> Result<()> {
PathBuf::from("/")
};
if let Err(e) = fs::create_dir_all(dir.to_str().unwrap()) {
fs::create_dir_all(dir.to_str().unwrap()).or_else(|e| {
if e.kind() != std::io::ErrorKind::AlreadyExists {
return Err(e.into());
return Err(e);
}
}
Ok(())
})?;
std::fs::set_permissions(
dir.to_str().unwrap(),