git-annex/Logs/FsckResults.hs
Joey Hess 6f9a9c81f6
convert all readFile, writeFile, and appendFile to close-on-exec safe versions
Even in the Build system. This allows grepping to make sure that there
are none left un-converted:

git grep "writeFile" |grep -v F\\.| grep -v doc/|grep -v writeFileString | grep -v writeFileProtected |grep -v Utility/FileIO
git grep "readFile" |grep -v F\\.| grep -v doc/|grep -v readFileString |grep -v Utility/FileIO
git grep "appendFile" |grep -v F\\.| grep -v doc/|grep -v appendFileString |grep -v Utility/FileIO

Might be nice to automate that to prevent future mistakes...

Sponsored-by: the NIH-funded NICEMAN (ReproNim TR&D3) project
2025-09-05 15:44:32 -04:00

62 lines
1.7 KiB
Haskell

{- git-annex fsck results log files
-
- Copyright 2013 Joey Hess <id@joeyh.name>
-
- Licensed under the GNU AGPL version 3 or higher.
-}
module Logs.FsckResults (
writeFsckResults,
readFsckResults,
clearFsckResults,
) where
import Annex.Common
import Git.Fsck
import Git.Types
import Logs.File
import qualified Data.Set as S
writeFsckResults :: UUID -> FsckResults -> Annex ()
writeFsckResults u fsckresults = do
logfile <- fromRepo $ gitAnnexFsckResultsLog u
case serializeFsckResults fsckresults of
Just s -> store s logfile
Nothing -> liftIO $
removeWhenExistsWith removeFile logfile
where
store s logfile = writeLogFile logfile s
serializeFsckResults :: FsckResults -> Maybe String
serializeFsckResults fsckresults = case fsckresults of
FsckFailed -> Just (serialize S.empty False)
FsckFoundMissing s t
| S.null s -> Nothing
| otherwise -> Just (serialize s t)
where
serialize s t =
let ls = map fromRef (S.toList s)
in if t
then unlines ("truncated":ls)
else unlines ls
readFsckResults :: UUID -> Annex FsckResults
readFsckResults u = do
logfile <- fromRepo $ gitAnnexFsckResultsLog u
liftIO $ catchDefaultIO (FsckFoundMissing S.empty False) $
deserializeFsckResults <$> readFileString logfile
deserializeFsckResults :: String -> FsckResults
deserializeFsckResults = deserialize . lines
where
deserialize ("truncated":ls) = deserialize' ls True
deserialize ls = deserialize' ls False
deserialize' ls t =
let s = S.fromList $ map (Ref . encodeBS) ls
in if S.null s then FsckFailed else FsckFoundMissing s t
clearFsckResults :: UUID -> Annex ()
clearFsckResults = liftIO . removeWhenExistsWith removeFile
<=< fromRepo . gitAnnexFsckResultsLog