Improved the incremental sending of file lists in two ways: (1)
[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
ba2133d6 6 * Copyright (C) 2003-2007 Wayne Davison
0f78b815
WD
7 *
8 * This program is free software; you can redistribute it and/or modify
ba2133d6
WD
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
0f78b815
WD
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
e7c67065
WD
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
0f78b815 20 */
2f03f956
AT
21
22#include "rsync.h"
23
24extern int verbose;
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;
605fed4b 35extern int allowed_lull;
d6631cf3 36extern int protocol_version;
47c11975 37extern int remove_source_files;
53f8519a 38extern int updating_basis_file;
8b115ac8 39extern int make_backups;
8a9cfb24 40extern int do_progress;
53f8519a 41extern int inplace;
a0545709
WD
42extern int batch_fd;
43extern int write_batch;
a3221d2a 44extern struct stats stats;
f3d6d480 45extern struct file_list *cur_flist, *first_flist;
2f03f956 46
be91bd81
WD
47#define FILECNT_LOOKAHEAD 1000
48
0f9c48b1
MP
49/**
50 * @file
51 *
52 * The sender gets checksums from the generator, calculates deltas,
53 * and transmits them to the receiver. The sender process runs on the
54 * machine holding the source files.
55 **/
fc0257c9 56
ce8149b6
MP
57/**
58 * Receive the checksums for a buffer
59 **/
2f03f956
AT
60static struct sum_struct *receive_sums(int f)
61{
62 struct sum_struct *s;
a1cbe76e 63 int32 i;
605fed4b 64 int lull_mod = allowed_lull * 5;
2f03f956
AT
65 OFF_T offset = 0;
66
a3221d2a
WD
67 if (!(s = new(struct sum_struct)))
68 out_of_memory("receive_sums");
2f03f956 69
fc0257c9
S
70 read_sum_head(f, s);
71
2f03f956
AT
72 s->sums = NULL;
73
da9d12f5 74 if (verbose > 3) {
6c495e0d
WD
75 rprintf(FINFO, "count=%.0f n=%ld rem=%ld\n",
76 (double)s->count, (long)s->blength, (long)s->remainder);
da9d12f5 77 }
2f03f956 78
f3d6d480 79 if (append_mode > 0) {
6cc11982
WD
80 s->flength = (OFF_T)s->count * s->blength;
81 if (s->remainder)
82 s->flength -= s->blength - s->remainder;
83 return s;
84 }
85
e6e3f12f 86 if (s->count == 0)
2f03f956
AT
87 return(s);
88
a3221d2a
WD
89 if (!(s->sums = new_array(struct sum_buf, s->count)))
90 out_of_memory("receive_sums");
2f03f956 91
eed47b6b 92 for (i = 0; i < s->count; i++) {
2f03f956 93 s->sums[i].sum1 = read_int(f);
e6e3f12f 94 read_buf(f, s->sums[i].sum2, s->s2length);
2f03f956
AT
95
96 s->sums[i].offset = offset;
a3221d2a 97 s->sums[i].flags = 0;
2f03f956 98
eed47b6b 99 if (i == s->count-1 && s->remainder != 0)
2f03f956 100 s->sums[i].len = s->remainder;
a3221d2a 101 else
fc0257c9 102 s->sums[i].len = s->blength;
2f03f956
AT
103 offset += s->sums[i].len;
104
605fed4b
WD
105 if (allowed_lull && !(i % lull_mod))
106 maybe_send_keepalive();
107
a3221d2a
WD
108 if (verbose > 3) {
109 rprintf(FINFO,
110 "chunk[%d] len=%d offset=%.0f sum1=%08x\n",
111 i, s->sums[i].len, (double)s->sums[i].offset,
112 s->sums[i].sum1);
113 }
2f03f956
AT
114 }
115
116 s->flength = offset;
117
118 return s;
119}
120
5f40615c 121void successful_send(int ndx)
95306d9d
WD
122{
123 char fname[MAXPATHLEN];
124 struct file_struct *file;
f3d6d480 125 struct file_list *flist;
95306d9d 126
f3d6d480 127 if (!remove_source_files)
95306d9d
WD
128 return;
129
f3d6d480
WD
130 if (!(flist = flist_for_ndx(ndx))) {
131 rprintf(FERROR,
132 "INTERNAL ERROR: unable to find flist for item %d\n",
133 ndx);
134 return;
acee1ad8 135 }
f3d6d480
WD
136
137 file = flist->files[ndx - flist->ndx_start];
138 if (!push_flist_dir(F_ROOTDIR(file), -1))
139 return;
140 f_name(file, fname);
141
142 if (do_unlink(fname) == 0) {
143 if (verbose > 1)
144 rprintf(FINFO, "sender removed %s\n", fname);
145 } else
146 rsyserr(FERROR, errno, "sender failed to remove %s", fname);
95306d9d 147}
2f03f956 148
d144e43b
WD
149void write_ndx_and_attrs(int f_out, int ndx, int iflags,
150 uchar fnamecmp_type, char *buf, int len)
151{
9ae7a2cd 152 write_ndx(f_out, ndx);
d144e43b
WD
153 if (protocol_version < 29)
154 return;
155 write_shortint(f_out, iflags);
156 if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
157 write_byte(f_out, fnamecmp_type);
158 if (iflags & ITEM_XNAME_FOLLOWS)
159 write_vstring(f_out, buf, len);
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;
1b7c47cb 174 struct stats initial_stats;
2fedf3d5
WD
175 int itemizing = am_server ? logfile_format_has_i : stdout_format_has_i;
176 enum logcode log_code = log_before_transfer ? FLOG : FINFO;
a0545709 177 int f_xfer = write_batch < 0 ? batch_fd : f_out;
f3d6d480 178 int ndx, j;
2f03f956
AT
179
180 if (verbose > 2)
e6e3f12f 181 rprintf(FINFO, "send_files starting\n");
2f03f956 182
2f03f956 183 while (1) {
3ea6e0e7 184 if (inc_recurse)
be91bd81 185 send_extra_file_list(f_out, FILECNT_LOOKAHEAD);
f3d6d480
WD
186
187 /* This call also sets cur_flist. */
188 ndx = read_ndx_and_attrs(f_in, f_out, &iflags,
189 &fnamecmp_type, xname, &xlen);
190 if (ndx == NDX_DONE) {
3ea6e0e7 191 if (inc_recurse && first_flist) {
f3d6d480
WD
192 flist_free(first_flist);
193 if (first_flist) {
9ae7a2cd 194 write_ndx(f_out, NDX_DONE);
f3d6d480
WD
195 continue;
196 }
197 }
11290705 198 if (++phase > max_phase)
8b48bf11 199 break;
8b48bf11
WD
200 if (verbose > 2)
201 rprintf(FINFO, "send_files phase=%d\n", phase);
9ae7a2cd 202 write_ndx(f_out, NDX_DONE);
8b48bf11 203 continue;
2f03f956
AT
204 }
205
be91bd81
WD
206 if (inc_recurse)
207 send_extra_file_list(f_out, FILECNT_LOOKAHEAD);
208
f3d6d480 209 file = cur_flist->files[ndx - cur_flist->ndx_start];
dbd9ea3e 210 if (F_ROOTDIR(file)) {
f3d6d480
WD
211 path = F_ROOTDIR(file);
212 slash = "/";
213 } else {
214 path = slash = "";
215 }
216 if (!push_flist_dir(F_ROOTDIR(file), -1))
217 continue;
218 f_name(file, fname);
7f37167d
WD
219
220 if (verbose > 2)
f3d6d480 221 rprintf(FINFO, "send_files(%d, %s%s%s)\n", ndx, path,slash,fname);
8a9cfb24 222
fad3dc42 223 if (!(iflags & ITEM_TRANSFER)) {
6087ef2a 224 maybe_log_item(file, iflags, itemizing, xname);
165e6d44
WD
225 continue;
226 }
11290705
WD
227 if (phase == 2) {
228 rprintf(FERROR,
229 "got transfer request in phase 2 [%s]\n",
230 who_am_i());
231 exit_cleanup(RERR_PROTOCOL);
232 }
8a9cfb24 233
f3d6d480
WD
234 if (file->flags & FLAG_FILE_SENT) {
235 if (csum_length == SHORT_SUM_LENGTH) {
236 /* For inplace: redo phase turns off the backup
237 * flag so that we do a regular inplace send. */
238 make_backups = -make_backups;
239 append_mode = -append_mode;
240 csum_length = SUM_LENGTH;
241 }
242 } else {
243 if (csum_length != SHORT_SUM_LENGTH) {
244 make_backups = -make_backups;
245 append_mode = -append_mode;
246 csum_length = SHORT_SUM_LENGTH;
247 }
248 }
249
6087ef2a 250 updating_basis_file = inplace && (protocol_version >= 29
f3d6d480 251 ? fnamecmp_type == FNAMECMP_FNAME : make_backups <= 0);
b951e023 252
f3d6d480 253 stats.current_file_index = ndx;
2f03f956 254 stats.num_transferred_files++;
112d728f 255 stats.total_transferred_size += F_LENGTH(file);
2f03f956 256
a0545709 257 if (!do_xfers) { /* log the transfer */
85909931 258 log_item(FCLIENT, file, &stats, iflags, NULL);
f3d6d480 259 write_ndx_and_attrs(f_out, ndx, iflags, fnamecmp_type,
927c8068 260 xname, xlen);
2f03f956
AT
261 continue;
262 }
263
1b7c47cb
AT
264 initial_stats = stats;
265
efa95a18 266 if (!(s = receive_sums(f_in))) {
06c28400 267 io_error |= IOERR_GENERAL;
e6e3f12f 268 rprintf(FERROR, "receive_sums failed\n");
f3d6d480 269 exit_cleanup(RERR_PROTOCOL);
2f03f956 270 }
76f79ba7 271
b9f592fb
WD
272 fd = do_open(fname, O_RDONLY, 0);
273 if (fd == -1) {
274 if (errno == ENOENT) {
275 enum logcode c = am_daemon
276 && protocol_version < 28 ? FERROR
277 : FINFO;
278 io_error |= IOERR_VANISHED;
279 rprintf(c, "file has vanished: %s\n",
280 full_fname(fname));
281 } else {
06c28400 282 io_error |= IOERR_GENERAL;
b9f592fb
WD
283 rsyserr(FERROR, errno,
284 "send_files failed to open %s",
285 full_fname(fname));
41cc97ae 286 }
b9f592fb 287 free_sums(s);
f3d6d480
WD
288 if (protocol_version >= 30)
289 send_msg_int(MSG_NO_SEND, ndx);
b9f592fb
WD
290 continue;
291 }
e6e3f12f 292
b9f592fb
WD
293 /* map the local file */
294 if (do_fstat(fd, &st) != 0) {
295 io_error |= IOERR_GENERAL;
296 rsyserr(FERROR, errno, "fstat failed");
297 free_sums(s);
298 close(fd);
f3d6d480 299 exit_cleanup(RERR_PROTOCOL);
b9f592fb 300 }
11a5a3c7 301
96d910c7 302 if (st.st_size) {
9b919d59
WD
303 int32 read_size = MAX(s->blength * 3, MAX_MAP_SIZE);
304 mbuf = map_file(fd, st.st_size, read_size, s->blength);
96d910c7
WD
305 } else
306 mbuf = NULL;
6902ed17 307
b9f592fb 308 if (verbose > 2) {
f3d6d480
WD
309 rprintf(FINFO, "send_files mapped %s%s%s of size %.0f\n",
310 path,slash,fname, (double)st.st_size);
6902ed17 311 }
e6e3f12f 312
f3d6d480 313 write_ndx_and_attrs(f_out, ndx, iflags, fnamecmp_type,
927c8068 314 xname, xlen);
a0545709 315 write_sum_head(f_xfer, s);
b9f592fb 316
45c49b52 317 if (verbose > 2)
f3d6d480 318 rprintf(FINFO, "calling match_sums %s%s%s\n", path,slash,fname);
83fff1aa 319
8a9cfb24 320 if (log_before_transfer)
85909931 321 log_item(FCLIENT, file, &initial_stats, iflags, NULL);
8a832465 322 else if (!am_server && verbose && do_progress)
f3d6d480 323 rprintf(FCLIENT, "%s\n", fname);
ecc81fce 324
83fff1aa 325 set_compression(fname);
1b7c47cb 326
a0545709 327 match_sums(f_xfer, s, mbuf, st.st_size);
0394e34a
WD
328 if (do_progress)
329 end_progress(st.st_size);
330
2fedf3d5 331 log_item(log_code, file, &initial_stats, iflags, NULL);
6902ed17 332
b9f592fb
WD
333 if (mbuf) {
334 j = unmap_file(mbuf);
335 if (j) {
336 io_error |= IOERR_GENERAL;
337 rsyserr(FERROR, j,
338 "read errors mapping %s",
339 full_fname(fname));
6a7cc46c 340 }
6902ed17 341 }
b9f592fb 342 close(fd);
e6e3f12f 343
2f03f956 344 free_sums(s);
e6e3f12f 345
45c49b52 346 if (verbose > 2)
f3d6d480 347 rprintf(FINFO, "sender finished %s%s%s\n", path,slash,fname);
95306d9d
WD
348
349 /* Flag that we actually sent this entry. */
f3d6d480 350 file->flags |= FLAG_FILE_SENT;
2f03f956 351 }
f3d6d480
WD
352 if (make_backups < 0)
353 make_backups = -make_backups;
2f03f956
AT
354
355 if (verbose > 2)
e6e3f12f 356 rprintf(FINFO, "send files finished\n");
2f03f956
AT
357
358 match_report();
359
9ae7a2cd 360 write_ndx(f_out, NDX_DONE);
2f03f956 361}