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