| 1 |
package sideband |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"fmt" |
| 6 |
"io" |
| 7 |
|
| 8 |
"github.com/go-git/go-git/v5/plumbing/format/pktline" |
| 9 |
) |
| 10 |
|
| 11 |
// ErrMaxPackedExceeded returned by Read, if the maximum packed size is exceeded |
| 12 |
var ErrMaxPackedExceeded = errors.New("max. packed size exceeded") |
| 13 |
|
| 14 |
// Progress where the progress information is stored |
| 15 |
type Progress interface { |
| 16 |
io.Writer |
| 17 |
} |
| 18 |
|
| 19 |
// Demuxer demultiplexes the progress reports and error info interleaved with the |
| 20 |
// packfile itself. |
| 21 |
// |
| 22 |
// A sideband has three different channels the main one, called PackData, contains |
| 23 |
// the packfile data; the ErrorMessage channel, that contains server errors; and |
| 24 |
// the last one, ProgressMessage channel, containing information about the ongoing |
| 25 |
// task happening in the server (optional, can be suppressed sending NoProgress |
| 26 |
// or Quiet capabilities to the server) |
| 27 |
// |
| 28 |
// In order to demultiplex the data stream, method `Read` should be called to |
| 29 |
// retrieve the PackData channel, the incoming data from the ProgressMessage is |
| 30 |
// written at `Progress` (if any), if any message is retrieved from the |
| 31 |
// ErrorMessage channel an error is returned and we can assume that the |
| 32 |
// connection has been closed. |
| 33 |
type Demuxer struct { |
| 34 |
t Type |
| 35 |
r io.Reader |
| 36 |
s *pktline.Scanner |
| 37 |
|
| 38 |
max int |
| 39 |
pending []byte |
| 40 |
|
| 41 |
// Progress is where the progress messages are stored |
| 42 |
Progress Progress |
| 43 |
} |
| 44 |
|
| 45 |
// NewDemuxer returns a new Demuxer for the given t and read from r |
| 46 |
func NewDemuxer(t Type, r io.Reader) *Demuxer { |
| 47 |
max := MaxPackedSize64k |
| 48 |
if t == Sideband { |
| 49 |
max = MaxPackedSize |
| 50 |
} |
| 51 |
|
| 52 |
return &Demuxer{ |
| 53 |
t: t, |
| 54 |
r: r, |
| 55 |
max: max, |
| 56 |
s: pktline.NewScanner(r), |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
// Read reads up to len(p) bytes from the PackData channel into p, an error can |
| 61 |
// be return if an error happens when reading or if a message is sent in the |
| 62 |
// ErrorMessage channel. |
| 63 |
// |
| 64 |
// When a ProgressMessage is read, is not copy to b, instead of this is written |
| 65 |
// to the Progress |
| 66 |
func (d *Demuxer) Read(b []byte) (n int, err error) { |
| 67 |
var read, req int |
| 68 |
|
| 69 |
req = len(b) |
| 70 |
for read < req { |
| 71 |
n, err := d.doRead(b[read:req]) |
| 72 |
read += n |
| 73 |
|
| 74 |
if err != nil { |
| 75 |
return read, err |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
return read, nil |
| 80 |
} |
| 81 |
|
| 82 |
func (d *Demuxer) doRead(b []byte) (int, error) { |
| 83 |
read, err := d.nextPackData() |
| 84 |
size := len(read) |
| 85 |
wanted := len(b) |
| 86 |
|
| 87 |
if size > wanted { |
| 88 |
d.pending = read[wanted:] |
| 89 |
} |
| 90 |
|
| 91 |
if wanted > size { |
| 92 |
wanted = size |
| 93 |
} |
| 94 |
|
| 95 |
size = copy(b, read[:wanted]) |
| 96 |
return size, err |
| 97 |
} |
| 98 |
|
| 99 |
func (d *Demuxer) nextPackData() ([]byte, error) { |
| 100 |
content := d.getPending() |
| 101 |
if len(content) != 0 { |
| 102 |
return content, nil |
| 103 |
} |
| 104 |
|
| 105 |
if !d.s.Scan() { |
| 106 |
if err := d.s.Err(); err != nil { |
| 107 |
return nil, err |
| 108 |
} |
| 109 |
|
| 110 |
return nil, io.EOF |
| 111 |
} |
| 112 |
|
| 113 |
content = d.s.Bytes() |
| 114 |
|
| 115 |
size := len(content) |
| 116 |
if size == 0 { |
| 117 |
return nil, io.EOF |
| 118 |
} else if size > d.max { |
| 119 |
return nil, ErrMaxPackedExceeded |
| 120 |
} |
| 121 |
|
| 122 |
switch Channel(content[0]) { |
| 123 |
case PackData: |
| 124 |
return content[1:], nil |
| 125 |
case ProgressMessage: |
| 126 |
if d.Progress != nil { |
| 127 |
_, err := d.Progress.Write(content[1:]) |
| 128 |
return nil, err |
| 129 |
} |
| 130 |
case ErrorMessage: |
| 131 |
return nil, fmt.Errorf("unexpected error: %s", content[1:]) |
| 132 |
default: |
| 133 |
return nil, fmt.Errorf("unknown channel %s", content) |
| 134 |
} |
| 135 |
|
| 136 |
return nil, nil |
| 137 |
} |
| 138 |
|
| 139 |
func (d *Demuxer) getPending() (b []byte) { |
| 140 |
if len(d.pending) == 0 { |
| 141 |
return nil |
| 142 |
} |
| 143 |
|
| 144 |
content := d.pending |
| 145 |
d.pending = nil |
| 146 |
|
| 147 |
return content |
| 148 |
} |