-
Notifications
You must be signed in to change notification settings - Fork 1
/
sync.go
185 lines (155 loc) · 3.24 KB
/
sync.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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package dadb
import (
"encoding/binary"
"errors"
"fmt"
"io"
)
const list = "LIST"
const recv = "RECV"
const send = "SEND"
const stat = "STAT"
const data = "DATA"
const done = "DONE"
const okay = "OKAY"
const quit = "QUIT"
const fail = "FAIL"
type syncPacket struct {
id string
arg uint32
}
type syncStream struct {
s Stream
payload []byte
}
func Push(d Dadb, r io.Reader, remotePath string, mode uint32, lastModifiedSec uint32) error {
remote := fmt.Sprintf("%s,%d", remotePath, mode)
ss, err := openSyncStream(d, send, remote)
if err != nil {
return err
}
defer ss.quit()
// If needed, we can try increasing the buffer size here to improve performance
_, err = io.Copy(ss, r)
if err != nil {
return err
}
err = writeSyncPacket(ss.s, done, lastModifiedSec)
if err != nil {
return err
}
packet, err := readSyncPacket(ss.s)
if err != nil {
return err
}
if packet.id != okay {
return fmt.Errorf("expected okay packet: %s", packet.id)
}
return nil
}
func Pull(dadb Dadb, w io.Writer, remotePath string) error {
ss, err := openSyncStream(dadb, recv, remotePath)
if err != nil {
return err
}
defer ss.quit()
_, err = io.Copy(w, ss)
if err != nil {
return err
}
return nil
}
func (s syncStream) Read(p []byte) (int, error) {
if len(s.payload) == 0 {
packet, err := readSyncPacket(s.s)
if err != nil {
return 0, err
}
switch packet.id {
case done:
return 0, io.EOF
case fail:
bytes, err := readNBytes(s.s, int(packet.arg))
if err != nil {
return 0, err
}
return 0, errors.New(string(bytes))
case data:
bytes, err := readNBytes(s.s, int(packet.arg))
if err != nil {
return 0, err
}
s.payload = bytes
}
}
n := copy(p, s.payload)
s.payload = s.payload[n:]
return n, nil
}
func (s syncStream) Write(p []byte) (int, error) {
err := writeSyncPacket(s.s, data, uint32(len(p)))
if err != nil {
return 0, err
}
return s.s.Write(p)
}
func (s syncStream) quit() {
_ = writeSyncPacket(s.s, quit, 0)
}
func openSyncStream(d Dadb, id string, remote string) (syncStream, error) {
stream, err := d.Open("sync:")
if err != nil {
return syncStream{}, err
}
err = writeSyncPacketWithPayload(stream, id, []byte(remote))
if err != nil {
return syncStream{}, err
}
return syncStream{s: stream}, nil
}
func readSyncPacket(r io.Reader) (syncPacket, error) {
idBytes := make([]byte, 4)
_, err := io.ReadFull(r, idBytes)
if err != nil {
return syncPacket{}, err
}
argBytes, err := readNBytes(r, 4)
if err != nil {
return syncPacket{}, err
}
arg := binary.LittleEndian.Uint32(argBytes)
return syncPacket{
id: string(idBytes),
arg: arg,
}, nil
}
func readNBytes(r io.Reader, n int) ([]byte, error) {
bytes := make([]byte, n)
_, err := r.Read(bytes)
if err != nil {
return nil, err
}
return bytes, nil
}
func writeSyncPacketWithPayload(w io.Writer, id string, payload []byte) error {
err := writeSyncPacket(w, id, uint32(len(payload)))
if err != nil {
return err
}
_, err = w.Write(payload)
if err != nil {
return err
}
return nil
}
func writeSyncPacket(w io.Writer, id string, arg uint32) error {
_, err := io.WriteString(w, id)
if err != nil {
return err
}
err = binary.Write(w, binary.LittleEndian, arg)
if err != nil {
return err
}
return nil
}