module Network.NineP.Internal.File
( NineFile(..)
, boringFile
, boringDir
) where
import Control.Exception
import Control.Monad.EmbedIO
import qualified Data.ByteString.Lazy.Char8 as B
import qualified Data.Map as M
import Data.Word
import Data.NineP
import Network.NineP.Error
data NineFile m =
RegularFile {
read :: Word64
-> Word32
-> m (B.ByteString),
write :: Word64
-> B.ByteString
-> m (Word32),
remove :: m (),
stat :: m Stat,
wstat :: Stat -> m (),
version :: m Word32
} | Directory {
getFiles :: m [NineFile m],
parent :: m (Maybe (NineFile m)),
descend :: String -> m (NineFile m),
create :: String -> Word32 -> m (NineFile m),
remove :: m (),
stat :: m Stat,
wstat :: Stat -> m (),
version :: m Word32
}
boringStat :: Stat
boringStat = Stat 0 0 (Qid 0 0 0) 0o0777 0 0 0 "boring" "root" "root" "root"
boringFile :: (Monad m, EmbedIO m) => String -> NineFile m
boringFile name = RegularFile
(\_ _ -> return "")
(\_ _ -> return 0)
(return ())
(return $ boringStat {st_name = name})
(const $ return ())
(return 0)
boringDir :: (Monad m, EmbedIO m) => String -> [(String, NineFile m)] -> NineFile m
boringDir name contents = let m = M.fromList contents in Directory {
getFiles = (return $ map snd $ contents),
descend = (\x -> case M.lookup x m of
Nothing -> throw $ ENoFile x
Just f -> return f),
create = (\name perms -> return $ boringFile name),
remove = (return ()),
stat = (return $ boringStat {st_name = name}),
wstat = (const $ return ()),
version = (return 0),
parent = return Nothing }