17
17
-----------------------------------------------------------------------------
18
18
module Codec.Archive.Tar.Pack (
19
19
pack ,
20
- packWith ,
20
+ packAndCheck ,
21
21
packFileEntry ,
22
22
packDirectoryEntry ,
23
23
packSymlinkEntry ,
@@ -31,6 +31,7 @@ import Codec.Archive.Tar.Types
31
31
import Control.Monad (join , when , forM , (>=>) )
32
32
import qualified Data.ByteString as B
33
33
import qualified Data.ByteString.Lazy as BL
34
+ import Data.Foldable
34
35
import System.FilePath
35
36
( (</>) )
36
37
import qualified System.FilePath as FilePath.Native
68
69
:: FilePath -- ^ Base directory
69
70
-> [FilePath ] -- ^ Files and directories to pack, relative to the base dir
70
71
-> IO [Entry ]
71
- pack = packWith checkSecurity
72
+ pack = packAndCheck ( const $ pure () )
72
73
73
- -- | Like 'pack', but does not perform any sanity/security checks on the input.
74
- -- You can do so yourself, e.g.: @packWith@ 'checkSecurity' @dir@ @files@ .
74
+ -- | Like 'pack', but allows to specify any sanity/security checks on the input
75
+ -- filenames .
75
76
--
76
77
-- @since 0.6.0.0
77
- packWith
78
+ packAndCheck
78
79
:: CheckSecurityCallback
79
80
-> FilePath -- ^ Base directory
80
81
-> [FilePath ] -- ^ Files and directories to pack, relative to the base dir
81
82
-> IO [Entry ]
82
- packWith secCB baseDir =
83
- preparePaths baseDir >=>
84
- packPaths secCB baseDir >=>
85
- (pure . concatMap encodeLongNames)
83
+ packAndCheck secCB baseDir relpaths = do
84
+ paths <- preparePaths baseDir relpaths
85
+ entries <- packPaths baseDir paths
86
+ traverse_ secCB entries
87
+ pure $ concatMap encodeLongNames entries
86
88
87
89
preparePaths :: FilePath -> [FilePath ] -> IO [FilePath ]
88
90
preparePaths baseDir = fmap concat . interleave . map go
@@ -101,21 +103,18 @@ preparePaths baseDir = fmap concat . interleave . map go
101
103
102
104
-- | Pack paths while accounting for overlong filepaths.
103
105
packPaths
104
- :: CheckSecurityCallback
105
- -> FilePath
106
+ :: FilePath
106
107
-> [FilePath ]
107
108
-> IO [GenEntry FilePath FilePath ]
108
- packPaths secCB baseDir paths = interleave $ flip map paths $ \ relpath -> do
109
+ packPaths baseDir paths = interleave $ flip map paths $ \ relpath -> do
109
110
let isDir = FilePath.Native. hasTrailingPathSeparator abspath
110
111
abspath = baseDir </> relpath
111
112
isSymlink <- pathIsSymbolicLink abspath
112
113
let mkEntry
113
114
| isSymlink = packSymlinkEntry
114
115
| isDir = packDirectoryEntry
115
116
| otherwise = packFileEntry
116
- e <- mkEntry abspath relpath
117
- secCB e
118
- pure e
117
+ mkEntry abspath relpath
119
118
120
119
interleave :: [IO a ] -> IO [a ]
121
120
interleave = unsafeInterleaveIO . go
0 commit comments