From 7ad3fd1f6fe3c6719b69f3638542f24b32a3b09c Mon Sep 17 00:00:00 2001 From: Sam T Date: Tue, 16 Jul 2013 07:47:28 +0400 Subject: + Add posix-like file interface. --- src/Data/Torrent.hs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'src/Data/Torrent.hs') diff --git a/src/Data/Torrent.hs b/src/Data/Torrent.hs index 2ebe9374..5971aa9c 100644 --- a/src/Data/Torrent.hs +++ b/src/Data/Torrent.hs @@ -33,7 +33,8 @@ module Data.Torrent -- * Files layout , Layout, contentLayout - , contentLength, pieceCount, blockCount + , contentLength, fileOffset + , pieceCount, blockCount , isSingleFile, isMultiFile , checkPiece @@ -54,6 +55,7 @@ module Data.Torrent -- * Internal , Data.Torrent.hash , Data.Torrent.hashlazy + , layoutOffsets #endif ) where @@ -449,6 +451,17 @@ contentLayout rootPath = filesLayout fl (FileInfo { fiPath = p, fiLength = len }) = (p, fromIntegral len) +layoutOffsets :: Layout -> Layout +layoutOffsets = go 0 + where + go !_ [] = [] + go !offset ((n, s) : xs) = (n, offset) : go (offset + s) xs + +-- | Gives global offset of a content file for a given full path. +fileOffset :: FilePath -> ContentInfo -> Maybe Integer +fileOffset fullPath + = fmap fromIntegral . lookup fullPath . layoutOffsets . contentLayout "" + -- | Test if this is single file torrent. isSingleFile :: ContentInfo -> Bool isSingleFile SingleFile {} = True -- cgit v1.2.3