Debian Codegraph @

internal/rsyncwire/wire.go

raw symbols SCIP
1 package rsyncwire
2
3 import (
4 "bytes"
5 "encoding/binary"
6 "fmt"
7 "io"
8
9 "github.com/gokrazy/rsync/internal/rsyncos"
10 )
11
12 const (
13 MsgData uint8 = 0
14 MsgInfo uint8 = 2
15 MsgError uint8 = 1
16 )
17
18 const mplexBase = 7
19
20 type MultiplexWriter struct {
21 Writer io.WriteCloser
22 }
23
24 func (w *MultiplexWriter) Write(p []byte) (n int, err error) {
25 return w.WriteMsg(MsgData, p)
26 }
27
28 func (w *MultiplexWriter) WriteMsg(tag uint8, p []byte) (n int, err error) {
29 header := uint32(mplexBase+tag)<<24 | uint32(len(p))
30 // log.Printf("len %d (hex %x)", len(p), uint32(len(p)))
31 // log.Printf("header=%v (%x)", header, header)
32 if err := binary.Write(w.Writer, binary.LittleEndian, header); err != nil {
33 return 0, err
34 }
35 return w.Writer.Write(p)
36 }
37
38 func (w *MultiplexWriter) Close() error { return w.Writer.Close() }
39
40 type MultiplexReader struct {
41 Env *rsyncos.Env
42 Reader io.Reader
43 }
44
45 // rsync.h defines IO_BUFFER_SIZE as 32 * 1024, but gokr-rsyncd increases it to
46 // 256K. Since we use this as the maximum message size, too, we need to at least
47 // match it.
48 const ioBufferSize = 256 * 1024
49 const maxMessageSize = ioBufferSize
50
51 func (w *MultiplexReader) ReadMsg() (tag uint8, p []byte, err error) {
52 var header uint32
53 if err := binary.Read(w.Reader, binary.LittleEndian, &header); err != nil {
54 return 0, nil, err
55 }
56
57 tag = uint8(header>>24) - mplexBase
58 length := header & 0x00FFFFFF
59 if length > maxMessageSize {
60 // NOTE: if you run into this error, one alternative to bumping
61 // maxMessageSize is to restructure the program to work with i/o buffer
62 // windowing.
63 return 0, nil, fmt.Errorf("length %d exceeds max message size (%d)", length, maxMessageSize)
64 }
65 p = make([]byte, int(length))
66 if _, err := io.ReadFull(w.Reader, p); err != nil {
67 return 0, nil, err
68 }
69 // log.Printf("header=%v (%x), tag=%v, length=%v", header, header, tag, length)
70 // log.Printf("payload=%x / %q", p, p)
71 return tag, p, nil
72 }
73
74 func (w *MultiplexReader) Read(p []byte) (n int, err error) {
75 tag, payload, err := w.ReadMsg()
76 if err != nil {
77 return 0, err
78 }
79 switch tag {
80 case MsgError:
81 return 0, fmt.Errorf("%s", payload)
82 case MsgInfo:
83 w.Env.Logf("info: %s", payload)
84 // io.ReadFull will call Read again
85 return 0, nil
86 case MsgData:
87 // continues below
88 default:
89 return 0, fmt.Errorf("unexpected tag: got %v, want %v", tag, MsgData)
90 }
91 if len(p) < len(payload) {
92 panic(fmt.Sprintf("not enough buffer space! %d < %d", len(p), len(payload)))
93 }
94 return copy(p, payload), nil
95 }
96
97 type Buffer struct {
98 // buf.Write() never fails, making for a convenient API.
99 buf bytes.Buffer
100 }
101
102 func (b *Buffer) WriteByte(data byte) {
103 binary.Write(&b.buf, binary.LittleEndian, data)
104 }
105
106 func (b *Buffer) WriteInt32(data int32) {
107 binary.Write(&b.buf, binary.LittleEndian, data)
108 }
109
110 func (b *Buffer) WriteInt64(data int64) {
111 // send as a 32-bit integer if possible
112 if data <= 0x7FFFFFFF && data >= 0 {
113 b.WriteInt32(int32(data))
114 return
115 }
116 // otherwise, send -1 followed by the 64-bit integer
117 b.WriteInt32(-1)
118 binary.Write(&b.buf, binary.LittleEndian, data)
119 }
120
121 func (b *Buffer) WriteString(data string) {
122 io.WriteString(&b.buf, data)
123 }
124
125 func (b *Buffer) String() string {
126 return b.buf.String()
127 }
128
129 func (b *Buffer) Reset() {
130 b.buf.Reset()
131 }
132
133 type Conn struct {
134 Writer io.WriteCloser
135 Reader io.ReadCloser
136 }
137
138 func (c *Conn) Close() error {
139 wcErr := c.Writer.Close()
140 rcErr := c.Reader.Close()
141 if wcErr != nil {
142 return wcErr
143 }
144 return rcErr
145 }
146
147 func (c *Conn) WriteByte(data byte) error {
148 return binary.Write(c.Writer, binary.LittleEndian, data)
149 }
150
151 func (c *Conn) WriteInt32(data int32) error {
152 return binary.Write(c.Writer, binary.LittleEndian, data)
153 }
154
155 func (c *Conn) WriteInt64(data int64) error {
156 // send as a 32-bit integer if possible
157 if data <= 0x7FFFFFFF && data >= 0 {
158 return c.WriteInt32(int32(data))
159 }
160 // otherwise, send -1 followed by the 64-bit integer
161 if err := c.WriteInt32(-1); err != nil {
162 return err
163 }
164 return binary.Write(c.Writer, binary.LittleEndian, data)
165 }
166
167 func (c *Conn) WriteString(data string) error {
168 _, err := io.WriteString(c.Writer, data)
169 return err
170 }
171
172 func (c *Conn) ReadByte() (byte, error) {
173 var buf [1]byte
174 if _, err := io.ReadFull(c.Reader, buf[:]); err != nil {
175 return 0, err
176 }
177 return buf[0], nil
178 }
179
180 func (c *Conn) ReadInt32() (int32, error) {
181 var buf [4]byte
182 if _, err := io.ReadFull(c.Reader, buf[:]); err != nil {
183 return 0, err
184 }
185 return int32(binary.LittleEndian.Uint32(buf[:])), nil
186 }
187
188 func (c *Conn) ReadInt64() (int64, error) {
189 {
190 data, err := c.ReadInt32()
191 if err != nil {
192 return 0, err
193 }
194 if data != -1 {
195 // The value was small enough to fit into a 32 bit int, so it was
196 // transferred directly.
197 return int64(data), nil
198 }
199 // Otherwise, -1 was transmitted, followed by the int64.
200 }
201 var data int64
202 if err := binary.Read(c.Reader, binary.LittleEndian, &data); err != nil {
203 return 0, err
204 }
205 return data, nil
206 }
207
208 type CountingReader struct {
209 R io.ReadCloser
210 BytesRead int64
211 }
212
213 func (r *CountingReader) Read(p []byte) (n int, err error) {
214 n, err = r.R.Read(p)
215 r.BytesRead += int64(n)
216 return n, err
217 }
218
219 func (r *CountingReader) Close() error { return r.R.Close() }
220
221 type CountingWriter struct {
222 W io.WriteCloser
223 BytesWritten int64
224 }
225
226 func (w *CountingWriter) Write(p []byte) (n int, err error) {
227 n, err = w.W.Write(p)
228 w.BytesWritten += int64(n)
229 return n, err
230 }
231
232 func (w *CountingWriter) Close() error { return w.W.Close() }
233
234 func CounterPair(r io.ReadCloser, w io.WriteCloser) (*CountingReader, *CountingWriter) {
235 crd := &CountingReader{R: r}
236 cwr := &CountingWriter{W: w}
237 return crd, cwr
238 }