Safe Haskell | None |
---|---|
Language | Haskell2010 |
This module is about stream-processing tar archives. It is currently
not very well tested. See the documentation of withEntries
for an usage sample.
- untar :: Monad m => ConduitM ByteString TarChunk m ()
- withEntry :: MonadThrow m => (Header -> ConduitM ByteString o m r) -> ConduitM TarChunk o m r
- withEntries :: MonadThrow m => (Header -> ConduitM ByteString o m ()) -> ConduitM TarChunk o m ()
- headerFileType :: Header -> FileType
- headerFilePath :: Header -> FilePath
- data Header = Header {
- headerOffset :: !Offset
- headerPayloadOffset :: !Offset
- headerFileNameSuffix :: !ShortByteString
- headerFileMode :: !CMode
- headerOwnerId :: !Int
- headerGroupId :: !Int
- headerPayloadSize :: !Size
- headerTime :: !Int64
- headerLinkIndicator :: !Word8
- headerOwnerName :: !ShortByteString
- headerGroupName :: !ShortByteString
- headerDeviceMajor :: !Int
- headerDeviceMinor :: !Int
- headerFileNamePrefix :: !ShortByteString
- data TarChunk
- data TarException
- type Offset = Int
- type Size = Int
- data FileType
Basic functions
withEntry :: MonadThrow m => (Header -> ConduitM ByteString o m r) -> ConduitM TarChunk o m r Source #
Process a single tar entry. See withEntries
for more details.
withEntries :: MonadThrow m => (Header -> ConduitM ByteString o m ()) -> ConduitM TarChunk o m () Source #
This function handles each entry of the tar archive according to the behaviour of the function passed as first argument.
Here is a full example function, that reads a compressed tar archive and for each entry that is a simple file, it prints its file path and SHA256 digest. Note that this function can throw exceptions!
import qualified Crypto.Hash.Conduit as CH import qualified Data.Conduit.Tar as CT import Conduit import Crypto.Hash (Digest, SHA256) import Control.Monad (when) import Data.Conduit.Zlib (ungzip) import Data.ByteString (ByteString) filedigests :: FilePath -> IO () filedigests fp = runConduitRes ( sourceFileBS fp -- read the raw file .| ungzip -- gunzip .| CT.untar -- decode the tar archive .| CT.withEntries hashentry -- process each file .| printC -- print the results ) where hashentry :: Monad m => CT.Header -> Conduit ByteString m (FilePath, Digest SHA256) hashentry hdr = when (CT.headerFileType hdr == CT.FTNormal) $ do hash <- CH.sinkHash yield (CT.headerFilePath hdr, hash)
The hashentry
function handles a single entry, based on its first Header
argument.
In this example, a Consumer
is used to process the whole entry.
Note that the benefits of stream processing are easily lost when working with a Consumer
. For example, the following implementation would have used an unbounded amount of memory:
hashentry hdr = when (CT.headerFileType hdr == CT.FTNormal) $ do content <- mconcat <$> sinkList yield (CT.headerFilePath hdr, hash content)
Helper functions
headerFileType :: Header -> FileType Source #
headerFilePath :: Header -> FilePath Source #
Types
Header | |
|
data TarException Source #
This the the exception type that is used in this module.
More constructors are susceptible to be added without bumping the major version of this module.