From 2b2b675011ec6377d68f9466d8708080642b3903 Mon Sep 17 00:00:00 2001 From: Cole Helbling Date: Fri, 3 Apr 2020 10:17:04 -0700 Subject: [PATCH 1/2] nixenv: describe what went wrong when io fails Instead of just propagating a raw `std::io::Error`, wrap that in our own `Error` enum to provide more context for what actually went wrong. --- ofborg/src/nixenv.rs | 54 +++++++++++++++++++++++++++++++++----------- 1 file changed, 41 insertions(+), 13 deletions(-) diff --git a/ofborg/src/nixenv.rs b/ofborg/src/nixenv.rs index 80afa98..fc3753f 100644 --- a/ofborg/src/nixenv.rs +++ b/ofborg/src/nixenv.rs @@ -5,7 +5,7 @@ use crate::nixstats::EvaluationStats; use crate::outpathdiff; use std::fs::{self, File}; -use std::io::{BufRead, BufReader, Read, Seek, SeekFrom, Write}; +use std::io::{self, BufRead, BufReader, Read, Seek, SeekFrom, Write}; use std::path::PathBuf; pub struct HydraNixEnv { @@ -59,16 +59,32 @@ impl HydraNixEnv { /// Put outpaths.nix in to the project root, which is what /// emulates Hydra's behavior. - fn place_nix(&self) -> Result<(), std::io::Error> { - let mut file = File::create(self.outpath_nix_path())?; - file.write_all(include_bytes!("outpaths.nix"))?; + fn place_nix(&self) -> Result<(), Error> { + let outpath = self.outpath_nix_path(); - Ok(()) + let mut file = match File::create(&outpath) { + Ok(f) => f, + Err(e) => return Err(Error::CreateFile(outpath, e)), + }; + + match file.write_all(include_bytes!("outpaths.nix")) { + Ok(_) => Ok(()), + Err(e) => Err(Error::WriteFile(file, e)), + } } - fn remove_nix(&self) -> Result<(), std::io::Error> { - fs::remove_file(self.outpath_nix_path())?; - fs::remove_file(self.outpath_stats_path())?; + fn remove_nix(&self) -> Result<(), Error> { + let outpath_nix = self.outpath_nix_path(); + let outpath_stats = self.outpath_stats_path(); + + if let Err(e) = fs::remove_file(&outpath_nix) { + return Err(Error::RemoveFile(outpath_nix, e)); + } + + if let Err(e) = fs::remove_file(&outpath_stats) { + return Err(Error::RemoveFile(outpath_stats, e)); + } + Ok(()) } @@ -80,7 +96,7 @@ impl HydraNixEnv { self.path.join(".gc-of-borg-stats.json") } - fn run_nix_env(&self) -> (bool, File, File, Result) { + fn run_nix_env(&self) -> (bool, File, File, Result) { let check_meta = if self.check_meta { "true" } else { "false" }; let mut cmd = self.nix.safe_command( @@ -106,14 +122,17 @@ impl HydraNixEnv { } pub enum Error { - Io(std::io::Error), + Io(io::Error), + CreateFile(PathBuf, io::Error), + RemoveFile(PathBuf, io::Error), + WriteFile(File, io::Error), CommandFailed(File), - StatsParse(File, Result, serde_json::Error), + StatsParse(File, Result, serde_json::Error), UncleanEvaluation(Vec), } -impl From for Error { - fn from(e: std::io::Error) -> Error { +impl From for Error { + fn from(e: io::Error) -> Error { Error::Io(e) } } @@ -122,6 +141,15 @@ impl Error { pub fn display(self) -> String { match self { Error::Io(e) => format!("Failed during the setup of executing nix-env: {:?}", e), + Error::CreateFile(path, err) => { + format!("Failed to create file '{:?}': {:?}", path, err) + } + Error::RemoveFile(path, err) => { + format!("Failed to remove file '{:?}': {:?}", path, err) + } + Error::WriteFile(file, err) => { + format!("Failed to write to file '{:?}': {:?}", file, err) + } Error::CommandFailed(mut fd) => { let mut buffer = Vec::new(); let read_result = fd.read_to_end(&mut buffer); From 9dc0fd123a6a01b10fc5ad140b31fb4d1d2a5feb Mon Sep 17 00:00:00 2001 From: Cole Helbling Date: Fri, 3 Apr 2020 10:51:02 -0700 Subject: [PATCH 2/2] `match` -> `map_err` --- ofborg/src/nixenv.rs | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/ofborg/src/nixenv.rs b/ofborg/src/nixenv.rs index fc3753f..756489c 100644 --- a/ofborg/src/nixenv.rs +++ b/ofborg/src/nixenv.rs @@ -61,29 +61,18 @@ impl HydraNixEnv { /// emulates Hydra's behavior. fn place_nix(&self) -> Result<(), Error> { let outpath = self.outpath_nix_path(); + let mut file = File::create(&outpath).map_err(|e| Error::CreateFile(outpath, e))?; - let mut file = match File::create(&outpath) { - Ok(f) => f, - Err(e) => return Err(Error::CreateFile(outpath, e)), - }; - - match file.write_all(include_bytes!("outpaths.nix")) { - Ok(_) => Ok(()), - Err(e) => Err(Error::WriteFile(file, e)), - } + file.write_all(include_bytes!("outpaths.nix")) + .map_err(|e| Error::WriteFile(file, e)) } fn remove_nix(&self) -> Result<(), Error> { let outpath_nix = self.outpath_nix_path(); let outpath_stats = self.outpath_stats_path(); - if let Err(e) = fs::remove_file(&outpath_nix) { - return Err(Error::RemoveFile(outpath_nix, e)); - } - - if let Err(e) = fs::remove_file(&outpath_stats) { - return Err(Error::RemoveFile(outpath_stats, e)); - } + fs::remove_file(&outpath_nix).map_err(|e| Error::RemoveFile(outpath_nix, e))?; + fs::remove_file(&outpath_stats).map_err(|e| Error::RemoveFile(outpath_stats, e))?; Ok(()) }