-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathtypes.go
86 lines (71 loc) · 1.96 KB
/
types.go
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
package rsync
import (
"fmt"
"github.com/gokrazy/rsync/internal/rsyncwire"
)
// rsync/rsync.h:struct sum_buf
type SumBuf struct {
Offset int64
Len int64
Index int32
Sum1 uint32
Sum2 [16]byte
}
// TODO: remove connection.go:sumHead in favor of this type
type SumHead struct {
// “number of blocks” (openrsync)
// “how many chunks” (rsync)
ChecksumCount int32
// “block length in the file” (openrsync)
// maximum (1 << 29) for older rsync, (1 << 17) for newer
BlockLength int32
// “long checksum length” (openrsync)
ChecksumLength int32
// “terminal (remainder) block length” (openrsync)
// RemainderLength is flength % BlockLength
RemainderLength int32
Sums []SumBuf
}
func (sh *SumHead) ReadFrom(c *rsyncwire.Conn) error {
// TODO(protocol>=30): update maxBlockLen
const maxBlockLen = 1 << 29 // see rsync.h:OLD_MAX_BLOCK_SIZE
var err error
sh.ChecksumCount, err = c.ReadInt32()
if err != nil {
return err
}
if sh.ChecksumCount < 0 {
return fmt.Errorf("invalid checksum count %d", sh.ChecksumCount)
}
sh.BlockLength, err = c.ReadInt32()
if err != nil {
return err
}
if sh.BlockLength < 0 || sh.BlockLength > maxBlockLen {
return fmt.Errorf("invalid block length %d", sh.BlockLength)
}
sh.ChecksumLength, err = c.ReadInt32()
if err != nil {
return err
}
// TODO(protocol>=27): update max sh.ChecksumLength check
if sh.ChecksumLength < 0 || sh.ChecksumLength > 16 {
return fmt.Errorf("invalid checksum length %d", sh.ChecksumLength)
}
sh.RemainderLength, err = c.ReadInt32()
if err != nil {
return err
}
if sh.RemainderLength < 0 || sh.RemainderLength > sh.BlockLength {
return fmt.Errorf("invalid remainder length %d", sh.RemainderLength)
}
return nil
}
func (sh *SumHead) WriteTo(c *rsyncwire.Conn) error {
var buf rsyncwire.Buffer
buf.WriteInt32(sh.ChecksumCount)
buf.WriteInt32(sh.BlockLength)
buf.WriteInt32(sh.ChecksumLength)
buf.WriteInt32(sh.RemainderLength)
return c.WriteString(buf.String())
}