From 9def624c054d34052cf7c34444810ce15165f64f Mon Sep 17 00:00:00 2001 From: Tim Zhang Date: Tue, 13 Oct 2020 14:35:22 +0800 Subject: [PATCH] agent: replace `if let Err` with `or_else` Fixes #934 Signed-off-by: Tim Zhang --- src/agent/rustjail/src/container.rs | 11 +++++++---- src/agent/src/mount.rs | 9 ++++++--- src/agent/src/rpc.rs | 8 +++++--- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/agent/rustjail/src/container.rs b/src/agent/rustjail/src/container.rs index 91657e322a..4e80799a07 100644 --- a/src/agent/rustjail/src/container.rs +++ b/src/agent/rustjail/src/container.rs @@ -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 { diff --git a/src/agent/src/mount.rs b/src/agent/src/mount.rs index 45d89bd520..a9c2ba1b08 100644 --- a/src/agent/src/mount.rs +++ b/src/agent/src/mount.rs @@ -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(()) } diff --git a/src/agent/src/rpc.rs b/src/agent/src/rpc.rs index ed383b21ae..fcfa4adcfa 100644 --- a/src/agent/src/rpc.rs +++ b/src/agent/src/rpc.rs @@ -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(),