- Tweaked code in itemize() a bit.
[rsync/rsync.git] / sender.c
CommitLineData
e6e3f12f 1/*
2f03f956
AT
2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
e6e3f12f 4
2f03f956
AT
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
e6e3f12f 9
2f03f956
AT
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
e6e3f12f 14
2f03f956
AT
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18*/
19
20#include "rsync.h"
21
22extern int verbose;
8a9cfb24 23extern int log_before_transfer;
2f03f956
AT
24extern int csum_length;
25extern struct stats stats;
26extern int io_error;
27extern int dry_run;
28extern int am_server;
d6631cf3
WD
29extern int am_daemon;
30extern int protocol_version;
53f8519a 31extern int updating_basis_file;
8b115ac8 32extern int make_backups;
8a9cfb24 33extern int do_progress;
53f8519a 34extern int inplace;
8a9cfb24 35extern char *log_format;
a3221d2a 36extern struct stats stats;
2f03f956
AT
37
38
0f9c48b1
MP
39/**
40 * @file
41 *
42 * The sender gets checksums from the generator, calculates deltas,
43 * and transmits them to the receiver. The sender process runs on the
44 * machine holding the source files.
45 **/
fc0257c9 46
ce8149b6
MP
47/**
48 * Receive the checksums for a buffer
49 **/
2f03f956
AT
50static struct sum_struct *receive_sums(int f)
51{
52 struct sum_struct *s;
a1cbe76e 53 int32 i;
2f03f956
AT
54 OFF_T offset = 0;
55
a3221d2a
WD
56 if (!(s = new(struct sum_struct)))
57 out_of_memory("receive_sums");
2f03f956 58
fc0257c9
S
59 read_sum_head(f, s);
60
2f03f956
AT
61 s->sums = NULL;
62
da9d12f5 63 if (verbose > 3) {
6c495e0d
WD
64 rprintf(FINFO, "count=%.0f n=%ld rem=%ld\n",
65 (double)s->count, (long)s->blength, (long)s->remainder);
da9d12f5 66 }
2f03f956 67
e6e3f12f 68 if (s->count == 0)
2f03f956
AT
69 return(s);
70
a3221d2a
WD
71 if (!(s->sums = new_array(struct sum_buf, s->count)))
72 out_of_memory("receive_sums");
2f03f956 73
eed47b6b 74 for (i = 0; i < s->count; i++) {
2f03f956 75 s->sums[i].sum1 = read_int(f);
e6e3f12f 76 read_buf(f, s->sums[i].sum2, s->s2length);
2f03f956
AT
77
78 s->sums[i].offset = offset;
a3221d2a 79 s->sums[i].flags = 0;
2f03f956 80
eed47b6b 81 if (i == s->count-1 && s->remainder != 0)
2f03f956 82 s->sums[i].len = s->remainder;
a3221d2a 83 else
fc0257c9 84 s->sums[i].len = s->blength;
2f03f956
AT
85 offset += s->sums[i].len;
86
a3221d2a
WD
87 if (verbose > 3) {
88 rprintf(FINFO,
89 "chunk[%d] len=%d offset=%.0f sum1=%08x\n",
90 i, s->sums[i].len, (double)s->sums[i].offset,
91 s->sums[i].sum1);
92 }
2f03f956
AT
93 }
94
95 s->flength = offset;
96
97 return s;
98}
99
100
101
e6e3f12f
S
102void send_files(struct file_list *flist, int f_out, int f_in)
103{
c1659c79 104 int fd = -1;
2f03f956 105 struct sum_struct *s;
41cc97ae 106 struct map_struct *mbuf = NULL;
2f03f956 107 STRUCT_STAT st;
ecc81fce 108 char *fname2, fname[MAXPATHLEN];
8a9cfb24 109 int iflags;
2f03f956
AT
110 struct file_struct *file;
111 int phase = 0;
1b7c47cb 112 struct stats initial_stats;
8b115ac8 113 int save_make_backups = make_backups;
8a9cfb24 114 int i, j;
2f03f956
AT
115
116 if (verbose > 2)
e6e3f12f 117 rprintf(FINFO, "send_files starting\n");
2f03f956 118
2f03f956 119 while (1) {
3fef5364 120 unsigned int offset;
2f03f956
AT
121
122 i = read_int(f_in);
123 if (i == -1) {
e6e3f12f 124 if (phase == 0) {
2f03f956
AT
125 phase++;
126 csum_length = SUM_LENGTH;
e6e3f12f 127 write_int(f_out, -1);
2f03f956 128 if (verbose > 2)
e6e3f12f 129 rprintf(FINFO, "send_files phase=%d\n", phase);
9715c589
WD
130 /* For inplace: redo phase turns off the backup
131 * flag so that we do a regular inplace send. */
8b115ac8 132 make_backups = 0;
2f03f956
AT
133 continue;
134 }
135 break;
136 }
137
138 if (i < 0 || i >= flist->count) {
e6e3f12f 139 rprintf(FERROR, "Invalid file index %d (count=%d)\n",
2f03f956 140 i, flist->count);
65417579 141 exit_cleanup(RERR_PROTOCOL);
2f03f956
AT
142 }
143
8a9cfb24
WD
144 file = flist->files[i];
145
8a832465 146 if (protocol_version >= 29) {
8a9cfb24 147 iflags = read_byte(f_in);
8a832465 148 iflags |= read_byte(f_in) << 8;
8a9cfb24
WD
149 if (!(iflags & ITEM_UPDATING) || !S_ISREG(file->mode)) {
150 if (am_server) {
151 write_int(f_out, i);
152 write_byte(f_out, iflags);
8a832465 153 write_byte(f_out, iflags >> 8);
8a9cfb24
WD
154 } else
155 log_send(file, &stats, iflags);
156 continue;
157 }
158 } else
159 iflags = ITEM_UPDATING | ITEM_MISSING_DATA;
160
b951e023
WD
161 if (inplace && protocol_version >= 29) {
162 uchar fnamecmp_type = read_byte(f_in);
163 updating_basis_file = fnamecmp_type == FNAMECMP_FNAME;
164 } else
165 updating_basis_file = inplace && !make_backups;
166
8a9cfb24
WD
167 if (!S_ISREG(file->mode)) {
168 rprintf(FERROR, "[%s] got index of non-regular file: %d\n",
afd72c78
WD
169 who_am_i(), i);
170 exit_cleanup(RERR_PROTOCOL);
171 }
2f03f956 172
97feb557 173 stats.current_file_index = i;
2f03f956
AT
174 stats.num_transferred_files++;
175 stats.total_transferred_size += file->length;
176
2b136663 177 if (file->dir.root) {
421c2a24 178 /* N.B. We're sure that this fits, so offset is OK. */
2b136663 179 offset = strlcpy(fname, file->dir.root, sizeof fname);
421c2a24
WD
180 if (!offset || fname[offset-1] != '/')
181 fname[offset++] = '/';
3fef5364
WD
182 } else
183 offset = 0;
ecc81fce 184 fname2 = f_name_to(file, fname + offset);
e6e3f12f
S
185
186 if (verbose > 2)
187 rprintf(FINFO, "send_files(%d, %s)\n", i, fname);
188
a8ed4958 189 if (dry_run) { /* log the transfer */
8a832465 190 if (!am_server && log_format)
8a9cfb24 191 log_send(file, &stats, iflags);
e6e3f12f 192 write_int(f_out, i);
8a832465 193 if (protocol_version >= 29) {
8a9cfb24 194 write_byte(f_out, iflags);
8a832465
WD
195 write_byte(f_out, iflags >> 8);
196 }
2f03f956
AT
197 continue;
198 }
199
1b7c47cb
AT
200 initial_stats = stats;
201
efa95a18 202 if (!(s = receive_sums(f_in))) {
06c28400 203 io_error |= IOERR_GENERAL;
e6e3f12f 204 rprintf(FERROR, "receive_sums failed\n");
2f03f956
AT
205 return;
206 }
76f79ba7 207
b9f592fb
WD
208 fd = do_open(fname, O_RDONLY, 0);
209 if (fd == -1) {
210 if (errno == ENOENT) {
211 enum logcode c = am_daemon
212 && protocol_version < 28 ? FERROR
213 : FINFO;
214 io_error |= IOERR_VANISHED;
215 rprintf(c, "file has vanished: %s\n",
216 full_fname(fname));
217 } else {
06c28400 218 io_error |= IOERR_GENERAL;
b9f592fb
WD
219 rsyserr(FERROR, errno,
220 "send_files failed to open %s",
221 full_fname(fname));
41cc97ae 222 }
b9f592fb
WD
223 free_sums(s);
224 continue;
225 }
e6e3f12f 226
b9f592fb
WD
227 /* map the local file */
228 if (do_fstat(fd, &st) != 0) {
229 io_error |= IOERR_GENERAL;
230 rsyserr(FERROR, errno, "fstat failed");
231 free_sums(s);
232 close(fd);
233 return;
234 }
11a5a3c7 235
96d910c7 236 if (st.st_size) {
9b919d59
WD
237 int32 read_size = MAX(s->blength * 3, MAX_MAP_SIZE);
238 mbuf = map_file(fd, st.st_size, read_size, s->blength);
96d910c7
WD
239 } else
240 mbuf = NULL;
6902ed17 241
b9f592fb
WD
242 if (verbose > 2) {
243 rprintf(FINFO, "send_files mapped %s of size %.0f\n",
ecc81fce 244 safe_fname(fname), (double)st.st_size);
6902ed17 245 }
e6e3f12f 246
b9f592fb 247 write_int(f_out, i);
8a832465 248 if (protocol_version >= 29) {
8a9cfb24 249 write_byte(f_out, iflags);
8a832465
WD
250 write_byte(f_out, iflags >> 8);
251 }
b9f592fb
WD
252 write_sum_head(f_out, s);
253
ecc81fce
WD
254 if (verbose > 2) {
255 rprintf(FINFO, "calling match_sums %s\n",
256 safe_fname(fname));
11a5a3c7 257 }
83fff1aa 258
8a9cfb24
WD
259 if (log_before_transfer)
260 log_send(file, &initial_stats, iflags);
8a832465 261 else if (!am_server && verbose && do_progress)
ecc81fce
WD
262 rprintf(FINFO, "%s\n", safe_fname(fname2));
263
83fff1aa 264 set_compression(fname);
1b7c47cb 265
b9f592fb 266 match_sums(f_out, s, mbuf, st.st_size);
8a9cfb24
WD
267 if (!log_before_transfer)
268 log_send(file, &initial_stats, iflags);
6902ed17 269
b9f592fb
WD
270 if (mbuf) {
271 j = unmap_file(mbuf);
272 if (j) {
273 io_error |= IOERR_GENERAL;
274 rsyserr(FERROR, j,
275 "read errors mapping %s",
276 full_fname(fname));
6a7cc46c 277 }
6902ed17 278 }
b9f592fb 279 close(fd);
e6e3f12f 280
2f03f956 281 free_sums(s);
e6e3f12f 282
ecc81fce
WD
283 if (verbose > 2) {
284 rprintf(FINFO, "sender finished %s\n",
285 safe_fname(fname));
286 }
2f03f956 287 }
8b115ac8 288 make_backups = save_make_backups;
2f03f956
AT
289
290 if (verbose > 2)
e6e3f12f 291 rprintf(FINFO, "send files finished\n");
2f03f956
AT
292
293 match_report();
294
e6e3f12f 295 write_int(f_out, -1);
2f03f956 296}