forked from canonical/lxd
/
rsync.go
231 lines (198 loc) · 5.28 KB
/
rsync.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package main
import (
"fmt"
"io"
"io/ioutil"
"net"
"os"
"os/exec"
"syscall"
"github.com/gorilla/websocket"
"github.com/pborman/uuid"
"github.com/lxc/lxd/shared"
"github.com/lxc/lxd/shared/logger"
)
// rsyncCopy copies a directory using rsync (with the --devices option).
func rsyncLocalCopy(source string, dest string, bwlimit string) (string, error) {
err := os.MkdirAll(dest, 0755)
if err != nil {
return "", err
}
rsyncVerbosity := "-q"
if debug {
rsyncVerbosity = "-vi"
}
if bwlimit == "" {
bwlimit = "0"
}
msg, err := shared.RunCommand("rsync",
"-a",
"-HAX",
"--sparse",
"--devices",
"--delete",
"--checksum",
"--numeric-ids",
"--bwlimit", bwlimit,
rsyncVerbosity,
shared.AddSlash(source),
dest)
if err != nil {
runError, ok := err.(shared.RunError)
if ok {
exitError, ok := runError.Err.(*exec.ExitError)
if ok {
waitStatus := exitError.Sys().(syscall.WaitStatus)
if waitStatus.ExitStatus() == 24 {
return msg, nil
}
}
}
return msg, err
}
return msg, nil
}
func rsyncSendSetup(name string, path string, bwlimit string, execPath string) (*exec.Cmd, net.Conn, io.ReadCloser, error) {
/*
* The way rsync works, it invokes a subprocess that does the actual
* talking (given to it by a -E argument). Since there isn't an easy
* way for us to capture this process' stdin/stdout, we just use netcat
* and write to/from a unix socket.
*
* In principle we don't need this socket. It seems to me that some
* clever invocation of rsync --server --sender and usage of that
* process' stdin/stdout could work around the need for this socket,
* but I couldn't get it to work. Another option would be to look at
* the spawned process' first child and read/write from its
* stdin/stdout, but that also seemed messy. In any case, this seems to
* work just fine.
*/
auds := fmt.Sprintf("@lxd/%s", uuid.NewRandom().String())
// We simply copy a part of the uuid if it's longer than the allowed
// maximum. That should be safe enough for our purposes.
if len(auds) > shared.ABSTRACT_UNIX_SOCK_LEN-1 {
auds = auds[:shared.ABSTRACT_UNIX_SOCK_LEN-1]
}
l, err := net.Listen("unix", auds)
if err != nil {
return nil, nil, nil, err
}
/*
* Here, the path /tmp/foo is ignored. Since we specify localhost,
* rsync thinks we are syncing to a remote host (in this case, the
* other end of the lxd websocket), and so the path specified on the
* --server instance of rsync takes precedence.
*
* Additionally, we use sh -c instead of just calling nc directly
* because rsync passes a whole bunch of arguments to the wrapper
* command (i.e. the command to run on --server). However, we're
* hardcoding that at the other end, so we can just ignore it.
*/
rsyncCmd := fmt.Sprintf("sh -c \"%s netcat %s %s\"", execPath, auds, name)
if bwlimit == "" {
bwlimit = "0"
}
cmd := exec.Command("rsync",
"-arvP",
"--devices",
"--numeric-ids",
"--partial",
"--sparse",
path,
"localhost:/tmp/foo",
"-e",
rsyncCmd,
"--bwlimit",
bwlimit)
stderr, err := cmd.StderrPipe()
if err != nil {
return nil, nil, nil, err
}
if err := cmd.Start(); err != nil {
return nil, nil, nil, err
}
conn, err := l.Accept()
if err != nil {
cmd.Process.Kill()
cmd.Wait()
return nil, nil, nil, err
}
l.Close()
return cmd, conn, stderr, nil
}
// RsyncSend sets up the sending half of an rsync, to recursively send the
// directory pointed to by path over the websocket.
func RsyncSend(name string, path string, conn *websocket.Conn, readWrapper func(io.ReadCloser) io.ReadCloser, bwlimit string, execPath string) error {
cmd, dataSocket, stderr, err := rsyncSendSetup(name, path, bwlimit, execPath)
if err != nil {
return err
}
if dataSocket != nil {
defer dataSocket.Close()
}
readPipe := io.ReadCloser(dataSocket)
if readWrapper != nil {
readPipe = readWrapper(dataSocket)
}
readDone, writeDone := shared.WebsocketMirror(conn, dataSocket, readPipe, nil, nil)
output, err := ioutil.ReadAll(stderr)
if err != nil {
cmd.Process.Kill()
cmd.Wait()
return err
}
err = cmd.Wait()
if err != nil {
logger.Errorf("Rsync send failed: %s: %s: %s", path, err, string(output))
}
<-readDone
<-writeDone
return err
}
// RsyncRecv sets up the receiving half of the websocket to rsync (the other
// half set up by RsyncSend), putting the contents in the directory specified
// by path.
func RsyncRecv(path string, conn *websocket.Conn, writeWrapper func(io.WriteCloser) io.WriteCloser) error {
cmd := exec.Command("rsync",
"--server",
"-vlogDtpre.iLsfx",
"--numeric-ids",
"--devices",
"--partial",
"--sparse",
".",
path)
stdin, err := cmd.StdinPipe()
if err != nil {
return err
}
stdout, err := cmd.StdoutPipe()
if err != nil {
return err
}
stderr, err := cmd.StderrPipe()
if err != nil {
return err
}
if err := cmd.Start(); err != nil {
return err
}
writePipe := io.WriteCloser(stdin)
if writeWrapper != nil {
writePipe = writeWrapper(stdin)
}
readDone, writeDone := shared.WebsocketMirror(conn, writePipe, stdout, nil, nil)
output, err := ioutil.ReadAll(stderr)
if err != nil {
cmd.Process.Kill()
cmd.Wait()
return err
}
err = cmd.Wait()
if err != nil {
logger.Errorf("Rsync receive failed: %s: %s: %s", path, err, string(output))
}
<-readDone
<-writeDone
return err
}