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 log_before_transfer;
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;
33 extern int do_progress;
35 extern char *log_format;
36 extern struct stats stats;
42 * The sender gets checksums from the generator, calculates deltas,
43 * and transmits them to the receiver. The sender process runs on the
44 * machine holding the source files.
48 * Receive the checksums for a buffer
50 static struct sum_struct *receive_sums(int f)
56 if (!(s = new(struct sum_struct)))
57 out_of_memory("receive_sums");
64 rprintf(FINFO, "count=%.0f n=%ld rem=%ld\n",
65 (double)s->count, (long)s->blength, (long)s->remainder);
71 if (!(s->sums = new_array(struct sum_buf, s->count)))
72 out_of_memory("receive_sums");
74 for (i = 0; i < s->count; i++) {
75 s->sums[i].sum1 = read_int(f);
76 read_buf(f, s->sums[i].sum2, s->s2length);
78 s->sums[i].offset = offset;
81 if (i == s->count-1 && s->remainder != 0)
82 s->sums[i].len = s->remainder;
84 s->sums[i].len = s->blength;
85 offset += s->sums[i].len;
89 "chunk[%d] len=%d offset=%.0f sum1=%08x\n",
90 i, s->sums[i].len, (double)s->sums[i].offset,
102 void send_files(struct file_list *flist, int f_out, int f_in)
105 struct sum_struct *s;
106 struct map_struct *mbuf = NULL;
108 char *fname2, fname[MAXPATHLEN];
110 struct file_struct *file;
112 struct stats initial_stats;
113 int save_make_backups = make_backups;
117 rprintf(FINFO, "send_files starting\n");
126 csum_length = SUM_LENGTH;
127 write_int(f_out, -1);
129 rprintf(FINFO, "send_files phase=%d\n", phase);
130 /* For inplace: redo phase turns off the backup
131 * flag so that we do a regular inplace send. */
138 if (i < 0 || i >= flist->count) {
139 rprintf(FERROR, "Invalid file index %d (count=%d)\n",
141 exit_cleanup(RERR_PROTOCOL);
144 file = flist->files[i];
146 if (protocol_version >= 29) {
147 iflags = read_byte(f_in);
148 iflags |= read_byte(f_in) << 8;
149 if (!(iflags & ITEM_UPDATING) || !S_ISREG(file->mode)) {
152 write_byte(f_out, iflags);
153 write_byte(f_out, iflags >> 8);
155 log_send(file, &stats, iflags);
159 iflags = ITEM_UPDATING | ITEM_MISSING_DATA;
161 if (inplace && protocol_version >= 29) {
162 uchar fnamecmp_type = read_byte(f_in);
163 updating_basis_file = fnamecmp_type == FNAMECMP_FNAME;
165 updating_basis_file = inplace && !make_backups;
167 if (!S_ISREG(file->mode)) {
168 rprintf(FERROR, "[%s] got index of non-regular file: %d\n",
170 exit_cleanup(RERR_PROTOCOL);
173 stats.current_file_index = i;
174 stats.num_transferred_files++;
175 stats.total_transferred_size += file->length;
177 if (file->dir.root) {
178 /* N.B. We're sure that this fits, so offset is OK. */
179 offset = strlcpy(fname, file->dir.root, sizeof fname);
180 if (!offset || fname[offset-1] != '/')
181 fname[offset++] = '/';
184 fname2 = f_name_to(file, fname + offset);
187 rprintf(FINFO, "send_files(%d, %s)\n", i, fname);
189 if (dry_run) { /* log the transfer */
190 if (!am_server && log_format)
191 log_send(file, &stats, iflags);
193 if (protocol_version >= 29) {
194 write_byte(f_out, iflags);
195 write_byte(f_out, iflags >> 8);
200 initial_stats = stats;
202 if (!(s = receive_sums(f_in))) {
203 io_error |= IOERR_GENERAL;
204 rprintf(FERROR, "receive_sums failed\n");
208 fd = do_open(fname, O_RDONLY, 0);
210 if (errno == ENOENT) {
211 enum logcode c = am_daemon
212 && protocol_version < 28 ? FERROR
214 io_error |= IOERR_VANISHED;
215 rprintf(c, "file has vanished: %s\n",
218 io_error |= IOERR_GENERAL;
219 rsyserr(FERROR, errno,
220 "send_files failed to open %s",
227 /* map the local file */
228 if (do_fstat(fd, &st) != 0) {
229 io_error |= IOERR_GENERAL;
230 rsyserr(FERROR, errno, "fstat failed");
237 int32 read_size = MAX(s->blength * 3, MAX_MAP_SIZE);
238 mbuf = map_file(fd, st.st_size, read_size, s->blength);
243 rprintf(FINFO, "send_files mapped %s of size %.0f\n",
244 safe_fname(fname), (double)st.st_size);
248 if (protocol_version >= 29) {
249 write_byte(f_out, iflags);
250 write_byte(f_out, iflags >> 8);
252 write_sum_head(f_out, s);
255 rprintf(FINFO, "calling match_sums %s\n",
259 if (log_before_transfer)
260 log_send(file, &initial_stats, iflags);
261 else if (!am_server && verbose && do_progress)
262 rprintf(FINFO, "%s\n", safe_fname(fname2));
264 set_compression(fname);
266 match_sums(f_out, s, mbuf, st.st_size);
267 if (!log_before_transfer)
268 log_send(file, &initial_stats, iflags);
271 j = unmap_file(mbuf);
273 io_error |= IOERR_GENERAL;
275 "read errors mapping %s",
284 rprintf(FINFO, "sender finished %s\n",
288 make_backups = save_make_backups;
291 rprintf(FINFO, "send files finished\n");
295 write_int(f_out, -1);