If we get an ndx that is 1 entry prior to an incremental flist's
[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
4fd842f9 9 * it under the terms of the GNU General Public License version 3 as
ba2133d6 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 17 * You should have received a copy of the GNU General Public License along
4fd842f9 18 * with this program; if not, visit the http://fsf.org website.
0f78b815 19 */
2f03f956
AT
20
21#include "rsync.h"
22
23extern int verbose;
16edf865 24extern int dry_run;
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;
16edf865 36extern int preserve_xattrs;
d6631cf3 37extern int protocol_version;
47c11975 38extern int remove_source_files;
53f8519a 39extern int updating_basis_file;
8b115ac8 40extern int make_backups;
8a9cfb24 41extern int do_progress;
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
0f9c48b1
MP
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 **/
fc0257c9 55
ce8149b6
MP
56/**
57 * Receive the checksums for a buffer
58 **/
2f03f956
AT
59static struct sum_struct *receive_sums(int f)
60{
61 struct sum_struct *s;
a1cbe76e 62 int32 i;
605fed4b 63 int lull_mod = allowed_lull * 5;
2f03f956
AT
64 OFF_T offset = 0;
65
a3221d2a
WD
66 if (!(s = new(struct sum_struct)))
67 out_of_memory("receive_sums");
2f03f956 68
fc0257c9
S
69 read_sum_head(f, s);
70
2f03f956
AT
71 s->sums = NULL;
72
da9d12f5 73 if (verbose > 3) {
6c495e0d
WD
74 rprintf(FINFO, "count=%.0f n=%ld rem=%ld\n",
75 (double)s->count, (long)s->blength, (long)s->remainder);
da9d12f5 76 }
2f03f956 77
f3d6d480 78 if (append_mode > 0) {
6cc11982
WD
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
e6e3f12f 85 if (s->count == 0)
2f03f956
AT
86 return(s);
87
a3221d2a
WD
88 if (!(s->sums = new_array(struct sum_buf, s->count)))
89 out_of_memory("receive_sums");
2f03f956 90
eed47b6b 91 for (i = 0; i < s->count; i++) {
2f03f956 92 s->sums[i].sum1 = read_int(f);
e6e3f12f 93 read_buf(f, s->sums[i].sum2, s->s2length);
2f03f956
AT
94
95 s->sums[i].offset = offset;
a3221d2a 96 s->sums[i].flags = 0;
2f03f956 97
eed47b6b 98 if (i == s->count-1 && s->remainder != 0)
2f03f956 99 s->sums[i].len = s->remainder;
a3221d2a 100 else
fc0257c9 101 s->sums[i].len = s->blength;
2f03f956
AT
102 offset += s->sums[i].len;
103
605fed4b
WD
104 if (allowed_lull && !(i % lull_mod))
105 maybe_send_keepalive();
106
a3221d2a
WD
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 }
2f03f956
AT
113 }
114
115 s->flength = offset;
116
117 return s;
118}
119
5f40615c 120void successful_send(int ndx)
95306d9d
WD
121{
122 char fname[MAXPATHLEN];
123 struct file_struct *file;
f3d6d480 124 struct file_list *flist;
95306d9d 125
f3d6d480 126 if (!remove_source_files)
95306d9d
WD
127 return;
128
f3d6d480
WD
129 if (!(flist = flist_for_ndx(ndx))) {
130 rprintf(FERROR,
131 "INTERNAL ERROR: unable to find flist for item %d\n",
132 ndx);
133 return;
acee1ad8 134 }
f3d6d480
WD
135
136 file = flist->files[ndx - flist->ndx_start];
8fc4033e 137 if (!push_pathname(F_PATHNAME(file), -1))
f3d6d480
WD
138 return;
139 f_name(file, fname);
140
141 if (do_unlink(fname) == 0) {
142 if (verbose > 1)
143 rprintf(FINFO, "sender removed %s\n", fname);
144 } else
145 rsyserr(FERROR, errno, "sender failed to remove %s", fname);
95306d9d 146}
2f03f956 147
16edf865
WD
148static void write_ndx_and_attrs(int f_out, int ndx, int iflags,
149 const char *fname, struct file_struct *file,
150 uchar fnamecmp_type, char *buf, int len)
d144e43b 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);
16edf865
WD
160#ifdef SUPPORT_XATTRS
161 if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && !dry_run)
162 send_xattr_request(fname, file, f_out);
163#endif
d144e43b
WD
164}
165
f3d6d480 166void send_files(int f_in, int f_out)
e6e3f12f 167{
c1659c79 168 int fd = -1;
2f03f956 169 struct sum_struct *s;
41cc97ae 170 struct map_struct *mbuf = NULL;
2f03f956 171 STRUCT_STAT st;
f3d6d480
WD
172 char fname[MAXPATHLEN], xname[MAXPATHLEN];
173 const char *path, *slash;
4d53c4dd 174 uchar fnamecmp_type;
6087ef2a 175 int iflags, xlen;
2f03f956 176 struct file_struct *file;
11290705 177 int phase = 0, max_phase = protocol_version >= 29 ? 2 : 1;
1b7c47cb 178 struct stats initial_stats;
2fedf3d5
WD
179 int itemizing = am_server ? logfile_format_has_i : stdout_format_has_i;
180 enum logcode log_code = log_before_transfer ? FLOG : FINFO;
a0545709 181 int f_xfer = write_batch < 0 ? batch_fd : f_out;
f3d6d480 182 int ndx, j;
2f03f956
AT
183
184 if (verbose > 2)
e6e3f12f 185 rprintf(FINFO, "send_files starting\n");
2f03f956 186
2f03f956 187 while (1) {
3ea6e0e7 188 if (inc_recurse)
be91bd81 189 send_extra_file_list(f_out, FILECNT_LOOKAHEAD);
f3d6d480
WD
190
191 /* This call also sets cur_flist. */
16edf865
WD
192 ndx = read_ndx_and_attrs(f_in, &iflags, &fnamecmp_type,
193 xname, &xlen);
f3d6d480 194 if (ndx == NDX_DONE) {
3ea6e0e7 195 if (inc_recurse && first_flist) {
f3d6d480
WD
196 flist_free(first_flist);
197 if (first_flist) {
9ae7a2cd 198 write_ndx(f_out, NDX_DONE);
f3d6d480
WD
199 continue;
200 }
201 }
11290705 202 if (++phase > max_phase)
8b48bf11 203 break;
8b48bf11
WD
204 if (verbose > 2)
205 rprintf(FINFO, "send_files phase=%d\n", phase);
9ae7a2cd 206 write_ndx(f_out, NDX_DONE);
8b48bf11 207 continue;
2f03f956
AT
208 }
209
16edf865
WD
210 if (inc_recurse)
211 send_extra_file_list(f_out, FILECNT_LOOKAHEAD);
212
6755a7d7
WD
213 if (ndx - cur_flist->ndx_start >= 0)
214 file = cur_flist->files[ndx - cur_flist->ndx_start];
215 else
216 file = dir_flist->files[cur_flist->parent_ndx];
8fc4033e
WD
217 if (F_PATHNAME(file)) {
218 path = F_PATHNAME(file);
f3d6d480
WD
219 slash = "/";
220 } else {
221 path = slash = "";
222 }
8fc4033e 223 if (!push_pathname(F_PATHNAME(file), -1))
f3d6d480
WD
224 continue;
225 f_name(file, fname);
7f37167d
WD
226
227 if (verbose > 2)
f3d6d480 228 rprintf(FINFO, "send_files(%d, %s%s%s)\n", ndx, path,slash,fname);
8a9cfb24 229
16edf865
WD
230#ifdef SUPPORT_XATTRS
231 if (preserve_xattrs && iflags & ITEM_REPORT_XATTR)
232 recv_xattr_request(file, f_in);
233#endif
234
fad3dc42 235 if (!(iflags & ITEM_TRANSFER)) {
6087ef2a 236 maybe_log_item(file, iflags, itemizing, xname);
16edf865
WD
237 write_ndx_and_attrs(f_out, ndx, iflags, fname, file,
238 fnamecmp_type, xname, xlen);
165e6d44
WD
239 continue;
240 }
11290705
WD
241 if (phase == 2) {
242 rprintf(FERROR,
243 "got transfer request in phase 2 [%s]\n",
244 who_am_i());
245 exit_cleanup(RERR_PROTOCOL);
246 }
8a9cfb24 247
f3d6d480
WD
248 if (file->flags & FLAG_FILE_SENT) {
249 if (csum_length == SHORT_SUM_LENGTH) {
250 /* For inplace: redo phase turns off the backup
251 * flag so that we do a regular inplace send. */
252 make_backups = -make_backups;
253 append_mode = -append_mode;
254 csum_length = SUM_LENGTH;
255 }
256 } else {
257 if (csum_length != SHORT_SUM_LENGTH) {
258 make_backups = -make_backups;
259 append_mode = -append_mode;
260 csum_length = SHORT_SUM_LENGTH;
261 }
262 }
263
6087ef2a 264 updating_basis_file = inplace && (protocol_version >= 29
f3d6d480 265 ? fnamecmp_type == FNAMECMP_FNAME : make_backups <= 0);
b951e023 266
f3d6d480 267 stats.current_file_index = ndx;
2f03f956 268 stats.num_transferred_files++;
112d728f 269 stats.total_transferred_size += F_LENGTH(file);
2f03f956 270
a0545709 271 if (!do_xfers) { /* log the transfer */
85909931 272 log_item(FCLIENT, file, &stats, iflags, NULL);
16edf865
WD
273 write_ndx_and_attrs(f_out, ndx, iflags, fname, file,
274 fnamecmp_type, xname, xlen);
2f03f956
AT
275 continue;
276 }
277
1b7c47cb
AT
278 initial_stats = stats;
279
efa95a18 280 if (!(s = receive_sums(f_in))) {
06c28400 281 io_error |= IOERR_GENERAL;
e6e3f12f 282 rprintf(FERROR, "receive_sums failed\n");
f3d6d480 283 exit_cleanup(RERR_PROTOCOL);
2f03f956 284 }
76f79ba7 285
b9f592fb
WD
286 fd = do_open(fname, O_RDONLY, 0);
287 if (fd == -1) {
288 if (errno == ENOENT) {
289 enum logcode c = am_daemon
290 && protocol_version < 28 ? FERROR
291 : FINFO;
292 io_error |= IOERR_VANISHED;
293 rprintf(c, "file has vanished: %s\n",
294 full_fname(fname));
295 } else {
06c28400 296 io_error |= IOERR_GENERAL;
b9f592fb
WD
297 rsyserr(FERROR, errno,
298 "send_files failed to open %s",
299 full_fname(fname));
41cc97ae 300 }
b9f592fb 301 free_sums(s);
f3d6d480
WD
302 if (protocol_version >= 30)
303 send_msg_int(MSG_NO_SEND, ndx);
b9f592fb
WD
304 continue;
305 }
e6e3f12f 306
b9f592fb
WD
307 /* map the local file */
308 if (do_fstat(fd, &st) != 0) {
309 io_error |= IOERR_GENERAL;
310 rsyserr(FERROR, errno, "fstat failed");
311 free_sums(s);
312 close(fd);
f3d6d480 313 exit_cleanup(RERR_PROTOCOL);
b9f592fb 314 }
11a5a3c7 315
96d910c7 316 if (st.st_size) {
9b919d59
WD
317 int32 read_size = MAX(s->blength * 3, MAX_MAP_SIZE);
318 mbuf = map_file(fd, st.st_size, read_size, s->blength);
96d910c7
WD
319 } else
320 mbuf = NULL;
6902ed17 321
b9f592fb 322 if (verbose > 2) {
f3d6d480
WD
323 rprintf(FINFO, "send_files mapped %s%s%s of size %.0f\n",
324 path,slash,fname, (double)st.st_size);
6902ed17 325 }
e6e3f12f 326
16edf865
WD
327 write_ndx_and_attrs(f_out, ndx, iflags, fname, file,
328 fnamecmp_type, xname, xlen);
a0545709 329 write_sum_head(f_xfer, s);
b9f592fb 330
45c49b52 331 if (verbose > 2)
f3d6d480 332 rprintf(FINFO, "calling match_sums %s%s%s\n", path,slash,fname);
83fff1aa 333
8a9cfb24 334 if (log_before_transfer)
85909931 335 log_item(FCLIENT, file, &initial_stats, iflags, NULL);
8a832465 336 else if (!am_server && verbose && do_progress)
f3d6d480 337 rprintf(FCLIENT, "%s\n", fname);
ecc81fce 338
83fff1aa 339 set_compression(fname);
1b7c47cb 340
a0545709 341 match_sums(f_xfer, s, mbuf, st.st_size);
0394e34a
WD
342 if (do_progress)
343 end_progress(st.st_size);
344
2fedf3d5 345 log_item(log_code, file, &initial_stats, iflags, NULL);
6902ed17 346
b9f592fb
WD
347 if (mbuf) {
348 j = unmap_file(mbuf);
349 if (j) {
350 io_error |= IOERR_GENERAL;
351 rsyserr(FERROR, j,
352 "read errors mapping %s",
353 full_fname(fname));
6a7cc46c 354 }
6902ed17 355 }
b9f592fb 356 close(fd);
e6e3f12f 357
2f03f956 358 free_sums(s);
e6e3f12f 359
45c49b52 360 if (verbose > 2)
f3d6d480 361 rprintf(FINFO, "sender finished %s%s%s\n", path,slash,fname);
95306d9d
WD
362
363 /* Flag that we actually sent this entry. */
f3d6d480 364 file->flags |= FLAG_FILE_SENT;
2f03f956 365 }
f3d6d480
WD
366 if (make_backups < 0)
367 make_backups = -make_backups;
2f03f956
AT
368
369 if (verbose > 2)
e6e3f12f 370 rprintf(FINFO, "send files finished\n");
2f03f956
AT
371
372 match_report();
373
9ae7a2cd 374 write_ndx(f_out, NDX_DONE);
2f03f956 375}