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