| 1 |
package pack |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/binary" |
| 5 |
"hash" |
| 6 |
) |
| 7 |
|
| 8 |
// V2 implements IndexVersion for v2 packfiles. |
| 9 |
type V2 struct { |
| 10 |
hash hash.Hash |
| 11 |
} |
| 12 |
|
| 13 |
// Name implements IndexVersion.Name by returning the 20 byte SHA-1 object name |
| 14 |
// for the given entry at offset "at" in the v2 index file "idx". |
| 15 |
func (v *V2) Name(idx *Index, at int64) ([]byte, error) { |
| 16 |
var sha [MaxHashSize]byte |
| 17 |
|
| 18 |
hashlen := v.hash.Size() |
| 19 |
|
| 20 |
if _, err := idx.readAt(sha[:hashlen], v2ShaOffset(at, int64(hashlen))); err != nil { |
| 21 |
return nil, err |
| 22 |
} |
| 23 |
|
| 24 |
return sha[:hashlen], nil |
| 25 |
} |
| 26 |
|
| 27 |
// Entry implements IndexVersion.Entry for v2 packfiles by parsing and returning |
| 28 |
// the IndexEntry specified at the offset "at" in the given index file. |
| 29 |
func (v *V2) Entry(idx *Index, at int64) (*IndexEntry, error) { |
| 30 |
var offs [4]byte |
| 31 |
|
| 32 |
hashlen := v.hash.Size() |
| 33 |
|
| 34 |
if _, err := idx.readAt(offs[:], v2SmallOffsetOffset(at, int64(idx.Count()), int64(hashlen))); err != nil { |
| 35 |
return nil, err |
| 36 |
} |
| 37 |
|
| 38 |
loc := uint64(binary.BigEndian.Uint32(offs[:])) |
| 39 |
if loc&0x80000000 > 0 { |
| 40 |
// If the most significant bit (MSB) of the offset is set, then |
| 41 |
// the offset encodes the indexed location for an 8-byte offset. |
| 42 |
// |
| 43 |
// Mask away (offs&0x7fffffff) the MSB to use as an index to |
| 44 |
// find the offset of the 8-byte pack offset. |
| 45 |
lo := v2LargeOffsetOffset(int64(loc&0x7fffffff), int64(idx.Count()), int64(hashlen)) |
| 46 |
|
| 47 |
var offs [8]byte |
| 48 |
if _, err := idx.readAt(offs[:], lo); err != nil { |
| 49 |
return nil, err |
| 50 |
} |
| 51 |
|
| 52 |
loc = binary.BigEndian.Uint64(offs[:]) |
| 53 |
} |
| 54 |
return &IndexEntry{PackOffset: loc}, nil |
| 55 |
} |
| 56 |
|
| 57 |
// Width implements IndexVersion.Width() by returning the number of bytes that |
| 58 |
// v2 packfile index header occupy. |
| 59 |
func (v *V2) Width() int64 { |
| 60 |
return indexV2Width |
| 61 |
} |
| 62 |
|
| 63 |
// v2ShaOffset returns the offset of a SHA1 given at "at" in the V2 index file. |
| 64 |
func v2ShaOffset(at int64, hashlen int64) int64 { |
| 65 |
// Skip the packfile index header and the L1 fanout table. |
| 66 |
return indexOffsetV2Start + |
| 67 |
// Skip until the desired name in the sorted names table. |
| 68 |
(hashlen * at) |
| 69 |
} |
| 70 |
|
| 71 |
// v2SmallOffsetOffset returns the offset of an object's small (4-byte) offset |
| 72 |
// given by "at". |
| 73 |
func v2SmallOffsetOffset(at, total, hashlen int64) int64 { |
| 74 |
// Skip the packfile index header and the L1 fanout table. |
| 75 |
return indexOffsetV2Start + |
| 76 |
// Skip the name table. |
| 77 |
(hashlen * total) + |
| 78 |
// Skip the CRC table. |
| 79 |
(indexObjectCRCWidth * total) + |
| 80 |
// Skip until the desired index in the small offsets table. |
| 81 |
(indexObjectSmallOffsetWidth * at) |
| 82 |
} |
| 83 |
|
| 84 |
// v2LargeOffsetOffset returns the offset of an object's large (4-byte) offset, |
| 85 |
// given by the index "at". |
| 86 |
func v2LargeOffsetOffset(at, total, hashlen int64) int64 { |
| 87 |
// Skip the packfile index header and the L1 fanout table. |
| 88 |
return indexOffsetV2Start + |
| 89 |
// Skip the name table. |
| 90 |
(hashlen * total) + |
| 91 |
// Skip the CRC table. |
| 92 |
(indexObjectCRCWidth * total) + |
| 93 |
// Skip the small offsets table. |
| 94 |
(indexObjectSmallOffsetWidth * total) + |
| 95 |
// Seek to the large offset within the large offset(s) table. |
| 96 |
(indexObjectLargeOffsetWidth * at) |
| 97 |
} |