2 * Routines only used by the sending process.
4 * Copyright (C) 1996 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2003, 2004, 2005, 2006, 2007 Wayne Davison
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
29 extern int inc_recurse;
30 extern int log_before_transfer;
31 extern int stdout_format_has_i;
32 extern int logfile_format_has_i;
33 extern int csum_length;
34 extern int append_mode;
36 extern int allowed_lull;
37 extern int protocol_version;
38 extern int remove_source_files;
39 extern int updating_basis_file;
40 extern int make_backups;
41 extern int do_progress;
44 extern int write_batch;
45 extern struct stats stats;
46 extern struct file_list *cur_flist, *first_flist;
51 * The sender gets checksums from the generator, calculates deltas,
52 * and transmits them to the receiver. The sender process runs on the
53 * machine holding the source files.
57 * Receive the checksums for a buffer
59 static struct sum_struct *receive_sums(int f)
63 int lull_mod = allowed_lull * 5;
66 if (!(s = new(struct sum_struct)))
67 out_of_memory("receive_sums");
74 rprintf(FINFO, "count=%.0f n=%ld rem=%ld\n",
75 (double)s->count, (long)s->blength, (long)s->remainder);
78 if (append_mode > 0) {
79 s->flength = (OFF_T)s->count * s->blength;
81 s->flength -= s->blength - s->remainder;
88 if (!(s->sums = new_array(struct sum_buf, s->count)))
89 out_of_memory("receive_sums");
91 for (i = 0; i < s->count; i++) {
92 s->sums[i].sum1 = read_int(f);
93 read_buf(f, s->sums[i].sum2, s->s2length);
95 s->sums[i].offset = offset;
98 if (i == s->count-1 && s->remainder != 0)
99 s->sums[i].len = s->remainder;
101 s->sums[i].len = s->blength;
102 offset += s->sums[i].len;
104 if (allowed_lull && !(i % lull_mod))
105 maybe_send_keepalive();
109 "chunk[%d] len=%d offset=%.0f sum1=%08x\n",
110 i, s->sums[i].len, (double)s->sums[i].offset,
120 void successful_send(int ndx)
122 char fname[MAXPATHLEN];
123 struct file_struct *file;
124 struct file_list *flist;
126 if (!remove_source_files)
129 if (!(flist = flist_for_ndx(ndx))) {
131 "INTERNAL ERROR: unable to find flist for item %d\n",
136 file = flist->files[ndx - flist->ndx_start];
137 if (!push_flist_dir(F_ROOTDIR(file), -1))
141 if (do_unlink(fname) == 0) {
143 rprintf(FINFO, "sender removed %s\n", fname);
145 rsyserr(FERROR, errno, "sender failed to remove %s", fname);
148 void write_ndx_and_attrs(int f_out, int ndx, int iflags,
149 uchar fnamecmp_type, char *buf, int len)
151 write_ndx(f_out, ndx);
152 if (protocol_version < 29)
154 write_shortint(f_out, iflags);
155 if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
156 write_byte(f_out, fnamecmp_type);
157 if (iflags & ITEM_XNAME_FOLLOWS)
158 write_vstring(f_out, buf, len);
161 void send_files(int f_in, int f_out)
164 struct sum_struct *s;
165 struct map_struct *mbuf = NULL;
167 char fname[MAXPATHLEN], xname[MAXPATHLEN];
168 const char *path, *slash;
171 struct file_struct *file;
172 int phase = 0, max_phase = protocol_version >= 29 ? 2 : 1;
173 struct stats initial_stats;
174 int itemizing = am_server ? logfile_format_has_i : stdout_format_has_i;
175 enum logcode log_code = log_before_transfer ? FLOG : FINFO;
176 int f_xfer = write_batch < 0 ? batch_fd : f_out;
180 rprintf(FINFO, "send_files starting\n");
184 send_extra_file_list(f_out, 1000);
186 /* This call also sets cur_flist. */
187 ndx = read_ndx_and_attrs(f_in, f_out, &iflags,
188 &fnamecmp_type, xname, &xlen);
189 if (ndx == NDX_DONE) {
190 if (inc_recurse && first_flist) {
191 flist_free(first_flist);
193 write_ndx(f_out, NDX_DONE);
197 if (++phase > max_phase)
200 rprintf(FINFO, "send_files phase=%d\n", phase);
201 write_ndx(f_out, NDX_DONE);
205 file = cur_flist->files[ndx - cur_flist->ndx_start];
206 if (F_ROOTDIR(file)) {
207 path = F_ROOTDIR(file);
212 if (!push_flist_dir(F_ROOTDIR(file), -1))
217 rprintf(FINFO, "send_files(%d, %s%s%s)\n", ndx, path,slash,fname);
219 if (!(iflags & ITEM_TRANSFER)) {
220 maybe_log_item(file, iflags, itemizing, xname);
225 "got transfer request in phase 2 [%s]\n",
227 exit_cleanup(RERR_PROTOCOL);
230 if (file->flags & FLAG_FILE_SENT) {
231 if (csum_length == SHORT_SUM_LENGTH) {
232 /* For inplace: redo phase turns off the backup
233 * flag so that we do a regular inplace send. */
234 make_backups = -make_backups;
235 append_mode = -append_mode;
236 csum_length = SUM_LENGTH;
239 if (csum_length != SHORT_SUM_LENGTH) {
240 make_backups = -make_backups;
241 append_mode = -append_mode;
242 csum_length = SHORT_SUM_LENGTH;
246 updating_basis_file = inplace && (protocol_version >= 29
247 ? fnamecmp_type == FNAMECMP_FNAME : make_backups <= 0);
249 stats.current_file_index = ndx;
250 stats.num_transferred_files++;
251 stats.total_transferred_size += F_LENGTH(file);
253 if (!do_xfers) { /* log the transfer */
254 log_item(FCLIENT, file, &stats, iflags, NULL);
255 write_ndx_and_attrs(f_out, ndx, iflags, fnamecmp_type,
260 initial_stats = stats;
262 if (!(s = receive_sums(f_in))) {
263 io_error |= IOERR_GENERAL;
264 rprintf(FERROR, "receive_sums failed\n");
265 exit_cleanup(RERR_PROTOCOL);
268 fd = do_open(fname, O_RDONLY, 0);
270 if (errno == ENOENT) {
271 enum logcode c = am_daemon
272 && protocol_version < 28 ? FERROR
274 io_error |= IOERR_VANISHED;
275 rprintf(c, "file has vanished: %s\n",
278 io_error |= IOERR_GENERAL;
279 rsyserr(FERROR, errno,
280 "send_files failed to open %s",
284 if (protocol_version >= 30)
285 send_msg_int(MSG_NO_SEND, ndx);
289 /* map the local file */
290 if (do_fstat(fd, &st) != 0) {
291 io_error |= IOERR_GENERAL;
292 rsyserr(FERROR, errno, "fstat failed");
295 exit_cleanup(RERR_PROTOCOL);
299 int32 read_size = MAX(s->blength * 3, MAX_MAP_SIZE);
300 mbuf = map_file(fd, st.st_size, read_size, s->blength);
305 rprintf(FINFO, "send_files mapped %s%s%s of size %.0f\n",
306 path,slash,fname, (double)st.st_size);
309 write_ndx_and_attrs(f_out, ndx, iflags, fnamecmp_type,
311 write_sum_head(f_xfer, s);
314 rprintf(FINFO, "calling match_sums %s%s%s\n", path,slash,fname);
316 if (log_before_transfer)
317 log_item(FCLIENT, file, &initial_stats, iflags, NULL);
318 else if (!am_server && verbose && do_progress)
319 rprintf(FCLIENT, "%s\n", fname);
321 set_compression(fname);
323 match_sums(f_xfer, s, mbuf, st.st_size);
325 end_progress(st.st_size);
327 log_item(log_code, file, &initial_stats, iflags, NULL);
330 j = unmap_file(mbuf);
332 io_error |= IOERR_GENERAL;
334 "read errors mapping %s",
343 rprintf(FINFO, "sender finished %s%s%s\n", path,slash,fname);
345 /* Flag that we actually sent this entry. */
346 file->flags |= FLAG_FILE_SENT;
348 if (make_backups < 0)
349 make_backups = -make_backups;
352 rprintf(FINFO, "send files finished\n");
356 write_ndx(f_out, NDX_DONE);