packages feed

ghcup-0.2.7.0 / lib / GHCup / Unpack.hs

{-# LANGUAGE CPP                   #-}{-# LANGUAGE DataKinds             #-}{-# LANGUAGE FlexibleContexts      #-}{-# LANGUAGE OverloadedStrings     #-}{-|Module      : GHCup.UnpackDescription : GHCup tar abstractionsCopyright   : (c) Julian Ospald, 2024License     : LGPL-3.0Maintainer  : hasufell@hasufell.deStability   : experimentalPortability : portable-}module GHCup.Unpack whereimport           GHCup.Types.Tar ( ArchiveResult(..) )import           GHCup.Errorsimport           GHCup.Preludeimport           GHCup.System.Directoryimport           GHCup.Types.Opticsimport           Control.Monad.Catch (MonadThrow)import           Control.Monad.Readerimport           Data.Listimport           Data.Variant.Exceptsimport           System.FilePath#if defined(TAR)import           Codec.Archive.Zipimport qualified Codec.Archive.Tar             as Tarimport qualified Codec.Archive.Tar.Entry       as Tarimport qualified Data.Map.Strict               as Map#elseimport           Codec.Archive           hiding ( Directory                                                , ArchiveResult -- imported from "GHCup.Unpack"                                                )import Codec.Archive.Internal.Unpack (unpackEntriesFp)import Codec.Archive.Internal.Unpack.Lazy (bslToArchiveAbs)import Codec.Archive.Foreign.Archive (archiveReadSupportFilterAll, archiveReadSupportFormatRaw, archiveReadSupportFormatAll)#endifimport qualified Codec.Compression.BZip        as BZipimport qualified Codec.Compression.GZip        as GZipimport qualified Codec.Compression.Lzma        as Lzmaimport qualified Data.ByteString.Lazy          as BLimport qualified Data.Text                     as T-- | Unpack an archive to a given directory.unpackToDir :: (MonadReader env m, HasLog env, MonadIO m, MonadThrow m)            => FilePath       -- ^ destination dir            -> FilePath       -- ^ archive path            -> Excepts '[UnknownArchive                        , ArchiveResult                        ] m ()unpackToDir dfp av = do  let fn = takeFileName av  lift $ logInfo $ "Unpacking: " <> T.pack fn <> " to " <> T.pack dfp#if defined(TAR)  let untar :: MonadIO m => BL.ByteString -> Excepts '[ArchiveResult] m ()      untar = liftIO . Tar.unpack dfp . Tar.read      rf :: MonadIO m => FilePath -> Excepts '[ArchiveResult] m BL.ByteString      rf = liftIO . BL.readFile#else  let untar :: MonadIO m => BL.ByteString -> Excepts '[ArchiveResult] m ()      untar = lEM . liftIO . runArchiveM . unpackToDirLazy dfp      unzstdTar :: MonadIO m => BL.ByteString -> Excepts '[ArchiveResult] m ()      unzstdTar bs = lEM . liftIO . runArchiveM $ do        ptr <- flip bslToArchiveAbs bs $ \ptr' -> do          _ <- archiveReadSupportFormatAll ptr'          archiveReadSupportFilterAll ptr'        unpackEntriesFp ptr dfp        pure ()      unzstd :: MonadIO m => BL.ByteString -> Excepts '[ArchiveResult] m ()      unzstd bs = lEM . liftIO . runArchiveM $ do        ptr <- flip bslToArchiveAbs bs $ \ptr' -> do          _ <- archiveReadSupportFormatRaw ptr'          archiveReadSupportFilterAll ptr'        unpackEntriesFp ptr dfp        pure ()      rf :: MonadIO m => FilePath -> Excepts '[ArchiveResult] m BL.ByteString      rf = liftIO . BL.readFile#endif  -- extract, depending on file extension  if    | ".tar.gz" `isSuffixOf` fn -> liftE      (untar . GZip.decompress =<< rf av)    | ".tar.xz" `isSuffixOf` fn -> do      filecontents <- liftE $ rf av      let decompressed = Lzma.decompressWith (Lzma.defaultDecompressParams { Lzma.decompressAutoDecoder= True }) filecontents      liftE $ untar decompressed    | ".tar.bz2" `isSuffixOf` fn ->      liftE (untar . BZip.decompress =<< rf av)    | ".tar" `isSuffixOf` fn -> liftE (untar =<< rf av)#if defined(TAR)    | ".zip" `isSuffixOf` fn -> withArchive av (unpackInto dfp)#else    -- libarchive supports more stuff    | ".zip" `isSuffixOf` fn -> liftE (untar =<< rf av)    | any (`isSuffixOf` fn) [".tzst", ".tar.zst"] -> liftE (unzstdTar =<< rf av)    -- single-file non-archives get extracted by libarchive    -- as 'data' without the executable bit    | any (`isSuffixOf` fn) [".zst", ".xz", ".bz2"] -> do      liftE (unzstd =<< rf av)      let from = dfp </> "data"          to = dfp </> dropExtension fn      liftIO $ renameFile from to      chmod_755 to    -- except .gz, which can have a filename header (or not),    -- so we check for the 'data' file first    | any (`isSuffixOf` fn) [".gz"] -> do      liftE (unzstd =<< rf av)      let from = dfp </> "data"          to = dfp </> dropExtension fn      e <- liftIO $ doesFileExist from      if e      then do        liftIO $ renameFile from to        chmod_755 to      else        chmod_755 (dfp </> dropExtension fn)#endif    | otherwise -> throwE $ UnknownArchive fn-- | Get all files from an archive.getArchiveFiles :: (MonadIO m, MonadThrow m)                => FilePath       -- ^ archive path                -> Excepts '[ UnknownArchive                            , ArchiveResult                            ] m [FilePath]getArchiveFiles av = do  let fn = takeFileName av#if defined(TAR)  let entries :: Monad m => BL.ByteString -> Excepts '[ArchiveResult] m [FilePath]      entries =          lE @ArchiveResult          . Tar.foldEntries            (\e x -> fmap (Tar.entryTarPath e :) x)            (Right [])            (\_ -> Left ArchiveFailed)          . Tar.decodeLongNames          . Tar.read      rf :: MonadIO m => FilePath -> Excepts '[ArchiveResult] m BL.ByteString      rf = liftIO . BL.readFile#else  let entries :: Monad m => BL.ByteString -> Excepts '[ArchiveResult] m [FilePath]      entries = (fmap . fmap) filepath . lE . readArchiveBSL      rf :: MonadIO m => FilePath -> Excepts '[ArchiveResult] m BL.ByteString      rf = liftIO . BL.readFile#endif  -- extract, depending on file extension  if    | ".tar.gz" `isSuffixOf` fn -> liftE      (entries . GZip.decompress =<< rf av)    | ".tar.xz" `isSuffixOf` fn -> do      filecontents <- liftE $ rf av      let decompressed = Lzma.decompressWith (Lzma.defaultDecompressParams { Lzma.decompressAutoDecoder= True }) filecontents      liftE $ entries decompressed    | ".tar.bz2" `isSuffixOf` fn ->      liftE (entries . BZip.decompress =<< rf av)    | ".tar" `isSuffixOf` fn -> liftE (entries =<< rf av)    | ".zip" `isSuffixOf` fn ->#if defined(TAR)        withArchive av $ do          entries' <- getEntries          pure $ fmap unEntrySelector $ Map.keys entries'#else        liftE (entries =<< rf av)#endif    | otherwise -> throwE $ UnknownArchive fn