{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE ViewPatterns #-}
{-# OPTIONS_HADDOCK hide #-}
{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}
{-# HLINT ignore "Avoid restricted function" #-}
module Codec.Archive.Tar.Pack (
pack,
pack',
defaultRead,
packAndCheck,
packFileEntry,
packDirectoryEntry,
packSymlinkEntry,
longLinkEntry,
) where
import Codec.Archive.Tar.LongNames
import Codec.Archive.Tar.PackAscii (filePathToOsPath, osPathToFilePath)
import Codec.Archive.Tar.Types
import Data.Int (Int64)
import Control.Applicative
import Prelude hiding (Applicative(..))
import Data.Bifunctor (bimap)
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as BL
import Data.Foldable
import System.File.OsPath
import System.OsPath
( OsPath, (</>) )
import qualified System.OsPath as FilePath.Native
( addTrailingPathSeparator, hasTrailingPathSeparator )
import System.Directory.OsPath
( doesDirectoryExist, getModificationTime
, pathIsSymbolicLink, getSymbolicLinkTarget
, Permissions(..), getPermissions, getFileSize )
import qualified System.Directory.OsPath.Types as FT
import System.Directory.OsPath.Streaming (getDirectoryContentsRecursive)
import Data.Time.Clock.POSIX
( utcTimeToPOSIXSeconds )
import System.IO
( IOMode(ReadMode), hFileSize, hClose )
import System.IO.Unsafe (unsafeInterleaveIO)
import Control.Exception (throwIO, SomeException)
import System.IO.Error (annotateIOError)
pack
:: FilePath
-> [FilePath]
-> IO [Entry]
pack :: FilePath -> [FilePath] -> IO [Entry]
pack = (GenEntry ByteString FilePath FilePath -> Maybe SomeException)
-> FilePath -> [FilePath] -> IO [Entry]
packAndCheck (Maybe SomeException
-> GenEntry ByteString FilePath FilePath -> Maybe SomeException
forall a b. a -> b -> a
const Maybe SomeException
forall a. Maybe a
Nothing)
pack'
:: FilePath
-> [FilePath]
-> IO [GenEntry OsPath TarPath LinkTarget]
pack' :: FilePath -> [FilePath] -> IO [GenEntry OsString TarPath LinkTarget]
pack' = (Int64 -> OsString -> IO OsString)
-> (GenEntry OsString FilePath FilePath -> Maybe SomeException)
-> FilePath
-> [FilePath]
-> IO [GenEntry OsString TarPath LinkTarget]
forall content.
(Int64 -> OsString -> IO content)
-> (GenEntry content FilePath FilePath -> Maybe SomeException)
-> FilePath
-> [FilePath]
-> IO [GenEntry content TarPath LinkTarget]
packAndCheckWithRead (\Int64
_ -> OsString -> IO OsString
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return) (Maybe SomeException
-> GenEntry OsString FilePath FilePath -> Maybe SomeException
forall a b. a -> b -> a
const Maybe SomeException
forall a. Maybe a
Nothing)
packAndCheck
:: (GenEntry BL.ByteString FilePath FilePath -> Maybe SomeException)
-> FilePath
-> [FilePath]
-> IO [Entry]
packAndCheck :: (GenEntry ByteString FilePath FilePath -> Maybe SomeException)
-> FilePath -> [FilePath] -> IO [Entry]
packAndCheck = (Int64 -> OsString -> IO ByteString)
-> (GenEntry ByteString FilePath FilePath -> Maybe SomeException)
-> FilePath
-> [FilePath]
-> IO [Entry]
forall content.
(Int64 -> OsString -> IO content)
-> (GenEntry content FilePath FilePath -> Maybe SomeException)
-> FilePath
-> [FilePath]
-> IO [GenEntry content TarPath LinkTarget]
packAndCheckWithRead Int64 -> OsString -> IO ByteString
defaultRead
packAndCheckWithRead
:: (Int64 -> OsPath -> IO content)
-> (GenEntry content FilePath FilePath -> Maybe SomeException)
-> FilePath
-> [FilePath]
-> IO [GenEntry content TarPath LinkTarget]
packAndCheckWithRead :: forall content.
(Int64 -> OsString -> IO content)
-> (GenEntry content FilePath FilePath -> Maybe SomeException)
-> FilePath
-> [FilePath]
-> IO [GenEntry content TarPath LinkTarget]
packAndCheckWithRead Int64 -> OsString -> IO content
r GenEntry content FilePath FilePath -> Maybe SomeException
secCB (FilePath -> OsString
filePathToOsPath -> OsString
baseDir) ((FilePath -> OsString) -> [FilePath] -> [OsString]
forall a b. (a -> b) -> [a] -> [b]
map FilePath -> OsString
filePathToOsPath -> [OsString]
relpaths) = do
paths <- OsString -> [OsString] -> IO [OsString]
preparePaths OsString
baseDir [OsString]
relpaths
entries' <- packPathsWithRead r baseDir paths
let entries = (GenEntry content OsString OsString
-> GenEntry content FilePath FilePath)
-> [GenEntry content OsString OsString]
-> [GenEntry content FilePath FilePath]
forall a b. (a -> b) -> [a] -> [b]
map ((OsString -> FilePath)
-> (OsString -> FilePath)
-> GenEntry content OsString OsString
-> GenEntry content FilePath FilePath
forall a b c d.
(a -> b)
-> (c -> d) -> GenEntry content a c -> GenEntry content b d
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap OsString -> FilePath
osPathToFilePath OsString -> FilePath
osPathToFilePath) [GenEntry content OsString OsString]
entries'
traverse_ (maybe (pure ()) throwIO . secCB) entries
pure $ concatMap encodeLongNames entries
preparePaths :: OsPath -> [OsPath] -> IO [OsPath]
preparePaths :: OsString -> [OsString] -> IO [OsString]
preparePaths OsString
baseDir = ([[OsString]] -> [OsString]) -> IO [[OsString]] -> IO [OsString]
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [[OsString]] -> [OsString]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat (IO [[OsString]] -> IO [OsString])
-> ([OsString] -> IO [[OsString]]) -> [OsString] -> IO [OsString]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [IO [OsString]] -> IO [[OsString]]
forall a. [IO a] -> IO [a]
interleavedSequence ([IO [OsString]] -> IO [[OsString]])
-> ([OsString] -> [IO [OsString]]) -> [OsString] -> IO [[OsString]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (OsString -> IO [OsString]) -> [OsString] -> [IO [OsString]]
forall a b. (a -> b) -> [a] -> [b]
map OsString -> IO [OsString]
go
where
go :: OsPath -> IO [OsPath]
go :: OsString -> IO [OsString]
go OsString
relpath = do
let abspath :: OsString
abspath = OsString
baseDir OsString -> OsString -> OsString
</> OsString
relpath
isDir <- OsString -> IO Bool
doesDirectoryExist OsString
abspath
isSymlink <- pathIsSymbolicLink abspath
if isDir && not isSymlink then do
entries <- getDirectoryContentsRecursive abspath
let entries' = ((OsString, FileType) -> OsString)
-> [(OsString, FileType)] -> [OsString]
forall a b. (a -> b) -> [a] -> [b]
map ((OsString
relpath OsString -> OsString -> OsString
</>) (OsString -> OsString)
-> ((OsString, FileType) -> OsString)
-> (OsString, FileType)
-> OsString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (OsString, FileType) -> OsString
addSeparatorIfDir) [(OsString, FileType)]
entries
return $ if relpath == mempty
then entries'
else FilePath.Native.addTrailingPathSeparator relpath : entries'
else return [relpath]
addSeparatorIfDir :: (OsString, FileType) -> OsString
addSeparatorIfDir (OsString
fn, FileType
ty) = case FileType
ty of
FT.Directory{} -> OsString -> OsString
FilePath.Native.addTrailingPathSeparator OsString
fn
FileType
_ -> OsString
fn
packPathsWithRead
:: (Int64 -> OsPath -> IO content)
-> OsPath
-> [OsPath]
-> IO [GenEntry content OsPath OsPath]
packPathsWithRead :: forall content.
(Int64 -> OsString -> IO content)
-> OsString
-> [OsString]
-> IO [GenEntry content OsString OsString]
packPathsWithRead Int64 -> OsString -> IO content
r OsString
baseDir [OsString]
paths = [IO (GenEntry content OsString OsString)]
-> IO [GenEntry content OsString OsString]
forall a. [IO a] -> IO [a]
interleavedSequence ([IO (GenEntry content OsString OsString)]
-> IO [GenEntry content OsString OsString])
-> [IO (GenEntry content OsString OsString)]
-> IO [GenEntry content OsString OsString]
forall a b. (a -> b) -> a -> b
$ ((OsString -> IO (GenEntry content OsString OsString))
-> [OsString] -> [IO (GenEntry content OsString OsString)])
-> [OsString]
-> (OsString -> IO (GenEntry content OsString OsString))
-> [IO (GenEntry content OsString OsString)]
forall a b c. (a -> b -> c) -> b -> a -> c
flip (OsString -> IO (GenEntry content OsString OsString))
-> [OsString] -> [IO (GenEntry content OsString OsString)]
forall a b. (a -> b) -> [a] -> [b]
map [OsString]
paths ((OsString -> IO (GenEntry content OsString OsString))
-> [IO (GenEntry content OsString OsString)])
-> (OsString -> IO (GenEntry content OsString OsString))
-> [IO (GenEntry content OsString OsString)]
forall a b. (a -> b) -> a -> b
$ \OsString
relpath -> do
let isDir :: Bool
isDir = OsString -> Bool
FilePath.Native.hasTrailingPathSeparator OsString
abspath
abspath :: OsString
abspath = OsString
baseDir OsString -> OsString -> OsString
</> OsString
relpath
isSymlink <- OsString -> IO Bool
pathIsSymbolicLink OsString
abspath
let mkEntry
| Bool
isSymlink = OsString -> tarPath -> IO (GenEntry content tarPath OsString)
forall tarPath content.
OsString -> tarPath -> IO (GenEntry content tarPath OsString)
packSymlinkEntry'
| Bool
isDir = OsString -> tarPath -> IO (GenEntry content tarPath OsString)
forall tarPath content linkTarget.
OsString -> tarPath -> IO (GenEntry content tarPath linkTarget)
packDirectoryEntry'
| Bool
otherwise = (Int64 -> OsString -> IO content)
-> OsString -> tarPath -> IO (GenEntry content tarPath OsString)
forall content tarPath linkTarget.
(Int64 -> OsString -> IO content)
-> OsString -> tarPath -> IO (GenEntry content tarPath linkTarget)
packFileEntryWithRead' Int64 -> OsString -> IO content
r
mkEntry abspath relpath
interleavedSequence :: [IO a] -> IO [a]
interleavedSequence :: forall a. [IO a] -> IO [a]
interleavedSequence =
(IO a -> IO [a] -> IO [a]) -> IO [a] -> [IO a] -> IO [a]
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr ((IO [a] -> IO [a]
forall a. IO a -> IO a
unsafeInterleaveIO (IO [a] -> IO [a]) -> (IO [a] -> IO [a]) -> IO [a] -> IO [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
.) ((IO [a] -> IO [a]) -> IO [a] -> IO [a])
-> (IO a -> IO [a] -> IO [a]) -> IO a -> IO [a] -> IO [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> [a] -> [a]) -> IO a -> IO [a] -> IO [a]
forall a b c. (a -> b -> c) -> IO a -> IO b -> IO c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (:)) ([a] -> IO [a]
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure [])
packFileEntry
:: FilePath
-> tarPath
-> IO (GenEntry BL.ByteString tarPath linkTarget)
packFileEntry :: forall tarPath linkTarget.
FilePath -> tarPath -> IO (GenEntry ByteString tarPath linkTarget)
packFileEntry = OsString -> tarPath -> IO (GenEntry ByteString tarPath linkTarget)
forall tarPath linkTarget.
OsString -> tarPath -> IO (GenEntry ByteString tarPath linkTarget)
packFileEntry' (OsString
-> tarPath -> IO (GenEntry ByteString tarPath linkTarget))
-> (FilePath -> OsString)
-> FilePath
-> tarPath
-> IO (GenEntry ByteString tarPath linkTarget)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> OsString
filePathToOsPath
packFileEntry'
:: OsPath
-> tarPath
-> IO (GenEntry BL.ByteString tarPath linkTarget)
packFileEntry' :: forall tarPath linkTarget.
OsString -> tarPath -> IO (GenEntry ByteString tarPath linkTarget)
packFileEntry' OsString
filepath tarPath
tarpath = do
mtime <- OsString -> IO Int64
getModTime OsString
filepath
perms <- getPermissions filepath
approxSize <- getFileSize filepath
(content, size) <- if approxSize < 131072
then do
cnt <- readFile' filepath
pure (BL.fromStrict cnt, fromIntegral $ B.length cnt)
else do
hndl <- openBinaryFile filepath ReadMode
sz <- hFileSize hndl
cnt <- BL.hGetContents hndl
pure (cnt, fromInteger sz)
pure (simpleEntry tarpath (NormalFile content size))
{ entryPermissions =
if executable perms then executableFilePermissions else ordinaryFilePermissions
, entryTime = mtime
}
defaultRead
:: Int64
-> OsPath
-> IO BL.ByteString
defaultRead :: Int64 -> OsString -> IO ByteString
defaultRead Int64
approxSize OsString
filepath = do
if Int64
approxSize Int64 -> Int64 -> Bool
forall a. Ord a => a -> a -> Bool
< Int64
131072
then do
cnt <- OsString -> IO ByteString
readFile' OsString
filepath
let sz = Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int
B.length ByteString
cnt) :: Int64
if sz /= approxSize
then throwWrongSize sz
else pure (BL.fromStrict cnt)
else do
hndl <- OsString -> IOMode -> IO Handle
openBinaryFile OsString
filepath IOMode
ReadMode
sz <- fromInteger <$> hFileSize hndl
if sz /= approxSize
then do
hClose hndl
throwWrongSize sz
else do
cnt <- BL.hGetContents hndl
pure cnt
where
throwWrongSize :: Int64 -> IO a
throwWrongSize :: forall a. Int64 -> IO a
throwWrongSize Int64
sz = do
let msg :: FilePath
msg = FilePath
"File size changed, expecting " FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ Int64 -> FilePath
forall a. Show a => a -> FilePath
show Int64
approxSize FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
"; got " FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ Int64 -> FilePath
forall a. Show a => a -> FilePath
show Int64
sz
IOError -> IO a
forall a. HasCallStack => IOError -> IO a
ioError (IOError -> FilePath -> Maybe Handle -> Maybe FilePath -> IOError
annotateIOError (FilePath -> IOError
userError FilePath
msg) FilePath
"defaultRead" Maybe Handle
forall a. Maybe a
Nothing (FilePath -> Maybe FilePath
forall a. a -> Maybe a
Just (OsString -> FilePath
osPathToFilePath OsString
filepath)))
packFileEntryWithRead'
:: (Int64 -> OsPath -> IO content)
-> OsPath
-> tarPath
-> IO (GenEntry content tarPath linkTarget)
packFileEntryWithRead' :: forall content tarPath linkTarget.
(Int64 -> OsString -> IO content)
-> OsString -> tarPath -> IO (GenEntry content tarPath linkTarget)
packFileEntryWithRead' Int64 -> OsString -> IO content
r OsString
filepath tarPath
tarpath = do
mtime <- OsString -> IO Int64
getModTime OsString
filepath
perms <- getPermissions filepath
size <- fromInteger <$> getFileSize filepath
content <- r size filepath
pure (simpleEntry tarpath (NormalFile content size))
{ entryPermissions =
if executable perms then executableFilePermissions else ordinaryFilePermissions
, entryTime = mtime
}
packDirectoryEntry
:: FilePath
-> tarPath
-> IO (GenEntry content tarPath linkTarget)
packDirectoryEntry :: forall tarPath content linkTarget.
FilePath -> tarPath -> IO (GenEntry content tarPath linkTarget)
packDirectoryEntry = OsString -> tarPath -> IO (GenEntry content tarPath linkTarget)
forall tarPath content linkTarget.
OsString -> tarPath -> IO (GenEntry content tarPath linkTarget)
packDirectoryEntry' (OsString -> tarPath -> IO (GenEntry content tarPath linkTarget))
-> (FilePath -> OsString)
-> FilePath
-> tarPath
-> IO (GenEntry content tarPath linkTarget)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> OsString
filePathToOsPath
packDirectoryEntry'
:: OsPath
-> tarPath
-> IO (GenEntry content tarPath linkTarget)
packDirectoryEntry' :: forall tarPath content linkTarget.
OsString -> tarPath -> IO (GenEntry content tarPath linkTarget)
packDirectoryEntry' OsString
filepath tarPath
tarpath = do
mtime <- OsString -> IO Int64
getModTime OsString
filepath
return (directoryEntry tarpath) {
entryTime = mtime
}
packSymlinkEntry
:: FilePath
-> tarPath
-> IO (GenEntry content tarPath FilePath)
packSymlinkEntry :: forall tarPath content.
FilePath -> tarPath -> IO (GenEntry content tarPath FilePath)
packSymlinkEntry = (((GenEntry content tarPath OsString
-> GenEntry content tarPath FilePath)
-> IO (GenEntry content tarPath OsString)
-> IO (GenEntry content tarPath FilePath)
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((OsString -> FilePath)
-> GenEntry content tarPath OsString
-> GenEntry content tarPath FilePath
forall a b.
(a -> b)
-> GenEntry content tarPath a -> GenEntry content tarPath b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap OsString -> FilePath
osPathToFilePath) (IO (GenEntry content tarPath OsString)
-> IO (GenEntry content tarPath FilePath))
-> (tarPath -> IO (GenEntry content tarPath OsString))
-> tarPath
-> IO (GenEntry content tarPath FilePath)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.) ((tarPath -> IO (GenEntry content tarPath OsString))
-> tarPath -> IO (GenEntry content tarPath FilePath))
-> (OsString -> tarPath -> IO (GenEntry content tarPath OsString))
-> OsString
-> tarPath
-> IO (GenEntry content tarPath FilePath)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. OsString -> tarPath -> IO (GenEntry content tarPath OsString)
forall tarPath content.
OsString -> tarPath -> IO (GenEntry content tarPath OsString)
packSymlinkEntry') (OsString -> tarPath -> IO (GenEntry content tarPath FilePath))
-> (FilePath -> OsString)
-> FilePath
-> tarPath
-> IO (GenEntry content tarPath FilePath)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> OsString
filePathToOsPath
packSymlinkEntry'
:: OsPath
-> tarPath
-> IO (GenEntry content tarPath OsPath)
packSymlinkEntry' :: forall tarPath content.
OsString -> tarPath -> IO (GenEntry content tarPath OsString)
packSymlinkEntry' OsString
filepath tarPath
tarpath = do
linkTarget <- OsString -> IO OsString
getSymbolicLinkTarget OsString
filepath
pure $ symlinkEntry tarpath linkTarget
getModTime :: OsPath -> IO EpochTime
getModTime :: OsString -> IO Int64
getModTime OsString
path = do
t <- OsString -> IO UTCTime
getModificationTime OsString
path
return . floor . utcTimeToPOSIXSeconds $ t