@@ -29,8 +29,8 @@ module Codec.Archive.Tar.Pack (
29
29
import Codec.Archive.Tar.LongNames
30
30
import Codec.Archive.Tar.Types
31
31
import Control.Monad (join , when , forM , (>=>) )
32
- import qualified Data.ByteString as BSS
33
- import qualified Data.ByteString.Lazy as BS
32
+ import qualified Data.ByteString as B
33
+ import qualified Data.ByteString.Lazy as BL
34
34
import System.FilePath
35
35
( (</>) )
36
36
import qualified System.FilePath as FilePath.Native
@@ -64,19 +64,21 @@ import Codec.Archive.Tar.Check.Internal (checkSecurity)
64
64
-- * This function returns results lazily. Subdirectories are scanned
65
65
-- and files are read one by one as the list of entries is consumed.
66
66
--
67
- pack :: FilePath -- ^ Base directory
68
- -> [FilePath ] -- ^ Files and directories to pack, relative to the base dir
69
- -> IO [Entry ]
67
+ pack
68
+ :: FilePath -- ^ Base directory
69
+ -> [FilePath ] -- ^ Files and directories to pack, relative to the base dir
70
+ -> IO [Entry ]
70
71
pack = packWith checkSecurity
71
72
72
73
-- | Like 'pack', but does not perform any sanity/security checks on the input.
73
74
-- You can do so yourself, e.g.: @packWith@ 'checkSecurity' @dir@ @files@.
74
75
--
75
76
-- @since 0.6.0.0
76
- packWith :: CheckSecurityCallback
77
- -> FilePath -- ^ Base directory
78
- -> [FilePath ] -- ^ Files and directories to pack, relative to the base dir
79
- -> IO [Entry ]
77
+ packWith
78
+ :: CheckSecurityCallback
79
+ -> FilePath -- ^ Base directory
80
+ -> [FilePath ] -- ^ Files and directories to pack, relative to the base dir
81
+ -> IO [Entry ]
80
82
packWith secCB baseDir =
81
83
preparePaths baseDir >=>
82
84
packPaths secCB baseDir >=>
@@ -98,13 +100,19 @@ preparePaths baseDir = fmap concat . interleave . map go
98
100
else return [relpath]
99
101
100
102
-- | Pack paths while accounting for overlong filepaths.
101
- packPaths :: CheckSecurityCallback -> FilePath -> [FilePath ] -> IO [GenEntry FilePath FilePath ]
103
+ packPaths
104
+ :: CheckSecurityCallback
105
+ -> FilePath
106
+ -> [FilePath ]
107
+ -> IO [GenEntry FilePath FilePath ]
102
108
packPaths secCB baseDir paths = interleave $ flip map paths $ \ relpath -> do
103
109
let isDir = FilePath.Native. hasTrailingPathSeparator abspath
104
110
abspath = baseDir </> relpath
105
111
isSymlink <- pathIsSymbolicLink abspath
106
- let mkEntry = if isSymlink then packSymlinkEntry else
107
- (if isDir then packDirectoryEntry else packFileEntry)
112
+ let mkEntry
113
+ | isSymlink = packSymlinkEntry
114
+ | isDir = packDirectoryEntry
115
+ | otherwise = packFileEntry
108
116
e <- mkEntry abspath relpath
109
117
secCB e
110
118
pure e
@@ -126,15 +134,16 @@ interleave = unsafeInterleaveIO . go
126
134
--
127
135
-- * The file contents is read lazily.
128
136
--
129
- packFileEntry :: FilePath -- ^ Full path to find the file on the local disk
130
- -> tarPath -- ^ Path to use for the tar Entry in the archive
131
- -> IO (GenEntry tarPath linkTarget )
137
+ packFileEntry
138
+ :: FilePath -- ^ Full path to find the file on the local disk
139
+ -> tarPath -- ^ Path to use for the tar 'Entry' in the archive
140
+ -> IO (GenEntry tarPath linkTarget )
132
141
packFileEntry filepath tarpath = do
133
142
mtime <- getModTime filepath
134
143
perms <- getPermissions filepath
135
144
file <- openBinaryFile filepath ReadMode
136
145
size <- hFileSize file
137
- content <- BS . hGetContents file
146
+ content <- BL . hGetContents file
138
147
return (simpleEntry tarpath (NormalFile content (fromIntegral size))) {
139
148
entryPermissions = if executable perms then executableFilePermissions
140
149
else ordinaryFilePermissions,
@@ -146,9 +155,10 @@ packFileEntry filepath tarpath = do
146
155
-- The only attribute of the directory that is used is its modification time.
147
156
-- Directory ownership and detailed permissions are not preserved.
148
157
--
149
- packDirectoryEntry :: FilePath -- ^ Full path to find the file on the local disk
150
- -> tarPath -- ^ Path to use for the tar Entry in the archive
151
- -> IO (GenEntry tarPath linkTarget )
158
+ packDirectoryEntry
159
+ :: FilePath -- ^ Full path to find the file on the local disk
160
+ -> tarPath -- ^ Path to use for the tar 'Entry' in the archive
161
+ -> IO (GenEntry tarPath linkTarget )
152
162
packDirectoryEntry filepath tarpath = do
153
163
mtime <- getModTime filepath
154
164
return (directoryEntry tarpath) {
@@ -160,9 +170,10 @@ packDirectoryEntry filepath tarpath = do
160
170
-- This automatically checks symlink safety via 'checkEntrySecurity'.
161
171
--
162
172
-- @since 0.6.0.0
163
- packSymlinkEntry :: FilePath -- ^ Full path to find the file on the local disk
164
- -> tarPath -- ^ Path to use for the tar Entry in the archive
165
- -> IO (GenEntry tarPath FilePath )
173
+ packSymlinkEntry
174
+ :: FilePath -- ^ Full path to find the file on the local disk
175
+ -> tarPath -- ^ Path to use for the tar 'Entry' in the archive
176
+ -> IO (GenEntry tarPath FilePath )
166
177
packSymlinkEntry filepath tarpath = do
167
178
linkTarget <- getSymbolicLinkTarget filepath
168
179
pure $ symlinkEntry tarpath linkTarget
0 commit comments