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.
24 extern int log_before_transfer;
25 extern int log_format_has_i;
26 extern int daemon_log_format_has_i;
29 extern int csum_length;
30 extern struct stats stats;
32 extern int protocol_version;
33 extern int updating_basis_file;
34 extern int make_backups;
35 extern int do_progress;
37 extern char *log_format;
38 extern struct stats stats;
44 * The sender gets checksums from the generator, calculates deltas,
45 * and transmits them to the receiver. The sender process runs on the
46 * machine holding the source files.
50 * Receive the checksums for a buffer
52 static struct sum_struct *receive_sums(int f)
58 if (!(s = new(struct sum_struct)))
59 out_of_memory("receive_sums");
66 rprintf(FINFO, "count=%.0f n=%ld rem=%ld\n",
67 (double)s->count, (long)s->blength, (long)s->remainder);
73 if (!(s->sums = new_array(struct sum_buf, s->count)))
74 out_of_memory("receive_sums");
76 for (i = 0; i < s->count; i++) {
77 s->sums[i].sum1 = read_int(f);
78 read_buf(f, s->sums[i].sum2, s->s2length);
80 s->sums[i].offset = offset;
83 if (i == s->count-1 && s->remainder != 0)
84 s->sums[i].len = s->remainder;
86 s->sums[i].len = s->blength;
87 offset += s->sums[i].len;
91 "chunk[%d] len=%d offset=%.0f sum1=%08x\n",
92 i, s->sums[i].len, (double)s->sums[i].offset,
104 void send_files(struct file_list *flist, int f_out, int f_in)
107 struct sum_struct *s;
108 struct map_struct *mbuf = NULL;
110 char *fname2, fname[MAXPATHLEN];
112 struct file_struct *file;
114 struct stats initial_stats;
115 int save_make_backups = make_backups;
116 int itemizing = am_daemon ? daemon_log_format_has_i
117 : !am_server && log_format_has_i;
121 rprintf(FINFO, "send_files starting\n");
130 csum_length = SUM_LENGTH;
131 write_int(f_out, -1);
133 rprintf(FINFO, "send_files phase=%d\n", phase);
134 /* For inplace: redo phase turns off the backup
135 * flag so that we do a regular inplace send. */
142 if (i < 0 || i >= flist->count) {
143 rprintf(FERROR, "Invalid file index %d (count=%d)\n",
145 exit_cleanup(RERR_PROTOCOL);
148 file = flist->files[i];
149 if (file->dir.root) {
150 /* N.B. We're sure that this fits, so offset is OK. */
151 offset = strlcpy(fname, file->dir.root, sizeof fname);
152 if (!offset || fname[offset-1] != '/')
153 fname[offset++] = '/';
156 fname2 = f_name_to(file, fname + offset);
159 rprintf(FINFO, "send_files(%d, %s)\n", i, fname);
161 if (protocol_version >= 29) {
162 iflags = read_shortint(f_in);
163 if (!(iflags & ITEM_UPDATING) || !S_ISREG(file->mode)) {
164 int see_item = itemizing && (iflags || verbose > 1);
166 write_shortint(f_out, iflags);
168 if (am_daemon && !dry_run && see_item)
169 log_send(file, &stats, iflags);
170 } else if (see_item || iflags & ITEM_UPDATING
171 || (S_ISDIR(file->mode)
172 && iflags & ITEM_REPORT_TIME))
173 log_send(file, &stats, iflags);
177 iflags = ITEM_UPDATING | ITEM_MISSING_DATA;
179 if (inplace && protocol_version >= 29) {
180 uchar fnamecmp_type = read_byte(f_in);
181 updating_basis_file = fnamecmp_type == FNAMECMP_FNAME;
183 updating_basis_file = inplace && !make_backups;
185 if (!S_ISREG(file->mode)) {
186 rprintf(FERROR, "[%s] got index of non-regular file: %d\n",
188 exit_cleanup(RERR_PROTOCOL);
191 stats.current_file_index = i;
192 stats.num_transferred_files++;
193 stats.total_transferred_size += file->length;
195 if (dry_run) { /* log the transfer */
196 if (!am_server && log_format)
197 log_send(file, &stats, iflags);
199 if (protocol_version >= 29)
200 write_shortint(f_out, iflags);
204 initial_stats = stats;
206 if (!(s = receive_sums(f_in))) {
207 io_error |= IOERR_GENERAL;
208 rprintf(FERROR, "receive_sums failed\n");
212 fd = do_open(fname, O_RDONLY, 0);
214 if (errno == ENOENT) {
215 enum logcode c = am_daemon
216 && protocol_version < 28 ? FERROR
218 io_error |= IOERR_VANISHED;
219 rprintf(c, "file has vanished: %s\n",
222 io_error |= IOERR_GENERAL;
223 rsyserr(FERROR, errno,
224 "send_files failed to open %s",
231 /* map the local file */
232 if (do_fstat(fd, &st) != 0) {
233 io_error |= IOERR_GENERAL;
234 rsyserr(FERROR, errno, "fstat failed");
241 int32 read_size = MAX(s->blength * 3, MAX_MAP_SIZE);
242 mbuf = map_file(fd, st.st_size, read_size, s->blength);
247 rprintf(FINFO, "send_files mapped %s of size %.0f\n",
248 safe_fname(fname), (double)st.st_size);
252 if (protocol_version >= 29)
253 write_shortint(f_out, iflags);
254 write_sum_head(f_out, s);
257 rprintf(FINFO, "calling match_sums %s\n",
261 if (log_before_transfer)
262 log_send(file, &initial_stats, iflags);
263 else if (!am_server && verbose && do_progress)
264 rprintf(FINFO, "%s\n", safe_fname(fname2));
266 set_compression(fname);
268 match_sums(f_out, s, mbuf, st.st_size);
269 if (!log_before_transfer)
270 log_send(file, &initial_stats, iflags);
273 j = unmap_file(mbuf);
275 io_error |= IOERR_GENERAL;
277 "read errors mapping %s",
286 rprintf(FINFO, "sender finished %s\n",
290 make_backups = save_make_backups;
293 rprintf(FINFO, "send files finished\n");
297 write_int(f_out, -1);