From 4f42f6f687499ed62f330dcb254fdb9602f3de5b Mon Sep 17 00:00:00 2001 From: hashirama Date: Wed, 19 Feb 2025 03:48:11 +0000 Subject: [PATCH] Update ZfsDevices.hs --- ZfsDevices.hs | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/ZfsDevices.hs b/ZfsDevices.hs index adca037..9fd257a 100644 --- a/ZfsDevices.hs +++ b/ZfsDevices.hs @@ -1,16 +1,34 @@ -- Copyright © 2025 Hashirama Senju + import System.Zfs.Zpool import System.Zfs.Types import System.Zfs import Control.Monad.IO.Class -import System.Process (callCommand) +import System.Process (createProcess, shell, waitForProcess, CreateProcess(..), StdStream(..)) +import System.IO (openTempFile, hClose, IOMode(WriteMode)) +import System.Directory (getTemporaryDirectory, removeFile) +import Control.Exception (bracket) + +callCommandSilently :: String -> IO () +callCommandSilently cmd = do + tempDir <- getTemporaryDirectory + let tempFile = tempDir ++ "/temp_output.log" + bracket + (openTempFile tempDir "temp_output.log") + (\(path, handle) -> hClose handle >> removeFile path) + (\(path, handle) -> do + let process = (shell cmd) { std_out = UseHandle handle, std_err = UseHandle handle } + (_, _, _, ph) <- createProcess process + waitForProcess ph + return () + ) main' :: Zfs () main' = do - forZpools_ $ \zpool -> do - our_pools <- getZpools + + forZpools_ $ \zpool -> do nm <- getZpoolName zpool liftIO $ putStrLn ("Pool: "++nm) vdevs <- getVdevs zpool @@ -23,7 +41,7 @@ main' = do main :: IO () main = do - callCommand "zpool import -a" -- should make a ffi function for it + callCommandSilently "zpool import -a -N" -- detection, a ffi would be more elegant res <- runZfs main' case res of Left e -> print e