Mention the mapfrom/mapto scripts and how they work.
[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
d3d07a5e 6 * Copyright (C) 2003-2008 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
16edf865 25extern int dry_run;
a0545709 26extern int do_xfers;
8715db2c
WD
27extern int am_server;
28extern int am_daemon;
3ea6e0e7 29extern int inc_recurse;
8a9cfb24 30extern int log_before_transfer;
2fedf3d5 31extern int stdout_format_has_i;
ecc7623e 32extern int logfile_format_has_i;
2f03f956 33extern int csum_length;
6cc11982 34extern int append_mode;
2f03f956 35extern int io_error;
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
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
5a18b34d 73 if (DEBUG_GTE(DELTASUM, 3)) {
6d56efa6 74 rprintf(FINFO, "count=%s n=%ld rem=%ld\n",
adc2476f 75 big_num(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
5a18b34d 107 if (DEBUG_GTE(DELTASUM, 3)) {
a3221d2a 108 rprintf(FINFO,
6d56efa6 109 "chunk[%d] len=%d offset=%s sum1=%08x\n",
adc2476f 110 i, s->sums[i].len, big_num(s->sums[i].offset),
a3221d2a
WD
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
e982d591 129 flist = flist_for_ndx(ndx, "successful_send");
f3d6d480 130 file = flist->files[ndx - flist->ndx_start];
29a89172 131 if (!change_pathname(file, NULL, 0))
f3d6d480
WD
132 return;
133 f_name(file, fname);
134
135 if (do_unlink(fname) == 0) {
951e826b 136 if (INFO_GTE(REMOVE, 1))
f3d6d480
WD
137 rprintf(FINFO, "sender removed %s\n", fname);
138 } else
139 rsyserr(FERROR, errno, "sender failed to remove %s", fname);
95306d9d 140}
2f03f956 141
16edf865
WD
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)
d144e43b 145{
9ae7a2cd 146 write_ndx(f_out, ndx);
d144e43b
WD
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);
16edf865
WD
154#ifdef SUPPORT_XATTRS
155 if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && !dry_run)
156 send_xattr_request(fname, file, f_out);
157#endif
d144e43b
WD
158}
159
f3d6d480 160void send_files(int f_in, int f_out)
e6e3f12f 161{
c1659c79 162 int fd = -1;
2f03f956 163 struct sum_struct *s;
41cc97ae 164 struct map_struct *mbuf = NULL;
2f03f956 165 STRUCT_STAT st;
f3d6d480
WD
166 char fname[MAXPATHLEN], xname[MAXPATHLEN];
167 const char *path, *slash;
4d53c4dd 168 uchar fnamecmp_type;
6087ef2a 169 int iflags, xlen;
2f03f956 170 struct file_struct *file;
11290705 171 int phase = 0, max_phase = protocol_version >= 29 ? 2 : 1;
1b7c47cb 172 struct stats initial_stats;
2fedf3d5
WD
173 int itemizing = am_server ? logfile_format_has_i : stdout_format_has_i;
174 enum logcode log_code = log_before_transfer ? FLOG : FINFO;
a0545709 175 int f_xfer = write_batch < 0 ? batch_fd : f_out;
f3d6d480 176 int ndx, j;
2f03f956 177
951e826b 178 if (DEBUG_GTE(SEND, 1))
e6e3f12f 179 rprintf(FINFO, "send_files starting\n");
2f03f956 180
2f03f956 181 while (1) {
3ea6e0e7 182 if (inc_recurse)
be91bd81 183 send_extra_file_list(f_out, FILECNT_LOOKAHEAD);
f3d6d480
WD
184
185 /* This call also sets cur_flist. */
16edf865
WD
186 ndx = read_ndx_and_attrs(f_in, &iflags, &fnamecmp_type,
187 xname, &xlen);
f3d6d480 188 if (ndx == NDX_DONE) {
951e826b
WD
189 if (!am_server && INFO_GTE(PROGRESS, 2) && cur_flist) {
190 set_current_file_index(NULL, 0);
191 end_progress(0);
192 }
3ea6e0e7 193 if (inc_recurse && first_flist) {
f3d6d480
WD
194 flist_free(first_flist);
195 if (first_flist) {
9ae7a2cd 196 write_ndx(f_out, NDX_DONE);
f3d6d480
WD
197 continue;
198 }
199 }
11290705 200 if (++phase > max_phase)
8b48bf11 201 break;
951e826b 202 if (DEBUG_GTE(SEND, 1))
8b48bf11 203 rprintf(FINFO, "send_files phase=%d\n", phase);
e366e530
WD
204 if (phase == 2)
205 read_del_stats(f_in);
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 }
29a89172 223 if (!change_pathname(file, NULL, 0))
f3d6d480
WD
224 continue;
225 f_name(file, fname);
7f37167d 226
951e826b 227 if (DEBUG_GTE(SEND, 1))
f3d6d480 228 rprintf(FINFO, "send_files(%d, %s%s%s)\n", ndx, path,slash,fname);
8a9cfb24 229
16edf865 230#ifdef SUPPORT_XATTRS
c0f4228d 231 if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && !dry_run)
16edf865
WD
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);
e366e530
WD
239 if (iflags & ITEM_IS_NEW) {
240 stats.created_files++;
241 if (S_ISREG(file->mode)) {
242 /* Nothing further to count. */
243 } else if (S_ISDIR(file->mode))
244 stats.created_dirs++;
245#ifdef SUPPORT_LINKS
246 else if (S_ISLNK(file->mode))
247 stats.created_symlinks++;
248#endif
249 else if (IS_DEVICE(file->mode))
250 stats.created_devices++;
251 else
252 stats.created_specials++;
253 }
165e6d44
WD
254 continue;
255 }
11290705
WD
256 if (phase == 2) {
257 rprintf(FERROR,
258 "got transfer request in phase 2 [%s]\n",
259 who_am_i());
260 exit_cleanup(RERR_PROTOCOL);
261 }
8a9cfb24 262
f3d6d480
WD
263 if (file->flags & FLAG_FILE_SENT) {
264 if (csum_length == SHORT_SUM_LENGTH) {
265 /* For inplace: redo phase turns off the backup
266 * flag so that we do a regular inplace send. */
267 make_backups = -make_backups;
268 append_mode = -append_mode;
269 csum_length = SUM_LENGTH;
270 }
271 } else {
272 if (csum_length != SHORT_SUM_LENGTH) {
273 make_backups = -make_backups;
274 append_mode = -append_mode;
275 csum_length = SHORT_SUM_LENGTH;
276 }
e366e530
WD
277 if (iflags & ITEM_IS_NEW)
278 stats.created_files++;
f3d6d480
WD
279 }
280
6087ef2a 281 updating_basis_file = inplace && (protocol_version >= 29
f3d6d480 282 ? fnamecmp_type == FNAMECMP_FNAME : make_backups <= 0);
b951e023 283
951e826b 284 if (!am_server && INFO_GTE(PROGRESS, 1))
65b4e4b2 285 set_current_file_index(file, ndx);
e366e530 286 stats.xferred_files++;
112d728f 287 stats.total_transferred_size += F_LENGTH(file);
2f03f956 288
a0545709 289 if (!do_xfers) { /* log the transfer */
85909931 290 log_item(FCLIENT, file, &stats, iflags, NULL);
16edf865
WD
291 write_ndx_and_attrs(f_out, ndx, iflags, fname, file,
292 fnamecmp_type, xname, xlen);
2f03f956
AT
293 continue;
294 }
295
1b7c47cb
AT
296 initial_stats = stats;
297
efa95a18 298 if (!(s = receive_sums(f_in))) {
06c28400 299 io_error |= IOERR_GENERAL;
342bfb5e 300 rprintf(FERROR_XFER, "receive_sums failed\n");
f3d6d480 301 exit_cleanup(RERR_PROTOCOL);
2f03f956 302 }
76f79ba7 303
b9f592fb
WD
304 fd = do_open(fname, O_RDONLY, 0);
305 if (fd == -1) {
306 if (errno == ENOENT) {
307 enum logcode c = am_daemon
308 && protocol_version < 28 ? FERROR
3f0211b6 309 : FWARNING;
b9f592fb
WD
310 io_error |= IOERR_VANISHED;
311 rprintf(c, "file has vanished: %s\n",
312 full_fname(fname));
313 } else {
06c28400 314 io_error |= IOERR_GENERAL;
3f0211b6 315 rsyserr(FERROR_XFER, errno,
b9f592fb
WD
316 "send_files failed to open %s",
317 full_fname(fname));
41cc97ae 318 }
b9f592fb 319 free_sums(s);
f3d6d480
WD
320 if (protocol_version >= 30)
321 send_msg_int(MSG_NO_SEND, ndx);
b9f592fb
WD
322 continue;
323 }
e6e3f12f 324
b9f592fb
WD
325 /* map the local file */
326 if (do_fstat(fd, &st) != 0) {
327 io_error |= IOERR_GENERAL;
342bfb5e 328 rsyserr(FERROR_XFER, errno, "fstat failed");
b9f592fb
WD
329 free_sums(s);
330 close(fd);
f3d6d480 331 exit_cleanup(RERR_PROTOCOL);
b9f592fb 332 }
11a5a3c7 333
96d910c7 334 if (st.st_size) {
9b919d59
WD
335 int32 read_size = MAX(s->blength * 3, MAX_MAP_SIZE);
336 mbuf = map_file(fd, st.st_size, read_size, s->blength);
96d910c7
WD
337 } else
338 mbuf = NULL;
6902ed17 339
5a18b34d 340 if (DEBUG_GTE(DELTASUM, 2)) {
6d56efa6 341 rprintf(FINFO, "send_files mapped %s%s%s of size %s\n",
adc2476f 342 path,slash,fname, big_num(st.st_size));
6902ed17 343 }
e6e3f12f 344
16edf865
WD
345 write_ndx_and_attrs(f_out, ndx, iflags, fname, file,
346 fnamecmp_type, xname, xlen);
a0545709 347 write_sum_head(f_xfer, s);
b9f592fb 348
5a18b34d 349 if (DEBUG_GTE(DELTASUM, 2))
f3d6d480 350 rprintf(FINFO, "calling match_sums %s%s%s\n", path,slash,fname);
83fff1aa 351
8a9cfb24 352 if (log_before_transfer)
85909931 353 log_item(FCLIENT, file, &initial_stats, iflags, NULL);
951e826b 354 else if (!am_server && INFO_GTE(NAME, 1) && INFO_EQ(PROGRESS, 1))
f3d6d480 355 rprintf(FCLIENT, "%s\n", fname);
ecc81fce 356
83fff1aa 357 set_compression(fname);
1b7c47cb 358
a0545709 359 match_sums(f_xfer, s, mbuf, st.st_size);
951e826b 360 if (INFO_GTE(PROGRESS, 1))
0394e34a
WD
361 end_progress(st.st_size);
362
2fedf3d5 363 log_item(log_code, file, &initial_stats, iflags, NULL);
6902ed17 364
b9f592fb
WD
365 if (mbuf) {
366 j = unmap_file(mbuf);
367 if (j) {
368 io_error |= IOERR_GENERAL;
3f0211b6 369 rsyserr(FERROR_XFER, j,
b9f592fb
WD
370 "read errors mapping %s",
371 full_fname(fname));
6a7cc46c 372 }
6902ed17 373 }
b9f592fb 374 close(fd);
e6e3f12f 375
2f03f956 376 free_sums(s);
e6e3f12f 377
951e826b 378 if (DEBUG_GTE(SEND, 1))
f3d6d480 379 rprintf(FINFO, "sender finished %s%s%s\n", path,slash,fname);
95306d9d
WD
380
381 /* Flag that we actually sent this entry. */
f3d6d480 382 file->flags |= FLAG_FILE_SENT;
2f03f956 383 }
f3d6d480
WD
384 if (make_backups < 0)
385 make_backups = -make_backups;
2f03f956 386
951e826b 387 if (DEBUG_GTE(SEND, 1))
e6e3f12f 388 rprintf(FINFO, "send files finished\n");
2f03f956
AT
389
390 match_report();
391
9ae7a2cd 392 write_ndx(f_out, NDX_DONE);
2f03f956 393}