File: pkt_line_reader.go

package info (click to toggle)
git-lfs 2.13.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 4,384 kB
  • sloc: sh: 16,421; makefile: 418; ruby: 100
file content (66 lines) | stat: -rw-r--r-- 1,319 bytes parent folder | download
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
package git

import (
	"io"

	"github.com/git-lfs/git-lfs/tools"
)

type pktlineReader struct {
	pl *pktline

	buf []byte

	eof bool
}

var _ io.Reader = new(pktlineReader)

func (r *pktlineReader) Read(p []byte) (int, error) {
	var n int

	if r.eof {
		return 0, io.EOF
	}

	if len(r.buf) > 0 {
		// If there is data in the buffer, shift as much out of it and
		// into the given "p" as we can.
		n = tools.MinInt(len(p), len(r.buf))

		copy(p, r.buf[:n])
		r.buf = r.buf[n:]
	}

	// Loop and grab as many packets as we can in a given "run", until we
	// have either, a) overfilled the given buffer "p", or we have started
	// to internally buffer in "r.buf".
	for len(r.buf) == 0 {
		chunk, err := r.pl.readPacket()
		if err != nil {
			return n, err
		}

		if len(chunk) == 0 {
			// If we got an empty chunk, then we know that we have
			// reached the end of processing for this particular
			// packet, so let's terminate.

			r.eof = true
			return n, io.EOF
		}

		// Figure out how much of the packet we can read into "p".
		nn := tools.MinInt(len(chunk), len(p[n:]))

		// Move that amount into "p", from where we left off.
		copy(p[n:], chunk[:nn])
		// And move the rest into the buffer.
		r.buf = append(r.buf, chunk[nn:]...)

		// Mark that we have read "nn" bytes into "p"
		n += nn
	}

	return n, nil
}