summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Joey Hess <joey@kitenet.net>2010-10-31 01:06:58 -0400
committerGravatar Joey Hess <joey@kitenet.net>2010-10-31 01:06:58 -0400
commit59672d32edd9cc30942a9200670b3e1f817d26aa (patch)
tree72e1bba9d6b43422849d736fcccafedafb964e7d
parentd5a0c16298a625d80288276a92dcfebf1cb9b3de (diff)
write to tmp file
Writing to a tmp file means no locking is needed, and it fixes a bug introduced by the last commit, which made log files be read lazily, so they could still be open when written, which breaks due to haskell's internal locking.
-rw-r--r--LocationLog.hs14
1 files changed, 5 insertions, 9 deletions
diff --git a/LocationLog.hs b/LocationLog.hs
index 20e777c56..14ae88abc 100644
--- a/LocationLog.hs
+++ b/LocationLog.hs
@@ -35,6 +35,7 @@ import qualified Data.Map as Map
import System.IO
import System.Directory
import Data.Char
+import System.Posix.Process
import qualified GitRepo as Git
import Utility
@@ -111,19 +112,14 @@ readLog file = do
else do
return []
-{- Adds a LogLine to a log file -}
-appendLog :: FilePath -> LogLine -> IO ()
-appendLog file line = do
- createDirectoryIfMissing True (parentDir file)
- withFileLocked file AppendMode $ \h ->
- hPutStrLn h $ show line
-
{- Writes a set of lines to a log file -}
writeLog :: FilePath -> [LogLine] -> IO ()
writeLog file lines = do
+ pid <- getProcessID
+ let tmpfile = file ++ ".tmp" ++ show pid
createDirectoryIfMissing True (parentDir file)
- withFileLocked file WriteMode $ \h ->
- hPutStr h $ unlines $ map show lines
+ writeFile tmpfile $ unlines $ map show lines
+ renameFile tmpfile file
{- Generates a new LogLine with the current date. -}
logNow :: LogStatus -> UUID -> IO LogLine