Improve some hard-link caveats in the manpage.
[rsync/rsync.git] / sender.c
... / ...
CommitLineData
1/*
2 * Routines only used by the sending process.
3 *
4 * Copyright (C) 1996 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2003-2009 Wayne Davison
7 *
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.
12 *
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.
17 *
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.
20 */
21
22#include "rsync.h"
23#include "inums.h"
24
25extern int do_xfers;
26extern int am_server;
27extern int am_daemon;
28extern int inc_recurse;
29extern int log_before_transfer;
30extern int stdout_format_has_i;
31extern int logfile_format_has_i;
32extern int csum_length;
33extern int append_mode;
34extern int io_error;
35extern int flist_eof;
36extern int allowed_lull;
37extern int preserve_xattrs;
38extern int protocol_version;
39extern int remove_source_files;
40extern int updating_basis_file;
41extern int make_backups;
42extern int inplace;
43extern int batch_fd;
44extern int write_batch;
45extern int file_old_total;
46extern struct stats stats;
47extern struct file_list *cur_flist, *first_flist, *dir_flist;
48
49BOOL extra_flist_sending_enabled;
50
51/**
52 * @file
53 *
54 * The sender gets checksums from the generator, calculates deltas,
55 * and transmits them to the receiver. The sender process runs on the
56 * machine holding the source files.
57 **/
58
59/**
60 * Receive the checksums for a buffer
61 **/
62static struct sum_struct *receive_sums(int f)
63{
64 struct sum_struct *s;
65 int32 i;
66 int lull_mod = protocol_version >= 31 ? 0 : allowed_lull * 5;
67 OFF_T offset = 0;
68
69 if (!(s = new(struct sum_struct)))
70 out_of_memory("receive_sums");
71
72 read_sum_head(f, s);
73
74 s->sums = NULL;
75
76 if (DEBUG_GTE(DELTASUM, 3)) {
77 rprintf(FINFO, "count=%s n=%ld rem=%ld\n",
78 big_num(s->count), (long)s->blength, (long)s->remainder);
79 }
80
81 if (append_mode > 0) {
82 s->flength = (OFF_T)s->count * s->blength;
83 if (s->remainder)
84 s->flength -= s->blength - s->remainder;
85 return s;
86 }
87
88 if (s->count == 0)
89 return(s);
90
91 if (!(s->sums = new_array(struct sum_buf, s->count)))
92 out_of_memory("receive_sums");
93
94 for (i = 0; i < s->count; i++) {
95 s->sums[i].sum1 = read_int(f);
96 read_buf(f, s->sums[i].sum2, s->s2length);
97
98 s->sums[i].offset = offset;
99 s->sums[i].flags = 0;
100
101 if (i == s->count-1 && s->remainder != 0)
102 s->sums[i].len = s->remainder;
103 else
104 s->sums[i].len = s->blength;
105 offset += s->sums[i].len;
106
107 if (lull_mod && !(i % lull_mod))
108 maybe_send_keepalive(time(NULL), True);
109
110 if (DEBUG_GTE(DELTASUM, 3)) {
111 rprintf(FINFO,
112 "chunk[%d] len=%d offset=%s sum1=%08x\n",
113 i, s->sums[i].len, big_num(s->sums[i].offset),
114 s->sums[i].sum1);
115 }
116 }
117
118 s->flength = offset;
119
120 return s;
121}
122
123void successful_send(int ndx)
124{
125 char fname[MAXPATHLEN];
126 struct file_struct *file;
127 struct file_list *flist;
128
129 if (!remove_source_files)
130 return;
131
132 flist = flist_for_ndx(ndx, "successful_send");
133 file = flist->files[ndx - flist->ndx_start];
134 if (!change_pathname(file, NULL, 0))
135 return;
136 f_name(file, fname);
137
138 if (do_unlink(fname) == 0) {
139 if (INFO_GTE(REMOVE, 1))
140 rprintf(FINFO, "sender removed %s\n", fname);
141 } else
142 rsyserr(FERROR, errno, "sender failed to remove %s", fname);
143}
144
145static void write_ndx_and_attrs(int f_out, int ndx, int iflags,
146 const char *fname, struct file_struct *file,
147 uchar fnamecmp_type, char *buf, int len)
148{
149 write_ndx(f_out, ndx);
150 if (protocol_version < 29)
151 return;
152 write_shortint(f_out, iflags);
153 if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
154 write_byte(f_out, fnamecmp_type);
155 if (iflags & ITEM_XNAME_FOLLOWS)
156 write_vstring(f_out, buf, len);
157#ifdef SUPPORT_XATTRS
158 if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && do_xfers)
159 send_xattr_request(fname, file, f_out);
160#endif
161}
162
163void send_files(int f_in, int f_out)
164{
165 int fd = -1;
166 struct sum_struct *s;
167 struct map_struct *mbuf = NULL;
168 STRUCT_STAT st;
169 char fname[MAXPATHLEN], xname[MAXPATHLEN];
170 const char *path, *slash;
171 uchar fnamecmp_type;
172 int iflags, xlen;
173 struct file_struct *file;
174 int phase = 0, max_phase = protocol_version >= 29 ? 2 : 1;
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;
178 int save_io_error = io_error;
179 int ndx, j;
180
181 if (DEBUG_GTE(SEND, 1))
182 rprintf(FINFO, "send_files starting\n");
183
184 while (1) {
185 if (inc_recurse) {
186 send_extra_file_list(f_out, MIN_FILECNT_LOOKAHEAD);
187 extra_flist_sending_enabled = !flist_eof;
188 }
189
190 /* This call also sets cur_flist. */
191 ndx = read_ndx_and_attrs(f_in, f_out, &iflags, &fnamecmp_type,
192 xname, &xlen);
193 extra_flist_sending_enabled = False;
194
195 if (ndx == NDX_DONE) {
196 if (!am_server && INFO_GTE(PROGRESS, 2) && cur_flist) {
197 set_current_file_index(NULL, 0);
198 end_progress(0);
199 }
200 if (inc_recurse && first_flist) {
201 file_old_total -= first_flist->used;
202 flist_free(first_flist);
203 if (first_flist) {
204 if (first_flist == cur_flist)
205 file_old_total = cur_flist->used;
206 write_ndx(f_out, NDX_DONE);
207 continue;
208 }
209 }
210 if (++phase > max_phase)
211 break;
212 if (DEBUG_GTE(SEND, 1))
213 rprintf(FINFO, "send_files phase=%d\n", phase);
214 write_ndx(f_out, NDX_DONE);
215 continue;
216 }
217
218 if (inc_recurse)
219 send_extra_file_list(f_out, MIN_FILECNT_LOOKAHEAD);
220
221 if (ndx - cur_flist->ndx_start >= 0)
222 file = cur_flist->files[ndx - cur_flist->ndx_start];
223 else
224 file = dir_flist->files[cur_flist->parent_ndx];
225 if (F_PATHNAME(file)) {
226 path = F_PATHNAME(file);
227 slash = "/";
228 } else {
229 path = slash = "";
230 }
231 if (!change_pathname(file, NULL, 0))
232 continue;
233 f_name(file, fname);
234
235 if (DEBUG_GTE(SEND, 1))
236 rprintf(FINFO, "send_files(%d, %s%s%s)\n", ndx, path,slash,fname);
237
238#ifdef SUPPORT_XATTRS
239 if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && do_xfers)
240 recv_xattr_request(file, f_in);
241#endif
242
243 if (!(iflags & ITEM_TRANSFER)) {
244 maybe_log_item(file, iflags, itemizing, xname);
245 write_ndx_and_attrs(f_out, ndx, iflags, fname, file,
246 fnamecmp_type, xname, xlen);
247 if (iflags & ITEM_IS_NEW) {
248 stats.created_files++;
249 if (S_ISREG(file->mode)) {
250 /* Nothing further to count. */
251 } else if (S_ISDIR(file->mode))
252 stats.created_dirs++;
253#ifdef SUPPORT_LINKS
254 else if (S_ISLNK(file->mode))
255 stats.created_symlinks++;
256#endif
257 else if (IS_DEVICE(file->mode))
258 stats.created_devices++;
259 else
260 stats.created_specials++;
261 }
262 continue;
263 }
264 if (phase == 2) {
265 rprintf(FERROR,
266 "got transfer request in phase 2 [%s]\n",
267 who_am_i());
268 exit_cleanup(RERR_PROTOCOL);
269 }
270
271 if (file->flags & FLAG_FILE_SENT) {
272 if (csum_length == SHORT_SUM_LENGTH) {
273 /* For inplace: redo phase turns off the backup
274 * flag so that we do a regular inplace send. */
275 make_backups = -make_backups;
276 append_mode = -append_mode;
277 csum_length = SUM_LENGTH;
278 }
279 } else {
280 if (csum_length != SHORT_SUM_LENGTH) {
281 make_backups = -make_backups;
282 append_mode = -append_mode;
283 csum_length = SHORT_SUM_LENGTH;
284 }
285 if (iflags & ITEM_IS_NEW)
286 stats.created_files++;
287 }
288
289 updating_basis_file = inplace && (protocol_version >= 29
290 ? fnamecmp_type == FNAMECMP_FNAME : make_backups <= 0);
291
292 if (!am_server && INFO_GTE(PROGRESS, 1))
293 set_current_file_index(file, ndx);
294 stats.xferred_files++;
295 stats.total_transferred_size += F_LENGTH(file);
296
297 if (!log_before_transfer)
298 remember_initial_stats();
299
300 if (!do_xfers) { /* log the transfer */
301 log_item(FCLIENT, file, iflags, NULL);
302 write_ndx_and_attrs(f_out, ndx, iflags, fname, file,
303 fnamecmp_type, xname, xlen);
304 continue;
305 }
306
307 if (!(s = receive_sums(f_in))) {
308 io_error |= IOERR_GENERAL;
309 rprintf(FERROR_XFER, "receive_sums failed\n");
310 exit_cleanup(RERR_PROTOCOL);
311 }
312
313 fd = do_open(fname, O_RDONLY, 0);
314 if (fd == -1) {
315 if (errno == ENOENT) {
316 enum logcode c = am_daemon
317 && protocol_version < 28 ? FERROR
318 : FWARNING;
319 io_error |= IOERR_VANISHED;
320 rprintf(c, "file has vanished: %s\n",
321 full_fname(fname));
322 } else {
323 io_error |= IOERR_GENERAL;
324 rsyserr(FERROR_XFER, errno,
325 "send_files failed to open %s",
326 full_fname(fname));
327 }
328 free_sums(s);
329 if (protocol_version >= 30)
330 send_msg_int(MSG_NO_SEND, ndx);
331 continue;
332 }
333
334 /* map the local file */
335 if (do_fstat(fd, &st) != 0) {
336 io_error |= IOERR_GENERAL;
337 rsyserr(FERROR_XFER, errno, "fstat failed");
338 free_sums(s);
339 close(fd);
340 exit_cleanup(RERR_FILEIO);
341 }
342
343 if (st.st_size) {
344 int32 read_size = MAX(s->blength * 3, MAX_MAP_SIZE);
345 mbuf = map_file(fd, st.st_size, read_size, s->blength);
346 } else
347 mbuf = NULL;
348
349 if (DEBUG_GTE(DELTASUM, 2)) {
350 rprintf(FINFO, "send_files mapped %s%s%s of size %s\n",
351 path,slash,fname, big_num(st.st_size));
352 }
353
354 write_ndx_and_attrs(f_out, ndx, iflags, fname, file,
355 fnamecmp_type, xname, xlen);
356 write_sum_head(f_xfer, s);
357
358 if (DEBUG_GTE(DELTASUM, 2))
359 rprintf(FINFO, "calling match_sums %s%s%s\n", path,slash,fname);
360
361 if (log_before_transfer)
362 log_item(FCLIENT, file, iflags, NULL);
363 else if (!am_server && INFO_GTE(NAME, 1) && INFO_EQ(PROGRESS, 1))
364 rprintf(FCLIENT, "%s\n", fname);
365
366 set_compression(fname);
367
368 match_sums(f_xfer, s, mbuf, st.st_size);
369 if (INFO_GTE(PROGRESS, 1))
370 end_progress(st.st_size);
371
372 log_item(log_code, file, iflags, NULL);
373
374 if (mbuf) {
375 j = unmap_file(mbuf);
376 if (j) {
377 io_error |= IOERR_GENERAL;
378 rsyserr(FERROR_XFER, j,
379 "read errors mapping %s",
380 full_fname(fname));
381 }
382 }
383 close(fd);
384
385 free_sums(s);
386
387 if (DEBUG_GTE(SEND, 1))
388 rprintf(FINFO, "sender finished %s%s%s\n", path,slash,fname);
389
390 /* Flag that we actually sent this entry. */
391 file->flags |= FLAG_FILE_SENT;
392 }
393 if (make_backups < 0)
394 make_backups = -make_backups;
395
396 if (io_error != save_io_error && protocol_version >= 30)
397 send_msg_int(MSG_IO_ERROR, io_error);
398
399 if (DEBUG_GTE(SEND, 1))
400 rprintf(FINFO, "send files finished\n");
401
402 match_report();
403
404 write_ndx(f_out, NDX_DONE);
405}