| 1 |
package pack |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"crypto/sha256" |
| 6 |
"fmt" |
| 7 |
"io" |
| 8 |
) |
| 9 |
|
| 10 |
const MaxHashSize = sha256.Size |
| 11 |
|
| 12 |
// Index stores information about the location of objects in a corresponding |
| 13 |
// packfile. |
| 14 |
type Index struct { |
| 15 |
// version is the encoding version used by this index. |
| 16 |
// |
| 17 |
// Currently, versions 1 and 2 are supported. |
| 18 |
version IndexVersion |
| 19 |
// fanout is the L1 fanout table stored in this index. For a given index |
| 20 |
// "i" into the array, the value stored at that index specifies the |
| 21 |
// number of objects in the packfile/index that are lexicographically |
| 22 |
// less than or equal to that index. |
| 23 |
// |
| 24 |
// See: https://github.com/git/git/blob/v2.13.0/Documentation/technical/pack-format.txt#L41-L45 |
| 25 |
fanout []uint32 |
| 26 |
|
| 27 |
// r is the underlying set of encoded data comprising this index file. |
| 28 |
r io.ReaderAt |
| 29 |
} |
| 30 |
|
| 31 |
// Count returns the number of objects in the packfile. |
| 32 |
func (i *Index) Count() int { |
| 33 |
return int(i.fanout[255]) |
| 34 |
} |
| 35 |
|
| 36 |
// Close closes the packfile index if the underlying data stream is closeable. |
| 37 |
// If so, it returns any error involved in closing. |
| 38 |
func (i *Index) Close() error { |
| 39 |
if close, ok := i.r.(io.Closer); ok { |
| 40 |
return close.Close() |
| 41 |
} |
| 42 |
return nil |
| 43 |
} |
| 44 |
|
| 45 |
var ( |
| 46 |
// errNotFound is an error returned by Index.Entry() (see: below) when |
| 47 |
// an object cannot be found in the index. |
| 48 |
errNotFound = fmt.Errorf("gitobj/pack: object not found in index") |
| 49 |
) |
| 50 |
|
| 51 |
// IsNotFound returns whether a given error represents a missing object in the |
| 52 |
// index. |
| 53 |
func IsNotFound(err error) bool { |
| 54 |
return err == errNotFound |
| 55 |
} |
| 56 |
|
| 57 |
// Entry returns an entry containing the offset of a given SHA1 "name". |
| 58 |
// |
| 59 |
// Entry operates in O(log(n))-time in the worst case, where "n" is the number |
| 60 |
// of objects that begin with the first byte of "name". |
| 61 |
// |
| 62 |
// If the entry cannot be found, (nil, ErrNotFound) will be returned. If there |
| 63 |
// was an error searching for or parsing an entry, it will be returned as (nil, |
| 64 |
// err). |
| 65 |
// |
| 66 |
// Otherwise, (entry, nil) will be returned. |
| 67 |
func (i *Index) Entry(name []byte) (*IndexEntry, error) { |
| 68 |
var last *bounds |
| 69 |
bounds := i.bounds(name) |
| 70 |
|
| 71 |
for bounds.Left() < bounds.Right() { |
| 72 |
if last.Equal(bounds) { |
| 73 |
// If the bounds are unchanged, that means either that |
| 74 |
// the object does not exist in the packfile, or the |
| 75 |
// fanout table is corrupt. |
| 76 |
// |
| 77 |
// Either way, we won't be able to find the object. |
| 78 |
// Return immediately to prevent infinite looping. |
| 79 |
return nil, errNotFound |
| 80 |
} |
| 81 |
last = bounds |
| 82 |
|
| 83 |
// Find the midpoint between the upper and lower bounds. |
| 84 |
mid := bounds.Left() + ((bounds.Right() - bounds.Left()) / 2) |
| 85 |
|
| 86 |
got, err := i.version.Name(i, mid) |
| 87 |
if err != nil { |
| 88 |
return nil, err |
| 89 |
} |
| 90 |
|
| 91 |
if cmp := bytes.Compare(name, got); cmp == 0 { |
| 92 |
// If "cmp" is zero, that means the object at that index |
| 93 |
// "at" had a SHA equal to the one given by name, and we |
| 94 |
// are done. |
| 95 |
return i.version.Entry(i, mid) |
| 96 |
} else if cmp < 0 { |
| 97 |
// If the comparison is less than 0, we searched past |
| 98 |
// the desired object, so limit the upper bound of the |
| 99 |
// search to the midpoint. |
| 100 |
bounds = bounds.WithRight(mid) |
| 101 |
} else if cmp > 0 { |
| 102 |
// Likewise, if the comparison is greater than 0, we |
| 103 |
// searched below the desired object. Modify the bounds |
| 104 |
// accordingly. |
| 105 |
bounds = bounds.WithLeft(mid) |
| 106 |
} |
| 107 |
|
| 108 |
} |
| 109 |
|
| 110 |
return nil, errNotFound |
| 111 |
} |
| 112 |
|
| 113 |
// readAt is a convenience method that allow reading into the underlying data |
| 114 |
// source from other callers within this package. |
| 115 |
func (i *Index) readAt(p []byte, at int64) (n int, err error) { |
| 116 |
return i.r.ReadAt(p, at) |
| 117 |
} |
| 118 |
|
| 119 |
// bounds returns the initial bounds for a given name using the fanout table to |
| 120 |
// limit search results. |
| 121 |
func (i *Index) bounds(name []byte) *bounds { |
| 122 |
var left, right int64 |
| 123 |
|
| 124 |
if name[0] == 0 { |
| 125 |
// If the lower bound is 0, there are no objects before it, |
| 126 |
// start at the beginning of the index file. |
| 127 |
left = 0 |
| 128 |
} else { |
| 129 |
// Otherwise, make the lower bound the slot before the given |
| 130 |
// object. |
| 131 |
left = int64(i.fanout[name[0]-1]) |
| 132 |
} |
| 133 |
|
| 134 |
if name[0] == 255 { |
| 135 |
// As above, if the upper bound is the max byte value, make the |
| 136 |
// upper bound the last object in the list. |
| 137 |
right = int64(i.Count()) |
| 138 |
} else { |
| 139 |
// Otherwise, make the upper bound the first object which is not |
| 140 |
// within the given slot. |
| 141 |
right = int64(i.fanout[name[0]+1]) |
| 142 |
} |
| 143 |
|
| 144 |
return newBounds(left, right) |
| 145 |
} |