2 * Routines only used by the sending process.
4 * Copyright (C) 1996 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2003-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 version 2 as
10 * published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
28 extern int inc_recurse;
29 extern int log_before_transfer;
30 extern int stdout_format_has_i;
31 extern int logfile_format_has_i;
32 extern int csum_length;
33 extern int append_mode;
35 extern int allowed_lull;
36 extern int protocol_version;
37 extern int remove_source_files;
38 extern int updating_basis_file;
39 extern int make_backups;
40 extern int do_progress;
43 extern int write_batch;
44 extern struct stats stats;
45 extern struct file_list *cur_flist, *first_flist;
47 #define FILECNT_LOOKAHEAD 1000
52 * The sender gets checksums from the generator, calculates deltas,
53 * and transmits them to the receiver. The sender process runs on the
54 * machine holding the source files.
58 * Receive the checksums for a buffer
60 static struct sum_struct *receive_sums(int f)
64 int lull_mod = allowed_lull * 5;
67 if (!(s = new(struct sum_struct)))
68 out_of_memory("receive_sums");
75 rprintf(FINFO, "count=%.0f n=%ld rem=%ld\n",
76 (double)s->count, (long)s->blength, (long)s->remainder);
79 if (append_mode > 0) {
80 s->flength = (OFF_T)s->count * s->blength;
82 s->flength -= s->blength - s->remainder;
89 if (!(s->sums = new_array(struct sum_buf, s->count)))
90 out_of_memory("receive_sums");
92 for (i = 0; i < s->count; i++) {
93 s->sums[i].sum1 = read_int(f);
94 read_buf(f, s->sums[i].sum2, s->s2length);
96 s->sums[i].offset = offset;
99 if (i == s->count-1 && s->remainder != 0)
100 s->sums[i].len = s->remainder;
102 s->sums[i].len = s->blength;
103 offset += s->sums[i].len;
105 if (allowed_lull && !(i % lull_mod))
106 maybe_send_keepalive();
110 "chunk[%d] len=%d offset=%.0f sum1=%08x\n",
111 i, s->sums[i].len, (double)s->sums[i].offset,
121 void successful_send(int ndx)
123 char fname[MAXPATHLEN];
124 struct file_struct *file;
125 struct file_list *flist;
127 if (!remove_source_files)
130 if (!(flist = flist_for_ndx(ndx))) {
132 "INTERNAL ERROR: unable to find flist for item %d\n",
137 file = flist->files[ndx - flist->ndx_start];
138 if (!push_flist_dir(F_ROOTDIR(file), -1))
142 if (do_unlink(fname) == 0) {
144 rprintf(FINFO, "sender removed %s\n", fname);
146 rsyserr(FERROR, errno, "sender failed to remove %s", fname);
149 void write_ndx_and_attrs(int f_out, int ndx, int iflags,
150 uchar fnamecmp_type, char *buf, int len)
152 write_ndx(f_out, ndx);
153 if (protocol_version < 29)
155 write_shortint(f_out, iflags);
156 if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
157 write_byte(f_out, fnamecmp_type);
158 if (iflags & ITEM_XNAME_FOLLOWS)
159 write_vstring(f_out, buf, len);
162 void send_files(int f_in, int f_out)
165 struct sum_struct *s;
166 struct map_struct *mbuf = NULL;
168 char fname[MAXPATHLEN], xname[MAXPATHLEN];
169 const char *path, *slash;
172 struct file_struct *file;
173 int phase = 0, max_phase = protocol_version >= 29 ? 2 : 1;
174 struct stats initial_stats;
175 int itemizing = am_server ? logfile_format_has_i : stdout_format_has_i;
176 enum logcode log_code = log_before_transfer ? FLOG : FINFO;
177 int f_xfer = write_batch < 0 ? batch_fd : f_out;
181 rprintf(FINFO, "send_files starting\n");
185 send_extra_file_list(f_out, FILECNT_LOOKAHEAD);
187 /* This call also sets cur_flist. */
188 ndx = read_ndx_and_attrs(f_in, f_out, &iflags,
189 &fnamecmp_type, xname, &xlen);
190 if (ndx == NDX_DONE) {
191 if (inc_recurse && first_flist) {
192 flist_free(first_flist);
194 write_ndx(f_out, NDX_DONE);
198 if (++phase > max_phase)
201 rprintf(FINFO, "send_files phase=%d\n", phase);
202 write_ndx(f_out, NDX_DONE);
207 send_extra_file_list(f_out, FILECNT_LOOKAHEAD);
209 file = cur_flist->files[ndx - cur_flist->ndx_start];
210 if (F_ROOTDIR(file)) {
211 path = F_ROOTDIR(file);
216 if (!push_flist_dir(F_ROOTDIR(file), -1))
221 rprintf(FINFO, "send_files(%d, %s%s%s)\n", ndx, path,slash,fname);
223 if (!(iflags & ITEM_TRANSFER)) {
224 maybe_log_item(file, iflags, itemizing, xname);
229 "got transfer request in phase 2 [%s]\n",
231 exit_cleanup(RERR_PROTOCOL);
234 if (file->flags & FLAG_FILE_SENT) {
235 if (csum_length == SHORT_SUM_LENGTH) {
236 /* For inplace: redo phase turns off the backup
237 * flag so that we do a regular inplace send. */
238 make_backups = -make_backups;
239 append_mode = -append_mode;
240 csum_length = SUM_LENGTH;
243 if (csum_length != SHORT_SUM_LENGTH) {
244 make_backups = -make_backups;
245 append_mode = -append_mode;
246 csum_length = SHORT_SUM_LENGTH;
250 updating_basis_file = inplace && (protocol_version >= 29
251 ? fnamecmp_type == FNAMECMP_FNAME : make_backups <= 0);
253 stats.current_file_index = ndx;
254 stats.num_transferred_files++;
255 stats.total_transferred_size += F_LENGTH(file);
257 if (!do_xfers) { /* log the transfer */
258 log_item(FCLIENT, file, &stats, iflags, NULL);
259 write_ndx_and_attrs(f_out, ndx, iflags, fnamecmp_type,
264 initial_stats = stats;
266 if (!(s = receive_sums(f_in))) {
267 io_error |= IOERR_GENERAL;
268 rprintf(FERROR, "receive_sums failed\n");
269 exit_cleanup(RERR_PROTOCOL);
272 fd = do_open(fname, O_RDONLY, 0);
274 if (errno == ENOENT) {
275 enum logcode c = am_daemon
276 && protocol_version < 28 ? FERROR
278 io_error |= IOERR_VANISHED;
279 rprintf(c, "file has vanished: %s\n",
282 io_error |= IOERR_GENERAL;
283 rsyserr(FERROR, errno,
284 "send_files failed to open %s",
288 if (protocol_version >= 30)
289 send_msg_int(MSG_NO_SEND, ndx);
293 /* map the local file */
294 if (do_fstat(fd, &st) != 0) {
295 io_error |= IOERR_GENERAL;
296 rsyserr(FERROR, errno, "fstat failed");
299 exit_cleanup(RERR_PROTOCOL);
303 int32 read_size = MAX(s->blength * 3, MAX_MAP_SIZE);
304 mbuf = map_file(fd, st.st_size, read_size, s->blength);
309 rprintf(FINFO, "send_files mapped %s%s%s of size %.0f\n",
310 path,slash,fname, (double)st.st_size);
313 write_ndx_and_attrs(f_out, ndx, iflags, fnamecmp_type,
315 write_sum_head(f_xfer, s);
318 rprintf(FINFO, "calling match_sums %s%s%s\n", path,slash,fname);
320 if (log_before_transfer)
321 log_item(FCLIENT, file, &initial_stats, iflags, NULL);
322 else if (!am_server && verbose && do_progress)
323 rprintf(FCLIENT, "%s\n", fname);
325 set_compression(fname);
327 match_sums(f_xfer, s, mbuf, st.st_size);
329 end_progress(st.st_size);
331 log_item(log_code, file, &initial_stats, iflags, NULL);
334 j = unmap_file(mbuf);
336 io_error |= IOERR_GENERAL;
338 "read errors mapping %s",
347 rprintf(FINFO, "sender finished %s%s%s\n", path,slash,fname);
349 /* Flag that we actually sent this entry. */
350 file->flags |= FLAG_FILE_SENT;
352 if (make_backups < 0)
353 make_backups = -make_backups;
356 rprintf(FINFO, "send files finished\n");
360 write_ndx(f_out, NDX_DONE);