-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathIO.hs
112 lines (99 loc) · 3.91 KB
/
IO.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
-- | IO utilities
{-# LANGUAGE CPP #-}
module Hackage.Security.RepoTool.Util.IO (
-- * Miscellaneous
compress
, getFileModTime
, createSymbolicLink
-- * Tar archives
, TarGzError
, tarExtractFile
) where
import Control.Exception
import Data.Typeable
import System.IO.Error
import qualified Codec.Archive.Tar as Tar
import qualified Codec.Archive.Tar.Entry as Tar
import qualified Codec.Compression.GZip as GZip
import qualified Data.ByteString.Lazy as BS.L
-- Unlike the hackage-security library properly,
-- this currently works on unix systems only
import System.Posix.Types (EpochTime)
import qualified System.Posix.Files as Posix
-- hackage-security
import Hackage.Security.Util.Path
-- hackage-repo-tool
import Hackage.Security.RepoTool.Options
import Hackage.Security.RepoTool.Layout
import Hackage.Security.RepoTool.Paths
-- | Get the modification time of the specified file
--
-- Returns 0 if the file does not exist .
getFileModTime :: GlobalOpts -> RepoLoc -> TargetPath' -> IO EpochTime
getFileModTime opts repoLoc targetPath =
handle handler $
Posix.modificationTime <$> Posix.getFileStatus (toFilePath fp)
where
fp :: Path Absolute
fp = anchorTargetPath' opts repoLoc targetPath
handler :: IOException -> IO EpochTime
handler ex = if isDoesNotExistError ex then return 0
else throwIO ex
compress :: Path Absolute -> Path Absolute -> IO ()
compress src dst =
withFile dst WriteMode $ \h ->
BS.L.hPut h =<< GZip.compress <$> readLazyByteString src
-- | Create a symbolic link (unix only)
--
-- Create the directory for the target if it does not exist.
--
-- TODO: Currently this always creates links to absolute locations, whether the
-- user specified an absolute or a relative target.
createSymbolicLink :: (FsRoot root, FsRoot root')
=> Path root -- ^ Link target
-> Path root' -- ^ Link location
-> IO ()
createSymbolicLink linkTarget linkLoc = do
createDirectoryIfMissing True (takeDirectory linkLoc)
linkTarget' <- toAbsoluteFilePath linkTarget
linkLoc' <- toAbsoluteFilePath linkLoc
Posix.createSymbolicLink linkTarget' linkLoc'
{-------------------------------------------------------------------------------
Working with tar archives
-------------------------------------------------------------------------------}
-- | Extract a file from a tar archive
--
-- Throws an exception if there is an error in the archive or when the entry
-- is not a file. Returns nothing if the entry cannot be found.
tarExtractFile :: GlobalOpts
-> RepoLoc
-> TargetPath'
-> FilePath
-> IO (Maybe (BS.L.ByteString, Tar.FileSize))
tarExtractFile opts repoLoc pathTarGz pathToExtract =
handle (throwIO . TarGzError (prettyTargetPath' opts pathTarGz)) $ do
let pathTarGz' = anchorTargetPath' opts repoLoc pathTarGz
go =<< Tar.read . GZip.decompress <$> readLazyByteString pathTarGz'
where
go :: Exception e => Tar.Entries e -> IO (Maybe (BS.L.ByteString, Tar.FileSize))
go Tar.Done = return Nothing
go (Tar.Fail err) = throwIO err
go (Tar.Next e es) =
if Tar.entryPath e == pathToExtract
then case Tar.entryContent e of
Tar.NormalFile bs sz -> return $ Just (bs, sz)
_ -> throwIO $ userError
$ "tarExtractFile: "
++ pathToExtract ++ " not a normal file"
else do -- putStrLn $ show (Tar.entryPath e) ++ " /= " ++ show path
go es
data TarGzError = TarGzError FilePath SomeException
deriving (Typeable)
instance Exception TarGzError where
#if MIN_VERSION_base(4,8,0)
displayException (TarGzError path e) = path ++ ": " ++ displayException e
deriving instance Show TarGzError
#else
instance Show TarGzError where
show (TarGzError path e) = path ++ ": " ++ show e
#endif