summaryrefslogtreecommitdiff
path: root/bittorrent/examples/FS.hs
blob: 550d85a7977bf3e2102555c14aafdb34f18555c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
{-# LANGUAGE RecordWildCards #-}
module Main (main) where

import Control.Arrow
import Data.ByteString.Char8 as BC
import Data.List as L
import Data.Map as M
import Data.Torrent as T
import Data.Torrent.Tree as T
import System.Environment
import System.Fuse
import System.FilePath
import System.Posix.Files


defStat :: FileStat
defStat = FileStat
  { statEntryType        = Unknown
  , statFileMode         = ownerReadMode
  , statLinkCount        = 2

  , statFileOwner        = 0
  , statFileGroup        = 0

  , statSpecialDeviceID  = 0

  , statFileSize         = 0
  , statBlocks           = 0

  , statAccessTime       = 0
  , statModificationTime = 0
  , statStatusChangeTime = 0
  }

dirStat :: FileStat
dirStat = defStat {
    statEntryType = Directory
  }

type Result a = IO (Either Errno a)
type Result'  = IO Errno

fsGetFileStat :: Torrent -> FilePath -> Result FileStat
fsGetFileStat _ path = return $ Right dirStat

fsOpenDirectory :: Torrent -> FilePath -> Result'
fsOpenDirectory _ _ = return eOK

fsReadDirectory :: Torrent -> FilePath -> Result [(FilePath, FileStat)]
fsReadDirectory Torrent {tInfoDict = InfoDict {..}} path
    | Just cs <- T.lookupDir (L.tail (splitDirectories path)) tree =
      return $ Right $ L.map (BC.unpack *** const defStat) cs
    | otherwise = return $ Left eNOENT
  where
    tree = build $ idLayoutInfo

fsReleaseDirectory :: Torrent -> FilePath -> Result'
fsReleaseDirectory _ _ = return eOK

exfsOps :: Torrent -> FuseOperations ()
exfsOps t = defaultFuseOps
  { fuseGetFileStat        = fsGetFileStat t

  , fuseOpenDirectory      = fsOpenDirectory t
  , fuseReadDirectory      = fsReadDirectory t
  , fuseReleaseDirectory   = fsReleaseDirectory t
  }

main :: IO ()
main = do
  x : xs <- getArgs
  t <- fromFile x
  withArgs xs $ do
    fuseMain (exfsOps t) defaultExceptionHandler