diff options
-rw-r--r-- | lib/Kiki.hs | 36 |
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 #-} |
3 | module Kiki where | 3 | module Kiki where |
4 | 4 | ||
5 | import Control.Exception | ||
5 | import Control.Applicative | 6 | import Control.Applicative |
6 | import Control.Arrow | 7 | import Control.Arrow |
7 | import Control.Monad | 8 | import Control.Monad |
@@ -19,9 +20,11 @@ import System.Directory | |||
19 | import System.FilePath.Posix | 20 | import System.FilePath.Posix |
20 | import System.IO | 21 | import System.IO |
21 | import System.IO.Temp | 22 | import System.IO.Temp |
23 | import System.IO.Error | ||
22 | import System.Posix.User | 24 | import System.Posix.User |
23 | import System.Process | 25 | import System.Process |
24 | import System.Posix.Files | 26 | import System.Posix.Files |
27 | import qualified Data.Traversable as T (mapM) | ||
25 | import qualified Codec.Binary.Base64 as Base64 | 28 | import qualified Codec.Binary.Base64 as Base64 |
26 | import qualified Data.ByteString.Lazy as L | 29 | import qualified Data.ByteString.Lazy as L |
27 | import qualified Data.ByteString.Lazy.Char8 as Char8 | 30 | import qualified Data.ByteString.Lazy.Char8 as Char8 |
@@ -30,6 +33,7 @@ import qualified SSHKey as SSH | |||
30 | 33 | ||
31 | import CommandLine | 34 | import CommandLine |
32 | import KeyRing | 35 | import KeyRing |
36 | import DotLock | ||
33 | 37 | ||
34 | -- | | 38 | -- | |
35 | -- Regenerate /var/cache/kiki | 39 | -- Regenerate /var/cache/kiki |
@@ -215,19 +219,25 @@ refreshCache :: KeyRingRuntime -> Maybe FilePath -> IO () | |||
215 | refreshCache rt rootdir = do | 219 | refreshCache 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 |