2 * Routines only used by the sending process.
4 * Copyright (C) 1996 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2003-2008 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 3 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, visit the http://fsf.org website.
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 preserve_xattrs;
37 extern int protocol_version;
38 extern int remove_source_files;
39 extern int updating_basis_file;
40 extern int make_backups;
43 extern int write_batch;
44 extern struct stats stats;
45 extern struct file_list *cur_flist, *first_flist, *dir_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");
72 if (DEBUG_GTE(DELTASUM, 3)) {
73 rprintf(FINFO, "count=%s n=%ld rem=%ld\n",
74 big_num(s->count, 0), (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();
106 if (DEBUG_GTE(DELTASUM, 3)) {
108 "chunk[%d] len=%d offset=%s sum1=%08x\n",
109 i, s->sums[i].len, big_num(s->sums[i].offset, 0),
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 (!change_pathname(file, NULL, 0))
140 if (do_unlink(fname) == 0) {
141 if (INFO_GTE(REMOVE, 1))
142 rprintf(FINFO, "sender removed %s\n", fname);
144 rsyserr(FERROR, errno, "sender failed to remove %s", fname);
147 static void write_ndx_and_attrs(int f_out, int ndx, int iflags,
148 const char *fname, struct file_struct *file,
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);
159 #ifdef SUPPORT_XATTRS
160 if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && !dry_run)
161 send_xattr_request(fname, file, f_out);
165 void send_files(int f_in, int f_out)
168 struct sum_struct *s;
169 struct map_struct *mbuf = NULL;
171 char fname[MAXPATHLEN], xname[MAXPATHLEN];
172 const char *path, *slash;
175 struct file_struct *file;
176 int phase = 0, max_phase = protocol_version >= 29 ? 2 : 1;
177 struct stats initial_stats;
178 int itemizing = am_server ? logfile_format_has_i : stdout_format_has_i;
179 enum logcode log_code = log_before_transfer ? FLOG : FINFO;
180 int f_xfer = write_batch < 0 ? batch_fd : f_out;
183 if (DEBUG_GTE(SEND, 1))
184 rprintf(FINFO, "send_files starting\n");
188 send_extra_file_list(f_out, FILECNT_LOOKAHEAD);
190 /* This call also sets cur_flist. */
191 ndx = read_ndx_and_attrs(f_in, &iflags, &fnamecmp_type,
193 if (ndx == NDX_DONE) {
194 if (!am_server && INFO_GTE(PROGRESS, 2) && cur_flist) {
195 set_current_file_index(NULL, 0);
198 if (inc_recurse && first_flist) {
199 flist_free(first_flist);
201 write_ndx(f_out, NDX_DONE);
205 if (++phase > max_phase)
207 if (DEBUG_GTE(SEND, 1))
208 rprintf(FINFO, "send_files phase=%d\n", phase);
209 write_ndx(f_out, NDX_DONE);
214 send_extra_file_list(f_out, FILECNT_LOOKAHEAD);
216 if (ndx - cur_flist->ndx_start >= 0)
217 file = cur_flist->files[ndx - cur_flist->ndx_start];
219 file = dir_flist->files[cur_flist->parent_ndx];
220 if (F_PATHNAME(file)) {
221 path = F_PATHNAME(file);
226 if (!change_pathname(file, NULL, 0))
230 if (DEBUG_GTE(SEND, 1))
231 rprintf(FINFO, "send_files(%d, %s%s%s)\n", ndx, path,slash,fname);
233 #ifdef SUPPORT_XATTRS
234 if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && !dry_run)
235 recv_xattr_request(file, f_in);
238 if (!(iflags & ITEM_TRANSFER)) {
239 maybe_log_item(file, iflags, itemizing, xname);
240 write_ndx_and_attrs(f_out, ndx, iflags, fname, file,
241 fnamecmp_type, xname, xlen);
246 "got transfer request in phase 2 [%s]\n",
248 exit_cleanup(RERR_PROTOCOL);
251 if (file->flags & FLAG_FILE_SENT) {
252 if (csum_length == SHORT_SUM_LENGTH) {
253 /* For inplace: redo phase turns off the backup
254 * flag so that we do a regular inplace send. */
255 make_backups = -make_backups;
256 append_mode = -append_mode;
257 csum_length = SUM_LENGTH;
260 if (csum_length != SHORT_SUM_LENGTH) {
261 make_backups = -make_backups;
262 append_mode = -append_mode;
263 csum_length = SHORT_SUM_LENGTH;
267 updating_basis_file = inplace && (protocol_version >= 29
268 ? fnamecmp_type == FNAMECMP_FNAME : make_backups <= 0);
270 if (!am_server && INFO_GTE(PROGRESS, 1))
271 set_current_file_index(file, ndx);
272 stats.num_transferred_files++;
273 stats.total_transferred_size += F_LENGTH(file);
275 if (!do_xfers) { /* log the transfer */
276 log_item(FCLIENT, file, &stats, iflags, NULL);
277 write_ndx_and_attrs(f_out, ndx, iflags, fname, file,
278 fnamecmp_type, xname, xlen);
282 initial_stats = stats;
284 if (!(s = receive_sums(f_in))) {
285 io_error |= IOERR_GENERAL;
286 rprintf(FERROR, "receive_sums failed\n");
287 exit_cleanup(RERR_PROTOCOL);
290 fd = do_open(fname, O_RDONLY, 0);
292 if (errno == ENOENT) {
293 enum logcode c = am_daemon
294 && protocol_version < 28 ? FERROR
296 io_error |= IOERR_VANISHED;
297 rprintf(c, "file has vanished: %s\n",
300 io_error |= IOERR_GENERAL;
301 rsyserr(FERROR_XFER, errno,
302 "send_files failed to open %s",
306 if (protocol_version >= 30)
307 send_msg_int(MSG_NO_SEND, ndx);
311 /* map the local file */
312 if (do_fstat(fd, &st) != 0) {
313 io_error |= IOERR_GENERAL;
314 rsyserr(FERROR, errno, "fstat failed");
317 exit_cleanup(RERR_PROTOCOL);
321 int32 read_size = MAX(s->blength * 3, MAX_MAP_SIZE);
322 mbuf = map_file(fd, st.st_size, read_size, s->blength);
326 if (DEBUG_GTE(DELTASUM, 2)) {
327 rprintf(FINFO, "send_files mapped %s%s%s of size %s\n",
328 path,slash,fname, big_num(st.st_size, 0));
331 write_ndx_and_attrs(f_out, ndx, iflags, fname, file,
332 fnamecmp_type, xname, xlen);
333 write_sum_head(f_xfer, s);
335 if (DEBUG_GTE(DELTASUM, 2))
336 rprintf(FINFO, "calling match_sums %s%s%s\n", path,slash,fname);
338 if (log_before_transfer)
339 log_item(FCLIENT, file, &initial_stats, iflags, NULL);
340 else if (!am_server && INFO_GTE(NAME, 1) && INFO_EQ(PROGRESS, 1))
341 rprintf(FCLIENT, "%s\n", fname);
343 set_compression(fname);
345 match_sums(f_xfer, s, mbuf, st.st_size);
346 if (INFO_GTE(PROGRESS, 1))
347 end_progress(st.st_size);
349 log_item(log_code, file, &initial_stats, iflags, NULL);
352 j = unmap_file(mbuf);
354 io_error |= IOERR_GENERAL;
355 rsyserr(FERROR_XFER, j,
356 "read errors mapping %s",
364 if (DEBUG_GTE(SEND, 1))
365 rprintf(FINFO, "sender finished %s%s%s\n", path,slash,fname);
367 /* Flag that we actually sent this entry. */
368 file->flags |= FLAG_FILE_SENT;
370 if (make_backups < 0)
371 make_backups = -make_backups;
373 if (DEBUG_GTE(SEND, 1))
374 rprintf(FINFO, "send files finished\n");
378 write_ndx(f_out, NDX_DONE);