Further modifications to the copyright comment section.
[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
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
da9d12f5 72 if (verbose > 3) {
6c495e0d
WD
73 rprintf(FINFO, "count=%.0f n=%ld rem=%ld\n",
74 (double)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
a3221d2a
WD
106 if (verbose > 3) {
107 rprintf(FINFO,
108 "chunk[%d] len=%d offset=%.0f sum1=%08x\n",
109 i, s->sums[i].len, (double)s->sums[i].offset,
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
f3d6d480
WD
128 if (!(flist = flist_for_ndx(ndx))) {
129 rprintf(FERROR,
130 "INTERNAL ERROR: unable to find flist for item %d\n",
131 ndx);
132 return;
acee1ad8 133 }
f3d6d480
WD
134
135 file = flist->files[ndx - flist->ndx_start];
136 if (!push_flist_dir(F_ROOTDIR(file), -1))
137 return;
138 f_name(file, fname);
139
140 if (do_unlink(fname) == 0) {
141 if (verbose > 1)
142 rprintf(FINFO, "sender removed %s\n", fname);
143 } else
144 rsyserr(FERROR, errno, "sender failed to remove %s", fname);
95306d9d 145}
2f03f956 146
d144e43b
WD
147void write_ndx_and_attrs(int f_out, int ndx, int iflags,
148 uchar fnamecmp_type, char *buf, int len)
149{
9ae7a2cd 150 write_ndx(f_out, ndx);
d144e43b
WD
151 if (protocol_version < 29)
152 return;
153 write_shortint(f_out, iflags);
154 if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
155 write_byte(f_out, fnamecmp_type);
156 if (iflags & ITEM_XNAME_FOLLOWS)
157 write_vstring(f_out, buf, len);
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
AT
177
178 if (verbose > 2)
e6e3f12f 179 rprintf(FINFO, "send_files starting\n");
2f03f956 180
2f03f956 181 while (1) {
3ea6e0e7 182 if (inc_recurse)
f3d6d480
WD
183 send_extra_file_list(f_out, 1000);
184
185 /* This call also sets cur_flist. */
186 ndx = read_ndx_and_attrs(f_in, f_out, &iflags,
187 &fnamecmp_type, xname, &xlen);
188 if (ndx == NDX_DONE) {
3ea6e0e7 189 if (inc_recurse && first_flist) {
f3d6d480
WD
190 flist_free(first_flist);
191 if (first_flist) {
9ae7a2cd 192 write_ndx(f_out, NDX_DONE);
f3d6d480
WD
193 continue;
194 }
195 }
11290705 196 if (++phase > max_phase)
8b48bf11 197 break;
8b48bf11
WD
198 if (verbose > 2)
199 rprintf(FINFO, "send_files phase=%d\n", phase);
9ae7a2cd 200 write_ndx(f_out, NDX_DONE);
8b48bf11 201 continue;
2f03f956
AT
202 }
203
f3d6d480 204 file = cur_flist->files[ndx - cur_flist->ndx_start];
dbd9ea3e 205 if (F_ROOTDIR(file)) {
f3d6d480
WD
206 path = F_ROOTDIR(file);
207 slash = "/";
208 } else {
209 path = slash = "";
210 }
211 if (!push_flist_dir(F_ROOTDIR(file), -1))
212 continue;
213 f_name(file, fname);
7f37167d
WD
214
215 if (verbose > 2)
f3d6d480 216 rprintf(FINFO, "send_files(%d, %s%s%s)\n", ndx, path,slash,fname);
8a9cfb24 217
fad3dc42 218 if (!(iflags & ITEM_TRANSFER)) {
6087ef2a 219 maybe_log_item(file, iflags, itemizing, xname);
165e6d44
WD
220 continue;
221 }
11290705
WD
222 if (phase == 2) {
223 rprintf(FERROR,
224 "got transfer request in phase 2 [%s]\n",
225 who_am_i());
226 exit_cleanup(RERR_PROTOCOL);
227 }
8a9cfb24 228
f3d6d480
WD
229 if (file->flags & FLAG_FILE_SENT) {
230 if (csum_length == SHORT_SUM_LENGTH) {
231 /* For inplace: redo phase turns off the backup
232 * flag so that we do a regular inplace send. */
233 make_backups = -make_backups;
234 append_mode = -append_mode;
235 csum_length = SUM_LENGTH;
236 }
237 } else {
238 if (csum_length != SHORT_SUM_LENGTH) {
239 make_backups = -make_backups;
240 append_mode = -append_mode;
241 csum_length = SHORT_SUM_LENGTH;
242 }
243 }
244
6087ef2a 245 updating_basis_file = inplace && (protocol_version >= 29
f3d6d480 246 ? fnamecmp_type == FNAMECMP_FNAME : make_backups <= 0);
b951e023 247
f3d6d480 248 stats.current_file_index = ndx;
2f03f956 249 stats.num_transferred_files++;
112d728f 250 stats.total_transferred_size += F_LENGTH(file);
2f03f956 251
a0545709 252 if (!do_xfers) { /* log the transfer */
85909931 253 log_item(FCLIENT, file, &stats, iflags, NULL);
f3d6d480 254 write_ndx_and_attrs(f_out, ndx, iflags, fnamecmp_type,
927c8068 255 xname, xlen);
2f03f956
AT
256 continue;
257 }
258
1b7c47cb
AT
259 initial_stats = stats;
260
efa95a18 261 if (!(s = receive_sums(f_in))) {
06c28400 262 io_error |= IOERR_GENERAL;
e6e3f12f 263 rprintf(FERROR, "receive_sums failed\n");
f3d6d480 264 exit_cleanup(RERR_PROTOCOL);
2f03f956 265 }
76f79ba7 266
b9f592fb
WD
267 fd = do_open(fname, O_RDONLY, 0);
268 if (fd == -1) {
269 if (errno == ENOENT) {
270 enum logcode c = am_daemon
271 && protocol_version < 28 ? FERROR
272 : FINFO;
273 io_error |= IOERR_VANISHED;
274 rprintf(c, "file has vanished: %s\n",
275 full_fname(fname));
276 } else {
06c28400 277 io_error |= IOERR_GENERAL;
b9f592fb
WD
278 rsyserr(FERROR, errno,
279 "send_files failed to open %s",
280 full_fname(fname));
41cc97ae 281 }
b9f592fb 282 free_sums(s);
f3d6d480
WD
283 if (protocol_version >= 30)
284 send_msg_int(MSG_NO_SEND, ndx);
b9f592fb
WD
285 continue;
286 }
e6e3f12f 287
b9f592fb
WD
288 /* map the local file */
289 if (do_fstat(fd, &st) != 0) {
290 io_error |= IOERR_GENERAL;
291 rsyserr(FERROR, errno, "fstat failed");
292 free_sums(s);
293 close(fd);
f3d6d480 294 exit_cleanup(RERR_PROTOCOL);
b9f592fb 295 }
11a5a3c7 296
96d910c7 297 if (st.st_size) {
9b919d59
WD
298 int32 read_size = MAX(s->blength * 3, MAX_MAP_SIZE);
299 mbuf = map_file(fd, st.st_size, read_size, s->blength);
96d910c7
WD
300 } else
301 mbuf = NULL;
6902ed17 302
b9f592fb 303 if (verbose > 2) {
f3d6d480
WD
304 rprintf(FINFO, "send_files mapped %s%s%s of size %.0f\n",
305 path,slash,fname, (double)st.st_size);
6902ed17 306 }
e6e3f12f 307
f3d6d480 308 write_ndx_and_attrs(f_out, ndx, iflags, fnamecmp_type,
927c8068 309 xname, xlen);
a0545709 310 write_sum_head(f_xfer, s);
b9f592fb 311
45c49b52 312 if (verbose > 2)
f3d6d480 313 rprintf(FINFO, "calling match_sums %s%s%s\n", path,slash,fname);
83fff1aa 314
8a9cfb24 315 if (log_before_transfer)
85909931 316 log_item(FCLIENT, file, &initial_stats, iflags, NULL);
8a832465 317 else if (!am_server && verbose && do_progress)
f3d6d480 318 rprintf(FCLIENT, "%s\n", fname);
ecc81fce 319
83fff1aa 320 set_compression(fname);
1b7c47cb 321
a0545709 322 match_sums(f_xfer, s, mbuf, st.st_size);
0394e34a
WD
323 if (do_progress)
324 end_progress(st.st_size);
325
2fedf3d5 326 log_item(log_code, file, &initial_stats, iflags, NULL);
6902ed17 327
b9f592fb
WD
328 if (mbuf) {
329 j = unmap_file(mbuf);
330 if (j) {
331 io_error |= IOERR_GENERAL;
332 rsyserr(FERROR, j,
333 "read errors mapping %s",
334 full_fname(fname));
6a7cc46c 335 }
6902ed17 336 }
b9f592fb 337 close(fd);
e6e3f12f 338
2f03f956 339 free_sums(s);
e6e3f12f 340
45c49b52 341 if (verbose > 2)
f3d6d480 342 rprintf(FINFO, "sender finished %s%s%s\n", path,slash,fname);
95306d9d
WD
343
344 /* Flag that we actually sent this entry. */
f3d6d480 345 file->flags |= FLAG_FILE_SENT;
2f03f956 346 }
f3d6d480
WD
347 if (make_backups < 0)
348 make_backups = -make_backups;
2f03f956
AT
349
350 if (verbose > 2)
e6e3f12f 351 rprintf(FINFO, "send files finished\n");
2f03f956
AT
352
353 match_report();
354
9ae7a2cd 355 write_ndx(f_out, NDX_DONE);
2f03f956 356}