aboutsummaryrefslogtreecommitdiffstats
path: root/plumbing/format/objfile/reader.go
blob: 2bf97624a9e254a0906ca64b05fdca65372de779 (plain) (blame)
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
package objfile

import (
	"compress/zlib"
	"errors"
	"io"
	"strconv"

	"srcd.works/go-git.v4/plumbing"
	"srcd.works/go-git.v4/plumbing/format/packfile"
)

var (
	ErrClosed       = errors.New("objfile: already closed")
	ErrHeader       = errors.New("objfile: invalid header")
	ErrNegativeSize = errors.New("objfile: negative object size")
)

// Reader reads and decodes compressed objfile data from a provided io.Reader.
// Reader implements io.ReadCloser. Close should be called when finished with
// the Reader. Close will not close the underlying io.Reader.
type Reader struct {
	multi  io.Reader
	zlib   io.ReadCloser
	hasher plumbing.Hasher
}

// NewReader returns a new Reader reading from r.
func NewReader(r io.Reader) (*Reader, error) {
	zlib, err := zlib.NewReader(r)
	if err != nil {
		return nil, packfile.ErrZLib.AddDetails(err.Error())
	}

	return &Reader{
		zlib: zlib,
	}, nil
}

// Header reads the type and the size of object, and prepares the reader for read
func (r *Reader) Header() (t plumbing.ObjectType, size int64, err error) {
	var raw []byte
	raw, err = r.readUntil(' ')
	if err != nil {
		return
	}

	t, err = plumbing.ParseObjectType(string(raw))
	if err != nil {
		return
	}

	raw, err = r.readUntil(0)
	if err != nil {
		return
	}

	size, err = strconv.ParseInt(string(raw), 10, 64)
	if err != nil {
		err = ErrHeader
		return
	}

	defer r.prepareForRead(t, size)
	return
}

// readSlice reads one byte at a time from r until it encounters delim or an
// error.
func (r *Reader) readUntil(delim byte) ([]byte, error) {
	var buf [1]byte
	value := make([]byte, 0, 16)
	for {
		if n, err := r.zlib.Read(buf[:]); err != nil && (err != io.EOF || n == 0) {
			if err == io.EOF {
				return nil, ErrHeader
			}
			return nil, err
		}

		if buf[0] == delim {
			return value, nil
		}

		value = append(value, buf[0])
	}
}

func (r *Reader) prepareForRead(t plumbing.ObjectType, size int64) {
	r.hasher = plumbing.NewHasher(t, size)
	r.multi = io.TeeReader(r.zlib, r.hasher)
}

// Read reads len(p) bytes into p from the object data stream. It returns
// the number of bytes read (0 <= n <= len(p)) and any error encountered. Even
// if Read returns n < len(p), it may use all of p as scratch space during the
// call.
//
// If Read encounters the end of the data stream it will return err == io.EOF,
// either in the current call if n > 0 or in a subsequent call.
func (r *Reader) Read(p []byte) (n int, err error) {
	return r.multi.Read(p)
}

// Hash returns the hash of the object data stream that has been read so far.
func (r *Reader) Hash() plumbing.Hash {
	return r.hasher.Sum()
}

// Close releases any resources consumed by the Reader. Calling Close does not
// close the wrapped io.Reader originally passed to NewReader.
func (r *Reader) Close() error {
	if err := r.zlib.Close(); err != nil {
		return err
	}

	return nil
}