fixed close after locking
This commit is contained in:
parent
11ad93f023
commit
f98fa53d7f
3 changed files with 16 additions and 8 deletions
|
@ -12,6 +12,11 @@
|
||||||
-
|
-
|
||||||
- A line of the log will look like: "date N reponame"
|
- A line of the log will look like: "date N reponame"
|
||||||
- Where N=1 when the repo has the file, and 0 otherwise.
|
- Where N=1 when the repo has the file, and 0 otherwise.
|
||||||
|
-
|
||||||
|
- TOOD: compact logs, by storing only current presence infomation when
|
||||||
|
- writing them.
|
||||||
|
-
|
||||||
|
- TODO: use ByteString
|
||||||
-}
|
-}
|
||||||
|
|
||||||
module LocationLog where
|
module LocationLog where
|
||||||
|
@ -67,9 +72,8 @@ readLog file = do
|
||||||
exists <- doesFileExist file
|
exists <- doesFileExist file
|
||||||
if exists
|
if exists
|
||||||
then do
|
then do
|
||||||
h <- openLocked file ReadMode
|
s <- withFileLocked file ReadMode $ \h ->
|
||||||
s <- hGetContentsStrict h
|
hGetContentsStrict h
|
||||||
hClose h
|
|
||||||
-- filter out any unparsable lines
|
-- filter out any unparsable lines
|
||||||
return $ filter (\l -> (status l) /= Undefined )
|
return $ filter (\l -> (status l) /= Undefined )
|
||||||
$ map read $ lines s
|
$ map read $ lines s
|
||||||
|
@ -80,9 +84,8 @@ readLog file = do
|
||||||
writeLog :: String -> LogLine -> IO ()
|
writeLog :: String -> LogLine -> IO ()
|
||||||
writeLog file line = do
|
writeLog file line = do
|
||||||
createDirectoryIfMissing True (parentDir file)
|
createDirectoryIfMissing True (parentDir file)
|
||||||
h <- openLocked file AppendMode
|
withFileLocked file AppendMode $ \h ->
|
||||||
hPutStrLn h $ show line
|
hPutStrLn h $ show line
|
||||||
hClose h
|
|
||||||
|
|
||||||
{- Generates a new LogLine with the current date. -}
|
{- Generates a new LogLine with the current date. -}
|
||||||
logNow :: LogStatus -> String -> IO LogLine
|
logNow :: LogStatus -> String -> IO LogLine
|
||||||
|
|
|
@ -9,12 +9,15 @@ import Data.String.Utils
|
||||||
|
|
||||||
{- Let's just say that Haskell makes reading/writing a file with
|
{- Let's just say that Haskell makes reading/writing a file with
|
||||||
- file locking excessively difficult. -}
|
- file locking excessively difficult. -}
|
||||||
openLocked file mode = do
|
withFileLocked file mode action = do
|
||||||
|
-- TODO: find a way to use bracket here
|
||||||
handle <- openFile file mode
|
handle <- openFile file mode
|
||||||
lockfd <- handleToFd handle -- closes handle
|
lockfd <- handleToFd handle -- closes handle
|
||||||
waitToSetLock lockfd (lockType mode, AbsoluteSeek, 0, 0)
|
waitToSetLock lockfd (lockType mode, AbsoluteSeek, 0, 0)
|
||||||
handle' <- fdToHandle lockfd
|
handle' <- fdToHandle lockfd
|
||||||
return handle'
|
ret <- action handle'
|
||||||
|
hClose handle'
|
||||||
|
return ret
|
||||||
where
|
where
|
||||||
lockType ReadMode = ReadLock
|
lockType ReadMode = ReadLock
|
||||||
lockType _ = WriteLock
|
lockType _ = WriteLock
|
||||||
|
|
|
@ -6,3 +6,5 @@ import GitRepo
|
||||||
|
|
||||||
main = do
|
main = do
|
||||||
gitPrep
|
gitPrep
|
||||||
|
l <- readLog "demo.log"
|
||||||
|
writeLog "demo2.log" $ l !! 0
|
||||||
|
|
Loading…
Reference in a new issue