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);