-- SPDX-FileCopyrightText: 2026 Alexandra de Wit
--
-- SPDX-License-Identifier: MIT
{-# LANGUAGE RankNTypes #-}

-- | Python Core Metadata selection over the shared complete tar traversal.
module Ecluse.Core.Registry.PyPI.Archive.Tar (extractTar) where

import Conduit
import Data.Map.Strict qualified as Map
import Data.Text qualified as T

import Ecluse.Core.Package (PackageName)
import Ecluse.Core.Registry (ParseError, parseRefusal)
import Ecluse.Core.Registry.Archive.Path (checkPath)
import Ecluse.Core.Registry.Archive.Tar (TarDecode, TarMember (Regular), resolveTarMember, scanTar, tarContents, tarMembers)
import Ecluse.Core.Registry.PyPI.Archive.Path (metadataPath)
import Ecluse.Core.Registry.PyPI.Project (DistributionKind (Sdist))
import Ecluse.Core.Security.Limits (Limits)
import Ecluse.Core.Version (Version)

-- | A linked metadata file needs at most one more local pass over the same source.
extractTar :: Limits -> (Int -> IO ()) -> PackageName -> Version -> (forall a. (ConduitT () ByteString TarDecode () -> IO a) -> IO a) -> IO (Either ParseError ByteString)
extractTar :: Limits
-> (Int -> IO ())
-> PackageName
-> Version
-> (forall a.
    (ConduitT () ByteString TarDecode () -> IO a) -> IO a)
-> IO (Either ParseError ByteString)
extractTar Limits
limits Int -> IO ()
charge PackageName
package Version
version forall a. (ConduitT () ByteString TarDecode () -> IO a) -> IO a
withSource = do
    firstPass <- 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 (ConduitT () ByteString TarDecode () -> IO a) -> IO a
forall a. (ConduitT () ByteString TarDecode () -> IO a) -> IO a
withSource
    case firstPass >>= locate of
        Left ParseError
fault -> Either ParseError ByteString -> IO (Either ParseError ByteString)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ParseError -> Either ParseError ByteString
forall a b. a -> Either a b
Left ParseError
fault)
        Right (Left ByteString
bytes) -> Either ParseError ByteString -> IO (Either ParseError ByteString)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ByteString -> Either ParseError ByteString
forall a b. b -> Either a b
Right ByteString
bytes)
        Right (Right Text
target) -> do
            secondPass <- 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 (Bool -> Either ParseError Bool
forall a b. b -> Either a b
Right (Bool -> Either ParseError Bool)
-> ([Text] -> Bool) -> [Text] -> Either ParseError Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
target) (Text -> Bool) -> ([Text] -> Text) -> [Text] -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> [Text] -> Text
T.intercalate Text
"/") (ConduitT () ByteString TarDecode () -> IO a) -> IO a
forall a. (ConduitT () ByteString TarDecode () -> IO a) -> IO a
withSource
            pure $ secondPass >>= maybe (parseRefusal "metadata link target has no regular-file content") Right . Map.lookup target . tarContents
  where
    select :: [Text] -> Either ParseError Bool
select = PackageName
-> Version -> DistributionKind -> [Text] -> Either ParseError Bool
metadataPath PackageName
package Version
version DistributionKind
Sdist
    locate :: TarScan -> Either ParseError (Either ByteString Text)
locate TarScan
scan = do
        selected <- (Text -> Either ParseError Bool)
-> [Text] -> Either ParseError [Text]
forall (m :: * -> *) a.
Applicative m =>
(a -> m Bool) -> [a] -> m [a]
filterM (Text -> Either ParseError [Text]
checkPath (Text -> Either ParseError [Text])
-> ([Text] -> Either ParseError Bool)
-> Text
-> Either ParseError Bool
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> [Text] -> Either ParseError Bool
select) (Map Text TarMember -> [Text]
forall k a. Map k a -> [k]
Map.keys (TarScan -> Map Text TarMember
tarMembers TarScan
scan))
        path <- case selected of
            [] -> Text -> Either ParseError Text
forall a. Text -> Either ParseError a
parseRefusal Text
"archive has no Core Metadata"
            [Text
path] -> Text -> Either ParseError Text
forall a b. b -> Either a b
Right Text
path
            [Text]
_ -> Text -> Either ParseError Text
forall a. Text -> Either ParseError a
parseRefusal Text
"ambiguous archive Core Metadata"
        target <- resolveTarMember limits scan path
        case Map.lookup target (tarMembers scan) of
            Just TarMember
Regular -> Either ByteString Text
-> Either ParseError (Either ByteString Text)
forall a. a -> Either ParseError a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either ByteString Text
-> (ByteString -> Either ByteString Text)
-> Maybe ByteString
-> Either ByteString Text
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Text -> Either ByteString Text
forall a b. b -> Either a b
Right Text
target) ByteString -> Either ByteString Text
forall a b. a -> Either a b
Left (Text -> Map Text ByteString -> Maybe ByteString
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Text
target (TarScan -> Map Text ByteString
tarContents TarScan
scan)))
            Maybe TarMember
_ -> Text -> Either ParseError (Either ByteString Text)
forall a. Text -> Either ParseError a
parseRefusal Text
"Core Metadata link does not name a regular file"