Mention my recent changes.
[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;
2f03f956
AT
23extern int csum_length;
24extern struct stats stats;
25extern int io_error;
26extern int dry_run;
27extern int am_server;
28
29
0f9c48b1
MP
30/**
31 * @file
32 *
33 * The sender gets checksums from the generator, calculates deltas,
34 * and transmits them to the receiver. The sender process runs on the
35 * machine holding the source files.
36 **/
37
38
fc0257c9
S
39void read_sum_head(int f, struct sum_struct *sum)
40{
d04e9c51 41 extern int protocol_version;
fc0257c9
S
42
43 sum->count = read_int(f);
44 sum->blength = read_int(f);
d04e9c51 45 if (protocol_version < 27)
fc0257c9 46 sum->s2length = csum_length;
d04e9c51 47 else
e6e3f12f 48 sum->s2length = read_int(f);
e6e3f12f 49 sum->remainder = read_int(f);
fc0257c9
S
50}
51
ce8149b6
MP
52/**
53 * Receive the checksums for a buffer
54 **/
2f03f956
AT
55static struct sum_struct *receive_sums(int f)
56{
57 struct sum_struct *s;
58 int i;
59 OFF_T offset = 0;
60
61 s = (struct sum_struct *)malloc(sizeof(*s));
62 if (!s) out_of_memory("receive_sums");
63
fc0257c9
S
64 read_sum_head(f, s);
65
2f03f956
AT
66 s->sums = NULL;
67
68 if (verbose > 3)
e6e3f12f 69 rprintf(FINFO, "count=%ld n=%ld rem=%ld\n",
fc0257c9
S
70 (long) s->count, (long) s->blength,
71 (long) s->remainder);
2f03f956 72
e6e3f12f 73 if (s->count == 0)
2f03f956
AT
74 return(s);
75
76 s->sums = (struct sum_buf *)malloc(sizeof(s->sums[0])*s->count);
77 if (!s->sums) out_of_memory("receive_sums");
78
e6e3f12f 79 for (i = 0; i < (int) s->count; i++) {
2f03f956 80 s->sums[i].sum1 = read_int(f);
e6e3f12f 81 read_buf(f, s->sums[i].sum2, s->s2length);
2f03f956
AT
82
83 s->sums[i].offset = offset;
84 s->sums[i].i = i;
85
a261989c 86 if (i == (int) s->count-1 && s->remainder != 0) {
2f03f956
AT
87 s->sums[i].len = s->remainder;
88 } else {
fc0257c9 89 s->sums[i].len = s->blength;
2f03f956
AT
90 }
91 offset += s->sums[i].len;
92
93 if (verbose > 3)
e6e3f12f
S
94 rprintf(FINFO, "chunk[%d] len=%d offset=%.0f sum1=%08x\n",
95 i, s->sums[i].len, (double)s->sums[i].offset, s->sums[i].sum1);
2f03f956
AT
96 }
97
98 s->flength = offset;
99
100 return s;
101}
102
103
104
e6e3f12f
S
105void send_files(struct file_list *flist, int f_out, int f_in)
106{
c1659c79 107 int fd = -1;
2f03f956 108 struct sum_struct *s;
c1659c79 109 struct map_struct *buf = NULL;
2f03f956 110 STRUCT_STAT st;
e6e3f12f 111 char fname[MAXPATHLEN];
2f03f956
AT
112 int i;
113 struct file_struct *file;
114 int phase = 0;
e6e3f12f 115 extern struct stats stats;
1b7c47cb 116 struct stats initial_stats;
6902ed17 117 extern int write_batch; /* dw */
6902ed17
MP
118 extern int read_batch; /* dw */
119 int checksums_match; /* dw */
120 int buff_len; /* dw */
121 char buff[CHUNK_SIZE]; /* dw */
122 int j; /* dw */
123 int done; /* dw */
2f03f956
AT
124
125 if (verbose > 2)
e6e3f12f 126 rprintf(FINFO, "send_files starting\n");
2f03f956 127
2f03f956 128 while (1) {
e6e3f12f 129 int offset = 0;
2f03f956
AT
130
131 i = read_int(f_in);
132 if (i == -1) {
e6e3f12f 133 if (phase == 0) {
2f03f956
AT
134 phase++;
135 csum_length = SUM_LENGTH;
e6e3f12f 136 write_int(f_out, -1);
2f03f956 137 if (verbose > 2)
e6e3f12f 138 rprintf(FINFO, "send_files phase=%d\n", phase);
2f03f956
AT
139 continue;
140 }
141 break;
142 }
143
144 if (i < 0 || i >= flist->count) {
e6e3f12f 145 rprintf(FERROR, "Invalid file index %d (count=%d)\n",
2f03f956 146 i, flist->count);
65417579 147 exit_cleanup(RERR_PROTOCOL);
2f03f956
AT
148 }
149
150 file = flist->files[i];
151
152 stats.num_transferred_files++;
153 stats.total_transferred_size += file->length;
154
155 fname[0] = 0;
156 if (file->basedir) {
e6e3f12f 157 strlcpy(fname, file->basedir, MAXPATHLEN);
2f03f956
AT
158 if (strlen(fname) == MAXPATHLEN-1) {
159 io_error = 1;
160 rprintf(FERROR, "send_files failed on long-named directory %s\n",
ea42541f 161 full_fname(fname));
2f03f956
AT
162 return;
163 }
e6e3f12f 164 strlcat(fname, "/", MAXPATHLEN);
2f03f956
AT
165 offset = strlen(file->basedir)+1;
166 }
e6e3f12f
S
167 strlcat(fname, f_name(file), MAXPATHLEN);
168
169 if (verbose > 2)
170 rprintf(FINFO, "send_files(%d, %s)\n", i, fname);
171
172 if (dry_run) {
42d4edc0
S
173 if (!am_server && verbose) { /* log transfer */
174 rprintf(FINFO, "%s\n", fname+offset);
11a5a3c7 175 }
e6e3f12f 176 write_int(f_out, i);
2f03f956
AT
177 continue;
178 }
179
1b7c47cb
AT
180 initial_stats = stats;
181
2f03f956
AT
182 s = receive_sums(f_in);
183 if (!s) {
184 io_error = 1;
e6e3f12f 185 rprintf(FERROR, "receive_sums failed\n");
2f03f956
AT
186 return;
187 }
76f79ba7
MP
188
189 if (write_batch)
e6e3f12f
S
190 write_batch_csum_info(&i, flist->count, s);
191
6902ed17 192 if (!read_batch) {
c1659c79
MP
193 fd = do_open(fname, O_RDONLY, 0);
194 if (fd == -1) {
195 io_error = 1;
e6e3f12f 196 rprintf(FERROR, "send_files failed to open %s: %s\n",
ea42541f 197 full_fname(fname), strerror(errno));
c1659c79
MP
198 free_sums(s);
199 continue;
200 }
e6e3f12f 201
c1659c79 202 /* map the local file */
e6e3f12f 203 if (do_fstat(fd, &st) != 0) {
c1659c79 204 io_error = 1;
ea42541f 205 rprintf(FERROR, "fstat failed: %s\n", strerror(errno));
c1659c79
MP
206 free_sums(s);
207 close(fd);
208 return;
209 }
e6e3f12f 210
c1659c79 211 if (st.st_size > 0) {
e6e3f12f 212 buf = map_file(fd, st.st_size);
c1659c79
MP
213 } else {
214 buf = NULL;
215 }
e6e3f12f 216
c1659c79 217 if (verbose > 2)
e6e3f12f
S
218 rprintf(FINFO, "send_files mapped %s of size %.0f\n",
219 fname, (double)st.st_size);
220
221 write_int(f_out, i);
11a5a3c7 222
c1659c79 223 if (write_batch)
e6e3f12f 224 write_batch_delta_file((char *)&i, sizeof(i));
6902ed17 225
fc0257c9 226 write_sum_head(f_out, s);
6902ed17 227 }
e6e3f12f
S
228
229 if (verbose > 2 && !read_batch)
230 rprintf(FINFO, "calling match_sums %s\n", fname);
231
42d4edc0
S
232 if (!am_server && verbose) { /* log transfer */
233 rprintf(FINFO, "%s\n", fname+offset);
11a5a3c7 234 }
83fff1aa
AT
235
236 set_compression(fname);
1b7c47cb 237
e6e3f12f
S
238 if (read_batch) { /* dw */
239 /* read checksums originally computed on sender side */
240 read_batch_csum_info(i, s, &checksums_match);
241 if (checksums_match) {
242 read_batch_delta_file( (char *) &j, sizeof(int) );
243 if (j != i) { /* if flist index entries don't match*/
244 rprintf(FINFO, "index mismatch in send_files\n");
245 rprintf(FINFO, "read index = %d flist ndx = %d\n", j, i);
246 close_batch_delta_file();
247 close_batch_csums_file();
248 exit_cleanup(1);
249 } else {
250 write_int(f_out, j);
251 write_sum_head(f_out, s);
252 done = 0;
253 while (!done) {
254 read_batch_delta_file( (char *) &buff_len, sizeof(int) );
255 write_int(f_out, buff_len);
256 if (buff_len == 0) {
257 done = 1;
258 } else {
259 if (buff_len > 0) {
260 read_batch_delta_file(buff, buff_len);
261 write_buf(f_out, buff, buff_len);
262 }
263 }
264 } /* end while */
265 read_batch_delta_file( buff, MD4_SUM_LENGTH);
266 write_buf(f_out, buff, MD4_SUM_LENGTH);
267
268 } /* j=i */
269 } else { /* not checksum match */
270 rprintf (FINFO, "readbatch & checksums don't match\n");
271 rprintf (FINFO, "filename=%s is being skipped\n", fname);
272 continue;
273 }
274 } else {
275 match_sums(f_out, s, buf, st.st_size);
276 log_send(file, &initial_stats);
277 }
6902ed17
MP
278
279 if (!read_batch) { /* dw */
e6e3f12f
S
280 if (buf) unmap_file(buf);
281 close(fd);
6902ed17 282 }
e6e3f12f 283
2f03f956 284 free_sums(s);
e6e3f12f 285
2f03f956 286 if (verbose > 2)
e6e3f12f 287 rprintf(FINFO, "sender finished %s\n", fname);
2f03f956
AT
288 }
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);
6902ed17 296 if (write_batch || read_batch) { /* dw */
e6e3f12f
S
297 close_batch_csums_file();
298 close_batch_delta_file();
6902ed17
MP
299 }
300
2f03f956
AT
301}
302
303
304
305
306