From 39b32521291dde5254d6148b3601b2c17bb4cdad Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Fri, 27 Jan 2023 14:44:36 +0000 Subject: clean up error handling By adding an implicit conversion from std::io::Error to SessionError, a lot of the .or(SessionError) bits can be dropped. --- src/lib.rs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index 1b55c8f..73cd9b0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -27,20 +27,25 @@ fn create_and_open_dir + Copy>( struct SessionError; +impl From for SessionError { + fn from(_: std::io::Error) -> Self { + SessionError + } +} + fn open_session(h: &mut PamHandle) -> Result<(), SessionError> { let user = h.get_user(None).or(Err(SessionError))?; let user = users::get_user_by_name(&user).ok_or(SessionError)?; let aa = cap_std::ambient_authority(); - let d = Dir::open_ambient_dir(CG_MOUNT, aa).or(Err(SessionError))?; - let d = create_and_open_dir(&d, "user").or(Err(SessionError))?; - let d = create_and_open_dir(&d, &user.uid().to_string()) - .or(Err(SessionError))?; - let d = create_and_open_dir(&d, "leaf").or(Err(SessionError))?; + let d = Dir::open_ambient_dir(CG_MOUNT, aa)?; + let d = create_and_open_dir(&d, "user")?; + let d = create_and_open_dir(&d, &user.uid().to_string())?; + let d = create_and_open_dir(&d, "leaf")?; let pid = process::id().to_string(); let mut options = OpenOptions::new(); options.write(true); - let mut procs = d.open_with("cgroup.procs", &options).or(Err(SessionError))?; - procs.write_all(pid.as_bytes()).or(Err(SessionError))?; + let mut procs = d.open_with("cgroup.procs", &options)?; + procs.write_all(pid.as_bytes())?; Ok(()) } -- cgit v1.2.3-54-g00ecf