2 * Routines only used by the sending process.
4 * Copyright (C) 1996 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2003-2009 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;
36 extern int allowed_lull;
37 extern int preserve_xattrs;
38 extern int protocol_version;
39 extern int remove_source_files;
40 extern int updating_basis_file;
41 extern int make_backups;
44 extern int write_batch;
45 extern int file_old_total;
46 extern BOOL we_send_keepalive_messages;
47 extern struct stats stats;
48 extern struct file_list *cur_flist, *first_flist, *dir_flist;
50 BOOL extra_flist_sending_enabled;
55 * The sender gets checksums from the generator, calculates deltas,
56 * and transmits them to the receiver. The sender process runs on the
57 * machine holding the source files.
61 * Receive the checksums for a buffer
63 static struct sum_struct *receive_sums(int f)
67 int lull_mod = allowed_lull * 5;
70 if (!(s = new(struct sum_struct)))
71 out_of_memory("receive_sums");
77 if (DEBUG_GTE(DELTASUM, 3)) {
78 rprintf(FINFO, "count=%s n=%ld rem=%ld\n",
79 big_num(s->count), (long)s->blength, (long)s->remainder);
82 if (append_mode > 0) {
83 s->flength = (OFF_T)s->count * s->blength;
85 s->flength -= s->blength - s->remainder;
92 if (!(s->sums = new_array(struct sum_buf, s->count)))
93 out_of_memory("receive_sums");
95 for (i = 0; i < s->count; i++) {
96 s->sums[i].sum1 = read_int(f);
97 read_buf(f, s->sums[i].sum2, s->s2length);
99 s->sums[i].offset = offset;
100 s->sums[i].flags = 0;
102 if (i == s->count-1 && s->remainder != 0)
103 s->sums[i].len = s->remainder;
105 s->sums[i].len = s->blength;
106 offset += s->sums[i].len;
108 if (we_send_keepalive_messages && !(i % lull_mod))
109 maybe_send_keepalive(time(NULL), True);
111 if (DEBUG_GTE(DELTASUM, 3)) {
113 "chunk[%d] len=%d offset=%s sum1=%08x\n",
114 i, s->sums[i].len, big_num(s->sums[i].offset),
124 void successful_send(int ndx)
126 char fname[MAXPATHLEN];
127 struct file_struct *file;
128 struct file_list *flist;
130 if (!remove_source_files)
133 flist = flist_for_ndx(ndx, "successful_send");
134 file = flist->files[ndx - flist->ndx_start];
135 if (!change_pathname(file, NULL, 0))
139 if (do_unlink(fname) == 0) {
140 if (INFO_GTE(REMOVE, 1))
141 rprintf(FINFO, "sender removed %s\n", fname);
143 rsyserr(FERROR, errno, "sender failed to remove %s", fname);
146 static void write_ndx_and_attrs(int f_out, int ndx, int iflags,
147 const char *fname, struct file_struct *file,
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);
158 #ifdef SUPPORT_XATTRS
159 if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && do_xfers)
160 send_xattr_request(fname, file, f_out);
164 void send_files(int f_in, int f_out)
167 struct sum_struct *s;
168 struct map_struct *mbuf = NULL;
170 char fname[MAXPATHLEN], xname[MAXPATHLEN];
171 const char *path, *slash;
174 struct file_struct *file;
175 int phase = 0, max_phase = protocol_version >= 29 ? 2 : 1;
176 int itemizing = am_server ? logfile_format_has_i : stdout_format_has_i;
177 enum logcode log_code = log_before_transfer ? FLOG : FINFO;
178 int f_xfer = write_batch < 0 ? batch_fd : f_out;
179 int save_io_error = io_error;
182 if (DEBUG_GTE(SEND, 1))
183 rprintf(FINFO, "send_files starting\n");
187 send_extra_file_list(f_out, MIN_FILECNT_LOOKAHEAD);
188 extra_flist_sending_enabled = !flist_eof;
191 /* This call also sets cur_flist. */
192 ndx = read_ndx_and_attrs(f_in, f_out, &iflags, &fnamecmp_type,
194 extra_flist_sending_enabled = False;
196 if (ndx == NDX_DONE) {
197 if (!am_server && INFO_GTE(PROGRESS, 2) && cur_flist) {
198 set_current_file_index(NULL, 0);
201 if (inc_recurse && first_flist) {
202 file_old_total -= first_flist->used;
203 flist_free(first_flist);
205 if (first_flist == cur_flist)
206 file_old_total = cur_flist->used;
207 write_ndx(f_out, NDX_DONE);
211 if (++phase > max_phase)
213 if (DEBUG_GTE(SEND, 1))
214 rprintf(FINFO, "send_files phase=%d\n", phase);
215 write_ndx(f_out, NDX_DONE);
220 send_extra_file_list(f_out, MIN_FILECNT_LOOKAHEAD);
222 if (ndx - cur_flist->ndx_start >= 0)
223 file = cur_flist->files[ndx - cur_flist->ndx_start];
225 file = dir_flist->files[cur_flist->parent_ndx];
226 if (F_PATHNAME(file)) {
227 path = F_PATHNAME(file);
232 if (!change_pathname(file, NULL, 0))
236 if (DEBUG_GTE(SEND, 1))
237 rprintf(FINFO, "send_files(%d, %s%s%s)\n", ndx, path,slash,fname);
239 #ifdef SUPPORT_XATTRS
240 if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && do_xfers)
241 recv_xattr_request(file, f_in);
244 if (!(iflags & ITEM_TRANSFER)) {
245 maybe_log_item(file, iflags, itemizing, xname);
246 write_ndx_and_attrs(f_out, ndx, iflags, fname, file,
247 fnamecmp_type, xname, xlen);
248 if (iflags & ITEM_IS_NEW) {
249 stats.created_files++;
250 if (S_ISREG(file->mode)) {
251 /* Nothing further to count. */
252 } else if (S_ISDIR(file->mode))
253 stats.created_dirs++;
255 else if (S_ISLNK(file->mode))
256 stats.created_symlinks++;
258 else if (IS_DEVICE(file->mode))
259 stats.created_devices++;
261 stats.created_specials++;
267 "got transfer request in phase 2 [%s]\n",
269 exit_cleanup(RERR_PROTOCOL);
272 if (file->flags & FLAG_FILE_SENT) {
273 if (csum_length == SHORT_SUM_LENGTH) {
274 /* For inplace: redo phase turns off the backup
275 * flag so that we do a regular inplace send. */
276 make_backups = -make_backups;
277 append_mode = -append_mode;
278 csum_length = SUM_LENGTH;
281 if (csum_length != SHORT_SUM_LENGTH) {
282 make_backups = -make_backups;
283 append_mode = -append_mode;
284 csum_length = SHORT_SUM_LENGTH;
286 if (iflags & ITEM_IS_NEW)
287 stats.created_files++;
290 updating_basis_file = inplace && (protocol_version >= 29
291 ? fnamecmp_type == FNAMECMP_FNAME : make_backups <= 0);
293 if (!am_server && INFO_GTE(PROGRESS, 1))
294 set_current_file_index(file, ndx);
295 stats.xferred_files++;
296 stats.total_transferred_size += F_LENGTH(file);
298 if (!log_before_transfer)
299 remember_initial_stats();
301 if (!do_xfers) { /* log the transfer */
302 log_item(FCLIENT, file, iflags, NULL);
303 write_ndx_and_attrs(f_out, ndx, iflags, fname, file,
304 fnamecmp_type, xname, xlen);
308 if (!(s = receive_sums(f_in))) {
309 io_error |= IOERR_GENERAL;
310 rprintf(FERROR_XFER, "receive_sums failed\n");
311 exit_cleanup(RERR_PROTOCOL);
314 fd = do_open(fname, O_RDONLY, 0);
316 if (errno == ENOENT) {
317 enum logcode c = am_daemon
318 && protocol_version < 28 ? FERROR
320 io_error |= IOERR_VANISHED;
321 rprintf(c, "file has vanished: %s\n",
324 io_error |= IOERR_GENERAL;
325 rsyserr(FERROR_XFER, errno,
326 "send_files failed to open %s",
330 if (protocol_version >= 30)
331 send_msg_int(MSG_NO_SEND, ndx);
335 /* map the local file */
336 if (do_fstat(fd, &st) != 0) {
337 io_error |= IOERR_GENERAL;
338 rsyserr(FERROR_XFER, errno, "fstat failed");
341 exit_cleanup(RERR_FILEIO);
345 int32 read_size = MAX(s->blength * 3, MAX_MAP_SIZE);
346 mbuf = map_file(fd, st.st_size, read_size, s->blength);
350 if (DEBUG_GTE(DELTASUM, 2)) {
351 rprintf(FINFO, "send_files mapped %s%s%s of size %s\n",
352 path,slash,fname, big_num(st.st_size));
355 write_ndx_and_attrs(f_out, ndx, iflags, fname, file,
356 fnamecmp_type, xname, xlen);
357 write_sum_head(f_xfer, s);
359 if (DEBUG_GTE(DELTASUM, 2))
360 rprintf(FINFO, "calling match_sums %s%s%s\n", path,slash,fname);
362 if (log_before_transfer)
363 log_item(FCLIENT, file, iflags, NULL);
364 else if (!am_server && INFO_GTE(NAME, 1) && INFO_EQ(PROGRESS, 1))
365 rprintf(FCLIENT, "%s\n", fname);
367 set_compression(fname);
369 match_sums(f_xfer, s, mbuf, st.st_size);
370 if (INFO_GTE(PROGRESS, 1))
371 end_progress(st.st_size);
373 log_item(log_code, file, iflags, NULL);
376 j = unmap_file(mbuf);
378 io_error |= IOERR_GENERAL;
379 rsyserr(FERROR_XFER, j,
380 "read errors mapping %s",
388 if (DEBUG_GTE(SEND, 1))
389 rprintf(FINFO, "sender finished %s%s%s\n", path,slash,fname);
391 /* Flag that we actually sent this entry. */
392 file->flags |= FLAG_FILE_SENT;
394 if (make_backups < 0)
395 make_backups = -make_backups;
397 if (io_error != save_io_error && protocol_version >= 30)
398 send_msg_int(MSG_IO_ERROR, io_error);
400 if (DEBUG_GTE(SEND, 1))
401 rprintf(FINFO, "send files finished\n");
405 write_ndx(f_out, NDX_DONE);