summaryrefslogtreecommitdiff
path: root/lib/Kiki.hs
diff options
context:
space:
mode:
authorJames Crayne <jim.crayne@gmail.com>2016-04-27 23:45:30 -0400
committerJames Crayne <jim.crayne@gmail.com>2016-04-27 23:45:30 -0400
commit94763e1e7928377ddc935bed497575c5df4941fb (patch)
treec5bf82f41c443fcd9d7a6769d1771036907960bf /lib/Kiki.hs
parent15a96a1856d924eb3436bc37800dbc3de99e347b (diff)
cleanup old commits, and use dotlock on symlink
Diffstat (limited to 'lib/Kiki.hs')
-rw-r--r--lib/Kiki.hs36
1 files changed, 23 insertions, 13 deletions
diff --git a/lib/Kiki.hs b/lib/Kiki.hs
index 4c9f98c..9fb71be 100644
--- a/lib/Kiki.hs
+++ b/lib/Kiki.hs
@@ -2,6 +2,7 @@
2{-# LANGUAGE OverloadedStrings #-} 2{-# LANGUAGE OverloadedStrings #-}
3module Kiki where 3module Kiki where
4 4
5import Control.Exception
5import Control.Applicative 6import Control.Applicative
6import Control.Arrow 7import Control.Arrow
7import Control.Monad 8import Control.Monad
@@ -19,9 +20,11 @@ import System.Directory
19import System.FilePath.Posix 20import System.FilePath.Posix
20import System.IO 21import System.IO
21import System.IO.Temp 22import System.IO.Temp
23import System.IO.Error
22import System.Posix.User 24import System.Posix.User
23import System.Process 25import System.Process
24import System.Posix.Files 26import System.Posix.Files
27import qualified Data.Traversable as T (mapM)
25import qualified Codec.Binary.Base64 as Base64 28import qualified Codec.Binary.Base64 as Base64
26import qualified Data.ByteString.Lazy as L 29import qualified Data.ByteString.Lazy as L
27import qualified Data.ByteString.Lazy.Char8 as Char8 30import qualified Data.ByteString.Lazy.Char8 as Char8
@@ -30,6 +33,7 @@ import qualified SSHKey as SSH
30 33
31import CommandLine 34import CommandLine
32import KeyRing 35import KeyRing
36import DotLock
33 37
34-- | 38-- |
35-- Regenerate /var/cache/kiki 39-- Regenerate /var/cache/kiki
@@ -215,19 +219,25 @@ refreshCache :: KeyRingRuntime -> Maybe FilePath -> IO ()
215refreshCache rt rootdir = do 219refreshCache rt rootdir = do
216 220
217 let getMkPathAndCommit destdir = do 221 let getMkPathAndCommit destdir = do
218 let cachedir = takeDirectory destdir 222 let cachedir = takeDirectory destdir
219 unslash ('/':xs) = xs 223 unslash ('/':xs) = xs
220 unslash xs = xs 224 unslash xs = xs
221 createDirectoryIfMissing True cachedir 225 timeout = -1 -- TODO: set milisecond timeout on dotlock
222 tmpdir <- createTempDirectory cachedir ("transaction." ++ takeBaseName destdir) 226 createDirectoryIfMissing True cachedir
223 createSymbolicLink tmpdir (tmpdir ++ ".link") 227 tmpdir <- createTempDirectory cachedir ("transaction." ++ takeBaseName destdir)
224 let mkpath pth = tmpdir </> unslash pth 228 createSymbolicLink tmpdir (tmpdir ++ ".link")
225 commit = do 229 lock <- dotlock_create destdir 0
226 -- oldcommit <- readSymbolicLink destdir 230 T.mapM (flip dotlock_take timeout) lock
227 rename (tmpdir ++ ".link") destdir 231 let mkpath pth = tmpdir </> unslash pth
228 -- FIXME: somehow cleanup old commits 232 commit = do
229 -- removeDirectoryRecursive oldcommit 233 oldcommit <- (Just <$> readSymbolicLink destdir)
230 return (mkpath,commit) 234 `catch` \e -> do
235 when (not $ isDoesNotExistError e) $ warn (show e)
236 return Nothing
237 rename (tmpdir ++ ".link") destdir
238 er <- T.mapM dotlock_release lock
239 void $ T.mapM removeDirectoryRecursive oldcommit
240 return (mkpath,commit)
231 (mkpath, commit) <- getMkPathAndCommit (fromMaybe "" rootdir ++ "/var/cache/kiki/config") 241 (mkpath, commit) <- getMkPathAndCommit (fromMaybe "" rootdir ++ "/var/cache/kiki/config")
232 242
233 let write' wr f bs = do 243 let write' wr f bs = do