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.
26 extern int log_before_transfer;
27 extern int log_format_has_i;
28 extern int daemon_log_format_has_i;
29 extern int csum_length;
31 extern int protocol_version;
32 extern int remove_sent_files;
33 extern int updating_basis_file;
34 extern int make_backups;
35 extern int do_progress;
37 extern struct stats stats;
38 extern char *log_format;
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,
102 static struct file_list *the_flist;
104 void successful_send(int i)
106 char fname[MAXPATHLEN];
107 struct file_struct *file;
110 if (!the_flist || i < 0 || i >= the_flist->count)
113 file = the_flist->files[i];
114 /* The generator might tell us about symlinks we didn't send. */
115 if (!(file->flags & FLAG_SENT) && !S_ISLNK(file->mode))
117 if (file->dir.root) {
118 offset = stringjoin(fname, sizeof fname,
119 file->dir.root, "/", NULL);
122 f_name_to(file, fname + offset);
123 if (remove_sent_files && do_unlink(fname) == 0 && verbose > 1) {
124 rprintf(FINFO, "sender removed %s\n",
125 safe_fname(fname + offset));
129 void send_files(struct file_list *flist, int f_out, int f_in)
132 struct sum_struct *s;
133 struct map_struct *mbuf = NULL;
135 char *fname2, fname[MAXPATHLEN];
137 struct file_struct *file;
139 struct stats initial_stats;
140 int save_make_backups = make_backups;
141 int itemizing = am_daemon ? daemon_log_format_has_i
142 : !am_server && log_format_has_i;
146 rprintf(FINFO, "send_files starting\n");
157 csum_length = SUM_LENGTH;
158 write_int(f_out, -1);
160 rprintf(FINFO, "send_files phase=%d\n", phase);
161 /* For inplace: redo phase turns off the backup
162 * flag so that we do a regular inplace send. */
169 if (i < 0 || i >= flist->count) {
170 rprintf(FERROR, "Invalid file index %d (count=%d)\n",
172 exit_cleanup(RERR_PROTOCOL);
175 file = flist->files[i];
176 if (file->dir.root) {
177 /* N.B. We're sure that this fits, so offset is OK. */
178 offset = strlcpy(fname, file->dir.root, sizeof fname);
179 if (!offset || fname[offset-1] != '/')
180 fname[offset++] = '/';
183 fname2 = f_name_to(file, fname + offset);
186 rprintf(FINFO, "send_files(%d, %s)\n", i, fname);
188 if (protocol_version >= 29) {
189 iflags = read_shortint(f_in);
190 if (!(iflags & ITEM_UPDATING) || !S_ISREG(file->mode)) {
191 int see_item = itemizing && (iflags || verbose > 1);
193 write_shortint(f_out, iflags);
195 if (am_daemon && !dry_run && see_item)
196 log_send(file, &stats, iflags);
197 } else if (see_item || iflags & ITEM_UPDATING
198 || (S_ISDIR(file->mode)
199 && iflags & ITEM_REPORT_TIME))
200 log_send(file, &stats, iflags);
204 iflags = ITEM_UPDATING | ITEM_MISSING_DATA;
206 if (inplace && protocol_version >= 29) {
207 uchar fnamecmp_type = read_byte(f_in);
208 updating_basis_file = fnamecmp_type == FNAMECMP_FNAME;
210 updating_basis_file = inplace && !make_backups;
212 if (!S_ISREG(file->mode)) {
213 rprintf(FERROR, "[%s] got index of non-regular file: %d\n",
215 exit_cleanup(RERR_PROTOCOL);
218 stats.current_file_index = i;
219 stats.num_transferred_files++;
220 stats.total_transferred_size += file->length;
222 if (dry_run) { /* log the transfer */
223 if (!am_server && log_format)
224 log_send(file, &stats, iflags);
226 if (protocol_version >= 29)
227 write_shortint(f_out, iflags);
231 initial_stats = stats;
233 if (!(s = receive_sums(f_in))) {
234 io_error |= IOERR_GENERAL;
235 rprintf(FERROR, "receive_sums failed\n");
239 fd = do_open(fname, O_RDONLY, 0);
241 if (errno == ENOENT) {
242 enum logcode c = am_daemon
243 && protocol_version < 28 ? FERROR
245 io_error |= IOERR_VANISHED;
246 rprintf(c, "file has vanished: %s\n",
249 io_error |= IOERR_GENERAL;
250 rsyserr(FERROR, errno,
251 "send_files failed to open %s",
258 /* map the local file */
259 if (do_fstat(fd, &st) != 0) {
260 io_error |= IOERR_GENERAL;
261 rsyserr(FERROR, errno, "fstat failed");
268 int32 read_size = MAX(s->blength * 3, MAX_MAP_SIZE);
269 mbuf = map_file(fd, st.st_size, read_size, s->blength);
274 rprintf(FINFO, "send_files mapped %s of size %.0f\n",
275 safe_fname(fname), (double)st.st_size);
279 if (protocol_version >= 29)
280 write_shortint(f_out, iflags);
281 write_sum_head(f_out, s);
284 rprintf(FINFO, "calling match_sums %s\n",
288 if (log_before_transfer)
289 log_send(file, &initial_stats, iflags);
290 else if (!am_server && verbose && do_progress)
291 rprintf(FINFO, "%s\n", safe_fname(fname2));
293 set_compression(fname);
295 match_sums(f_out, s, mbuf, st.st_size);
296 if (!log_before_transfer)
297 log_send(file, &initial_stats, iflags);
300 j = unmap_file(mbuf);
302 io_error |= IOERR_GENERAL;
304 "read errors mapping %s",
313 rprintf(FINFO, "sender finished %s\n",
317 /* Flag that we actually sent this entry. */
318 file->flags |= FLAG_SENT;
320 make_backups = save_make_backups;
323 rprintf(FINFO, "send files finished\n");
327 write_int(f_out, -1);