Skip to content

Commit

Permalink
Merge branch 'expose-internal'
Browse files Browse the repository at this point in the history
  • Loading branch information
vmchale committed Nov 6, 2020
2 parents e3e51ae + 4cc1a79 commit 9909873
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 133 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# libarchive

## 3.0.1.0

* Add `Internal` modules

## 3.0.0.0

* Use `ForeignPtr` over `Ptr`
Expand Down
14 changes: 7 additions & 7 deletions libarchive.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cabal-version: 3.0
name: libarchive
version: 3.0.0.0
version: 3.0.1.0
license: BSD-3-Clause
license-file: LICENSE
copyright: Copyright: (c) 2018-2020 Vanessa McHale
Expand Down Expand Up @@ -55,20 +55,20 @@ library
Codec.Archive.Foreign
Codec.Archive.Foreign.Archive
Codec.Archive.Foreign.ArchiveEntry
Codec.Archive.Internal.Pack
Codec.Archive.Internal.Pack.Lazy
Codec.Archive.Internal.Pack.Common
Codec.Archive.Internal.Unpack.Lazy
Codec.Archive.Internal.Unpack
Codec.Archive.Internal.Monad

hs-source-dirs: src
other-modules:
Codec.Archive.Foreign.Archive.Macros
Codec.Archive.Foreign.ArchiveEntry.Macros
Codec.Archive.Pack
Codec.Archive.Pack.Lazy
Codec.Archive.Pack.Common
Codec.Archive.Unpack.Lazy
Codec.Archive.Unpack
Codec.Archive.Types
Codec.Archive.Types.Foreign
Codec.Archive.Permissions
Codec.Archive.Monad

default-language: Haskell2010
other-extensions: DeriveGeneric DeriveAnyClass
Expand Down
10 changes: 5 additions & 5 deletions src/Codec/Archive.hs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ module Codec.Archive
, executablePermissions
) where

import Codec.Archive.Monad
import Codec.Archive.Pack
import Codec.Archive.Pack.Lazy
import Codec.Archive.Internal.Monad
import Codec.Archive.Internal.Pack
import Codec.Archive.Internal.Pack.Lazy
import Codec.Archive.Permissions
import Codec.Archive.Types
import Codec.Archive.Unpack
import Codec.Archive.Unpack.Lazy
import Codec.Archive.Internal.Unpack
import Codec.Archive.Internal.Unpack.Lazy
38 changes: 12 additions & 26 deletions src/Codec/Archive/Monad.hs → src/Codec/Archive/Internal/Monad.hs
Original file line number Diff line number Diff line change
@@ -1,34 +1,28 @@
module Codec.Archive.Monad ( handle
, ignore
, lenient
, touchForeignPtrM
, runArchiveM
, throwArchiveM
-- * Bracketed resources within 'ArchiveM'
, withCStringArchiveM
, useAsCStringLenArchiveM
, allocaBytesArchiveM
, bracketM
, ArchiveM
) where
module Codec.Archive.Internal.Monad ( handle
, ignore
, lenient
, runArchiveM
, throwArchiveM
-- * Bracketed resources within 'ArchiveM'
, withCStringArchiveM
, useAsCStringLenArchiveM
, allocaBytesArchiveM
, ArchiveM
) where

import Codec.Archive.Types
import Control.Exception (bracket, throw)
import Control.Exception (throw)
import Control.Monad (void)
import Control.Monad.Except (ExceptT, runExceptT, throwError)
import Control.Monad.IO.Class
import qualified Data.ByteString as BS
import qualified Data.ByteString.Unsafe as BS
import Foreign.C.String
import Foreign.ForeignPtr (ForeignPtr, touchForeignPtr)
import Foreign.Marshal.Alloc (allocaBytes)
import Foreign.Ptr (Ptr)

type ArchiveM = ExceptT ArchiveResult IO

touchForeignPtrM :: ForeignPtr a -> ArchiveM ()
touchForeignPtrM = liftIO . touchForeignPtr

-- for things we don't think is going to fail
ignore :: IO ArchiveResult -> ArchiveM ()
ignore = void . liftIO
Expand Down Expand Up @@ -82,11 +76,3 @@ withCStringArchiveM = genBracket withCString

useAsCStringLenArchiveM :: BS.ByteString -> (CStringLen -> ExceptT a IO b) -> ExceptT a IO b
useAsCStringLenArchiveM = genBracket BS.unsafeUseAsCStringLen

bracketM :: IO a -- ^ Allocate/aquire a resource
-> (a -> IO b) -- ^ Free/release a resource (assumed not to fail)
-> (a -> ArchiveM c)
-> ArchiveM c
bracketM get free act =
flipExceptIO $
bracket get free (runArchiveM.act)
73 changes: 40 additions & 33 deletions src/Codec/Archive/Pack.hs → src/Codec/Archive/Internal/Pack.hs
Original file line number Diff line number Diff line change
@@ -1,40 +1,47 @@
module Codec.Archive.Pack ( entriesToFile
, entriesToFileZip
, entriesToFile7Zip
, entriesToFileCpio
, entriesToFileXar
, entriesToFileShar
, entriesToBS
, entriesToBSzip
, entriesToBS7zip
, packEntries
, noFail
, packToFile
, packToFileZip
, packToFile7Zip
, packToFileCpio
, packToFileXar
, packToFileShar
) where
module Codec.Archive.Internal.Pack ( entriesToFile
, entriesToFileZip
, entriesToFile7Zip
, entriesToFileCpio
, entriesToFileXar
, entriesToFileShar
, entriesToFileGeneral
, entriesToBS
, entriesToBSGeneral
, entriesToBSzip
, entriesToBS7zip
, filePacker
, packEntries
, noFail
, packToFile
, packToFileZip
, packToFile7Zip
, packToFileCpio
, packToFileXar
, packToFileShar
, archiveEntryAdd
, contentAdd
, setTime
, setOwnership
) where

import Codec.Archive.Foreign
import Codec.Archive.Monad
import Codec.Archive.Pack.Common
import Codec.Archive.Internal.Monad
import Codec.Archive.Internal.Pack.Common
import Codec.Archive.Types
import Control.Monad (forM_, void)
import Control.Monad.IO.Class (MonadIO (..))
import Data.ByteString (packCStringLen)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BSL
import Data.Coerce (coerce)
import Data.Foldable (sequenceA_, traverse_)
import Data.Semigroup (Sum (..))
import Control.Monad (forM_, void)
import Control.Monad.IO.Class (MonadIO (..))
import Data.ByteString (packCStringLen)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BSL
import Data.Coerce (coerce)
import Data.Foldable (sequenceA_, traverse_)
import Data.Semigroup (Sum (..))
import Foreign.C.String
import Foreign.C.Types (CLLong (..), CLong (..))
import Foreign.Concurrent (newForeignPtr)
import Foreign.ForeignPtr (castForeignPtr)
import Foreign.Ptr (castPtr)
import System.IO.Unsafe (unsafeDupablePerformIO)
import Foreign.C.Types (CLLong (..), CLong (..))
import Foreign.Concurrent (newForeignPtr)
import Foreign.ForeignPtr (castForeignPtr)
import Foreign.Ptr (castPtr)
import System.IO.Unsafe (unsafeDupablePerformIO)

maybeDo :: Applicative f => Maybe (f ()) -> f ()
maybeDo = sequenceA_
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Codec.Archive.Pack.Common ( mkEntry ) where
module Codec.Archive.Internal.Pack.Common ( mkEntry ) where

import Codec.Archive.Types
import qualified Data.ByteString as BS
Expand All @@ -22,4 +22,3 @@ mkEntry fp = do
status <- getFileStatus fp
content' <- mkContent fp status
pure $ Entry fp content' (fileMode status) (Ownership Nothing Nothing (fromIntegral $ fileOwner status) (fromIntegral $ fileGroup status)) Nothing

Original file line number Diff line number Diff line change
@@ -1,36 +1,39 @@
module Codec.Archive.Pack.Lazy ( entriesToBSL
, entriesToBSL7zip
, entriesToBSLzip
, entriesToBSLCpio
, entriesToBSLXar
, entriesToBSLShar
, packFiles
, packFilesZip
, packFiles7zip
, packFilesCpio
, packFilesXar
, packFilesShar
) where
module Codec.Archive.Internal.Pack.Lazy ( entriesToBSL
, entriesToBSL7zip
, entriesToBSLzip
, entriesToBSLCpio
, entriesToBSLXar
, entriesToBSLShar
, entriesToBSLGeneral
, entriesToIOChunks
, packer
, packFiles
, packFilesZip
, packFiles7zip
, packFilesCpio
, packFilesXar
, packFilesShar
) where

import Codec.Archive.Foreign
import Codec.Archive.Monad
import Codec.Archive.Pack
import Codec.Archive.Pack.Common
import Codec.Archive.Internal.Monad
import Codec.Archive.Internal.Pack
import Codec.Archive.Internal.Pack.Common
import Codec.Archive.Types
import Control.Composition ((.@))
import Control.Monad.IO.Class (liftIO)
import Data.ByteString (packCStringLen)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BSL
import qualified Data.DList as DL
import Data.Foldable (toList)
import Data.Functor (($>))
import Data.IORef (modifyIORef', newIORef, readIORef)
import Foreign.Concurrent (newForeignPtr)
import Foreign.ForeignPtr (castForeignPtr, finalizeForeignPtr)
import Foreign.Marshal.Alloc (free, mallocBytes)
import Foreign.Ptr (castPtr, freeHaskellFunPtr)
import System.IO.Unsafe (unsafeDupablePerformIO)
import Control.Composition ((.@))
import Control.Monad.IO.Class (liftIO)
import Data.ByteString (packCStringLen)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BSL
import qualified Data.DList as DL
import Data.Foldable (toList)
import Data.Functor (($>))
import Data.IORef (modifyIORef', newIORef, readIORef)
import Foreign.Concurrent (newForeignPtr)
import Foreign.ForeignPtr (castForeignPtr, finalizeForeignPtr)
import Foreign.Marshal.Alloc (free, mallocBytes)
import Foreign.Ptr (castPtr, freeHaskellFunPtr)
import System.IO.Unsafe (unsafeDupablePerformIO)

packer :: (Traversable t) => (t (Entry FilePath BS.ByteString) -> BSL.ByteString) -> t FilePath -> IO BSL.ByteString
packer = traverse mkEntry .@ fmap
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
module Codec.Archive.Unpack ( hsEntriesAbs
, unpackEntriesFp
, unpackArchive
, readArchiveFile
, readArchiveBS
, unpackToDir
, readBS
) where
module Codec.Archive.Internal.Unpack ( hsEntriesAbs
, unpackEntriesFp
, unpackArchive
, readArchiveFile
, readArchiveBS
, archiveFile
, bsToArchive
, unpackToDir
, readBS
, readBSL
, readEntry
, readContents
, readOwnership
, readTimes
, getHsEntry
, hsEntries
, hsEntriesST
, hsEntriesSTAbs
, archiveGetterHelper
, archiveGetterNull
) where

import Codec.Archive.Foreign
import Codec.Archive.Monad
import Codec.Archive.Internal.Monad
import Codec.Archive.Types
import Control.Monad ((<=<))
import Control.Monad.IO.Class (liftIO)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
module Codec.Archive.Unpack.Lazy ( readArchiveBSL
, unpackToDirLazy
) where
module Codec.Archive.Internal.Unpack.Lazy ( readArchiveBSL
, readArchiveBSLAbs
, unpackToDirLazy
, bslToArchive
) where

import Codec.Archive.Foreign
import Codec.Archive.Monad
import Codec.Archive.Internal.Monad
import Codec.Archive.Internal.Unpack
import Codec.Archive.Types
import Codec.Archive.Unpack
import Control.Monad ((<=<))
import Control.Monad ((<=<))
import Control.Monad.IO.Class
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BSL
import qualified Data.ByteString.Unsafe as BS
import Data.Foldable (traverse_)
import Data.Functor (($>))
import Data.IORef (modifyIORef', newIORef, readIORef, writeIORef)
import Foreign.Concurrent (newForeignPtr)
import Foreign.ForeignPtr (castForeignPtr)
import Foreign.Marshal.Alloc (free, mallocBytes, reallocBytes)
import Foreign.Marshal.Utils (copyBytes)
import Foreign.Ptr (castPtr, freeHaskellFunPtr)
import Foreign.Storable (poke)
import System.IO.Unsafe (unsafeDupablePerformIO)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BSL
import qualified Data.ByteString.Unsafe as BS
import Data.Foldable (traverse_)
import Data.Functor (($>))
import Data.IORef (modifyIORef', newIORef, readIORef, writeIORef)
import Foreign.Concurrent (newForeignPtr)
import Foreign.ForeignPtr (castForeignPtr)
import Foreign.Marshal.Alloc (free, mallocBytes, reallocBytes)
import Foreign.Marshal.Utils (copyBytes)
import Foreign.Ptr (castPtr, freeHaskellFunPtr)
import Foreign.Storable (poke)
import System.IO.Unsafe (unsafeDupablePerformIO)

-- | In general, this will be more efficient than 'unpackToDir'
--
Expand Down Expand Up @@ -57,7 +59,7 @@ bslToArchive bs = do
bufPtrRef <- liftIO $ newIORef bufPtr
bsChunksRef <- liftIO $ newIORef bsChunks
bufSzRef <- liftIO $ newIORef (32 * 1024)
rc <- liftIO $ mkReadCallback (readBSL bsChunksRef bufSzRef bufPtrRef)
rc <- liftIO $ mkReadCallback (readBSL' bsChunksRef bufSzRef bufPtrRef)
cc <- liftIO $ mkCloseCallback (\_ ptr -> freeHaskellFunPtr rc *> free ptr $> ArchiveOk)
a <- liftIO $ castForeignPtr <$> newForeignPtr (castPtr preA) (archiveFree preA *> freeHaskellFunPtr cc *> (free =<< readIORef bufPtrRef))
ignore $ archiveReadSupportFormatAll a
Expand All @@ -70,7 +72,7 @@ bslToArchive bs = do
]
pure a

where readBSL bsRef bufSzRef bufPtrRef _ _ dataPtr = do
where readBSL' bsRef bufSzRef bufPtrRef _ _ dataPtr = do
bs' <- readIORef bsRef
case bs' of
[] -> pure 0
Expand Down

0 comments on commit 9909873

Please sign in to comment.