Commit | Line | Data |
---|---|---|
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 | ||
22 | extern int verbose; | |
2f03f956 AT |
23 | extern int csum_length; |
24 | extern struct stats stats; | |
25 | extern int io_error; | |
26 | extern int dry_run; | |
27 | extern int am_server; | |
d6631cf3 WD |
28 | extern int am_daemon; |
29 | extern int protocol_version; | |
a3221d2a | 30 | extern struct stats stats; |
2f03f956 AT |
31 | |
32 | ||
0f9c48b1 MP |
33 | /** |
34 | * @file | |
35 | * | |
36 | * The sender gets checksums from the generator, calculates deltas, | |
37 | * and transmits them to the receiver. The sender process runs on the | |
38 | * machine holding the source files. | |
39 | **/ | |
fc0257c9 S |
40 | void read_sum_head(int f, struct sum_struct *sum) |
41 | { | |
fc0257c9 S |
42 | sum->count = read_int(f); |
43 | sum->blength = read_int(f); | |
dfad66a8 | 44 | if (protocol_version < 27) { |
fc0257c9 | 45 | sum->s2length = csum_length; |
dfad66a8 | 46 | } else { |
e6e3f12f | 47 | sum->s2length = read_int(f); |
dfad66a8 | 48 | if (sum->s2length > MD4_SUM_LENGTH) { |
ef5075e0 WD |
49 | rprintf(FERROR, "Invalid checksum length %ld\n", |
50 | (long)sum->s2length); | |
dfad66a8 S |
51 | exit_cleanup(RERR_PROTOCOL); |
52 | } | |
53 | } | |
e6e3f12f | 54 | sum->remainder = read_int(f); |
fc0257c9 S |
55 | } |
56 | ||
ce8149b6 MP |
57 | /** |
58 | * Receive the checksums for a buffer | |
59 | **/ | |
2f03f956 AT |
60 | static struct sum_struct *receive_sums(int f) |
61 | { | |
62 | struct sum_struct *s; | |
63 | int i; | |
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 | ||
da9d12f5 WD |
73 | if (verbose > 3) { |
74 | rprintf(FINFO, "count=%ld n=%u rem=%u\n", | |
75 | (long)s->count, s->blength, s->remainder); | |
76 | } | |
2f03f956 | 77 | |
e6e3f12f | 78 | if (s->count == 0) |
2f03f956 AT |
79 | return(s); |
80 | ||
a3221d2a WD |
81 | if (!(s->sums = new_array(struct sum_buf, s->count))) |
82 | out_of_memory("receive_sums"); | |
2f03f956 | 83 | |
a3221d2a | 84 | for (i = 0; i < (int)s->count; i++) { |
2f03f956 | 85 | s->sums[i].sum1 = read_int(f); |
e6e3f12f | 86 | read_buf(f, s->sums[i].sum2, s->s2length); |
2f03f956 AT |
87 | |
88 | s->sums[i].offset = offset; | |
a3221d2a | 89 | s->sums[i].flags = 0; |
2f03f956 | 90 | |
a3221d2a | 91 | if (i == (int)s->count-1 && s->remainder != 0) |
2f03f956 | 92 | s->sums[i].len = s->remainder; |
a3221d2a | 93 | else |
fc0257c9 | 94 | s->sums[i].len = s->blength; |
2f03f956 AT |
95 | offset += s->sums[i].len; |
96 | ||
a3221d2a WD |
97 | if (verbose > 3) { |
98 | rprintf(FINFO, | |
99 | "chunk[%d] len=%d offset=%.0f sum1=%08x\n", | |
100 | i, s->sums[i].len, (double)s->sums[i].offset, | |
101 | s->sums[i].sum1); | |
102 | } | |
2f03f956 AT |
103 | } |
104 | ||
105 | s->flength = offset; | |
106 | ||
107 | return s; | |
108 | } | |
109 | ||
110 | ||
111 | ||
e6e3f12f S |
112 | void send_files(struct file_list *flist, int f_out, int f_in) |
113 | { | |
c1659c79 | 114 | int fd = -1; |
2f03f956 | 115 | struct sum_struct *s; |
41cc97ae | 116 | struct map_struct *mbuf = NULL; |
2f03f956 | 117 | STRUCT_STAT st; |
ecc81fce | 118 | char *fname2, fname[MAXPATHLEN]; |
2f03f956 AT |
119 | int i; |
120 | struct file_struct *file; | |
121 | int phase = 0; | |
1b7c47cb | 122 | struct stats initial_stats; |
64c3523a | 123 | int j; |
2f03f956 AT |
124 | |
125 | if (verbose > 2) | |
e6e3f12f | 126 | rprintf(FINFO, "send_files starting\n"); |
2f03f956 | 127 | |
2f03f956 | 128 | while (1) { |
3fef5364 | 129 | unsigned int offset; |
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 | ||
97feb557 | 152 | stats.current_file_index = i; |
2f03f956 AT |
153 | stats.num_transferred_files++; |
154 | stats.total_transferred_size += file->length; | |
155 | ||
2f03f956 | 156 | if (file->basedir) { |
421c2a24 WD |
157 | /* N.B. We're sure that this fits, so offset is OK. */ |
158 | offset = strlcpy(fname, file->basedir, sizeof fname); | |
159 | if (!offset || fname[offset-1] != '/') | |
160 | fname[offset++] = '/'; | |
3fef5364 WD |
161 | } else |
162 | offset = 0; | |
ecc81fce | 163 | fname2 = f_name_to(file, fname + offset); |
e6e3f12f S |
164 | |
165 | if (verbose > 2) | |
166 | rprintf(FINFO, "send_files(%d, %s)\n", i, fname); | |
167 | ||
168 | if (dry_run) { | |
ecc81fce WD |
169 | if (!am_server && verbose) /* log the transfer */ |
170 | rprintf(FINFO, "%s\n", safe_fname(fname2)); | |
e6e3f12f | 171 | write_int(f_out, i); |
2f03f956 AT |
172 | continue; |
173 | } | |
174 | ||
1b7c47cb AT |
175 | initial_stats = stats; |
176 | ||
efa95a18 | 177 | if (!(s = receive_sums(f_in))) { |
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 | |
efa95a18 | 211 | mbuf = st.st_size ? map_file(fd, st.st_size, s->blength) : NULL; |
6902ed17 | 212 | |
b9f592fb WD |
213 | if (verbose > 2) { |
214 | rprintf(FINFO, "send_files mapped %s of size %.0f\n", | |
ecc81fce | 215 | safe_fname(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 | ||
ecc81fce WD |
221 | if (verbose > 2) { |
222 | rprintf(FINFO, "calling match_sums %s\n", | |
223 | safe_fname(fname)); | |
11a5a3c7 | 224 | } |
83fff1aa | 225 | |
ecc81fce WD |
226 | if (!am_server && verbose) /* log the transfer */ |
227 | rprintf(FINFO, "%s\n", safe_fname(fname2)); | |
228 | ||
83fff1aa | 229 | set_compression(fname); |
1b7c47cb | 230 | |
b9f592fb WD |
231 | match_sums(f_out, s, mbuf, st.st_size); |
232 | log_send(file, &initial_stats); | |
6902ed17 | 233 | |
b9f592fb WD |
234 | if (mbuf) { |
235 | j = unmap_file(mbuf); | |
236 | if (j) { | |
237 | io_error |= IOERR_GENERAL; | |
238 | rsyserr(FERROR, j, | |
239 | "read errors mapping %s", | |
240 | full_fname(fname)); | |
6a7cc46c | 241 | } |
6902ed17 | 242 | } |
b9f592fb | 243 | close(fd); |
e6e3f12f | 244 | |
2f03f956 | 245 | free_sums(s); |
e6e3f12f | 246 | |
ecc81fce WD |
247 | if (verbose > 2) { |
248 | rprintf(FINFO, "sender finished %s\n", | |
249 | safe_fname(fname)); | |
250 | } | |
2f03f956 AT |
251 | } |
252 | ||
253 | if (verbose > 2) | |
e6e3f12f | 254 | rprintf(FINFO, "send files finished\n"); |
2f03f956 AT |
255 | |
256 | match_report(); | |
257 | ||
e6e3f12f | 258 | write_int(f_out, -1); |
2f03f956 | 259 | } |