{-# LANGUAGE RankNTypes #-}
module Ecluse.Core.Registry.Archive.Tar (
TarDecode,
TarMember (..),
TarScan,
scanTar,
tarMembers,
tarContents,
tarRawNames,
resolveTarMember,
sourceReader,
) where
import Codec.Archive.Tar qualified as Tar
import Conduit
import Control.Monad.Except (throwError)
import Data.ByteString qualified as BS
import Data.ByteString.Lazy qualified as BSL
import Data.Map.Strict qualified as Map
import Data.Set qualified as Set
import Data.Text qualified as T
import Data.Text.Encoding qualified as TE
import Ecluse.Core.Registry (ParseError (..), parseRefusal)
import Ecluse.Core.Registry.Archive.Path (checkPath, resolveArchivePath)
import Ecluse.Core.Security.Limits (Limits (..))
type TarDecode = ExceptT ParseError IO
data TarMember = Regular | Directory | Link Text
deriving stock (TarMember -> TarMember -> Bool
(TarMember -> TarMember -> Bool)
-> (TarMember -> TarMember -> Bool) -> Eq TarMember
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: TarMember -> TarMember -> Bool
== :: TarMember -> TarMember -> Bool
$c/= :: TarMember -> TarMember -> Bool
/= :: TarMember -> TarMember -> Bool
Eq, Int -> TarMember -> ShowS
[TarMember] -> ShowS
TarMember -> String
(Int -> TarMember -> ShowS)
-> (TarMember -> String)
-> ([TarMember] -> ShowS)
-> Show TarMember
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> TarMember -> ShowS
showsPrec :: Int -> TarMember -> ShowS
$cshow :: TarMember -> String
show :: TarMember -> String
$cshowList :: [TarMember] -> ShowS
showList :: [TarMember] -> ShowS
Show)
data TarScan = TarState
{ TarScan -> Int
tsCount :: !Int
, TarScan -> Int
tsCharge :: !Int
, TarScan -> Map Text TarMember
tsMembers :: Map Text TarMember
, TarScan -> Map ByteString ByteString
tsGlobal :: Map ByteString ByteString
, TarScan -> Map ByteString ByteString
tsLocal :: Map ByteString ByteString
, TarScan -> Bool
tsPending :: !Bool
, TarScan -> Map Text ByteString
tsCaptured :: Map Text ByteString
, TarScan -> Bool
tsRawNames :: !Bool
}
emptyState :: TarScan
emptyState :: TarScan
emptyState =
TarState
{ tsCount :: Int
tsCount = Int
0
, tsCharge :: Int
tsCharge = Int
0
, tsMembers :: Map Text TarMember
tsMembers = Map Text TarMember
forall k a. Map k a
Map.empty
, tsGlobal :: Map ByteString ByteString
tsGlobal = Map ByteString ByteString
forall k a. Map k a
Map.empty
, tsLocal :: Map ByteString ByteString
tsLocal = Map ByteString ByteString
forall k a. Map k a
Map.empty
, tsPending :: Bool
tsPending = Bool
False
, tsCaptured :: Map Text ByteString
tsCaptured = Map Text ByteString
forall k a. Map k a
Map.empty
, tsRawNames :: Bool
tsRawNames = Bool
True
}
scanTar :: Limits -> (Int -> IO ()) -> ([Text] -> Either ParseError Bool) -> (forall a. (ConduitT () ByteString TarDecode () -> IO a) -> IO a) -> IO (Either ParseError TarScan)
scanTar :: Limits
-> (Int -> IO ())
-> ([Text] -> Either ParseError Bool)
-> (forall a.
(ConduitT () ByteString TarDecode () -> IO a) -> IO a)
-> IO (Either ParseError TarScan)
scanTar Limits
limits Int -> IO ()
charge [Text] -> Either ParseError Bool
select forall a. (ConduitT () ByteString TarDecode () -> IO a) -> IO a
withSource = do
result <- (ConduitT () ByteString TarDecode ()
-> IO (Either ParseError TarScan))
-> IO (Either ParseError TarScan)
forall a. (ConduitT () ByteString TarDecode () -> IO a) -> IO a
withSource ((ConduitT () ByteString TarDecode ()
-> IO (Either ParseError TarScan))
-> IO (Either ParseError TarScan))
-> (ConduitT () ByteString TarDecode ()
-> IO (Either ParseError TarScan))
-> IO (Either ParseError TarScan)
forall a b. (a -> b) -> a -> b
$ \ConduitT () ByteString TarDecode ()
source -> ExceptT ParseError IO TarScan -> IO (Either ParseError TarScan)
forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT (ExceptT ParseError IO TarScan -> IO (Either ParseError TarScan))
-> (ConduitT () Void TarDecode TarScan
-> ExceptT ParseError IO TarScan)
-> ConduitT () Void TarDecode TarScan
-> IO (Either ParseError TarScan)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ConduitT () Void TarDecode TarScan -> ExceptT ParseError IO TarScan
forall (m :: * -> *) r. Monad m => ConduitT () Void m r -> m r
runConduit (ConduitT () Void TarDecode TarScan
-> IO (Either ParseError TarScan))
-> ConduitT () Void TarDecode TarScan
-> IO (Either ParseError TarScan)
forall a b. (a -> b) -> a -> b
$ ConduitT () ByteString TarDecode ()
source ConduitT () ByteString TarDecode ()
-> ConduitT ByteString Void TarDecode TarScan
-> ConduitT () Void TarDecode TarScan
forall (m :: * -> *) a b c r.
Monad m =>
ConduitT a b m () -> ConduitT b c m r -> ConduitT a c m r
.| Limits -> ConduitT ByteString ByteString TarDecode ()
boundInflated Limits
limits ConduitT ByteString ByteString TarDecode ()
-> ConduitT ByteString Void TarDecode TarScan
-> ConduitT ByteString Void TarDecode TarScan
forall (m :: * -> *) a b c r.
Monad m =>
ConduitT a b m () -> ConduitT b c m r -> ConduitT a c m r
.| Limits
-> (Int -> IO ())
-> ([Text] -> Either ParseError Bool)
-> TarScan
-> ConduitT ByteString Void TarDecode TarScan
forall o.
Limits
-> (Int -> IO ())
-> ([Text] -> Either ParseError Bool)
-> TarScan
-> ConduitT ByteString o TarDecode TarScan
walkTar Limits
limits Int -> IO ()
charge [Text] -> Either ParseError Bool
select TarScan
emptyState
pure $ result >>= \TarScan
scan -> TarScan
scan TarScan -> Either ParseError () -> Either ParseError TarScan
forall a b. a -> Either ParseError b -> Either ParseError a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Limits -> TarScan -> Either ParseError ()
validateMembers Limits
limits TarScan
scan
tarMembers :: TarScan -> Map Text TarMember
tarMembers :: TarScan -> Map Text TarMember
tarMembers = TarScan -> Map Text TarMember
tsMembers
tarContents :: TarScan -> Map Text ByteString
tarContents :: TarScan -> Map Text ByteString
tarContents = TarScan -> Map Text ByteString
tsCaptured
tarRawNames :: TarScan -> Bool
tarRawNames :: TarScan -> Bool
tarRawNames = TarScan -> Bool
tsRawNames
resolveTarMember :: Limits -> TarScan -> Text -> Either ParseError Text
resolveTarMember :: Limits -> TarScan -> Text -> Either ParseError Text
resolveTarMember Limits
limits TarScan
scan = Limits
-> Map Text TarMember -> Set Text -> Text -> Either ParseError Text
resolveMember Limits
limits (TarScan -> Map Text TarMember
tsMembers TarScan
scan) Set Text
forall a. Set a
Set.empty
boundInflated :: Limits -> ConduitT ByteString ByteString TarDecode ()
boundInflated :: Limits -> ConduitT ByteString ByteString TarDecode ()
boundInflated Limits
limits = Integer -> ConduitT ByteString ByteString TarDecode ()
go Integer
0
where
go :: Integer -> ConduitT ByteString ByteString TarDecode ()
go Integer
seen =
ConduitT ByteString ByteString TarDecode (Maybe ByteString)
forall (m :: * -> *) i o. Monad m => ConduitT i o m (Maybe i)
await ConduitT ByteString ByteString TarDecode (Maybe ByteString)
-> (Maybe ByteString
-> ConduitT ByteString ByteString TarDecode ())
-> ConduitT ByteString ByteString TarDecode ()
forall a b.
ConduitT ByteString ByteString TarDecode a
-> (a -> ConduitT ByteString ByteString TarDecode b)
-> ConduitT ByteString ByteString TarDecode b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Maybe ByteString
Nothing -> () -> ConduitT ByteString ByteString TarDecode ()
forall a. a -> ConduitT ByteString ByteString TarDecode a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
Just ByteString
chunk -> do
let next :: Integer
next = Integer
seen Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Int -> Integer
forall a. Integral a => a -> Integer
toInteger (ByteString -> Int
BS.length ByteString
chunk)
Bool
-> ConduitT ByteString ByteString TarDecode ()
-> ConduitT ByteString ByteString TarDecode ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Integer
next Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Int -> Integer
forall a. Integral a => a -> Integer
toInteger (Limits -> Int
maxMirrorArtifactBytes Limits
limits)) (Text -> ConduitT ByteString ByteString TarDecode ()
forall i o a. Text -> ConduitT i o TarDecode a
refuse Text
"archive inflated bytes exceed their bound")
ByteString -> ConduitT ByteString ByteString TarDecode ()
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield ByteString
chunk
Integer -> ConduitT ByteString ByteString TarDecode ()
go Integer
next
walkTar :: Limits -> (Int -> IO ()) -> ([Text] -> Either ParseError Bool) -> TarScan -> ConduitT ByteString o TarDecode TarScan
walkTar :: forall o.
Limits
-> (Int -> IO ())
-> ([Text] -> Either ParseError Bool)
-> TarScan
-> ConduitT ByteString o TarDecode TarScan
walkTar Limits
limits Int -> IO ()
charge [Text] -> Either ParseError Bool
select TarScan
tarState = do
IO () -> ConduitT ByteString o TarDecode ()
forall a. IO a -> ConduitT ByteString o TarDecode a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Int -> IO ()
charge (TarScan -> Int
tarWorkspace TarScan
tarState))
header <- Int -> ConduitT ByteString o TarDecode ByteString
forall o. Int -> ConduitT ByteString o TarDecode ByteString
takeBytes Int
512
when (BS.length header /= 512) (refuse "tar archive has no complete trailer")
if BS.all (== 0) header
then do
trailer <- takeBytes 512
unless (BS.length trailer == 512 && BS.all (== 0) trailer) (refuse "invalid tar trailer")
awaitForever $ \ByteString
chunk -> Bool
-> ConduitT ByteString o TarDecode ()
-> ConduitT ByteString o TarDecode ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless ((Word8 -> Bool) -> ByteString -> Bool
BS.all (Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
0) ByteString
chunk) (Text -> ConduitT ByteString o TarDecode ()
forall i o a. Text -> ConduitT i o TarDecode a
refuse Text
"tar archive has trailing nonzero bytes")
when (tsPending tarState) (refuse "orphan extended tar header")
pure tarState
else do
when (tsCount tarState >= archiveEntryCeiling limits) (refuse "archive entry count exceeds its bound")
entry <- checked (readHeader header)
next <- case Tar.entryContent entry of
Tar.OtherEntryType TypeCode
code ByteString
_ FileSize
size | TypeCode
code TypeCode -> String -> Bool
forall (f :: * -> *) a.
(Foldable f, DisallowElem f, Eq a) =>
a -> f a -> Bool
`elem` [TypeCode
'x', TypeCode
'g', TypeCode
'L', TypeCode
'K'] -> Limits
-> (Int -> IO ())
-> TypeCode
-> FileSize
-> TarScan
-> ConduitT ByteString o TarDecode TarScan
forall o.
Limits
-> (Int -> IO ())
-> TypeCode
-> FileSize
-> TarScan
-> ConduitT ByteString o TarDecode TarScan
extendedEntry Limits
limits Int -> IO ()
charge TypeCode
code FileSize
size TarScan
tarState{tsRawNames = False}
GenEntryContent LinkTarget
content -> Limits
-> (Int -> IO ())
-> ([Text] -> Either ParseError Bool)
-> ByteString
-> GenEntryContent LinkTarget
-> TarScan
-> ConduitT ByteString o TarDecode TarScan
forall o.
Limits
-> (Int -> IO ())
-> ([Text] -> Either ParseError Bool)
-> ByteString
-> GenEntryContent LinkTarget
-> TarScan
-> ConduitT ByteString o TarDecode TarScan
ordinaryEntry Limits
limits Int -> IO ()
charge [Text] -> Either ParseError Bool
select ByteString
header GenEntryContent LinkTarget
content TarScan
tarState
walkTar limits charge select next{tsCount = tsCount tarState + 1}
readHeader :: ByteString -> Either ParseError Tar.Entry
ByteString
header = case ByteString -> Entries FormatError
Tar.read (ByteString -> ByteString
BSL.fromStrict ByteString
header) of
Tar.Next Entry
entry Entries FormatError
_ -> Entry -> Either ParseError Entry
forall a b. b -> Either a b
Right Entry
entry
Entries FormatError
_ -> Text -> Either ParseError Entry
forall a. Text -> Either ParseError a
parseRefusal Text
"malformed tar header"
takeBytes :: Int -> ConduitT ByteString o TarDecode ByteString
takeBytes :: forall o. Int -> ConduitT ByteString o TarDecode ByteString
takeBytes Int
size = Index ByteString -> ConduitT ByteString ByteString TarDecode ()
forall (m :: * -> *) seq.
(Monad m, IsSequence seq) =>
Index seq -> ConduitT seq seq m ()
takeCE Int
Index ByteString
size ConduitT ByteString ByteString TarDecode ()
-> ConduitT ByteString o TarDecode ByteString
-> ConduitT ByteString o TarDecode ByteString
forall (m :: * -> *) a b c r.
Monad m =>
ConduitT a b m () -> ConduitT b c m r -> ConduitT a c m r
.| ConduitT ByteString o TarDecode ByteString
forall (m :: * -> *) a o. (Monad m, Monoid a) => ConduitT a o m a
foldC
skipPayload :: Integer -> ConduitT ByteString o TarDecode ()
skipPayload :: forall o. Integer -> ConduitT ByteString o TarDecode ()
skipPayload = (Int -> IO ())
-> Maybe (IORef [ByteString])
-> Integer
-> ConduitT ByteString o TarDecode ()
forall o.
(Int -> IO ())
-> Maybe (IORef [ByteString])
-> Integer
-> ConduitT ByteString o TarDecode ()
consumePayload (IO () -> Int -> IO ()
forall a b. a -> b -> a
const (() -> IO ()
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ())) Maybe (IORef [ByteString])
forall a. Maybe a
Nothing
consumePayload :: (Int -> IO ()) -> Maybe (IORef [ByteString]) -> Integer -> ConduitT ByteString o TarDecode ()
consumePayload :: forall o.
(Int -> IO ())
-> Maybe (IORef [ByteString])
-> Integer
-> ConduitT ByteString o TarDecode ()
consumePayload Int -> IO ()
charge Maybe (IORef [ByteString])
output = Int -> Int -> Integer -> ConduitT ByteString o TarDecode ()
forall {o}.
Int -> Int -> Integer -> ConduitT ByteString o TarDecode ()
go Int
0 Int
0
where
go :: Int -> Int -> Integer -> ConduitT ByteString o TarDecode ()
go Int
seen Int
chunks Integer
remaining
| Integer
remaining Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
0 = () -> ConduitT ByteString o TarDecode ()
forall a. a -> ConduitT ByteString o TarDecode a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
| Bool
otherwise =
ConduitT ByteString o TarDecode (Maybe ByteString)
forall (m :: * -> *) i o. Monad m => ConduitT i o m (Maybe i)
await ConduitT ByteString o TarDecode (Maybe ByteString)
-> (Maybe ByteString -> ConduitT ByteString o TarDecode ())
-> ConduitT ByteString o TarDecode ()
forall a b.
ConduitT ByteString o TarDecode a
-> (a -> ConduitT ByteString o TarDecode b)
-> ConduitT ByteString o TarDecode b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Maybe ByteString
Nothing -> Text -> ConduitT ByteString o TarDecode ()
forall i o a. Text -> ConduitT i o TarDecode a
refuse Text
"truncated tar payload"
Just ByteString
chunk -> do
let count :: Int
count = Integer -> Int
forall a. Num a => Integer -> a
fromInteger (Integer -> Integer -> Integer
forall a. Ord a => a -> a -> a
min Integer
remaining (Int -> Integer
forall a. Integral a => a -> Integer
toInteger (ByteString -> Int
BS.length ByteString
chunk)))
(ByteString
wanted, ByteString
rest) = Int -> ByteString -> (ByteString, ByteString)
BS.splitAt Int
count ByteString
chunk
seen' :: Int
seen' = Int
seen Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
count
chunks' :: Int
chunks' = Int
chunks Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
Maybe (IORef [ByteString])
-> (IORef [ByteString] -> ConduitT ByteString o TarDecode ())
-> ConduitT ByteString o TarDecode ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ Maybe (IORef [ByteString])
output ((IORef [ByteString] -> ConduitT ByteString o TarDecode ())
-> ConduitT ByteString o TarDecode ())
-> (IORef [ByteString] -> ConduitT ByteString o TarDecode ())
-> ConduitT ByteString o TarDecode ()
forall a b. (a -> b) -> a -> b
$ \IORef [ByteString]
held -> IO () -> ConduitT ByteString o TarDecode ()
forall a. IO a -> ConduitT ByteString o TarDecode a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> ConduitT ByteString o TarDecode ())
-> IO () -> ConduitT ByteString o TarDecode ()
forall a b. (a -> b) -> a -> b
$ do
Int -> IO ()
charge (Int
3 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
seen' Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
64 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
chunks')
IORef [ByteString] -> ([ByteString] -> [ByteString]) -> IO ()
forall (m :: * -> *) a. MonadIO m => IORef a -> (a -> a) -> m ()
modifyIORef' IORef [ByteString]
held (ByteString
wanted ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
:)
Bool
-> ConduitT ByteString o TarDecode ()
-> ConduitT ByteString o TarDecode ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (ByteString -> Bool
BS.null ByteString
rest) (ByteString -> ConduitT ByteString o TarDecode ()
forall i o (m :: * -> *). i -> ConduitT i o m ()
leftover ByteString
rest)
Int -> Int -> Integer -> ConduitT ByteString o TarDecode ()
go Int
seen' Int
chunks' (Integer
remaining Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Int -> Integer
forall a. Integral a => a -> Integer
toInteger Int
count)
readPayload :: (Int -> IO ()) -> Int -> Integer -> ConduitT ByteString o TarDecode ByteString
readPayload :: forall o.
(Int -> IO ())
-> Int -> Integer -> ConduitT ByteString o TarDecode ByteString
readPayload Int -> IO ()
charge Int
cap Integer
size = do
Bool
-> ConduitT ByteString o TarDecode ()
-> ConduitT ByteString o TarDecode ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Integer
size Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Integer
0 Bool -> Bool -> Bool
|| Integer
size Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Int -> Integer
forall a. Integral a => a -> Integer
toInteger Int
cap) (Text -> ConduitT ByteString o TarDecode ()
forall i o a. Text -> ConduitT i o TarDecode a
refuse Text
"tar metadata or extended header exceeds its bound")
held <- IO (IORef [ByteString])
-> ConduitT ByteString o TarDecode (IORef [ByteString])
forall a. IO a -> ConduitT ByteString o TarDecode a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO ([ByteString] -> IO (IORef [ByteString])
forall (m :: * -> *) a. MonadIO m => a -> m (IORef a)
newIORef [])
consumePayload charge (Just held) size
chunks <- liftIO (readIORef held)
pure (BS.concat (reverse chunks))
tarWorkspace :: TarScan -> Int
tarWorkspace :: TarScan -> Int
tarWorkspace TarScan
tarState = Int
65536 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
* TarScan -> Int
tsCharge TarScan
tarState Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Map ByteString ByteString -> Int
attributesWeight (TarScan -> Map ByteString ByteString
tsGlobal TarScan
tarState) Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Map ByteString ByteString -> Int
attributesWeight (TarScan -> Map ByteString ByteString
tsLocal TarScan
tarState) Int -> Int -> Int
forall a. Num a => a -> a -> a
+ [Int] -> Int
forall a (f :: * -> *). (Foldable f, Num a) => f a -> a
sum ((ByteString -> Int) -> [ByteString] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map ByteString -> Int
BS.length (Map Text ByteString -> [ByteString]
forall k a. Map k a -> [a]
Map.elems (TarScan -> Map Text ByteString
tsCaptured TarScan
tarState)))
where
attributesWeight :: Map ByteString ByteString -> Int
attributesWeight = (Int -> ByteString -> ByteString -> Int)
-> Int -> Map ByteString ByteString -> Int
forall a k b. (a -> k -> b -> a) -> a -> Map k b -> a
Map.foldlWithKey' (\Int
total ByteString
key ByteString
value -> Int
total Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
192 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ ByteString -> Int
BS.length ByteString
key Int -> Int -> Int
forall a. Num a => a -> a -> a
+ ByteString -> Int
BS.length ByteString
value) Int
0
padding :: Integer -> Integer
padding :: Integer -> Integer
padding Integer
size = (Integer
512 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Integer
size Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`mod` Integer
512) Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`mod` Integer
512
extendedEntry :: Limits -> (Int -> IO ()) -> Char -> Int64 -> TarScan -> ConduitT ByteString o TarDecode TarScan
extendedEntry :: forall o.
Limits
-> (Int -> IO ())
-> TypeCode
-> FileSize
-> TarScan
-> ConduitT ByteString o TarDecode TarScan
extendedEntry Limits
limits Int -> IO ()
charge TypeCode
code FileSize
rawSize TarScan
tarState = do
let size :: Integer
size = FileSize -> Integer
forall a. Integral a => a -> Integer
toInteger FileSize
rawSize
payload <- (Int -> IO ())
-> Int -> Integer -> ConduitT ByteString o TarDecode ByteString
forall o.
(Int -> IO ())
-> Int -> Integer -> ConduitT ByteString o TarDecode ByteString
readPayload (\Int
wanted -> Int -> IO ()
charge (TarScan -> Int
tarWorkspace TarScan
tarState Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
wanted)) (Int -> Int -> Int
forall a. Ord a => a -> a -> a
min Int
16384 (Limits -> Int
maxMetadataBytes Limits
limits)) Integer
size
skipPayload (padding size)
values <-
checked $
if code `elem` ['x', 'g']
then parsePax payload
else do
let value = (Word8 -> Bool) -> ByteString -> ByteString
BS.takeWhile (Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
/= Word8
0) ByteString
payload
pure (Map.singleton (if code == 'L' then "path" else "linkpath") value)
if code == 'g'
then pure tarState{tsGlobal = Map.filter (not . BS.null) (Map.union values (tsGlobal tarState))}
else do
unless (Map.null (Map.intersection values (tsLocal tarState))) (refuse "ambiguous extended tar headers")
pure tarState{tsLocal = Map.union values (tsLocal tarState), tsPending = True}
ordinaryEntry :: Limits -> (Int -> IO ()) -> ([Text] -> Either ParseError Bool) -> ByteString -> Tar.EntryContent -> TarScan -> ConduitT ByteString o TarDecode TarScan
ordinaryEntry :: forall o.
Limits
-> (Int -> IO ())
-> ([Text] -> Either ParseError Bool)
-> ByteString
-> GenEntryContent LinkTarget
-> TarScan
-> ConduitT ByteString o TarDecode TarScan
ordinaryEntry Limits
limits Int -> IO ()
charge [Text] -> Either ParseError Bool
select ByteString
header GenEntryContent LinkTarget
content TarScan
tarState = do
let attrs :: Map ByteString ByteString
attrs = (ByteString -> Bool)
-> Map ByteString ByteString -> Map ByteString ByteString
forall a k. (a -> Bool) -> Map k a -> Map k a
Map.filter (Bool -> Bool
not (Bool -> Bool) -> (ByteString -> Bool) -> ByteString -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Bool
BS.null) (Map ByteString ByteString
-> Map ByteString ByteString -> Map ByteString ByteString
forall k a. Ord k => Map k a -> Map k a -> Map k a
Map.union (TarScan -> Map ByteString ByteString
tsLocal TarScan
tarState) (TarScan -> Map ByteString ByteString
tsGlobal TarScan
tarState))
path <- Either ParseError Text -> ConduitT ByteString o TarDecode Text
forall a i o. Either ParseError a -> ConduitT i o TarDecode a
checked (ByteString -> Either ParseError Text
utf8 (ByteString -> ByteString -> Map ByteString ByteString -> ByteString
forall k a. Ord k => a -> k -> Map k a -> a
Map.findWithDefault (ByteString -> ByteString
headerPath ByteString
header) ByteString
"path" Map ByteString ByteString
attrs))
parts <- checked (checkPath path)
when (length parts > maxNestingDepth limits) (refuse "tar path depth exceeds its bound")
let canonical = Text -> Text
T.copy (Text -> [Text] -> Text
T.intercalate Text
"/" [Text]
parts)
selected <- checked (select parts)
when (Map.member canonical (tsMembers tarState)) (refuse "duplicate archive entry")
member <- checked (memberOf canonical attrs header content)
size <- checked (payloadSize attrs header content)
when (size > toInteger (maxMirrorArtifactBytes limits)) (refuse "tar entry size exceeds its bound")
let pathCost = ByteString -> Int
BS.length (Text -> ByteString
TE.encodeUtf8 Text
canonical) Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
192 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ case TarMember
member of Link Text
link -> ByteString -> Int
BS.length (Text -> ByteString
TE.encodeUtf8 Text
link); TarMember
_ -> Int
0
when (pathCost > maxMetadataBytes limits - tsCharge tarState) (refuse "archive path inventory exceeds its bound")
liftIO (charge (tarWorkspace tarState + 2 * pathCost))
captured <- case member of
TarMember
Regular | Bool
selected -> ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just (ByteString -> Maybe ByteString)
-> (ByteString -> ByteString) -> ByteString -> Maybe ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
BS.copy (ByteString -> Maybe ByteString)
-> ConduitT ByteString o TarDecode ByteString
-> ConduitT ByteString o TarDecode (Maybe ByteString)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Int -> IO ())
-> Int -> Integer -> ConduitT ByteString o TarDecode ByteString
forall o.
(Int -> IO ())
-> Int -> Integer -> ConduitT ByteString o TarDecode ByteString
readPayload (\Int
wanted -> Int -> IO ()
charge (TarScan -> Int
tarWorkspace TarScan
tarState Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
pathCost Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
wanted)) (Limits -> Int
maxMetadataBytes Limits
limits Int -> Int -> Int
forall a. Num a => a -> a -> a
- [Int] -> Int
forall a (f :: * -> *). (Foldable f, Num a) => f a -> a
sum ((ByteString -> Int) -> [ByteString] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map ByteString -> Int
BS.length (Map Text ByteString -> [ByteString]
forall k a. Map k a -> [a]
Map.elems (TarScan -> Map Text ByteString
tsCaptured TarScan
tarState)))) Integer
size
TarMember
_ -> Maybe ByteString
forall a. Maybe a
Nothing Maybe ByteString
-> ConduitT ByteString o TarDecode ()
-> ConduitT ByteString o TarDecode (Maybe ByteString)
forall a b.
a
-> ConduitT ByteString o TarDecode b
-> ConduitT ByteString o TarDecode a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Integer -> ConduitT ByteString o TarDecode ()
forall o. Integer -> ConduitT ByteString o TarDecode ()
skipPayload Integer
size
skipPayload (padding size)
let retained = Map Text ByteString
-> (ByteString -> Map Text ByteString)
-> Maybe ByteString
-> Map Text ByteString
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (TarScan -> Map Text ByteString
tsCaptured TarScan
tarState) (\ByteString
bytes -> Text -> ByteString -> Map Text ByteString -> Map Text ByteString
forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert Text
canonical ByteString
bytes (TarScan -> Map Text ByteString
tsCaptured TarScan
tarState)) Maybe ByteString
captured
when (sum (map BS.length (Map.elems retained)) > maxMetadataBytes limits) (refuse "selected archive content exceeds its bound")
pure
tarState
{ tsCharge = tsCharge tarState + pathCost
, tsMembers = Map.insert canonical member (tsMembers tarState)
, tsLocal = Map.empty
, tsPending = False
, tsCaptured = retained
, tsRawNames = tsRawNames tarState && rawHeaderName header
}
memberOf :: Text -> Map ByteString ByteString -> ByteString -> Tar.EntryContent -> Either ParseError TarMember
memberOf :: Text
-> Map ByteString ByteString
-> ByteString
-> GenEntryContent LinkTarget
-> Either ParseError TarMember
memberOf Text
path Map ByteString ByteString
attrs ByteString
header = \case
Tar.NormalFile{} -> TarMember -> Either ParseError TarMember
forall a b. b -> Either a b
Right TarMember
Regular
GenEntryContent LinkTarget
Tar.Directory -> TarMember -> Either ParseError TarMember
forall a b. b -> Either a b
Right TarMember
Directory
Tar.SymbolicLink{} -> Text -> TarMember
Link (Text -> TarMember)
-> Either ParseError Text -> Either ParseError TarMember
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Bool -> Either ParseError Text
linkTarget Bool
True
Tar.HardLink{} -> Text -> TarMember
Link (Text -> TarMember)
-> Either ParseError Text -> Either ParseError TarMember
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Bool -> Either ParseError Text
linkTarget Bool
False
GenEntryContent LinkTarget
_ -> Text -> Either ParseError TarMember
forall a. Text -> Either ParseError a
parseRefusal Text
"unsupported tar device or framing extension"
where
linkTarget :: Bool -> Either ParseError Text
linkTarget Bool
relative = do
value <- ByteString -> Either ParseError Text
utf8 (ByteString -> ByteString -> Map ByteString ByteString -> ByteString
forall k a. Ord k => a -> k -> Map k a -> a
Map.findWithDefault ((Word8 -> Bool) -> ByteString -> ByteString
BS.takeWhile (Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
/= Word8
0) (Int -> ByteString -> ByteString
BS.take Int
100 (Int -> ByteString -> ByteString
BS.drop Int
157 ByteString
header))) ByteString
"linkpath" Map ByteString ByteString
attrs)
resolveArchivePath (if relative then reverse (drop 1 (reverse (T.splitOn "/" path))) else []) value
payloadSize :: Map ByteString ByteString -> ByteString -> Tar.EntryContent -> Either ParseError Integer
payloadSize :: Map ByteString ByteString
-> ByteString
-> GenEntryContent LinkTarget
-> Either ParseError Integer
payloadSize Map ByteString ByteString
attrs ByteString
header GenEntryContent LinkTarget
content = do
original <- case GenEntryContent LinkTarget
content of
Tar.NormalFile ByteString
_ FileSize
size -> Integer -> Either ParseError Integer
forall a b. b -> Either a b
Right (FileSize -> Integer
forall a. Integral a => a -> Integer
toInteger FileSize
size)
Tar.OtherEntryType TypeCode
_ ByteString
_ FileSize
size -> Integer -> Either ParseError Integer
forall a b. b -> Either a b
Right (FileSize -> Integer
forall a. Integral a => a -> Integer
toInteger FileSize
size)
GenEntryContent LinkTarget
_ -> if ByteString -> Bool
zeroSize (Int -> ByteString -> ByteString
BS.take Int
12 (Int -> ByteString -> ByteString
BS.drop Int
124 ByteString
header)) then Integer -> Either ParseError Integer
forall a b. b -> Either a b
Right Integer
0 else Text -> Either ParseError Integer
forall a. Text -> Either ParseError a
parseRefusal Text
"non-file tar entry carries a payload"
size <- maybe (Right original) decimal (Map.lookup "size" attrs)
when (size < 0) (parseRefusal "negative tar payload size")
case content of
Tar.NormalFile{} -> Integer -> Either ParseError Integer
forall a b. b -> Either a b
Right Integer
size
GenEntryContent LinkTarget
_ | Integer
size Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
0 -> Integer -> Either ParseError Integer
forall a b. b -> Either a b
Right Integer
0
GenEntryContent LinkTarget
_ -> Text -> Either ParseError Integer
forall a. Text -> Either ParseError a
parseRefusal Text
"non-file tar entry carries a payload"
zeroSize :: ByteString -> Bool
zeroSize :: ByteString -> Bool
zeroSize ByteString
bytes = (Word8 -> Bool) -> ByteString -> Bool
BS.all (Word8 -> [Word8] -> Bool
forall (f :: * -> *) a.
(Foldable f, DisallowElem f, Eq a) =>
a -> f a -> Bool
`elem` [Word8
0, Word8
32, Word8
48]) ByteString
bytes Bool -> Bool -> Bool
|| ByteString
bytes ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== Word8 -> ByteString -> ByteString
BS.cons Word8
128 (Int -> Word8 -> ByteString
BS.replicate Int
11 Word8
0)
headerPath :: ByteString -> ByteString
ByteString
header
| ByteString -> Bool
BS.null (ByteString -> ByteString
headerPrefix ByteString
header) = ByteString -> ByteString
headerName ByteString
header
| Bool
otherwise = ByteString -> ByteString
headerPrefix ByteString
header ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
"/" ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString -> ByteString
headerName ByteString
header
headerName :: ByteString -> ByteString
= (Word8 -> Bool) -> ByteString -> ByteString
BS.takeWhile (Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
/= Word8
0) (ByteString -> ByteString)
-> (ByteString -> ByteString) -> ByteString -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
nameField
headerPrefix :: ByteString -> ByteString
= (Word8 -> Bool) -> ByteString -> ByteString
BS.takeWhile (Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
/= Word8
0) (ByteString -> ByteString)
-> (ByteString -> ByteString) -> ByteString -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
prefixField
rawHeaderName :: ByteString -> Bool
ByteString
header = (Word8 -> Bool) -> ByteString -> Bool
BS.all (Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
0) ((Word8 -> Bool) -> ByteString -> ByteString
BS.dropWhile (Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
/= Word8
0) (ByteString -> ByteString
nameField ByteString
header)) Bool -> Bool -> Bool
&& (Word8 -> Bool) -> ByteString -> Bool
BS.all (Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
0) (ByteString -> ByteString
prefixField ByteString
header)
nameField :: ByteString -> ByteString
nameField :: ByteString -> ByteString
nameField = Int -> ByteString -> ByteString
BS.take Int
100
prefixField :: ByteString -> ByteString
prefixField :: ByteString -> ByteString
prefixField = Int -> ByteString -> ByteString
BS.take Int
155 (ByteString -> ByteString)
-> (ByteString -> ByteString) -> ByteString -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> ByteString -> ByteString
BS.drop Int
345
parsePax :: ByteString -> Either ParseError (Map ByteString ByteString)
parsePax :: ByteString -> Either ParseError (Map ByteString ByteString)
parsePax = Map ByteString ByteString
-> ByteString -> Either ParseError (Map ByteString ByteString)
go Map ByteString ByteString
forall k a. Map k a
Map.empty
where
go :: Map ByteString ByteString
-> ByteString -> Either ParseError (Map ByteString ByteString)
go Map ByteString ByteString
fields ByteString
bytes
| ByteString -> Bool
BS.null ByteString
bytes = Map ByteString ByteString
-> Either ParseError (Map ByteString ByteString)
forall a b. b -> Either a b
Right Map ByteString ByteString
fields
| Bool
otherwise = do
let (ByteString
digits, ByteString
separated) = (Word8 -> Bool) -> ByteString -> (ByteString, ByteString)
BS.break (Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
32) ByteString
bytes
size <- ByteString -> Either ParseError Integer
decimal ByteString
digits
when (size <= toInteger (BS.length digits + 3) || size > toInteger (BS.length bytes) || BS.null separated) (parseRefusal "truncated pax record")
let record = Int -> ByteString -> ByteString
BS.take (Integer -> Int
forall a. Num a => Integer -> a
fromInteger Integer
size Int -> Int -> Int
forall a. Num a => a -> a -> a
- ByteString -> Int
BS.length ByteString
digits Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) (Int -> ByteString -> ByteString
BS.drop Int
1 ByteString
separated)
(key, value) = BS.break (== 61) record
payload = Int -> ByteString -> ByteString
BS.take (ByteString -> Int
BS.length ByteString
value Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
2) (Int -> ByteString -> ByteString
BS.drop Int
1 ByteString
value)
unless (BS.isSuffixOf "\n" record && not (BS.null key) && not (BS.null value) && not (BS.any (\Word8
c -> Word8
c Word8 -> Word8 -> Bool
forall a. Ord a => a -> a -> Bool
<= Word8
32 Bool -> Bool -> Bool
|| Word8
c Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
127) key)) (parseRefusal "malformed pax record")
when (any (`BS.isPrefixOf` key) ["GNU.sparse", "SCHILY.realsize", "SCHILY.filetype"]) (parseRefusal "unsupported pax framing extension")
when (key == "hdrcharset" && payload /= "UTF-8" && not (BS.null payload)) (parseRefusal "unsupported pax header charset")
next <-
if key `elem` ["path", "linkpath", "size"]
then do
when (Map.member key fields) (parseRefusal "duplicate pax framing attribute")
pure (Map.insert key (BS.copy payload) fields)
else Right fields
go next (BS.drop (fromInteger size) bytes)
decimal :: ByteString -> Either ParseError Integer
decimal :: ByteString -> Either ParseError Integer
decimal ByteString
bytes
| ByteString -> Bool
BS.null ByteString
bytes Bool -> Bool -> Bool
|| ByteString -> Int
BS.length ByteString
bytes Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
20 Bool -> Bool -> Bool
|| Bool -> Bool
not ((Word8 -> Bool) -> ByteString -> Bool
BS.all (\Word8
c -> Word8
c Word8 -> Word8 -> Bool
forall a. Ord a => a -> a -> Bool
>= Word8
48 Bool -> Bool -> Bool
&& Word8
c Word8 -> Word8 -> Bool
forall a. Ord a => a -> a -> Bool
<= Word8
57) ByteString
bytes) = Text -> Either ParseError Integer
forall a. Text -> Either ParseError a
parseRefusal Text
"malformed pax decimal"
| Bool
otherwise = Integer -> Either ParseError Integer
forall a b. b -> Either a b
Right ((Integer -> Word8 -> Integer) -> Integer -> ByteString -> Integer
forall a. (a -> Word8 -> a) -> a -> ByteString -> a
BS.foldl' (\Integer
n Word8
c -> Integer
n Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
10 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Word8 -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
c Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Integer
48) Integer
0 ByteString
bytes)
validateMembers :: Limits -> TarScan -> Either ParseError ()
validateMembers :: Limits -> TarScan -> Either ParseError ()
validateMembers Limits
limits TarScan
tarState =
[(Text, TarMember)]
-> ((Text, TarMember) -> Either ParseError ())
-> Either ParseError ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ (Map Text TarMember -> [(Text, TarMember)]
forall k a. Map k a -> [(k, a)]
Map.toList (TarScan -> Map Text TarMember
tsMembers TarScan
tarState)) (((Text, TarMember) -> Either ParseError ())
-> Either ParseError ())
-> ((Text, TarMember) -> Either ParseError ())
-> Either ParseError ()
forall a b. (a -> b) -> a -> b
$ \(Text
path, TarMember
member) -> do
[Text] -> (Text -> Either ParseError ()) -> Either ParseError ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ (Text -> [Text]
prefixes Text
path) ((Text -> Either ParseError ()) -> Either ParseError ())
-> (Text -> Either ParseError ()) -> Either ParseError ()
forall a b. (a -> b) -> a -> b
$ \Text
prefix -> case Text -> Map Text TarMember -> Maybe TarMember
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Text
prefix (TarScan -> Map Text TarMember
tsMembers TarScan
tarState) of
Just Link{} -> Text -> Either ParseError ()
forall a. Text -> Either ParseError a
parseRefusal Text
"archive member traverses a link"
Maybe TarMember
_ -> () -> Either ParseError ()
forall a b. b -> Either a b
Right ()
case TarMember
member of
Link Text
_ -> Either ParseError Text -> Either ParseError ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (Limits -> TarScan -> Text -> Either ParseError Text
resolveTarMember Limits
limits TarScan
tarState Text
path)
TarMember
_ -> () -> Either ParseError ()
forall a b. b -> Either a b
Right ()
resolveMember :: Limits -> Map Text TarMember -> Set Text -> Text -> Either ParseError Text
resolveMember :: Limits
-> Map Text TarMember -> Set Text -> Text -> Either ParseError Text
resolveMember Limits
limits Map Text TarMember
members Set Text
seen Text
path
| Text -> Set Text -> Bool
forall a. Ord a => a -> Set a -> Bool
Set.member Text
path Set Text
seen = Text -> Either ParseError Text
forall a. Text -> Either ParseError a
parseRefusal Text
"cyclic tar link"
| Set Text -> Int
forall a. Set a -> Int
Set.size Set Text
seen Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Limits -> Int
maxNestingDepth Limits
limits = Text -> Either ParseError Text
forall a. Text -> Either ParseError a
parseRefusal Text
"tar link chain exceeds its bound"
| Bool
otherwise = case Text -> Map Text TarMember -> Maybe TarMember
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Text
path Map Text TarMember
members of
Maybe TarMember
Nothing -> case Text -> Map Text TarMember -> Maybe (Text, TarMember)
forall k v. Ord k => k -> Map k v -> Maybe (k, v)
Map.lookupGE (Text
path Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"/") Map Text TarMember
members of
Just (Text
child, TarMember
_) | (Text
path Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"/") Text -> Text -> Bool
`T.isPrefixOf` Text
child -> Text -> Either ParseError Text
forall a b. b -> Either a b
Right Text
path
Maybe (Text, TarMember)
_ -> Text -> Either ParseError Text
forall a. Text -> Either ParseError a
parseRefusal Text
"tar link target is absent"
Just (Link Text
target) -> Limits
-> Map Text TarMember -> Set Text -> Text -> Either ParseError Text
resolveMember Limits
limits Map Text TarMember
members (Text -> Set Text -> Set Text
forall a. Ord a => a -> Set a -> Set a
Set.insert Text
path Set Text
seen) Text
target
Just TarMember
_ -> Text -> Either ParseError Text
forall a b. b -> Either a b
Right Text
path
prefixes :: Text -> [Text]
prefixes :: Text -> [Text]
prefixes Text
path = ([Text] -> Text) -> [[Text]] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map (Text -> [Text] -> Text
T.intercalate Text
"/") (Int -> [[Text]] -> [[Text]]
forall a. Int -> [a] -> [a]
drop Int
1 (([Text] -> Text -> [Text]) -> [Text] -> [Text] -> [[Text]]
forall b a. (b -> a -> b) -> b -> [a] -> [b]
scanl (\[Text]
held Text
part -> [Text]
held [Text] -> [Text] -> [Text]
forall a. Semigroup a => a -> a -> a
<> [Text
part]) [] ([Text] -> [Text]
forall a. [a] -> [a]
reverse (Int -> [Text] -> [Text]
forall a. Int -> [a] -> [a]
drop Int
1 ([Text] -> [Text]
forall a. [a] -> [a]
reverse (HasCallStack => Text -> Text -> [Text]
Text -> Text -> [Text]
T.splitOn Text
"/" Text
path))))))
utf8 :: ByteString -> Either ParseError Text
utf8 :: ByteString -> Either ParseError Text
utf8 = (UnicodeException -> ParseError)
-> Either UnicodeException Text -> Either ParseError Text
forall a b c. (a -> b) -> Either a c -> Either b c
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first (ParseError -> UnicodeException -> ParseError
forall a b. a -> b -> a
const (Text -> ParseError
ParseError Text
"archive path is not UTF-8")) (Either UnicodeException Text -> Either ParseError Text)
-> (ByteString -> Either UnicodeException Text)
-> ByteString
-> Either ParseError Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either UnicodeException Text
TE.decodeUtf8'
checked :: Either ParseError a -> ConduitT i o TarDecode a
checked :: forall a i o. Either ParseError a -> ConduitT i o TarDecode a
checked = (ParseError -> ConduitT i o TarDecode a)
-> (a -> ConduitT i o TarDecode a)
-> Either ParseError a
-> ConduitT i o TarDecode a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (TarDecode a -> ConduitT i o TarDecode a
forall (m :: * -> *) a. Monad m => m a -> ConduitT i o m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (TarDecode a -> ConduitT i o TarDecode a)
-> (ParseError -> TarDecode a)
-> ParseError
-> ConduitT i o TarDecode a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ParseError -> TarDecode a
forall a. ParseError -> TarDecode a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError) a -> ConduitT i o TarDecode a
forall a. a -> ConduitT i o TarDecode a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
refuse :: Text -> ConduitT i o TarDecode a
refuse :: forall i o a. Text -> ConduitT i o TarDecode a
refuse = TarDecode a -> ConduitT i o TarDecode a
forall (m :: * -> *) a. Monad m => m a -> ConduitT i o m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (TarDecode a -> ConduitT i o TarDecode a)
-> (Text -> TarDecode a) -> Text -> ConduitT i o TarDecode a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ParseError -> TarDecode a
forall a. ParseError -> TarDecode a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (ParseError -> TarDecode a)
-> (Text -> ParseError) -> Text -> TarDecode a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ParseError
ParseError
sourceReader :: IO (Either ParseError ByteString) -> ConduitT () ByteString TarDecode ()
sourceReader :: IO (Either ParseError ByteString)
-> ConduitT () ByteString TarDecode ()
sourceReader IO (Either ParseError ByteString)
readChunk = do
result <- IO (Either ParseError ByteString)
-> ConduitT () ByteString TarDecode (Either ParseError ByteString)
forall a. IO a -> ConduitT () ByteString TarDecode a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO (Either ParseError ByteString)
readChunk
case result of
Left ParseError
fault -> TarDecode () -> ConduitT () ByteString TarDecode ()
forall (m :: * -> *) a.
Monad m =>
m a -> ConduitT () ByteString m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ParseError -> TarDecode ()
forall a. ParseError -> TarDecode a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError ParseError
fault)
Right ByteString
bytes -> Bool
-> ConduitT () ByteString TarDecode ()
-> ConduitT () ByteString TarDecode ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (ByteString -> Bool
BS.null ByteString
bytes) (ByteString -> ConduitT () ByteString TarDecode ()
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield ByteString
bytes ConduitT () ByteString TarDecode ()
-> ConduitT () ByteString TarDecode ()
-> ConduitT () ByteString TarDecode ()
forall a b.
ConduitT () ByteString TarDecode a
-> ConduitT () ByteString TarDecode b
-> ConduitT () ByteString TarDecode b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> IO (Either ParseError ByteString)
-> ConduitT () ByteString TarDecode ()
sourceReader IO (Either ParseError ByteString)
readChunk)