2013-10-22 20:02:52 +00:00
|
|
|
{- git-annex fsck results log files
|
|
|
|
-
|
2015-01-21 16:50:09 +00:00
|
|
|
- Copyright 2013 Joey Hess <id@joeyh.name>
|
2013-10-22 20:02:52 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2013-10-22 20:02:52 +00:00
|
|
|
-}
|
|
|
|
|
|
|
|
module Logs.FsckResults (
|
|
|
|
writeFsckResults,
|
2013-11-30 18:29:11 +00:00
|
|
|
readFsckResults,
|
|
|
|
clearFsckResults,
|
2013-10-22 20:02:52 +00:00
|
|
|
) where
|
|
|
|
|
2016-01-20 20:36:33 +00:00
|
|
|
import Annex.Common
|
2013-10-22 20:02:52 +00:00
|
|
|
import Git.Fsck
|
|
|
|
import Git.Types
|
2018-01-02 21:17:10 +00:00
|
|
|
import Logs.File
|
2020-10-29 16:02:46 +00:00
|
|
|
import qualified Utility.RawFilePath as R
|
2013-10-22 20:02:52 +00:00
|
|
|
|
|
|
|
import qualified Data.Set as S
|
|
|
|
|
|
|
|
writeFsckResults :: UUID -> FsckResults -> Annex ()
|
|
|
|
writeFsckResults u fsckresults = do
|
|
|
|
logfile <- fromRepo $ gitAnnexFsckResultsLog u
|
2021-06-30 20:42:08 +00:00
|
|
|
case serializeFsckResults fsckresults of
|
|
|
|
Just s -> store s logfile
|
|
|
|
Nothing -> liftIO $
|
|
|
|
removeWhenExistsWith R.removeLink 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)
|
2013-10-22 20:02:52 +00:00
|
|
|
where
|
2014-03-12 19:18:43 +00:00
|
|
|
serialize s t =
|
|
|
|
let ls = map fromRef (S.toList s)
|
|
|
|
in if t
|
|
|
|
then unlines ("truncated":ls)
|
|
|
|
else unlines ls
|
2013-10-22 20:02:52 +00:00
|
|
|
|
|
|
|
readFsckResults :: UUID -> Annex FsckResults
|
|
|
|
readFsckResults u = do
|
|
|
|
logfile <- fromRepo $ gitAnnexFsckResultsLog u
|
2014-03-12 19:18:43 +00:00
|
|
|
liftIO $ catchDefaultIO (FsckFoundMissing S.empty False) $
|
2021-06-30 20:42:08 +00:00
|
|
|
deserializeFsckResults <$> readFile (fromRawFilePath logfile)
|
|
|
|
|
|
|
|
deserializeFsckResults :: String -> FsckResults
|
|
|
|
deserializeFsckResults = deserialize . lines
|
2013-10-22 20:02:52 +00:00
|
|
|
where
|
2014-03-12 19:18:43 +00:00
|
|
|
deserialize ("truncated":ls) = deserialize' ls True
|
|
|
|
deserialize ls = deserialize' ls False
|
|
|
|
deserialize' ls t =
|
2021-08-11 00:45:02 +00:00
|
|
|
let s = S.fromList $ map (Ref . encodeBS) ls
|
2014-03-12 19:18:43 +00:00
|
|
|
in if S.null s then FsckFailed else FsckFoundMissing s t
|
2013-11-30 18:29:11 +00:00
|
|
|
|
|
|
|
clearFsckResults :: UUID -> Annex ()
|
2020-10-29 16:02:46 +00:00
|
|
|
clearFsckResults = liftIO . removeWhenExistsWith R.removeLink
|
2020-10-29 14:33:12 +00:00
|
|
|
<=< fromRepo . gitAnnexFsckResultsLog
|
2013-11-30 18:29:11 +00:00
|
|
|
|