2018-01-02 21:17:10 +00:00
|
|
|
{- git-annex log files
|
|
|
|
-
|
2020-10-20 20:42:28 +00:00
|
|
|
- Copyright 2018-2020 Joey Hess <id@joeyh.name>
|
2018-01-02 21:17:10 +00:00
|
|
|
-
|
2018-10-25 18:43:13 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2018-01-02 21:17:10 +00:00
|
|
|
-}
|
|
|
|
|
2020-10-20 20:42:28 +00:00
|
|
|
{-# LANGUAGE BangPatterns #-}
|
|
|
|
|
|
|
|
module Logs.File (
|
|
|
|
writeLogFile,
|
|
|
|
withLogHandle,
|
|
|
|
appendLogFile,
|
|
|
|
modifyLogFile,
|
|
|
|
streamLogFile,
|
|
|
|
checkLogFile,
|
|
|
|
) where
|
2018-01-02 21:17:10 +00:00
|
|
|
|
|
|
|
import Annex.Common
|
|
|
|
import Annex.Perms
|
2018-10-25 18:43:13 +00:00
|
|
|
import Annex.LockFile
|
2019-05-20 20:37:04 +00:00
|
|
|
import Annex.ReplaceFile
|
2018-10-25 18:43:13 +00:00
|
|
|
import qualified Git
|
2018-01-02 21:17:10 +00:00
|
|
|
import Utility.Tmp
|
|
|
|
|
2020-10-20 20:42:28 +00:00
|
|
|
import qualified Data.ByteString.Lazy as L
|
|
|
|
import qualified Data.ByteString.Lazy.Char8 as L8
|
|
|
|
|
2018-01-04 18:46:58 +00:00
|
|
|
-- | Writes content to a file, replacing the file atomically, and
|
|
|
|
-- making the new file have whatever permissions the git repository is
|
|
|
|
-- configured to use. Creates the parent directory when necessary.
|
2020-10-29 16:02:46 +00:00
|
|
|
writeLogFile :: RawFilePath -> String -> Annex ()
|
|
|
|
writeLogFile f c = createDirWhenNeeded f $ viaTmp writelog (fromRawFilePath f) c
|
2018-01-02 21:17:10 +00:00
|
|
|
where
|
2020-11-05 22:45:37 +00:00
|
|
|
writelog tmp c' = do
|
|
|
|
liftIO $ writeFile tmp c'
|
|
|
|
setAnnexFilePerm (toRawFilePath tmp)
|
2018-10-25 18:43:13 +00:00
|
|
|
|
2019-05-20 20:37:04 +00:00
|
|
|
-- | Runs the action with a handle connected to a temp file.
|
|
|
|
-- The temp file replaces the log file once the action succeeds.
|
2020-10-29 16:02:46 +00:00
|
|
|
withLogHandle :: RawFilePath -> (Handle -> Annex a) -> Annex a
|
2019-05-20 20:37:04 +00:00
|
|
|
withLogHandle f a = do
|
|
|
|
createAnnexDirectory (parentDir f)
|
2020-10-29 16:02:46 +00:00
|
|
|
replaceGitAnnexDirFile (fromRawFilePath f) $ \tmp ->
|
2019-05-20 20:37:04 +00:00
|
|
|
bracket (setup tmp) cleanup a
|
|
|
|
where
|
|
|
|
setup tmp = do
|
2020-11-05 22:45:37 +00:00
|
|
|
setAnnexFilePerm (toRawFilePath tmp)
|
2019-05-20 20:37:04 +00:00
|
|
|
liftIO $ openFile tmp WriteMode
|
|
|
|
cleanup h = liftIO $ hClose h
|
|
|
|
|
2018-10-25 18:43:13 +00:00
|
|
|
-- | Appends a line to a log file, first locking it to prevent
|
|
|
|
-- concurrent writers.
|
2020-11-03 14:11:04 +00:00
|
|
|
appendLogFile :: RawFilePath -> (Git.Repo -> RawFilePath) -> L.ByteString -> Annex ()
|
2020-10-29 16:02:46 +00:00
|
|
|
appendLogFile f lck c =
|
2020-11-03 14:11:04 +00:00
|
|
|
createDirWhenNeeded f $
|
2020-10-29 16:02:46 +00:00
|
|
|
withExclusiveLock lck $ do
|
2020-11-03 14:11:04 +00:00
|
|
|
liftIO $ withFile f' AppendMode $
|
|
|
|
\h -> L8.hPutStrLn h c
|
2020-11-05 22:45:37 +00:00
|
|
|
setAnnexFilePerm (toRawFilePath f')
|
2020-11-03 14:11:04 +00:00
|
|
|
where
|
|
|
|
f' = fromRawFilePath f
|
2018-10-25 18:43:13 +00:00
|
|
|
|
2020-10-20 20:42:28 +00:00
|
|
|
-- | Modifies a log file.
|
|
|
|
--
|
|
|
|
-- If the function does not make any changes, avoids rewriting the file
|
|
|
|
-- for speed, but that does mean the whole file content has to be buffered
|
|
|
|
-- in memory.
|
|
|
|
--
|
|
|
|
-- The file is locked to prevent concurrent writers, and it is written
|
|
|
|
-- atomically.
|
2020-11-03 14:11:04 +00:00
|
|
|
modifyLogFile :: RawFilePath -> (Git.Repo -> RawFilePath) -> ([L.ByteString] -> [L.ByteString]) -> Annex ()
|
2020-10-20 20:42:28 +00:00
|
|
|
modifyLogFile f lck modf = withExclusiveLock lck $ do
|
|
|
|
ls <- liftIO $ fromMaybe []
|
2020-11-03 14:11:04 +00:00
|
|
|
<$> tryWhenExists (L8.lines <$> L.readFile f')
|
2020-10-20 20:42:28 +00:00
|
|
|
let ls' = modf ls
|
|
|
|
when (ls' /= ls) $
|
2020-11-03 14:11:04 +00:00
|
|
|
createDirWhenNeeded f $
|
|
|
|
viaTmp writelog f' (L8.unlines ls')
|
2020-10-20 20:42:28 +00:00
|
|
|
where
|
2020-11-03 14:11:04 +00:00
|
|
|
f' = fromRawFilePath f
|
|
|
|
writelog lf b = do
|
|
|
|
liftIO $ L.writeFile lf b
|
2020-11-05 22:45:37 +00:00
|
|
|
setAnnexFilePerm (toRawFilePath lf)
|
2020-10-20 20:42:28 +00:00
|
|
|
|
|
|
|
-- | Checks the content of a log file to see if any line matches.
|
|
|
|
--
|
|
|
|
-- This can safely be used while appendLogFile or any atomic
|
|
|
|
-- action is concurrently modifying the file. It does not lock the file,
|
|
|
|
-- for speed, but instead relies on the fact that a log file usually
|
|
|
|
-- ends in a newline.
|
2020-10-29 16:02:46 +00:00
|
|
|
checkLogFile :: FilePath -> (Git.Repo -> RawFilePath) -> (L.ByteString -> Bool) -> Annex Bool
|
2020-10-20 20:42:28 +00:00
|
|
|
checkLogFile f lck matchf = withExclusiveLock lck $ bracket setup cleanup go
|
|
|
|
where
|
|
|
|
setup = liftIO $ tryWhenExists $ openFile f ReadMode
|
|
|
|
cleanup Nothing = noop
|
|
|
|
cleanup (Just h) = liftIO $ hClose h
|
|
|
|
go Nothing = return False
|
|
|
|
go (Just h) = do
|
|
|
|
!r <- liftIO (any matchf . fullLines <$> L.hGetContents h)
|
|
|
|
return r
|
|
|
|
|
|
|
|
-- | Gets only the lines that end in a newline. If the last part of a file
|
|
|
|
-- does not, it's assumed to be a new line being logged that is incomplete,
|
|
|
|
-- and is omitted.
|
|
|
|
--
|
|
|
|
-- Unlike lines, this does not collapse repeated newlines etc.
|
|
|
|
fullLines :: L.ByteString -> [L.ByteString]
|
|
|
|
fullLines = go []
|
|
|
|
where
|
|
|
|
go c b = case L8.elemIndex '\n' b of
|
|
|
|
Nothing -> reverse c
|
|
|
|
Just n ->
|
|
|
|
let (l, b') = L.splitAt n b
|
|
|
|
in go (l:c) (L.drop 1 b')
|
|
|
|
|
2018-10-25 18:43:13 +00:00
|
|
|
-- | Streams lines from a log file, and then empties the file at the end.
|
|
|
|
--
|
|
|
|
-- If the action is interrupted or throws an exception, the log file is
|
|
|
|
-- left unchanged.
|
|
|
|
--
|
|
|
|
-- Does nothing if the log file does not exist.
|
|
|
|
--
|
|
|
|
-- Locking is used to prevent writes to to the log file while this
|
|
|
|
-- is running.
|
2020-10-29 16:02:46 +00:00
|
|
|
streamLogFile :: FilePath -> (Git.Repo -> RawFilePath) -> (String -> Annex ()) -> Annex ()
|
2018-10-25 18:43:13 +00:00
|
|
|
streamLogFile f lck a = withExclusiveLock lck $ bracketOnError setup cleanup go
|
|
|
|
where
|
|
|
|
setup = liftIO $ tryWhenExists $ openFile f ReadMode
|
|
|
|
cleanup Nothing = noop
|
|
|
|
cleanup (Just h) = liftIO $ hClose h
|
|
|
|
go Nothing = noop
|
|
|
|
go (Just h) = do
|
|
|
|
mapM_ a =<< liftIO (lines <$> hGetContents h)
|
|
|
|
liftIO $ hClose h
|
|
|
|
liftIO $ writeFile f ""
|
2020-11-05 22:45:37 +00:00
|
|
|
setAnnexFilePerm (toRawFilePath f)
|
2018-10-25 18:43:13 +00:00
|
|
|
|
2020-10-29 16:02:46 +00:00
|
|
|
createDirWhenNeeded :: RawFilePath -> Annex () -> Annex ()
|
2018-10-25 18:43:13 +00:00
|
|
|
createDirWhenNeeded f a = a `catchNonAsync` \_e -> do
|
|
|
|
-- Most of the time, the directory will exist, so this is only
|
|
|
|
-- done if writing the file fails.
|
|
|
|
createAnnexDirectory (parentDir f)
|
|
|
|
a
|