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