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;
50 * The sender gets checksums from the generator, calculates deltas,
51 * and transmits them to the receiver. The sender process runs on the
52 * machine holding the source files.
56 * Receive the checksums for a buffer
58 static struct sum_struct *receive_sums(int f)
62 int lull_mod = allowed_lull * 5;
65 if (!(s = new(struct sum_struct)))
66 out_of_memory("receive_sums");
73 rprintf(FINFO, "count=%.0f n=%ld rem=%ld\n",
74 (double)s->count, (long)s->blength, (long)s->remainder);
77 if (append_mode > 0) {
78 s->flength = (OFF_T)s->count * s->blength;
80 s->flength -= s->blength - s->remainder;
87 if (!(s->sums = new_array(struct sum_buf, s->count)))
88 out_of_memory("receive_sums");
90 for (i = 0; i < s->count; i++) {
91 s->sums[i].sum1 = read_int(f);
92 read_buf(f, s->sums[i].sum2, s->s2length);
94 s->sums[i].offset = offset;
97 if (i == s->count-1 && s->remainder != 0)
98 s->sums[i].len = s->remainder;
100 s->sums[i].len = s->blength;
101 offset += s->sums[i].len;
103 if (allowed_lull && !(i % lull_mod))
104 maybe_send_keepalive();
108 "chunk[%d] len=%d offset=%.0f sum1=%08x\n",
109 i, s->sums[i].len, (double)s->sums[i].offset,
119 void successful_send(int ndx)
121 char fname[MAXPATHLEN];
122 struct file_struct *file;
123 struct file_list *flist;
125 if (!remove_source_files)
128 if (!(flist = flist_for_ndx(ndx))) {
130 "INTERNAL ERROR: unable to find flist for item %d\n",
135 file = flist->files[ndx - flist->ndx_start];
136 if (!push_flist_dir(F_ROOTDIR(file), -1))
140 if (do_unlink(fname) == 0) {
142 rprintf(FINFO, "sender removed %s\n", fname);
144 rsyserr(FERROR, errno, "sender failed to remove %s", fname);
147 void write_ndx_and_attrs(int f_out, int ndx, int iflags,
148 uchar fnamecmp_type, char *buf, int len)
150 write_ndx(f_out, ndx);
151 if (protocol_version < 29)
153 write_shortint(f_out, iflags);
154 if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
155 write_byte(f_out, fnamecmp_type);
156 if (iflags & ITEM_XNAME_FOLLOWS)
157 write_vstring(f_out, buf, len);
160 void send_files(int f_in, int f_out)
163 struct sum_struct *s;
164 struct map_struct *mbuf = NULL;
166 char fname[MAXPATHLEN], xname[MAXPATHLEN];
167 const char *path, *slash;
170 struct file_struct *file;
171 int phase = 0, max_phase = protocol_version >= 29 ? 2 : 1;
172 struct stats initial_stats;
173 int itemizing = am_server ? logfile_format_has_i : stdout_format_has_i;
174 enum logcode log_code = log_before_transfer ? FLOG : FINFO;
175 int f_xfer = write_batch < 0 ? batch_fd : f_out;
179 rprintf(FINFO, "send_files starting\n");
183 send_extra_file_list(f_out, 1000);
185 /* This call also sets cur_flist. */
186 ndx = read_ndx_and_attrs(f_in, f_out, &iflags,
187 &fnamecmp_type, xname, &xlen);
188 if (ndx == NDX_DONE) {
189 if (inc_recurse && first_flist) {
190 flist_free(first_flist);
192 write_ndx(f_out, NDX_DONE);
196 if (++phase > max_phase)
199 rprintf(FINFO, "send_files phase=%d\n", phase);
200 write_ndx(f_out, NDX_DONE);
204 file = cur_flist->files[ndx - cur_flist->ndx_start];
205 if (F_ROOTDIR(file)) {
206 path = F_ROOTDIR(file);
211 if (!push_flist_dir(F_ROOTDIR(file), -1))
216 rprintf(FINFO, "send_files(%d, %s%s%s)\n", ndx, path,slash,fname);
218 if (!(iflags & ITEM_TRANSFER)) {
219 maybe_log_item(file, iflags, itemizing, xname);
224 "got transfer request in phase 2 [%s]\n",
226 exit_cleanup(RERR_PROTOCOL);
229 if (file->flags & FLAG_FILE_SENT) {
230 if (csum_length == SHORT_SUM_LENGTH) {
231 /* For inplace: redo phase turns off the backup
232 * flag so that we do a regular inplace send. */
233 make_backups = -make_backups;
234 append_mode = -append_mode;
235 csum_length = SUM_LENGTH;
238 if (csum_length != SHORT_SUM_LENGTH) {
239 make_backups = -make_backups;
240 append_mode = -append_mode;
241 csum_length = SHORT_SUM_LENGTH;
245 updating_basis_file = inplace && (protocol_version >= 29
246 ? fnamecmp_type == FNAMECMP_FNAME : make_backups <= 0);
248 stats.current_file_index = ndx;
249 stats.num_transferred_files++;
250 stats.total_transferred_size += F_LENGTH(file);
252 if (!do_xfers) { /* log the transfer */
253 log_item(FCLIENT, file, &stats, iflags, NULL);
254 write_ndx_and_attrs(f_out, ndx, iflags, fnamecmp_type,
259 initial_stats = stats;
261 if (!(s = receive_sums(f_in))) {
262 io_error |= IOERR_GENERAL;
263 rprintf(FERROR, "receive_sums failed\n");
264 exit_cleanup(RERR_PROTOCOL);
267 fd = do_open(fname, O_RDONLY, 0);
269 if (errno == ENOENT) {
270 enum logcode c = am_daemon
271 && protocol_version < 28 ? FERROR
273 io_error |= IOERR_VANISHED;
274 rprintf(c, "file has vanished: %s\n",
277 io_error |= IOERR_GENERAL;
278 rsyserr(FERROR, errno,
279 "send_files failed to open %s",
283 if (protocol_version >= 30)
284 send_msg_int(MSG_NO_SEND, ndx);
288 /* map the local file */
289 if (do_fstat(fd, &st) != 0) {
290 io_error |= IOERR_GENERAL;
291 rsyserr(FERROR, errno, "fstat failed");
294 exit_cleanup(RERR_PROTOCOL);
298 int32 read_size = MAX(s->blength * 3, MAX_MAP_SIZE);
299 mbuf = map_file(fd, st.st_size, read_size, s->blength);
304 rprintf(FINFO, "send_files mapped %s%s%s of size %.0f\n",
305 path,slash,fname, (double)st.st_size);
308 write_ndx_and_attrs(f_out, ndx, iflags, fnamecmp_type,
310 write_sum_head(f_xfer, s);
313 rprintf(FINFO, "calling match_sums %s%s%s\n", path,slash,fname);
315 if (log_before_transfer)
316 log_item(FCLIENT, file, &initial_stats, iflags, NULL);
317 else if (!am_server && verbose && do_progress)
318 rprintf(FCLIENT, "%s\n", fname);
320 set_compression(fname);
322 match_sums(f_xfer, s, mbuf, st.st_size);
324 end_progress(st.st_size);
326 log_item(log_code, file, &initial_stats, iflags, NULL);
329 j = unmap_file(mbuf);
331 io_error |= IOERR_GENERAL;
333 "read errors mapping %s",
342 rprintf(FINFO, "sender finished %s%s%s\n", path,slash,fname);
344 /* Flag that we actually sent this entry. */
345 file->flags |= FLAG_FILE_SENT;
347 if (make_backups < 0)
348 make_backups = -make_backups;
351 rprintf(FINFO, "send files finished\n");
355 write_ndx(f_out, NDX_DONE);