Skip to content

Commit 7d694a0

Browse files
committed
Pack: stylistic improvements
1 parent 48706b3 commit 7d694a0

File tree

1 file changed

+33
-22
lines changed

1 file changed

+33
-22
lines changed

Codec/Archive/Tar/Pack.hs

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ module Codec.Archive.Tar.Pack (
2929
import Codec.Archive.Tar.LongNames
3030
import Codec.Archive.Tar.Types
3131
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
3434
import System.FilePath
3535
( (</>) )
3636
import qualified System.FilePath as FilePath.Native
@@ -64,19 +64,21 @@ import Codec.Archive.Tar.Check.Internal (checkSecurity)
6464
-- * This function returns results lazily. Subdirectories are scanned
6565
-- and files are read one by one as the list of entries is consumed.
6666
--
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]
7071
pack = packWith checkSecurity
7172

7273
-- | Like 'pack', but does not perform any sanity/security checks on the input.
7374
-- You can do so yourself, e.g.: @packWith@ 'checkSecurity' @dir@ @files@.
7475
--
7576
-- @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]
8082
packWith secCB baseDir =
8183
preparePaths baseDir >=>
8284
packPaths secCB baseDir >=>
@@ -98,13 +100,19 @@ preparePaths baseDir = fmap concat . interleave . map go
98100
else return [relpath]
99101

100102
-- | 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]
102108
packPaths secCB baseDir paths = interleave $ flip map paths $ \relpath -> do
103109
let isDir = FilePath.Native.hasTrailingPathSeparator abspath
104110
abspath = baseDir </> relpath
105111
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
108116
e <- mkEntry abspath relpath
109117
secCB e
110118
pure e
@@ -126,15 +134,16 @@ interleave = unsafeInterleaveIO . go
126134
--
127135
-- * The file contents is read lazily.
128136
--
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)
132141
packFileEntry filepath tarpath = do
133142
mtime <- getModTime filepath
134143
perms <- getPermissions filepath
135144
file <- openBinaryFile filepath ReadMode
136145
size <- hFileSize file
137-
content <- BS.hGetContents file
146+
content <- BL.hGetContents file
138147
return (simpleEntry tarpath (NormalFile content (fromIntegral size))) {
139148
entryPermissions = if executable perms then executableFilePermissions
140149
else ordinaryFilePermissions,
@@ -146,9 +155,10 @@ packFileEntry filepath tarpath = do
146155
-- The only attribute of the directory that is used is its modification time.
147156
-- Directory ownership and detailed permissions are not preserved.
148157
--
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)
152162
packDirectoryEntry filepath tarpath = do
153163
mtime <- getModTime filepath
154164
return (directoryEntry tarpath) {
@@ -160,9 +170,10 @@ packDirectoryEntry filepath tarpath = do
160170
-- This automatically checks symlink safety via 'checkEntrySecurity'.
161171
--
162172
-- @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)
166177
packSymlinkEntry filepath tarpath = do
167178
linkTarget <- getSymbolicLinkTarget filepath
168179
pure $ symlinkEntry tarpath linkTarget

0 commit comments

Comments
 (0)