/
PackIndex.hs
224 lines (194 loc) · 6.54 KB
/
PackIndex.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
{-# LANGUAGE RecordWildCards #-}
{-# OPTIONS -Wall #-}
module Git.PackIndex (
dumpRawPackIndex,
findInPackIdxs,
-- * Paths
idxPath
) where
import Control.Applicative ((<$>))
import Control.Monad (forM_)
import qualified Data.ByteString as BS
import Data.Ord
import Data.Word (Word32)
import Foreign.Ptr
import Foreign.Storable
import Data.Storable.Endian
import System.Directory
import System.FilePath
import System.IO.MMap
import System.Posix.Types
import Text.Printf
import Git.SHA
import Git.Pack
import Git.Path
------------------------------------------------------------
data IDX = IDX1 {
idx1Pack :: FilePath
, idx1Size :: Int
, idx1Fanout :: Ptr (BigEndian Word32)
, idx1Offsets :: Ptr (BigEndian Word32)
} | IDX2 {
idx2Pack :: FilePath
, idx2Size :: Int
, idx2Fanout :: Ptr (BigEndian Word32)
, idx2SHA1s :: Ptr (BigEndian Word32)
, idx2CRCs :: Ptr (BigEndian Word32)
, idx2Offsets :: Ptr (BigEndian Word32)
, idx264bOffsets :: Ptr (BigEndian Word32)
-- , idx2PackCSum :: Ptr (BigEndian Word32)
-- , idx2IdxCSum :: Ptr (BigEndian Word32)
}
------------------------------------------------------------
-- | Public API
-- | Corresponding packfile path
idxPack :: IDX -> FilePath
idxPack IDX1{..} = idx1Pack
idxPack IDX2{..} = idx2Pack
-- | Number of objects in the corresponding .pack file
idxSize :: IDX -> Int
idxSize IDX1{..} = idx1Size
idxSize IDX2{..} = idx2Size
-- | Nth SHA1
idxSha1 :: IDX -> Int -> IO BS.ByteString
idxSha1 IDX1{..} n
| n >= idx1Size = outOfRange
| otherwise = do
let cs = idx1Offsets `plusPtr` (4 + (n * 24))
BS.packCStringLen (cs, 20)
idxSha1 IDX2{..} n
| n >= idx2Size = outOfRange
| otherwise = do
let cs = idx2SHA1s `plusPtr` (n * 20)
BS.packCStringLen (cs, 20)
-- | Nth CRC
idxCRC :: IDX -> Int -> IO (Maybe Word32)
idxCRC IDX1{..} n
| n >= idx1Size = outOfRange
| otherwise = return Nothing
idxCRC IDX2{..} n
| n >= idx2Size = outOfRange
| otherwise = do
BE crc <- peekElemOff idx2CRCs n
return (Just crc)
-- | Nth offset
idxOffset :: IDX -> Int -> IO FileOffset
idxOffset IDX1{..} n
| n >= idx1Size = outOfRange
| otherwise = do
BE off <- peekByteOff idx1Offsets (n * 24)
return . fromIntegral $ (off :: Word32)
idxOffset IDX2{..} n
| n >= idx2Size = outOfRange
| otherwise = do
BE off <- peekElemOff idx2Offsets n
return . fromIntegral $ off
outOfRange :: IO a
outOfRange = error "Index out of range"
------------------------------------------------------------
idxFiles :: IO [FilePath]
idxFiles = do
packDir <- gitPath ("objects" </> "pack")
map (packDir </>) . filter isIdx <$> getDirectoryContents packDir
where
isIdx = (== ".idx") . takeExtension
------------------------------------------------------------
idxFind :: IDX -> BS.ByteString -> IO (Maybe (IDX, Int))
idxFind idx sha = idxFind' 0 (idxSize idx)
where
idxFind' lo hi
| lo == hi = do
iSha <- idxSha1 idx lo
case (sha `compare` iSha) of
EQ -> return (Just (idx, lo))
_ -> return Nothing
| otherwise = do
iSha <- idxSha1 idx i
case (sha `compare` iSha) of
EQ -> return (Just (idx, i))
LT -> idxFind' lo i
GT -> idxFind' i hi
where
i = floor ((fromIntegral (lo + hi)) / 2.0)
findInPackIdxs :: BS.ByteString -> IO ()
findInPackIdxs sha = do
idxs <- idxFiles
mapM_ (findInPackIndex' sha) idxs
findInPackIndex' :: BS.ByteString -> FilePath -> IO ()
findInPackIndex' sha fp = do
idx <- readIdx fp
m'i <- idxFind idx sha
case m'i of
Just (_, i) -> do
off <- idxOffset idx i
m'po <- packReadObject (idxPack idx) off
case m'po of
Just PackObject{..} -> do
putStrLn $ "Found at index " ++ show i
putStrLn $ show poType
putStrLn $ show poData
Nothing -> putStrLn $ "Error reading pack"
Nothing -> putStrLn $ "Not found"
------------------------------------------------------------
-- Debugging
dumpIdx :: IDX -> IO ()
dumpIdx idx@IDX1{..} = do
putStrLn $ idx1Pack ++ ": IDX Version 1"
dumpIdx' idx
dumpIdx idx@IDX2{..} = do
putStrLn $ idx2Pack ++ ": IDX Version 2"
dumpIdx' idx
dumpIdx' :: IDX -> IO ()
dumpIdx' idx = do
putStrLn $ show (idxSize idx) ++ " objects"
mapM_ f [0..(idxSize idx)-1]
where
f i = do
o <- fromIntegral <$> idxOffset idx i
let o' = printf "0x%04x" (o :: Int)
s <- idxSha1 idx i
c <- maybe "" ((" CRC: " ++) . show) <$> idxCRC idx i
putStrLn $ show i ++ ": " ++ o' ++ " SHA: " ++ showDigestBS s ++ c
------------------------------------------------------------
-- | Generate the pathname for a given packfile
idxPath :: String -> IO FilePath
idxPath idx = gitPath ("objects" </> "pack" </> ("pack-" ++ idx ++ ".idx"))
idxHeader :: Word32
idxHeader = 0xff744f63
readIdx :: FilePath -> IO IDX
readIdx fp = do
(ptr, rawsize, offset, size) <- mmapFilePtr fp ReadOnly Nothing
let start :: Ptr (BigEndian Word32)
start = ptr `plusPtr` offset
BE hdr <- peek start
if (hdr == idxHeader)
then do
BE ver <- peekElemOff start 1
case ver of
2 -> mkIDX2 fp start size
_ -> error "Unknown version"
else mkIDX1 fp start size
dumpRawPackIndex :: FilePath -> IO String
dumpRawPackIndex fp = do
idx <- readIdx fp
dumpIdx idx
return "Woot"
mkIDX1 :: FilePath -> Ptr (BigEndian Word32) -> Int -> IO IDX
mkIDX1 fp start size = do
let pack = replaceExtension fp ".pack"
fanout = start
BE n <- peekElemOff fanout 255
let n' = fromIntegral (n :: Word32)
let offsets = fanout `plusPtr` (256 * 4)
return (IDX1 pack n' fanout offsets)
mkIDX2 :: FilePath -> Ptr (BigEndian Word32) -> Int -> IO IDX
mkIDX2 fp start size = do
let pack = replaceExtension fp ".pack"
fanout = start `plusPtr` (2 * 4)
BE n <- peekElemOff fanout 255
let n' = fromIntegral (n :: Word32)
let sha1s = fanout `plusPtr` (256 * 4)
crcs = sha1s `plusPtr` (n' * 20)
offsets = crcs `plusPtr` (n' * 4)
offset64s = offsets `plusPtr` (n' * 4)
return (IDX2 pack n' fanout sha1s crcs offsets offset64s)