From 8cdd3cdfb3afb76b01c8dc013d95e6336e4dfb82 Mon Sep 17 00:00:00 2001 From: sternenseemann <0rpkxez4ksa01gb3typccl0i@systemli.org> Date: Sat, 13 Feb 2021 23:41:29 +0100 Subject: pkgs/profpatsch/nman: use pkgs.sternenseemann.temp for TempDir temp is essentially the old TempDir code from nman, but using libc's mkdtemp(3) directly instead of mktemp(1) and a slightly better API. --- pkgs/profpatsch/nman/default.nix | 8 ++++++- pkgs/profpatsch/nman/nman.rs | 48 +++++----------------------------------- 2 files changed, 13 insertions(+), 43 deletions(-) (limited to 'pkgs/profpatsch/nman') diff --git a/pkgs/profpatsch/nman/default.nix b/pkgs/profpatsch/nman/default.nix index 9d74d63b..db081e5a 100644 --- a/pkgs/profpatsch/nman/default.nix +++ b/pkgs/profpatsch/nman/default.nix @@ -1,8 +1,14 @@ -{ lib, writeRustSimpleBin }: +{ lib +, writeRustSimpleBin +, temp +}: writeRustSimpleBin "nman" { meta = { license = lib.licenses.gpl3Only; description = "Open man page in a temporary nix-shell"; }; + dependencies = [ + temp + ]; } ./nman.rs diff --git a/pkgs/profpatsch/nman/nman.rs b/pkgs/profpatsch/nman/nman.rs index c289e6ff..6e7ecf5d 100644 --- a/pkgs/profpatsch/nman/nman.rs +++ b/pkgs/profpatsch/nman/nman.rs @@ -1,46 +1,10 @@ +extern crate temp; + use std::ffi::{OsStr, OsString}; -use std::io::{Error, ErrorKind, self, Write}; use std::os::unix::ffi::OsStrExt; -use std::path::{Path, PathBuf}; -use std::process::{Command,ExitStatus}; - -struct TempDir { - inner: Vec, -} - -impl AsRef for TempDir { - fn as_ref(&self) -> &Path { - OsStr::from_bytes(&self.inner[..]).as_ref() - } -} - -impl Drop for TempDir { - fn drop(&mut self) { - std::fs::remove_dir_all(self.as_ref()); - std::fs::remove_dir(self.as_ref()); - } -} - -fn mktemp(suffix: &str) -> std::io::Result { - let mut mktemp = Command::new("mktemp") - .arg("-d") - .arg("--suffix") - .arg(suffix) - .output()?; - - if mktemp.status.success() { - // remove trailing newline - if mktemp.stdout.ends_with(b"\n") { - mktemp.stdout.pop(); - } - - Ok(TempDir { - inner: mktemp.stdout - }) - } else { - Err(Error::new(ErrorKind::Other, "mktemp exited with a non-zero status")) - } -} +use std::path::PathBuf; +use std::process::Command; +use temp::TempDir; enum NmanError<'a> { NoTempDir, @@ -194,7 +158,7 @@ fn build_man_page<'a>(drv: DrvWithOutput, section: &str, page: &str, tempdir: &T } fn open_man_page<'a>(attr: &'a str, section: &'a str, page: &'a str) -> Result<(), NmanError<'a>> { - let tmpdir = mktemp("-nman").map_err(|_| NmanError::NoTempDir)?; + let tmpdir = TempDir::new("nman").map_err(|_| NmanError::NoTempDir)?; let expr = format!("with (import {{}}); builtins.map (o: {}.\"${{o}}\") {}.outputs", attr, attr); let inst = Command::new("nix-instantiate") .arg("-E") -- cgit 1.4.1