Skip to content

Commit 563a5f8

Browse files
committed
Share file read for archives
1 parent e8de003 commit 563a5f8

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

src/Stack/PackageLocation.hs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -109,31 +109,34 @@ resolveSinglePackageLocation projRoot (PLArchive (Archive url subdir msha)) = do
109109

110110
let fp = toFilePath file
111111

112-
let tryTargz = do
112+
withBinaryFile fp ReadMode $ \h -> do
113+
-- Share a single file read among all of the different
114+
-- parsing attempts. We're not worried about unbounded
115+
-- memory usage, as we will detect almost immediately if
116+
-- this is the wrong type of file.
117+
lbs <- liftIO $ L.hGetContents h
118+
119+
let tryTargz = do
113120
logDebug $ "Trying to ungzip/untar " <> T.pack fp
114-
liftIO $ withBinaryFile fp ReadMode $ \h -> do
115-
lbs <- L.hGetContents h
116-
let entries = Tar.read $ GZip.decompress lbs
117-
Tar.unpack (toFilePath dirTmp) entries
118-
tryZip = do
121+
let entries = Tar.read $ GZip.decompress lbs
122+
liftIO $ Tar.unpack (toFilePath dirTmp) entries
123+
tryZip = do
119124
logDebug $ "Trying to unzip " <> T.pack fp
120-
archive <- fmap Zip.toArchive $ liftIO $ L.readFile fp
125+
let archive = Zip.toArchive lbs
121126
liftIO $ Zip.extractFilesFromArchive [Zip.OptDestination
122127
(toFilePath dirTmp)] archive
123-
tryTar = do
128+
tryTar = do
124129
logDebug $ "Trying to untar (no ungzip) " <> T.pack fp
125-
liftIO $ withBinaryFile fp ReadMode $ \h -> do
126-
lbs <- L.hGetContents h
127-
let entries = Tar.read lbs
128-
Tar.unpack (toFilePath dirTmp) entries
129-
err = throwM $ UnableToExtractArchive url file
130+
let entries = Tar.read lbs
131+
liftIO $ Tar.unpack (toFilePath dirTmp) entries
132+
err = throwM $ UnableToExtractArchive url file
130133

131-
catchAnyLog goodpath handler =
132-
catchAny goodpath $ \e -> do
133-
logDebug $ "Got exception: " <> T.pack (show e)
134-
handler
134+
catchAnyLog goodpath handler =
135+
catchAny goodpath $ \e -> do
136+
logDebug $ "Got exception: " <> T.pack (show e)
137+
handler
135138

136-
tryTargz `catchAnyLog` tryZip `catchAnyLog` tryTar `catchAnyLog` err
139+
tryTargz `catchAnyLog` tryZip `catchAnyLog` tryTar `catchAnyLog` err
137140
renameDir dirTmp dir
138141

139142
x <- listDir dir

0 commit comments

Comments
 (0)