Fix %b and %c so that they count per-transfer bytes again.
[rsync/rsync.git] / sender.c
CommitLineData
e6e3f12f 1/*
0f78b815
WD
2 * Routines only used by the sending process.
3 *
4 * Copyright (C) 1996 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
b3bf9b9d 6 * Copyright (C) 2003-2009 Wayne Davison
0f78b815
WD
7 *
8 * This program is free software; you can redistribute it and/or modify
8e41b68e
WD
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.
0f78b815
WD
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 *
e7c67065 18 * You should have received a copy of the GNU General Public License along
4fd842f9 19 * with this program; if not, visit the http://fsf.org website.
0f78b815 20 */
2f03f956
AT
21
22#include "rsync.h"
5dd14f0c 23#include "inums.h"
2f03f956 24
a0545709 25extern int do_xfers;
8715db2c
WD
26extern int am_server;
27extern int am_daemon;
3ea6e0e7 28extern int inc_recurse;
8a9cfb24 29extern int log_before_transfer;
2fedf3d5 30extern int stdout_format_has_i;
ecc7623e 31extern int logfile_format_has_i;
2f03f956 32extern int csum_length;
6cc11982 33extern int append_mode;
2f03f956 34extern int io_error;
7a7810aa 35extern int flist_eof;
605fed4b 36extern int allowed_lull;
16edf865 37extern int preserve_xattrs;
d6631cf3 38extern int protocol_version;
47c11975 39extern int remove_source_files;
53f8519a 40extern int updating_basis_file;
8b115ac8 41extern int make_backups;
53f8519a 42extern int inplace;
a0545709
WD
43extern int batch_fd;
44extern int write_batch;
a3221d2a 45extern struct stats stats;
6755a7d7 46extern struct file_list *cur_flist, *first_flist, *dir_flist;
2f03f956 47
7a7810aa
WD
48BOOL extra_flist_sending_enabled;
49
0f9c48b1
MP
50/**
51 * @file
52 *
53 * The sender gets checksums from the generator, calculates deltas,
54 * and transmits them to the receiver. The sender process runs on the
55 * machine holding the source files.
56 **/
fc0257c9 57
ce8149b6
MP
58/**
59 * Receive the checksums for a buffer
60 **/
2f03f956
AT
61static struct sum_struct *receive_sums(int f)
62{
63 struct sum_struct *s;
a1cbe76e 64 int32 i;
605fed4b 65 int lull_mod = allowed_lull * 5;
2f03f956
AT
66 OFF_T offset = 0;
67
a3221d2a
WD
68 if (!(s = new(struct sum_struct)))
69 out_of_memory("receive_sums");
2f03f956 70
fc0257c9
S
71 read_sum_head(f, s);
72
2f03f956
AT
73 s->sums = NULL;
74
5a18b34d 75 if (DEBUG_GTE(DELTASUM, 3)) {
6d56efa6 76 rprintf(FINFO, "count=%s n=%ld rem=%ld\n",
adc2476f 77 big_num(s->count), (long)s->blength, (long)s->remainder);
da9d12f5 78 }
2f03f956 79
f3d6d480 80 if (append_mode > 0) {
6cc11982
WD
81 s->flength = (OFF_T)s->count * s->blength;
82 if (s->remainder)
83 s->flength -= s->blength - s->remainder;
84 return s;
85 }
86
e6e3f12f 87 if (s->count == 0)
2f03f956
AT
88 return(s);
89
a3221d2a
WD
90 if (!(s->sums = new_array(struct sum_buf, s->count)))
91 out_of_memory("receive_sums");
2f03f956 92
eed47b6b 93 for (i = 0; i < s->count; i++) {
2f03f956 94 s->sums[i].sum1 = read_int(f);
e6e3f12f 95 read_buf(f, s->sums[i].sum2, s->s2length);
2f03f956
AT
96
97 s->sums[i].offset = offset;
a3221d2a 98 s->sums[i].flags = 0;
2f03f956 99
eed47b6b 100 if (i == s->count-1 && s->remainder != 0)
2f03f956 101 s->sums[i].len = s->remainder;
a3221d2a 102 else
fc0257c9 103 s->sums[i].len = s->blength;
2f03f956
AT
104 offset += s->sums[i].len;
105
605fed4b
WD
106 if (allowed_lull && !(i % lull_mod))
107 maybe_send_keepalive();
108
5a18b34d 109 if (DEBUG_GTE(DELTASUM, 3)) {
a3221d2a 110 rprintf(FINFO,
6d56efa6 111 "chunk[%d] len=%d offset=%s sum1=%08x\n",
adc2476f 112 i, s->sums[i].len, big_num(s->sums[i].offset),
a3221d2a
WD
113 s->sums[i].sum1);
114 }
2f03f956
AT
115 }
116
117 s->flength = offset;
118
119 return s;
120}
121
5f40615c 122void successful_send(int ndx)
95306d9d
WD
123{
124 char fname[MAXPATHLEN];
125 struct file_struct *file;
f3d6d480 126 struct file_list *flist;
95306d9d 127
f3d6d480 128 if (!remove_source_files)
95306d9d
WD
129 return;
130
e982d591 131 flist = flist_for_ndx(ndx, "successful_send");
f3d6d480 132 file = flist->files[ndx - flist->ndx_start];
29a89172 133 if (!change_pathname(file, NULL, 0))
f3d6d480
WD
134 return;
135 f_name(file, fname);
136
137 if (do_unlink(fname) == 0) {
951e826b 138 if (INFO_GTE(REMOVE, 1))
f3d6d480
WD
139 rprintf(FINFO, "sender removed %s\n", fname);
140 } else
141 rsyserr(FERROR, errno, "sender failed to remove %s", fname);
95306d9d 142}
2f03f956 143
16edf865
WD
144static void write_ndx_and_attrs(int f_out, int ndx, int iflags,
145 const char *fname, struct file_struct *file,
146 uchar fnamecmp_type, char *buf, int len)
d144e43b 147{
9ae7a2cd 148 write_ndx(f_out, ndx);
d144e43b
WD
149 if (protocol_version < 29)
150 return;
151 write_shortint(f_out, iflags);
152 if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
153 write_byte(f_out, fnamecmp_type);
154 if (iflags & ITEM_XNAME_FOLLOWS)
155 write_vstring(f_out, buf, len);
16edf865 156#ifdef SUPPORT_XATTRS
d510e82f 157 if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && do_xfers)
16edf865
WD
158 send_xattr_request(fname, file, f_out);
159#endif
d144e43b
WD
160}
161
f3d6d480 162void send_files(int f_in, int f_out)
e6e3f12f 163{
c1659c79 164 int fd = -1;
2f03f956 165 struct sum_struct *s;
41cc97ae 166 struct map_struct *mbuf = NULL;
2f03f956 167 STRUCT_STAT st;
f3d6d480
WD
168 char fname[MAXPATHLEN], xname[MAXPATHLEN];
169 const char *path, *slash;
4d53c4dd 170 uchar fnamecmp_type;
6087ef2a 171 int iflags, xlen;
2f03f956 172 struct file_struct *file;
11290705 173 int phase = 0, max_phase = protocol_version >= 29 ? 2 : 1;
2fedf3d5
WD
174 int itemizing = am_server ? logfile_format_has_i : stdout_format_has_i;
175 enum logcode log_code = log_before_transfer ? FLOG : FINFO;
a0545709 176 int f_xfer = write_batch < 0 ? batch_fd : f_out;
59924aa8 177 int save_io_error = io_error;
f3d6d480 178 int ndx, j;
2f03f956 179
951e826b 180 if (DEBUG_GTE(SEND, 1))
e6e3f12f 181 rprintf(FINFO, "send_files starting\n");
2f03f956 182
2f03f956 183 while (1) {
7a7810aa 184 if (inc_recurse) {
ce827c3e 185 send_extra_file_list(f_out, MIN_FILECNT_LOOKAHEAD);
7a7810aa
WD
186 extra_flist_sending_enabled = !flist_eof;
187 }
f3d6d480
WD
188
189 /* This call also sets cur_flist. */
20caffd2 190 ndx = read_ndx_and_attrs(f_in, f_out, &iflags, &fnamecmp_type,
16edf865 191 xname, &xlen);
7a7810aa
WD
192 extra_flist_sending_enabled = False;
193
f3d6d480 194 if (ndx == NDX_DONE) {
951e826b
WD
195 if (!am_server && INFO_GTE(PROGRESS, 2) && cur_flist) {
196 set_current_file_index(NULL, 0);
197 end_progress(0);
198 }
3ea6e0e7 199 if (inc_recurse && first_flist) {
f3d6d480
WD
200 flist_free(first_flist);
201 if (first_flist) {
9ae7a2cd 202 write_ndx(f_out, NDX_DONE);
f3d6d480
WD
203 continue;
204 }
205 }
11290705 206 if (++phase > max_phase)
8b48bf11 207 break;
951e826b 208 if (DEBUG_GTE(SEND, 1))
8b48bf11 209 rprintf(FINFO, "send_files phase=%d\n", phase);
9ae7a2cd 210 write_ndx(f_out, NDX_DONE);
8b48bf11 211 continue;
2f03f956
AT
212 }
213
16edf865 214 if (inc_recurse)
ce827c3e 215 send_extra_file_list(f_out, MIN_FILECNT_LOOKAHEAD);
16edf865 216
6755a7d7
WD
217 if (ndx - cur_flist->ndx_start >= 0)
218 file = cur_flist->files[ndx - cur_flist->ndx_start];
219 else
220 file = dir_flist->files[cur_flist->parent_ndx];
8fc4033e
WD
221 if (F_PATHNAME(file)) {
222 path = F_PATHNAME(file);
f3d6d480
WD
223 slash = "/";
224 } else {
225 path = slash = "";
226 }
29a89172 227 if (!change_pathname(file, NULL, 0))
f3d6d480
WD
228 continue;
229 f_name(file, fname);
7f37167d 230
951e826b 231 if (DEBUG_GTE(SEND, 1))
f3d6d480 232 rprintf(FINFO, "send_files(%d, %s%s%s)\n", ndx, path,slash,fname);
8a9cfb24 233
16edf865 234#ifdef SUPPORT_XATTRS
d510e82f 235 if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && do_xfers)
16edf865
WD
236 recv_xattr_request(file, f_in);
237#endif
238
fad3dc42 239 if (!(iflags & ITEM_TRANSFER)) {
6087ef2a 240 maybe_log_item(file, iflags, itemizing, xname);
16edf865
WD
241 write_ndx_and_attrs(f_out, ndx, iflags, fname, file,
242 fnamecmp_type, xname, xlen);
e366e530
WD
243 if (iflags & ITEM_IS_NEW) {
244 stats.created_files++;
245 if (S_ISREG(file->mode)) {
246 /* Nothing further to count. */
247 } else if (S_ISDIR(file->mode))
248 stats.created_dirs++;
249#ifdef SUPPORT_LINKS
250 else if (S_ISLNK(file->mode))
251 stats.created_symlinks++;
252#endif
253 else if (IS_DEVICE(file->mode))
254 stats.created_devices++;
255 else
256 stats.created_specials++;
257 }
165e6d44
WD
258 continue;
259 }
11290705
WD
260 if (phase == 2) {
261 rprintf(FERROR,
262 "got transfer request in phase 2 [%s]\n",
263 who_am_i());
264 exit_cleanup(RERR_PROTOCOL);
265 }
8a9cfb24 266
f3d6d480
WD
267 if (file->flags & FLAG_FILE_SENT) {
268 if (csum_length == SHORT_SUM_LENGTH) {
269 /* For inplace: redo phase turns off the backup
270 * flag so that we do a regular inplace send. */
271 make_backups = -make_backups;
272 append_mode = -append_mode;
273 csum_length = SUM_LENGTH;
274 }
275 } else {
276 if (csum_length != SHORT_SUM_LENGTH) {
277 make_backups = -make_backups;
278 append_mode = -append_mode;
279 csum_length = SHORT_SUM_LENGTH;
280 }
e366e530
WD
281 if (iflags & ITEM_IS_NEW)
282 stats.created_files++;
f3d6d480
WD
283 }
284
6087ef2a 285 updating_basis_file = inplace && (protocol_version >= 29
f3d6d480 286 ? fnamecmp_type == FNAMECMP_FNAME : make_backups <= 0);
b951e023 287
951e826b 288 if (!am_server && INFO_GTE(PROGRESS, 1))
65b4e4b2 289 set_current_file_index(file, ndx);
e366e530 290 stats.xferred_files++;
112d728f 291 stats.total_transferred_size += F_LENGTH(file);
2f03f956 292
4dde3347
WD
293 if (!log_before_transfer)
294 remember_initial_stats();
295
a0545709 296 if (!do_xfers) { /* log the transfer */
4dde3347 297 log_item(FCLIENT, file, iflags, NULL);
16edf865
WD
298 write_ndx_and_attrs(f_out, ndx, iflags, fname, file,
299 fnamecmp_type, xname, xlen);
2f03f956
AT
300 continue;
301 }
302
efa95a18 303 if (!(s = receive_sums(f_in))) {
06c28400 304 io_error |= IOERR_GENERAL;
342bfb5e 305 rprintf(FERROR_XFER, "receive_sums failed\n");
f3d6d480 306 exit_cleanup(RERR_PROTOCOL);
2f03f956 307 }
76f79ba7 308
b9f592fb
WD
309 fd = do_open(fname, O_RDONLY, 0);
310 if (fd == -1) {
311 if (errno == ENOENT) {
312 enum logcode c = am_daemon
313 && protocol_version < 28 ? FERROR
3f0211b6 314 : FWARNING;
b9f592fb
WD
315 io_error |= IOERR_VANISHED;
316 rprintf(c, "file has vanished: %s\n",
317 full_fname(fname));
318 } else {
06c28400 319 io_error |= IOERR_GENERAL;
3f0211b6 320 rsyserr(FERROR_XFER, errno,
b9f592fb
WD
321 "send_files failed to open %s",
322 full_fname(fname));
41cc97ae 323 }
b9f592fb 324 free_sums(s);
f3d6d480
WD
325 if (protocol_version >= 30)
326 send_msg_int(MSG_NO_SEND, ndx);
b9f592fb
WD
327 continue;
328 }
e6e3f12f 329
b9f592fb
WD
330 /* map the local file */
331 if (do_fstat(fd, &st) != 0) {
332 io_error |= IOERR_GENERAL;
342bfb5e 333 rsyserr(FERROR_XFER, errno, "fstat failed");
b9f592fb
WD
334 free_sums(s);
335 close(fd);
f3d6d480 336 exit_cleanup(RERR_PROTOCOL);
b9f592fb 337 }
11a5a3c7 338
96d910c7 339 if (st.st_size) {
9b919d59
WD
340 int32 read_size = MAX(s->blength * 3, MAX_MAP_SIZE);
341 mbuf = map_file(fd, st.st_size, read_size, s->blength);
96d910c7
WD
342 } else
343 mbuf = NULL;
6902ed17 344
5a18b34d 345 if (DEBUG_GTE(DELTASUM, 2)) {
6d56efa6 346 rprintf(FINFO, "send_files mapped %s%s%s of size %s\n",
adc2476f 347 path,slash,fname, big_num(st.st_size));
6902ed17 348 }
e6e3f12f 349
16edf865
WD
350 write_ndx_and_attrs(f_out, ndx, iflags, fname, file,
351 fnamecmp_type, xname, xlen);
a0545709 352 write_sum_head(f_xfer, s);
b9f592fb 353
5a18b34d 354 if (DEBUG_GTE(DELTASUM, 2))
f3d6d480 355 rprintf(FINFO, "calling match_sums %s%s%s\n", path,slash,fname);
83fff1aa 356
8a9cfb24 357 if (log_before_transfer)
4dde3347 358 log_item(FCLIENT, file, iflags, NULL);
951e826b 359 else if (!am_server && INFO_GTE(NAME, 1) && INFO_EQ(PROGRESS, 1))
f3d6d480 360 rprintf(FCLIENT, "%s\n", fname);
ecc81fce 361
83fff1aa 362 set_compression(fname);
1b7c47cb 363
a0545709 364 match_sums(f_xfer, s, mbuf, st.st_size);
951e826b 365 if (INFO_GTE(PROGRESS, 1))
0394e34a
WD
366 end_progress(st.st_size);
367
4dde3347 368 log_item(log_code, file, iflags, NULL);
6902ed17 369
b9f592fb
WD
370 if (mbuf) {
371 j = unmap_file(mbuf);
372 if (j) {
373 io_error |= IOERR_GENERAL;
3f0211b6 374 rsyserr(FERROR_XFER, j,
b9f592fb
WD
375 "read errors mapping %s",
376 full_fname(fname));
6a7cc46c 377 }
6902ed17 378 }
b9f592fb 379 close(fd);
e6e3f12f 380
2f03f956 381 free_sums(s);
e6e3f12f 382
951e826b 383 if (DEBUG_GTE(SEND, 1))
f3d6d480 384 rprintf(FINFO, "sender finished %s%s%s\n", path,slash,fname);
95306d9d
WD
385
386 /* Flag that we actually sent this entry. */
f3d6d480 387 file->flags |= FLAG_FILE_SENT;
2f03f956 388 }
f3d6d480
WD
389 if (make_backups < 0)
390 make_backups = -make_backups;
2f03f956 391
59924aa8
WD
392 if (io_error != save_io_error && protocol_version >= 30)
393 send_msg_int(MSG_IO_ERROR, io_error);
394
951e826b 395 if (DEBUG_GTE(SEND, 1))
e6e3f12f 396 rprintf(FINFO, "send files finished\n");
2f03f956
AT
397
398 match_report();
399
9ae7a2cd 400 write_ndx(f_out, NDX_DONE);
2f03f956 401}