keter/Keter/TempFolder.hs

42 lines
1.2 KiB
Haskell
Raw Normal View History

2012-05-08 16:18:06 +04:00
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE RecordWildCards #-}
2012-05-14 12:18:09 +04:00
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
2012-05-08 16:18:06 +04:00
module Keter.TempFolder
( TempFolder
, setup
, getFolder
) where
2012-05-14 12:18:09 +04:00
import Keter.Prelude
2012-05-08 16:18:06 +04:00
import Data.Word (Word)
import Keter.Postgres (Appname)
2012-05-14 12:18:09 +04:00
import qualified Data.IORef as I
import System.Posix.Files.ByteString (setOwnerAndGroup)
import System.Posix.Types (UserID, GroupID)
import qualified Filesystem.Path.CurrentOS as F
2012-05-08 16:18:06 +04:00
data TempFolder = TempFolder
{ tfRoot :: FilePath
2012-05-14 12:18:09 +04:00
, tfCounter :: IORef Word
2012-05-08 16:18:06 +04:00
}
2012-05-14 12:18:09 +04:00
setup :: FilePath -> KIO (Either SomeException TempFolder)
setup fp = liftIO $ do
e <- isDirectory fp
when e $ removeTree fp
createTree fp
2012-05-08 16:18:06 +04:00
c <- I.newIORef minBound
return $ TempFolder fp c
getFolder :: Maybe (UserID, GroupID) -> TempFolder -> Appname -> KIO (Either SomeException FilePath)
getFolder muid TempFolder {..} appname = do
2012-05-14 12:18:09 +04:00
!i <- atomicModifyIORef tfCounter $ \i -> (succ i, i)
let fp = tfRoot </> fromText (appname ++ "-" ++ show i)
liftIO $ do
createTree fp
case muid of
Nothing -> return ()
Just (uid, gid) -> setOwnerAndGroup (F.encode fp) uid gid
return fp