My modified version of Chris Shoemaker's improved batch-file handling.
[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;
d6631cf3
WD
28extern int am_daemon;
29extern int protocol_version;
2f03f956
AT
30
31
0f9c48b1
MP
32/**
33 * @file
34 *
35 * The sender gets checksums from the generator, calculates deltas,
36 * and transmits them to the receiver. The sender process runs on the
37 * machine holding the source files.
38 **/
fc0257c9
S
39void read_sum_head(int f, struct sum_struct *sum)
40{
fc0257c9
S
41 sum->count = read_int(f);
42 sum->blength = read_int(f);
dfad66a8 43 if (protocol_version < 27) {
fc0257c9 44 sum->s2length = csum_length;
dfad66a8 45 } else {
e6e3f12f 46 sum->s2length = read_int(f);
dfad66a8 47 if (sum->s2length > MD4_SUM_LENGTH) {
ef5075e0
WD
48 rprintf(FERROR, "Invalid checksum length %ld\n",
49 (long)sum->s2length);
dfad66a8
S
50 exit_cleanup(RERR_PROTOCOL);
51 }
52 }
e6e3f12f 53 sum->remainder = read_int(f);
fc0257c9
S
54}
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;
62 int i;
63 OFF_T offset = 0;
64
58cadc86 65 s = new(struct sum_struct);
2f03f956
AT
66 if (!s) out_of_memory("receive_sums");
67
fc0257c9
S
68 read_sum_head(f, s);
69
2f03f956
AT
70 s->sums = NULL;
71
da9d12f5
WD
72 if (verbose > 3) {
73 rprintf(FINFO, "count=%ld n=%u rem=%u\n",
74 (long)s->count, s->blength, s->remainder);
75 }
2f03f956 76
e6e3f12f 77 if (s->count == 0)
2f03f956
AT
78 return(s);
79
58cadc86 80 s->sums = new_array(struct sum_buf, s->count);
2f03f956
AT
81 if (!s->sums) out_of_memory("receive_sums");
82
e6e3f12f 83 for (i = 0; i < (int) s->count; i++) {
2f03f956 84 s->sums[i].sum1 = read_int(f);
e6e3f12f 85 read_buf(f, s->sums[i].sum2, s->s2length);
2f03f956
AT
86
87 s->sums[i].offset = offset;
88 s->sums[i].i = i;
89
a261989c 90 if (i == (int) s->count-1 && s->remainder != 0) {
2f03f956
AT
91 s->sums[i].len = s->remainder;
92 } else {
fc0257c9 93 s->sums[i].len = s->blength;
2f03f956
AT
94 }
95 offset += s->sums[i].len;
96
97 if (verbose > 3)
e6e3f12f
S
98 rprintf(FINFO, "chunk[%d] len=%d offset=%.0f sum1=%08x\n",
99 i, s->sums[i].len, (double)s->sums[i].offset, s->sums[i].sum1);
2f03f956
AT
100 }
101
102 s->flength = offset;
103
104 return s;
105}
106
107
108
e6e3f12f
S
109void send_files(struct file_list *flist, int f_out, int f_in)
110{
c1659c79 111 int fd = -1;
2f03f956 112 struct sum_struct *s;
41cc97ae 113 struct map_struct *mbuf = NULL;
2f03f956 114 STRUCT_STAT st;
e6e3f12f 115 char fname[MAXPATHLEN];
2f03f956
AT
116 int i;
117 struct file_struct *file;
118 int phase = 0;
e6e3f12f 119 extern struct stats stats;
1b7c47cb 120 struct stats initial_stats;
64c3523a 121 int j;
2f03f956
AT
122
123 if (verbose > 2)
e6e3f12f 124 rprintf(FINFO, "send_files starting\n");
2f03f956 125
2f03f956 126 while (1) {
3fef5364 127 unsigned int offset;
2f03f956
AT
128
129 i = read_int(f_in);
130 if (i == -1) {
e6e3f12f 131 if (phase == 0) {
2f03f956
AT
132 phase++;
133 csum_length = SUM_LENGTH;
e6e3f12f 134 write_int(f_out, -1);
2f03f956 135 if (verbose > 2)
e6e3f12f 136 rprintf(FINFO, "send_files phase=%d\n", phase);
2f03f956
AT
137 continue;
138 }
139 break;
140 }
141
142 if (i < 0 || i >= flist->count) {
e6e3f12f 143 rprintf(FERROR, "Invalid file index %d (count=%d)\n",
2f03f956 144 i, flist->count);
65417579 145 exit_cleanup(RERR_PROTOCOL);
2f03f956
AT
146 }
147
148 file = flist->files[i];
149
97feb557 150 stats.current_file_index = i;
2f03f956
AT
151 stats.num_transferred_files++;
152 stats.total_transferred_size += file->length;
153
2f03f956 154 if (file->basedir) {
421c2a24
WD
155 /* N.B. We're sure that this fits, so offset is OK. */
156 offset = strlcpy(fname, file->basedir, sizeof fname);
157 if (!offset || fname[offset-1] != '/')
158 fname[offset++] = '/';
3fef5364
WD
159 } else
160 offset = 0;
161 f_name_to(file, fname + offset);
e6e3f12f
S
162
163 if (verbose > 2)
164 rprintf(FINFO, "send_files(%d, %s)\n", i, fname);
165
166 if (dry_run) {
9c513d67 167 if (!am_server && verbose) {
42d4edc0 168 rprintf(FINFO, "%s\n", fname+offset);
11a5a3c7 169 }
e6e3f12f 170 write_int(f_out, i);
2f03f956
AT
171 continue;
172 }
173
1b7c47cb
AT
174 initial_stats = stats;
175
2f03f956
AT
176 s = receive_sums(f_in);
177 if (!s) {
06c28400 178 io_error |= IOERR_GENERAL;
e6e3f12f 179 rprintf(FERROR, "receive_sums failed\n");
2f03f956
AT
180 return;
181 }
76f79ba7 182
b9f592fb
WD
183 fd = do_open(fname, O_RDONLY, 0);
184 if (fd == -1) {
185 if (errno == ENOENT) {
186 enum logcode c = am_daemon
187 && protocol_version < 28 ? FERROR
188 : FINFO;
189 io_error |= IOERR_VANISHED;
190 rprintf(c, "file has vanished: %s\n",
191 full_fname(fname));
192 } else {
06c28400 193 io_error |= IOERR_GENERAL;
b9f592fb
WD
194 rsyserr(FERROR, errno,
195 "send_files failed to open %s",
196 full_fname(fname));
41cc97ae 197 }
b9f592fb
WD
198 free_sums(s);
199 continue;
200 }
e6e3f12f 201
b9f592fb
WD
202 /* map the local file */
203 if (do_fstat(fd, &st) != 0) {
204 io_error |= IOERR_GENERAL;
205 rsyserr(FERROR, errno, "fstat failed");
206 free_sums(s);
207 close(fd);
208 return;
209 }
11a5a3c7 210
b9f592fb 211 mbuf = st.st_size ? map_file(fd, st.st_size) : NULL;
6902ed17 212
b9f592fb
WD
213 if (verbose > 2) {
214 rprintf(FINFO, "send_files mapped %s of size %.0f\n",
215 fname, (double)st.st_size);
6902ed17 216 }
e6e3f12f 217
b9f592fb
WD
218 write_int(f_out, i);
219 write_sum_head(f_out, s);
220
221 if (verbose > 2)
e6e3f12f
S
222 rprintf(FINFO, "calling match_sums %s\n", fname);
223
9c513d67 224 if (!am_server && verbose) {
42d4edc0 225 rprintf(FINFO, "%s\n", fname+offset);
11a5a3c7 226 }
83fff1aa
AT
227
228 set_compression(fname);
1b7c47cb 229
b9f592fb
WD
230 match_sums(f_out, s, mbuf, st.st_size);
231 log_send(file, &initial_stats);
6902ed17 232
b9f592fb
WD
233 if (mbuf) {
234 j = unmap_file(mbuf);
235 if (j) {
236 io_error |= IOERR_GENERAL;
237 rsyserr(FERROR, j,
238 "read errors mapping %s",
239 full_fname(fname));
6a7cc46c 240 }
6902ed17 241 }
b9f592fb 242 close(fd);
e6e3f12f 243
2f03f956 244 free_sums(s);
e6e3f12f 245
2f03f956 246 if (verbose > 2)
e6e3f12f 247 rprintf(FINFO, "sender finished %s\n", fname);
2f03f956
AT
248 }
249
250 if (verbose > 2)
e6e3f12f 251 rprintf(FINFO, "send files finished\n");
2f03f956
AT
252
253 match_report();
254
e6e3f12f 255 write_int(f_out, -1);
2f03f956 256}