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