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 csum_length;
24 extern struct stats stats;
29 extern int protocol_version;
30 extern int updating_basis_file;
31 extern int make_backups;
33 extern struct stats stats;
39 * The sender gets checksums from the generator, calculates deltas,
40 * and transmits them to the receiver. The sender process runs on the
41 * machine holding the source files.
45 * Receive the checksums for a buffer
47 static struct sum_struct *receive_sums(int f)
53 if (!(s = new(struct sum_struct)))
54 out_of_memory("receive_sums");
61 rprintf(FINFO, "count=%.0f n=%ld rem=%ld\n",
62 (double)s->count, (long)s->blength, (long)s->remainder);
68 if (!(s->sums = new_array(struct sum_buf, s->count)))
69 out_of_memory("receive_sums");
71 for (i = 0; i < (int)s->count; i++) {
72 s->sums[i].sum1 = read_int(f);
73 read_buf(f, s->sums[i].sum2, s->s2length);
75 s->sums[i].offset = offset;
78 if (i == (int)s->count-1 && s->remainder != 0)
79 s->sums[i].len = s->remainder;
81 s->sums[i].len = s->blength;
82 offset += s->sums[i].len;
86 "chunk[%d] len=%d offset=%.0f sum1=%08x\n",
87 i, s->sums[i].len, (double)s->sums[i].offset,
99 void send_files(struct file_list *flist, int f_out, int f_in)
102 struct sum_struct *s;
103 struct map_struct *mbuf = NULL;
105 char *fname2, fname[MAXPATHLEN];
107 struct file_struct *file;
109 struct stats initial_stats;
110 int save_make_backups = make_backups;
114 rprintf(FINFO, "send_files starting\n");
123 csum_length = SUM_LENGTH;
124 write_int(f_out, -1);
126 rprintf(FINFO, "send_files phase=%d\n", phase);
127 /* For inplace: redo phase turns off the backup
128 * flag so that we do a regular inplace send. */
135 if (i < 0 || i >= flist->count) {
136 rprintf(FERROR, "Invalid file index %d (count=%d)\n",
138 exit_cleanup(RERR_PROTOCOL);
141 file = flist->files[i];
143 stats.current_file_index = i;
144 stats.num_transferred_files++;
145 stats.total_transferred_size += file->length;
148 /* N.B. We're sure that this fits, so offset is OK. */
149 offset = strlcpy(fname, file->basedir, sizeof fname);
150 if (!offset || fname[offset-1] != '/')
151 fname[offset++] = '/';
154 fname2 = f_name_to(file, fname + offset);
155 if (inplace && protocol_version >= 29) {
156 uchar fnamecmp_type = read_byte(f_in);
157 updating_basis_file = fnamecmp_type == FNAMECMP_FNAME;
159 updating_basis_file = inplace && !make_backups;
162 rprintf(FINFO, "send_files(%d, %s)\n", i, fname);
165 if (!am_server && verbose) /* log the transfer */
166 rprintf(FINFO, "%s\n", safe_fname(fname2));
171 initial_stats = stats;
173 if (!(s = receive_sums(f_in))) {
174 io_error |= IOERR_GENERAL;
175 rprintf(FERROR, "receive_sums failed\n");
179 fd = do_open(fname, O_RDONLY, 0);
181 if (errno == ENOENT) {
182 enum logcode c = am_daemon
183 && protocol_version < 28 ? FERROR
185 io_error |= IOERR_VANISHED;
186 rprintf(c, "file has vanished: %s\n",
189 io_error |= IOERR_GENERAL;
190 rsyserr(FERROR, errno,
191 "send_files failed to open %s",
198 /* map the local file */
199 if (do_fstat(fd, &st) != 0) {
200 io_error |= IOERR_GENERAL;
201 rsyserr(FERROR, errno, "fstat failed");
208 int32 read_size = MAX(s->blength * 3, MAX_MAP_SIZE);
209 mbuf = map_file(fd, st.st_size, read_size, s->blength);
214 rprintf(FINFO, "send_files mapped %s of size %.0f\n",
215 safe_fname(fname), (double)st.st_size);
219 write_sum_head(f_out, s);
222 rprintf(FINFO, "calling match_sums %s\n",
226 if (!am_server && verbose) /* log the transfer */
227 rprintf(FINFO, "%s\n", safe_fname(fname2));
229 set_compression(fname);
231 match_sums(f_out, s, mbuf, st.st_size);
232 log_send(file, &initial_stats);
235 j = unmap_file(mbuf);
237 io_error |= IOERR_GENERAL;
239 "read errors mapping %s",
248 rprintf(FINFO, "sender finished %s\n",
252 make_backups = save_make_backups;
255 rprintf(FINFO, "send files finished\n");
259 write_int(f_out, -1);