| 1 |
package pack |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/binary" |
| 5 |
"hash" |
| 6 |
) |
| 7 |
|
| 8 |
// V1 implements IndexVersion for v1 packfiles. |
| 9 |
type V1 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 v1 index file "idx". |
| 15 |
func (v *V1) 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], v1ShaOffset(at, int64(hashlen))); err != nil { |
| 21 |
return nil, err |
| 22 |
} |
| 23 |
|
| 24 |
return sha[:hashlen], nil |
| 25 |
} |
| 26 |
|
| 27 |
// Entry implements IndexVersion.Entry for v1 packfiles by parsing and returning |
| 28 |
// the IndexEntry specified at the offset "at" in the given index file. |
| 29 |
func (v *V1) Entry(idx *Index, at int64) (*IndexEntry, error) { |
| 30 |
var offs [4]byte |
| 31 |
if _, err := idx.readAt(offs[:], v1EntryOffset(at, int64(v.hash.Size()))); err != nil { |
| 32 |
return nil, err |
| 33 |
} |
| 34 |
|
| 35 |
return &IndexEntry{ |
| 36 |
PackOffset: uint64(binary.BigEndian.Uint32(offs[:])), |
| 37 |
}, nil |
| 38 |
} |
| 39 |
|
| 40 |
// Width implements IndexVersion.Width() by returning the number of bytes that |
| 41 |
// v1 packfile index header occupy. |
| 42 |
func (v *V1) Width() int64 { |
| 43 |
return indexV1Width |
| 44 |
} |
| 45 |
|
| 46 |
// v1ShaOffset returns the location of the SHA1 of an object given at "at". |
| 47 |
func v1ShaOffset(at int64, hashlen int64) int64 { |
| 48 |
// Skip forward until the desired entry. |
| 49 |
return v1EntryOffset(at, hashlen) + |
| 50 |
// Skip past the 4-byte object offset in the desired entry to |
| 51 |
// the SHA1. |
| 52 |
indexObjectSmallOffsetWidth |
| 53 |
} |
| 54 |
|
| 55 |
// v1EntryOffset returns the location of the packfile offset for the object |
| 56 |
// given at "at". |
| 57 |
func v1EntryOffset(at int64, hashlen int64) int64 { |
| 58 |
// Skip the L1 fanout table |
| 59 |
return indexOffsetV1Start + |
| 60 |
// Skip the object entries before the one located at "at" |
| 61 |
((hashlen + indexObjectSmallOffsetWidth) * at) |
| 62 |
} |