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