My version of Matt's improvements related to missing source args:
[rsync/rsync.git] / sender.c
... / ...
CommitLineData
1/*
2 * Routines only used by the sending process.
3 *
4 * Copyright (C) 1996 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2003-2009 Wayne Davison
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, visit the http://fsf.org website.
20 */
21
22#include "rsync.h"
23
24extern int verbose;
25extern int do_xfers;
26extern int am_server;
27extern int am_daemon;
28extern int inc_recurse;
29extern int log_before_transfer;
30extern int stdout_format_has_i;
31extern int logfile_format_has_i;
32extern int csum_length;
33extern int append_mode;
34extern int io_error;
35extern int allowed_lull;
36extern int preserve_xattrs;
37extern int protocol_version;
38extern int remove_source_files;
39extern int updating_basis_file;
40extern int make_backups;
41extern int do_progress;
42extern int inplace;
43extern int batch_fd;
44extern int write_batch;
45extern struct stats stats;
46extern struct file_list *cur_flist, *first_flist, *dir_flist;
47
48/**
49 * @file
50 *
51 * The sender gets checksums from the generator, calculates deltas,
52 * and transmits them to the receiver. The sender process runs on the
53 * machine holding the source files.
54 **/
55
56/**
57 * Receive the checksums for a buffer
58 **/
59static struct sum_struct *receive_sums(int f)
60{
61 struct sum_struct *s;
62 int32 i;
63 int lull_mod = allowed_lull * 5;
64 OFF_T offset = 0;
65
66 if (!(s = new(struct sum_struct)))
67 out_of_memory("receive_sums");
68
69 read_sum_head(f, s);
70
71 s->sums = NULL;
72
73 if (verbose > 3) {
74 rprintf(FINFO, "count=%.0f n=%ld rem=%ld\n",
75 (double)s->count, (long)s->blength, (long)s->remainder);
76 }
77
78 if (append_mode > 0) {
79 s->flength = (OFF_T)s->count * s->blength;
80 if (s->remainder)
81 s->flength -= s->blength - s->remainder;
82 return s;
83 }
84
85 if (s->count == 0)
86 return(s);
87
88 if (!(s->sums = new_array(struct sum_buf, s->count)))
89 out_of_memory("receive_sums");
90
91 for (i = 0; i < s->count; i++) {
92 s->sums[i].sum1 = read_int(f);
93 read_buf(f, s->sums[i].sum2, s->s2length);
94
95 s->sums[i].offset = offset;
96 s->sums[i].flags = 0;
97
98 if (i == s->count-1 && s->remainder != 0)
99 s->sums[i].len = s->remainder;
100 else
101 s->sums[i].len = s->blength;
102 offset += s->sums[i].len;
103
104 if (allowed_lull && !(i % lull_mod))
105 maybe_send_keepalive();
106
107 if (verbose > 3) {
108 rprintf(FINFO,
109 "chunk[%d] len=%d offset=%.0f sum1=%08x\n",
110 i, s->sums[i].len, (double)s->sums[i].offset,
111 s->sums[i].sum1);
112 }
113 }
114
115 s->flength = offset;
116
117 return s;
118}
119
120void successful_send(int ndx)
121{
122 char fname[MAXPATHLEN];
123 struct file_struct *file;
124 struct file_list *flist;
125
126 if (!remove_source_files)
127 return;
128
129 flist = flist_for_ndx(ndx, "successful_send");
130 file = flist->files[ndx - flist->ndx_start];
131 if (!change_pathname(file, NULL, 0))
132 return;
133 f_name(file, fname);
134
135 if (do_unlink(fname) == 0) {
136 if (verbose > 1)
137 rprintf(FINFO, "sender removed %s\n", fname);
138 } else
139 rsyserr(FERROR, errno, "sender failed to remove %s", fname);
140}
141
142static void write_ndx_and_attrs(int f_out, int ndx, int iflags,
143 const char *fname, struct file_struct *file,
144 uchar fnamecmp_type, char *buf, int len)
145{
146 write_ndx(f_out, ndx);
147 if (protocol_version < 29)
148 return;
149 write_shortint(f_out, iflags);
150 if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
151 write_byte(f_out, fnamecmp_type);
152 if (iflags & ITEM_XNAME_FOLLOWS)
153 write_vstring(f_out, buf, len);
154#ifdef SUPPORT_XATTRS
155 if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && do_xfers)
156 send_xattr_request(fname, file, f_out);
157#endif
158}
159
160void send_files(int f_in, int f_out)
161{
162 int fd = -1;
163 struct sum_struct *s;
164 struct map_struct *mbuf = NULL;
165 STRUCT_STAT st;
166 char fname[MAXPATHLEN], xname[MAXPATHLEN];
167 const char *path, *slash;
168 uchar fnamecmp_type;
169 int iflags, xlen;
170 struct file_struct *file;
171 int phase = 0, max_phase = protocol_version >= 29 ? 2 : 1;
172 struct stats initial_stats;
173 int itemizing = am_server ? logfile_format_has_i : stdout_format_has_i;
174 enum logcode log_code = log_before_transfer ? FLOG : FINFO;
175 int f_xfer = write_batch < 0 ? batch_fd : f_out;
176 int ndx, j;
177
178 if (verbose > 2)
179 rprintf(FINFO, "send_files starting\n");
180
181 while (1) {
182 if (inc_recurse)
183 send_extra_file_list(f_out, FILECNT_LOOKAHEAD);
184
185 /* This call also sets cur_flist. */
186 ndx = read_ndx_and_attrs(f_in, &iflags, &fnamecmp_type,
187 xname, &xlen);
188 if (ndx == NDX_DONE) {
189 if (inc_recurse && first_flist) {
190 flist_free(first_flist);
191 if (first_flist) {
192 write_ndx(f_out, NDX_DONE);
193 continue;
194 }
195 }
196 if (++phase > max_phase)
197 break;
198 if (verbose > 2)
199 rprintf(FINFO, "send_files phase=%d\n", phase);
200 write_ndx(f_out, NDX_DONE);
201 continue;
202 }
203
204 if (inc_recurse)
205 send_extra_file_list(f_out, FILECNT_LOOKAHEAD);
206
207 if (ndx - cur_flist->ndx_start >= 0)
208 file = cur_flist->files[ndx - cur_flist->ndx_start];
209 else
210 file = dir_flist->files[cur_flist->parent_ndx];
211 if (F_PATHNAME(file)) {
212 path = F_PATHNAME(file);
213 slash = "/";
214 } else {
215 path = slash = "";
216 }
217 if (!change_pathname(file, NULL, 0))
218 continue;
219 f_name(file, fname);
220
221 if (verbose > 2)
222 rprintf(FINFO, "send_files(%d, %s%s%s)\n", ndx, path,slash,fname);
223
224#ifdef SUPPORT_XATTRS
225 if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && do_xfers)
226 recv_xattr_request(file, f_in);
227#endif
228
229 if (!(iflags & ITEM_TRANSFER)) {
230 maybe_log_item(file, iflags, itemizing, xname);
231 write_ndx_and_attrs(f_out, ndx, iflags, fname, file,
232 fnamecmp_type, xname, xlen);
233 continue;
234 }
235 if (phase == 2) {
236 rprintf(FERROR,
237 "got transfer request in phase 2 [%s]\n",
238 who_am_i());
239 exit_cleanup(RERR_PROTOCOL);
240 }
241
242 if (file->flags & FLAG_FILE_SENT) {
243 if (csum_length == SHORT_SUM_LENGTH) {
244 /* For inplace: redo phase turns off the backup
245 * flag so that we do a regular inplace send. */
246 make_backups = -make_backups;
247 append_mode = -append_mode;
248 csum_length = SUM_LENGTH;
249 }
250 } else {
251 if (csum_length != SHORT_SUM_LENGTH) {
252 make_backups = -make_backups;
253 append_mode = -append_mode;
254 csum_length = SHORT_SUM_LENGTH;
255 }
256 }
257
258 updating_basis_file = inplace && (protocol_version >= 29
259 ? fnamecmp_type == FNAMECMP_FNAME : make_backups <= 0);
260
261 if (!am_server && do_progress)
262 set_current_file_index(file, ndx);
263 stats.num_transferred_files++;
264 stats.total_transferred_size += F_LENGTH(file);
265
266 if (!do_xfers) { /* log the transfer */
267 log_item(FCLIENT, file, &stats, iflags, NULL);
268 write_ndx_and_attrs(f_out, ndx, iflags, fname, file,
269 fnamecmp_type, xname, xlen);
270 continue;
271 }
272
273 initial_stats = stats;
274
275 if (!(s = receive_sums(f_in))) {
276 io_error |= IOERR_GENERAL;
277 rprintf(FERROR_XFER, "receive_sums failed\n");
278 exit_cleanup(RERR_PROTOCOL);
279 }
280
281 fd = do_open(fname, O_RDONLY, 0);
282 if (fd == -1) {
283 if (errno == ENOENT) {
284 enum logcode c = am_daemon
285 && protocol_version < 28 ? FERROR
286 : FWARNING;
287 io_error |= IOERR_VANISHED;
288 rprintf(c, "file has vanished: %s\n",
289 full_fname(fname));
290 } else {
291 io_error |= IOERR_GENERAL;
292 rsyserr(FERROR_XFER, errno,
293 "send_files failed to open %s",
294 full_fname(fname));
295 }
296 free_sums(s);
297 if (protocol_version >= 30)
298 send_msg_int(MSG_NO_SEND, ndx);
299 continue;
300 }
301
302 /* map the local file */
303 if (do_fstat(fd, &st) != 0) {
304 io_error |= IOERR_GENERAL;
305 rsyserr(FERROR_XFER, errno, "fstat failed");
306 free_sums(s);
307 close(fd);
308 exit_cleanup(RERR_PROTOCOL);
309 }
310
311 if (st.st_size) {
312 int32 read_size = MAX(s->blength * 3, MAX_MAP_SIZE);
313 mbuf = map_file(fd, st.st_size, read_size, s->blength);
314 } else
315 mbuf = NULL;
316
317 if (verbose > 2) {
318 rprintf(FINFO, "send_files mapped %s%s%s of size %.0f\n",
319 path,slash,fname, (double)st.st_size);
320 }
321
322 write_ndx_and_attrs(f_out, ndx, iflags, fname, file,
323 fnamecmp_type, xname, xlen);
324 write_sum_head(f_xfer, s);
325
326 if (verbose > 2)
327 rprintf(FINFO, "calling match_sums %s%s%s\n", path,slash,fname);
328
329 if (log_before_transfer)
330 log_item(FCLIENT, file, &initial_stats, iflags, NULL);
331 else if (!am_server && verbose && do_progress)
332 rprintf(FCLIENT, "%s\n", fname);
333
334 set_compression(fname);
335
336 match_sums(f_xfer, s, mbuf, st.st_size);
337 if (do_progress)
338 end_progress(st.st_size);
339
340 log_item(log_code, file, &initial_stats, iflags, NULL);
341
342 if (mbuf) {
343 j = unmap_file(mbuf);
344 if (j) {
345 io_error |= IOERR_GENERAL;
346 rsyserr(FERROR_XFER, j,
347 "read errors mapping %s",
348 full_fname(fname));
349 }
350 }
351 close(fd);
352
353 free_sums(s);
354
355 if (verbose > 2)
356 rprintf(FINFO, "sender finished %s%s%s\n", path,slash,fname);
357
358 /* Flag that we actually sent this entry. */
359 file->flags |= FLAG_FILE_SENT;
360 }
361 if (make_backups < 0)
362 make_backups = -make_backups;
363
364 if (verbose > 2)
365 rprintf(FINFO, "send files finished\n");
366
367 match_report();
368
369 write_ndx(f_out, NDX_DONE);
370}