Skip to content

Commit a296301

Browse files
committed
Work-around hardlinks unpacking
Fixes #4
1 parent 5d3caaa commit a296301

2 files changed

Lines changed: 19 additions & 8 deletions

File tree

src/Codec/Archive/Unpack.hs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE LambdaCase #-}
12
module Codec.Archive.Unpack ( hsEntries
23
, unpackEntriesFp
34
, unpackArchive
@@ -96,16 +97,26 @@ unpackEntriesFp a fp = do
9697
case res of
9798
Nothing -> pure ()
9899
Just x -> do
99-
preFile <- liftIO $ archiveEntryPathname x
100-
file <- liftIO $ peekCString preFile
101-
let file' = fp </> file
102-
liftIO $ withCString file' $ \fileC ->
103-
archiveEntrySetPathname x fileC
104-
ignore $ archiveReadExtract a x archiveExtractTime
105-
liftIO $ archiveEntrySetPathname x preFile
100+
ignore . withPrefix fp x $ archiveReadExtract a x archiveExtractTime
106101
ignore $ archiveReadDataSkip a
107102
unpackEntriesFp a fp
108103

104+
-- TODO: work with libarchive to get rid of this unsafe hack
105+
-- See https://github.com/libarchive/libarchive/issues/1203
106+
withPrefix :: FilePath -> ArchiveEntryPtr -> IO a -> IO a
107+
withPrefix fp x inner = do
108+
file0 <- peekCString =<< archiveEntryPathname x
109+
withCString (fp </> file0) $ archiveEntrySetPathname x
110+
result <- archiveEntryFiletype x >>= \case
111+
Nothing -> archiveEntryHardlink x >>= peekCString >>= \target0 -> do
112+
withCString (fp </> target0) $ archiveEntrySetHardlink x
113+
result <- inner
114+
withCString target0 $ archiveEntrySetHardlink x
115+
return result
116+
_ -> inner
117+
withCString file0 $ archiveEntrySetPathname x
118+
return result
119+
109120
readBS :: Ptr Archive -> Int -> IO BS.ByteString
110121
readBS a sz =
111122
allocaBytes sz $ \buff ->

test/Spec.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ main = do
8989
, simpleFile "x/b.txt" (Hardlink "x/a.txt")
9090
]
9191
itPacksUnpacks entries
92-
xcontext "issue#4" $ itPacksUnpacksViaFS entries
92+
itPacksUnpacksViaFS entries
9393

9494
context "with forward referenced hardlinks" $ do
9595
let entries =

0 commit comments

Comments
 (0)