diff options
-rw-r--r-- | bittorrent.cabal | 2 | ||||
-rw-r--r-- | src/Data/Torrent/InfoHash.hs | 9 | ||||
-rw-r--r-- | src/Data/Torrent/Layout.hs | 2 | ||||
-rw-r--r-- | src/Data/Torrent/Tree.hs | 30 |
4 files changed, 31 insertions, 12 deletions
diff --git a/bittorrent.cabal b/bittorrent.cabal index 49f444e2..65e8208d 100644 --- a/bittorrent.cabal +++ b/bittorrent.cabal | |||
@@ -41,7 +41,7 @@ library | |||
41 | , RecordWildCards | 41 | , RecordWildCards |
42 | hs-source-dirs: src | 42 | hs-source-dirs: src |
43 | exposed-modules: Data.Torrent | 43 | exposed-modules: Data.Torrent |
44 | , Data.Torrent.Bitfield | 44 | -- , Data.Torrent.Bitfield |
45 | , Data.Torrent.Block | 45 | , Data.Torrent.Block |
46 | , Data.Torrent.InfoHash | 46 | , Data.Torrent.InfoHash |
47 | , Data.Torrent.Layout | 47 | , Data.Torrent.Layout |
diff --git a/src/Data/Torrent/InfoHash.hs b/src/Data/Torrent/InfoHash.hs index 5ec429b5..ac13aa6c 100644 --- a/src/Data/Torrent/InfoHash.hs +++ b/src/Data/Torrent/InfoHash.hs | |||
@@ -1,3 +1,12 @@ | |||
1 | -- | | ||
2 | -- Copyright : (c) Sam Truzjan 2013 | ||
3 | -- License : BSD3 | ||
4 | -- Maintainer : pxqr.sta@gmail.com | ||
5 | -- Stability : experimental | ||
6 | -- Portability : portable | ||
7 | -- | ||
8 | -- Infohash is a unique identifier of torrent. | ||
9 | -- | ||
1 | {-# LANGUAGE FlexibleInstances #-} | 10 | {-# LANGUAGE FlexibleInstances #-} |
2 | {-# LANGUAGE GeneralizedNewtypeDeriving #-} | 11 | {-# LANGUAGE GeneralizedNewtypeDeriving #-} |
3 | module Data.Torrent.InfoHash | 12 | module Data.Torrent.InfoHash |
diff --git a/src/Data/Torrent/Layout.hs b/src/Data/Torrent/Layout.hs index 6f0668f2..ea8fa894 100644 --- a/src/Data/Torrent/Layout.hs +++ b/src/Data/Torrent/Layout.hs | |||
@@ -5,7 +5,7 @@ | |||
5 | -- Stability : experimental | 5 | -- Stability : experimental |
6 | -- Portability : portable | 6 | -- Portability : portable |
7 | -- | 7 | -- |
8 | -- | 8 | -- Layout of files in torrent. |
9 | -- | 9 | -- |
10 | {-# LANGUAGE BangPatterns #-} | 10 | {-# LANGUAGE BangPatterns #-} |
11 | {-# LANGUAGE FlexibleInstances #-} | 11 | {-# LANGUAGE FlexibleInstances #-} |
diff --git a/src/Data/Torrent/Tree.hs b/src/Data/Torrent/Tree.hs index 8c18041a..cf71c2ec 100644 --- a/src/Data/Torrent/Tree.hs +++ b/src/Data/Torrent/Tree.hs | |||
@@ -5,21 +5,25 @@ | |||
5 | -- Stability : experimental | 5 | -- Stability : experimental |
6 | -- Portability : portable | 6 | -- Portability : portable |
7 | -- | 7 | -- |
8 | -- Directory tree can be used to easily manipulate file layout info. | ||
9 | -- | ||
8 | {-# LANGUAGE FlexibleInstances #-} | 10 | {-# LANGUAGE FlexibleInstances #-} |
9 | {-# LANGUAGE TemplateHaskell #-} | 11 | {-# LANGUAGE TemplateHaskell #-} |
10 | {-# LANGUAGE DeriveDataTypeable #-} | 12 | {-# LANGUAGE DeriveDataTypeable #-} |
11 | module Data.Torrent.Tree | 13 | module Data.Torrent.Tree |
12 | ( DirTree (..) | 14 | ( -- * Directory tree |
15 | DirTree (..) | ||
16 | |||
17 | -- * Construction | ||
13 | , build | 18 | , build |
14 | 19 | ||
20 | -- * Query | ||
15 | , Data.Torrent.Tree.lookup | 21 | , Data.Torrent.Tree.lookup |
16 | , lookupDir | 22 | , lookupDir |
17 | |||
18 | , fileNumber | 23 | , fileNumber |
19 | , dirNumber | 24 | , dirNumber |
20 | ) where | 25 | ) where |
21 | 26 | ||
22 | import Control.Arrow | ||
23 | import Data.ByteString as BS | 27 | import Data.ByteString as BS |
24 | import Data.ByteString.Char8 as BC | 28 | import Data.ByteString.Char8 as BC |
25 | import Data.Foldable | 29 | import Data.Foldable |
@@ -30,10 +34,12 @@ import Data.Monoid | |||
30 | import Data.Torrent.Layout | 34 | import Data.Torrent.Layout |
31 | 35 | ||
32 | 36 | ||
37 | -- | 'DirTree' is more convenient form of 'LayoutInfo'. | ||
33 | data DirTree a = Dir { children :: Map ByteString (DirTree a) } | 38 | data DirTree a = Dir { children :: Map ByteString (DirTree a) } |
34 | | File { node :: FileInfo a } | 39 | | File { node :: FileInfo a } |
35 | deriving Show | 40 | deriving Show |
36 | 41 | ||
42 | -- | Build directory tree from a list of files. | ||
37 | build :: LayoutInfo -> DirTree () | 43 | build :: LayoutInfo -> DirTree () |
38 | build SingleFile {liFile = FileInfo {..}} = Dir | 44 | build SingleFile {liFile = FileInfo {..}} = Dir |
39 | { children = M.singleton fiName (File fi) } | 45 | { children = M.singleton fiName (File fi) } |
@@ -46,26 +52,30 @@ build MultiFile {..} = Dir $ M.singleton liDirName files | |||
46 | where | 52 | where |
47 | ent = File $ FileInfo fiLength fiMD5Sum () | 53 | ent = File $ FileInfo fiLength fiMD5Sum () |
48 | 54 | ||
49 | decompress :: DirTree () -> [FileInfo ()] | 55 | --decompress :: DirTree () -> [FileInfo ()] |
50 | decompress = undefined | 56 | --decompress = undefined |
51 | 57 | ||
58 | -- | Lookup file by path. | ||
52 | lookup :: [FilePath] -> DirTree a -> Maybe (DirTree a) | 59 | lookup :: [FilePath] -> DirTree a -> Maybe (DirTree a) |
53 | lookup [] t = Just t | 60 | lookup [] t = Just t |
54 | lookup (p : ps) (Dir m) | Just subTree <- M.lookup (BC.pack p) m | 61 | lookup (p : ps) (Dir m) | Just subTree <- M.lookup (BC.pack p) m |
55 | = Data.Torrent.Tree.lookup ps subTree | 62 | = Data.Torrent.Tree.lookup ps subTree |
56 | lookup _ _ = Nothing | 63 | lookup _ _ = Nothing |
57 | 64 | ||
65 | -- | Lookup directory by path. | ||
58 | lookupDir :: [FilePath] -> DirTree a -> Maybe [(ByteString, DirTree a)] | 66 | lookupDir :: [FilePath] -> DirTree a -> Maybe [(ByteString, DirTree a)] |
59 | lookupDir ps d | 67 | lookupDir ps d = do |
60 | | Just subTree <- Data.Torrent.Tree.lookup ps d = | 68 | subTree <- Data.Torrent.Tree.lookup ps d |
61 | case subTree of | 69 | case subTree of |
62 | File _ -> Nothing | 70 | File _ -> Nothing |
63 | Dir es -> Just $ M.toList es | 71 | Dir es -> Just $ M.toList es |
64 | 72 | ||
73 | -- | Get total count of files in directory and subdirectories. | ||
65 | fileNumber :: DirTree a -> Sum Int | 74 | fileNumber :: DirTree a -> Sum Int |
66 | fileNumber File {..} = Sum 1 | 75 | fileNumber File {..} = Sum 1 |
67 | fileNumber Dir {..} = foldMap fileNumber children | 76 | fileNumber Dir {..} = foldMap fileNumber children |
68 | 77 | ||
78 | -- | Get total count of directories in the directory and subdirectories. | ||
69 | dirNumber :: DirTree a -> Sum Int | 79 | dirNumber :: DirTree a -> Sum Int |
70 | dirNumber File {..} = Sum 0 | 80 | dirNumber File {..} = Sum 0 |
71 | dirNumber Dir {..} = Sum 1 <> foldMap dirNumber children | 81 | dirNumber Dir {..} = Sum 1 <> foldMap dirNumber children |