summaryrefslogtreecommitdiff
path: root/bittorrent/src/System/Torrent/FileMap.hs
diff options
context:
space:
mode:
authorJoe Crayne <joe@jerkface.net>2018-08-10 22:30:48 -0400
committerJoe Crayne <joe@jerkface.net>2018-08-10 22:30:48 -0400
commitf4dd948176187f5fb46a2cf0dbfbfc4c32badfa5 (patch)
tree8fe92fc8471b1ad59943b651ab243d8f67ecf2f1 /bittorrent/src/System/Torrent/FileMap.hs
parente6c64ef4e5fc0870aacff06b4903b4cfa8666478 (diff)
Commented some unused bittorrent related code.
Diffstat (limited to 'bittorrent/src/System/Torrent/FileMap.hs')
-rw-r--r--bittorrent/src/System/Torrent/FileMap.hs16
1 files changed, 14 insertions, 2 deletions
diff --git a/bittorrent/src/System/Torrent/FileMap.hs b/bittorrent/src/System/Torrent/FileMap.hs
index 6e8d7f5a..38c475e8 100644
--- a/bittorrent/src/System/Torrent/FileMap.hs
+++ b/bittorrent/src/System/Torrent/FileMap.hs
@@ -39,7 +39,7 @@ import Data.Torrent
39 39
40data FileEntry = FileEntry 40data FileEntry = FileEntry
41 { filePosition :: {-# UNPACK #-} !FileOffset 41 { filePosition :: {-# UNPACK #-} !FileOffset
42 , fileBytes :: {-# UNPACK #-} !BS.ByteString 42 , fileBytes :: {-# UNPACK #-} !BS.ByteString -- XXX: mutable buffer (see 'writeBytes').
43 } deriving (Show, Eq) 43 } deriving (Show, Eq)
44 44
45type FileMap = Vector FileEntry 45type FileMap = Vector FileEntry
@@ -62,6 +62,7 @@ unmapFiles = V.mapM_ unmapEntry
62 where 62 where
63 unmapEntry (FileEntry _ (PS fptr _ _)) = finalizeForeignPtr fptr 63 unmapEntry (FileEntry _ (PS fptr _ _)) = finalizeForeignPtr fptr
64 64
65-- Unsafe: FileMap 'writeBytes' will modify supplied bytestrings in place.
65fromLazyByteString :: BL.ByteString -> FileMap 66fromLazyByteString :: BL.ByteString -> FileMap
66fromLazyByteString lbs = V.unfoldr f (0, lbs) 67fromLazyByteString lbs = V.unfoldr f (0, lbs)
67 where 68 where
@@ -70,6 +71,8 @@ fromLazyByteString lbs = V.unfoldr f (0, lbs)
70 where chunkSize = fromIntegral $ BS.length x 71 where chunkSize = fromIntegral $ BS.length x
71 72
72-- | /O(n)/. 73-- | /O(n)/.
74--
75-- Unsafe: mutable buffers are returned without copy.
73toLazyByteString :: FileMap -> BL.ByteString 76toLazyByteString :: FileMap -> BL.ByteString
74toLazyByteString = V.foldr f Empty 77toLazyByteString = V.foldr f Empty
75 where 78 where
@@ -82,6 +85,7 @@ size m
82 | FileEntry {..} <- V.unsafeLast m 85 | FileEntry {..} <- V.unsafeLast m
83 = filePosition + fromIntegral (BS.length fileBytes) 86 = filePosition + fromIntegral (BS.length fileBytes)
84 87
88-- | Find the file number for a particular byte offset within a torrent.
85bsearch :: FileOffset -> FileMap -> Maybe Int 89bsearch :: FileOffset -> FileMap -> Maybe Int
86bsearch x m 90bsearch x m
87 | V.null m = Nothing 91 | V.null m = Nothing
@@ -114,12 +118,17 @@ take len m
114 s = System.Torrent.FileMap.size m 118 s = System.Torrent.FileMap.size m
115 119
116-- | /O(log n + m)/. Do not use this function with 'unmapFiles'. 120-- | /O(log n + m)/. Do not use this function with 'unmapFiles'.
121--
122-- The returned bytestring points directly into an area memory mapped from a
123-- file.
117unsafeReadBytes :: FileOffset -> FileSize -> FileMap -> BL.ByteString 124unsafeReadBytes :: FileOffset -> FileSize -> FileMap -> BL.ByteString
118unsafeReadBytes off s m 125unsafeReadBytes off s m
119 | (l , m') <- System.Torrent.FileMap.drop off m 126 | (l , m') <- System.Torrent.FileMap.drop off m
120 , (m'', _ ) <- System.Torrent.FileMap.take (off + s) m' 127 , (m'', _ ) <- System.Torrent.FileMap.take (off + s) m'
121 = BL.take (fromIntegral s) $ BL.drop (fromIntegral l) $ toLazyByteString m'' 128 = BL.take (fromIntegral s) $ BL.drop (fromIntegral l) $ toLazyByteString m''
122 129
130-- The returned bytestring is copied and safe to use after the file is
131-- unmapped.
123readBytes :: FileOffset -> FileSize -> FileMap -> IO BL.ByteString 132readBytes :: FileOffset -> FileSize -> FileMap -> IO BL.ByteString
124readBytes off s m = do 133readBytes off s m = do
125 let bs_copy = BL.copy $ unsafeReadBytes off s m 134 let bs_copy = BL.copy $ unsafeReadBytes off s m
@@ -129,6 +138,8 @@ readBytes off s m = do
129 forceLBS Empty = return () 138 forceLBS Empty = return ()
130 forceLBS (Chunk _ x) = forceLBS x 139 forceLBS (Chunk _ x) = forceLBS x
131 140
141-- UNSAFE: Uses the first byte string as a pointer to mutable data and writes
142-- the contents of the second bytestring there.
132bscpy :: BL.ByteString -> BL.ByteString -> IO () 143bscpy :: BL.ByteString -> BL.ByteString -> IO ()
133bscpy (PS _ _ 0 `Chunk` dest_rest) src = bscpy dest_rest src 144bscpy (PS _ _ 0 `Chunk` dest_rest) src = bscpy dest_rest src
134bscpy dest (PS _ _ 0 `Chunk` src_rest) = bscpy dest src_rest 145bscpy dest (PS _ _ 0 `Chunk` src_rest) = bscpy dest src_rest
@@ -144,8 +155,9 @@ bscpy (PS dest_fptr dest_off dest_size `Chunk` dest_rest)
144 (PS src_fptr (src_off + csize) (src_size - csize) `Chunk` src_rest) 155 (PS src_fptr (src_off + csize) (src_size - csize) `Chunk` src_rest)
145bscpy _ _ = return () 156bscpy _ _ = return ()
146 157
158-- UNSAFE: Mutates bytestring contents within the provided FileMap.
147writeBytes :: FileOffset -> BL.ByteString -> FileMap -> IO () 159writeBytes :: FileOffset -> BL.ByteString -> FileMap -> IO ()
148writeBytes off lbs m = bscpy dest src 160writeBytes off lbs m = bscpy dest src
149 where 161 where
150 src = BL.take (fromIntegral (BL.length dest)) lbs 162 src = BL.take (fromIntegral (BL.length dest)) lbs
151 dest = unsafeReadBytes off (fromIntegral (BL.length lbs)) m \ No newline at end of file 163 dest = unsafeReadBytes off (fromIntegral (BL.length lbs)) m