From 5d7edcd9d08d86e72fdc65116f3debbde6086845 Mon Sep 17 00:00:00 2001 From: James Crayne Date: Wed, 27 Apr 2016 21:32:22 -0400 Subject: transactional refreshCache function in cokiki --- lib/Kiki.hs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'lib') diff --git a/lib/Kiki.hs b/lib/Kiki.hs index 0e06ee2..34594cc 100644 --- a/lib/Kiki.hs +++ b/lib/Kiki.hs @@ -18,8 +18,10 @@ import Data.Ord import System.Directory import System.FilePath.Posix import System.IO +import System.IO.Temp import System.Posix.User import System.Process +import System.Posix.Files import qualified Codec.Binary.Base64 as Base64 import qualified Data.ByteString.Lazy as L import qualified Data.ByteString.Lazy.Char8 as Char8 @@ -212,9 +214,19 @@ importAndRefresh root cmn = do refreshCache :: KeyRingRuntime -> Maybe FilePath -> IO () refreshCache rt rootdir = do - let mkpath pth = fromMaybe "" rootdir ++ "/var/cache/kiki/"++pth - - write' wr f bs = do + let getMkPathAndCommit destdir = do + let cachedir = takeDirectory destdir + unslash ('/':xs) = xs + unslash xs = xs + createDirectoryIfMissing True cachedir + tmpdir <- createTempDirectory cachedir "tmp.dir" + createSymbolicLink tmpdir (tmpdir ++ ".link") + let mkpath pth = tmpdir unslash pth + commit = rename (tmpdir ++ ".link") destdir + return (mkpath,commit) + (mkpath, commit) <- getMkPathAndCommit (fromMaybe "" rootdir ++ "/var/cache/kiki/config") + + let write' wr f bs = do createDirectoryIfMissing True $ takeDirectory f wr f bs write = write' writeFile @@ -347,7 +359,7 @@ refreshCache rt rootdir = do , " auto=route" , "" ] ++ filter (not . Char8.null) cons - return () + commit strongswanForContact addr oname = Char8.unlines [ "conn " <> p oname -- cgit v1.2.3