2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
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.
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.
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.
23 extern int what_has_changed;
24 extern int csum_length;
25 extern struct stats stats;
30 extern int protocol_version;
31 extern int updating_basis_file;
32 extern int make_backups;
34 extern struct stats stats;
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.
46 * Receive the checksums for a buffer
48 static struct sum_struct *receive_sums(int f)
54 if (!(s = new(struct sum_struct)))
55 out_of_memory("receive_sums");
62 rprintf(FINFO, "count=%.0f n=%ld rem=%ld\n",
63 (double)s->count, (long)s->blength, (long)s->remainder);
69 if (!(s->sums = new_array(struct sum_buf, s->count)))
70 out_of_memory("receive_sums");
72 for (i = 0; i < s->count; i++) {
73 s->sums[i].sum1 = read_int(f);
74 read_buf(f, s->sums[i].sum2, s->s2length);
76 s->sums[i].offset = offset;
79 if (i == s->count-1 && s->remainder != 0)
80 s->sums[i].len = s->remainder;
82 s->sums[i].len = s->blength;
83 offset += s->sums[i].len;
87 "chunk[%d] len=%d offset=%.0f sum1=%08x\n",
88 i, s->sums[i].len, (double)s->sums[i].offset,
100 void send_files(struct file_list *flist, int f_out, int f_in)
103 struct sum_struct *s;
104 struct map_struct *mbuf = NULL;
106 char *fname2, fname[MAXPATHLEN];
108 struct file_struct *file;
110 struct stats initial_stats;
111 int save_make_backups = make_backups;
115 rprintf(FINFO, "send_files starting\n");
124 csum_length = SUM_LENGTH;
125 write_int(f_out, -1);
127 rprintf(FINFO, "send_files phase=%d\n", phase);
128 /* For inplace: redo phase turns off the backup
129 * flag so that we do a regular inplace send. */
136 if (i < 0 || i >= flist->count) {
137 rprintf(FERROR, "Invalid file index %d (count=%d)\n",
139 exit_cleanup(RERR_PROTOCOL);
142 if (inplace && protocol_version >= 29) {
143 uchar fnamecmp_type = read_byte(f_in);
144 updating_basis_file = fnamecmp_type == FNAMECMP_FNAME;
146 updating_basis_file = inplace && !make_backups;
148 file = flist->files[i];
149 if (S_ISDIR(file->mode)) {
150 rprintf(FERROR, "[%s] got index of directory: %d\n",
152 exit_cleanup(RERR_PROTOCOL);
155 stats.current_file_index = i;
156 stats.num_transferred_files++;
157 stats.total_transferred_size += file->length;
159 if (file->dir.root) {
160 /* N.B. We're sure that this fits, so offset is OK. */
161 offset = strlcpy(fname, file->dir.root, sizeof fname);
162 if (!offset || fname[offset-1] != '/')
163 fname[offset++] = '/';
166 fname2 = f_name_to(file, fname + offset);
169 rprintf(FINFO, "send_files(%d, %s)\n", i, fname);
171 if (dry_run) { /* log the transfer */
172 if (!am_server && verbose && !what_has_changed)
173 rprintf(FINFO, "%s\n", safe_fname(fname2));
178 initial_stats = stats;
180 if (!(s = receive_sums(f_in))) {
181 io_error |= IOERR_GENERAL;
182 rprintf(FERROR, "receive_sums failed\n");
186 fd = do_open(fname, O_RDONLY, 0);
188 if (errno == ENOENT) {
189 enum logcode c = am_daemon
190 && protocol_version < 28 ? FERROR
192 io_error |= IOERR_VANISHED;
193 rprintf(c, "file has vanished: %s\n",
196 io_error |= IOERR_GENERAL;
197 rsyserr(FERROR, errno,
198 "send_files failed to open %s",
205 /* map the local file */
206 if (do_fstat(fd, &st) != 0) {
207 io_error |= IOERR_GENERAL;
208 rsyserr(FERROR, errno, "fstat failed");
215 int32 read_size = MAX(s->blength * 3, MAX_MAP_SIZE);
216 mbuf = map_file(fd, st.st_size, read_size, s->blength);
221 rprintf(FINFO, "send_files mapped %s of size %.0f\n",
222 safe_fname(fname), (double)st.st_size);
226 write_sum_head(f_out, s);
229 rprintf(FINFO, "calling match_sums %s\n",
233 /* log the transfer */
234 if (!am_server && verbose && !what_has_changed)
235 rprintf(FINFO, "%s\n", safe_fname(fname2));
237 set_compression(fname);
239 match_sums(f_out, s, mbuf, st.st_size);
240 log_send(file, &initial_stats);
243 j = unmap_file(mbuf);
245 io_error |= IOERR_GENERAL;
247 "read errors mapping %s",
256 rprintf(FINFO, "sender finished %s\n",
260 make_backups = save_make_backups;
263 rprintf(FINFO, "send files finished\n");
267 write_int(f_out, -1);