Corrected "log format" info and added info on --remove-sent-files.
[rsync/rsync.git] / receiver.c
... / ...
CommitLineData
1/* -*- c-file-style: "linux" -*-
2
3 Copyright (C) 1996-2000 by Andrew Tridgell
4 Copyright (C) Paul Mackerras 1996
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19*/
20
21#include "rsync.h"
22
23extern int verbose;
24extern int dry_run;
25extern int am_daemon;
26extern int am_server;
27extern int do_progress;
28extern int log_before_transfer;
29extern int log_format_has_i;
30extern int daemon_log_format_has_i;
31extern int delete_after;
32extern int csum_length;
33extern int read_batch;
34extern int batch_gen_fd;
35extern int protocol_version;
36extern int relative_paths;
37extern int keep_dirlinks;
38extern int preserve_hard_links;
39extern int preserve_perms;
40extern int io_error;
41extern int basis_dir_cnt;
42extern int make_backups;
43extern int cleanup_got_literal;
44extern int remove_sent_files;
45extern int module_id;
46extern int ignore_errors;
47extern int orig_umask;
48extern int keep_partial;
49extern int checksum_seed;
50extern int inplace;
51extern int delay_updates;
52extern struct stats stats;
53extern char *log_format;
54extern char *tmpdir;
55extern char *partial_dir;
56extern char *basis_dir[];
57
58extern struct filter_list_struct server_filter_list;
59
60
61/* This deletes any files on the receiving side that are not present on the
62 * sending side. This is used by --delete-before and --delete-after. */
63void delete_files(struct file_list *flist)
64{
65 char fbuf[MAXPATHLEN];
66 int j;
67
68 for (j = 0; j < flist->count; j++) {
69 struct file_struct *file = flist->files[j];
70
71 if (!(file->flags & FLAG_DEL_HERE))
72 continue;
73
74 f_name_to(file, fbuf);
75 if (verbose > 1 && file->flags & FLAG_TOP_DIR)
76 rprintf(FINFO, "deleting in %s\n", safe_fname(fbuf));
77
78 delete_in_dir(flist, fbuf, file);
79 }
80}
81
82
83/*
84 * get_tmpname() - create a tmp filename for a given filename
85 *
86 * If a tmpdir is defined, use that as the directory to
87 * put it in. Otherwise, the tmp filename is in the same
88 * directory as the given name. Note that there may be no
89 * directory at all in the given name!
90 *
91 * The tmp filename is basically the given filename with a
92 * dot prepended, and .XXXXXX appended (for mkstemp() to
93 * put its unique gunk in). Take care to not exceed
94 * either the MAXPATHLEN or NAME_MAX, esp. the last, as
95 * the basename basically becomes 8 chars longer. In that
96 * case, the original name is shortened sufficiently to
97 * make it all fit.
98 *
99 * Of course, there's no real reason for the tmp name to
100 * look like the original, except to satisfy us humans.
101 * As long as it's unique, rsync will work.
102 */
103
104static int get_tmpname(char *fnametmp, char *fname)
105{
106 char *f;
107 int length = 0;
108 int maxname;
109
110 if (tmpdir) {
111 /* Note: this can't overflow, so the return value is safe */
112 length = strlcpy(fnametmp, tmpdir, MAXPATHLEN - 2);
113 fnametmp[length++] = '/';
114 fnametmp[length] = '\0'; /* always NULL terminated */
115 }
116
117 if ((f = strrchr(fname, '/')) != NULL) {
118 ++f;
119 if (!tmpdir) {
120 length = f - fname;
121 /* copy up to and including the slash */
122 strlcpy(fnametmp, fname, length + 1);
123 }
124 } else
125 f = fname;
126 fnametmp[length++] = '.';
127 fnametmp[length] = '\0'; /* always NULL terminated */
128
129 maxname = MIN(MAXPATHLEN - 7 - length, NAME_MAX - 8);
130
131 if (maxname < 1) {
132 rprintf(FERROR, "temporary filename too long: %s\n",
133 safe_fname(fname));
134 fnametmp[0] = '\0';
135 return 0;
136 }
137
138 strlcpy(fnametmp + length, f, maxname);
139 strcat(fnametmp + length, ".XXXXXX");
140
141 return 1;
142}
143
144
145static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
146 char *fname, int fd, OFF_T total_size)
147{
148 static char file_sum1[MD4_SUM_LENGTH];
149 static char file_sum2[MD4_SUM_LENGTH];
150 struct map_struct *mapbuf;
151 struct sum_struct sum;
152 int32 len;
153 OFF_T offset = 0;
154 OFF_T offset2;
155 char *data;
156 int32 i;
157 char *map = NULL;
158
159 read_sum_head(f_in, &sum);
160
161 if (fd_r >= 0 && size_r > 0) {
162 int32 read_size = MAX(sum.blength * 2, 16*1024);
163 mapbuf = map_file(fd_r, size_r, read_size, sum.blength);
164 if (verbose > 2) {
165 rprintf(FINFO, "recv mapped %s of size %.0f\n",
166 safe_fname(fname_r), (double)size_r);
167 }
168 } else
169 mapbuf = NULL;
170
171 sum_init(checksum_seed);
172
173 while ((i = recv_token(f_in, &data)) != 0) {
174 if (do_progress)
175 show_progress(offset, total_size);
176
177 if (i > 0) {
178 if (verbose > 3) {
179 rprintf(FINFO,"data recv %d at %.0f\n",
180 i,(double)offset);
181 }
182
183 stats.literal_data += i;
184 cleanup_got_literal = 1;
185
186 sum_update(data, i);
187
188 if (fd != -1 && write_file(fd,data,i) != i)
189 goto report_write_error;
190 offset += i;
191 continue;
192 }
193
194 i = -(i+1);
195 offset2 = i * (OFF_T)sum.blength;
196 len = sum.blength;
197 if (i == (int)sum.count-1 && sum.remainder != 0)
198 len = sum.remainder;
199
200 stats.matched_data += len;
201
202 if (verbose > 3) {
203 rprintf(FINFO,
204 "chunk[%d] of size %ld at %.0f offset=%.0f\n",
205 i, (long)len, (double)offset2, (double)offset);
206 }
207
208 if (mapbuf) {
209 map = map_ptr(mapbuf,offset2,len);
210
211 see_token(map, len);
212 sum_update(map, len);
213 }
214
215 if (inplace) {
216 if (offset == offset2 && fd != -1) {
217 if (flush_write_file(fd) < 0)
218 goto report_write_error;
219 offset += len;
220 if (do_lseek(fd, len, SEEK_CUR) != offset) {
221 rsyserr(FERROR, errno,
222 "lseek failed on %s",
223 full_fname(fname));
224 exit_cleanup(RERR_FILEIO);
225 }
226 continue;
227 }
228 }
229 if (fd != -1 && write_file(fd, map, len) != (int)len)
230 goto report_write_error;
231 offset += len;
232 }
233
234 if (flush_write_file(fd) < 0)
235 goto report_write_error;
236
237#ifdef HAVE_FTRUNCATE
238 if (inplace && fd != -1)
239 ftruncate(fd, offset);
240#endif
241
242 if (do_progress)
243 end_progress(total_size);
244
245 if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
246 report_write_error:
247 rsyserr(FERROR, errno, "write failed on %s",
248 full_fname(fname));
249 exit_cleanup(RERR_FILEIO);
250 }
251
252 sum_end(file_sum1);
253
254 if (mapbuf)
255 unmap_file(mapbuf);
256
257 read_buf(f_in,file_sum2,MD4_SUM_LENGTH);
258 if (verbose > 2)
259 rprintf(FINFO,"got file_sum\n");
260 if (fd != -1 && memcmp(file_sum1, file_sum2, MD4_SUM_LENGTH) != 0)
261 return 0;
262 return 1;
263}
264
265
266static void read_gen_name(int fd, char *dirname, char *buf)
267{
268 int dlen;
269 int len = read_byte(fd);
270
271 if (len & 0x80) {
272#if MAXPATHLEN > 32767
273 uchar lenbuf[2];
274 read_buf(fd, (char *)lenbuf, 2);
275 len = (len & ~0x80) * 0x10000 + lenbuf[0] * 0x100 + lenbuf[1];
276#else
277 len = (len & ~0x80) * 0x100 + read_byte(fd);
278#endif
279 }
280
281 if (dirname) {
282 dlen = strlcpy(buf, dirname, MAXPATHLEN);
283 buf[dlen++] = '/';
284 } else
285 dlen = 0;
286
287 if (dlen + len >= MAXPATHLEN) {
288 rprintf(FERROR, "bogus data on generator name pipe\n");
289 exit_cleanup(RERR_PROTOCOL);
290 }
291
292 read_sbuf(fd, buf + dlen, len);
293}
294
295
296static void discard_receive_data(int f_in, OFF_T length)
297{
298 receive_data(f_in, NULL, -1, 0, NULL, -1, length);
299}
300
301
302/**
303 * main routine for receiver process.
304 *
305 * Receiver process runs on the same host as the generator process. */
306int recv_files(int f_in, struct file_list *flist, char *local_name,
307 int f_in_name)
308{
309 int next_gen_i = -1;
310 int fd1,fd2;
311 STRUCT_STAT st;
312 int iflags;
313 char *fname, fbuf[MAXPATHLEN];
314 char template[MAXPATHLEN];
315 char fnametmp[MAXPATHLEN];
316 char *fnamecmp, *partialptr, numbuf[4];
317 char fnamecmpbuf[MAXPATHLEN];
318 uchar *delayed_bits = NULL;
319 struct file_struct *file;
320 struct stats initial_stats;
321 int save_make_backups = make_backups;
322 int itemizing = am_daemon ? daemon_log_format_has_i
323 : !am_server && log_format_has_i;
324 int i, recv_ok, phase = 0;
325
326 if (verbose > 2)
327 rprintf(FINFO,"recv_files(%d) starting\n",flist->count);
328
329 if (flist->hlink_pool) {
330 pool_destroy(flist->hlink_pool);
331 flist->hlink_pool = NULL;
332 }
333
334 if (delay_updates) {
335 int sz = (flist->count + 7) / 8;
336 if (!(delayed_bits = new_array(uchar, sz)))
337 out_of_memory("recv_files");
338 memset(delayed_bits, 0, sz);
339 }
340
341 while (1) {
342 cleanup_disable();
343
344 i = read_int(f_in);
345 if (i == -1) {
346 if (read_batch) {
347 if (next_gen_i != flist->count) {
348 do {
349 if (f_in_name >= 0
350 && next_gen_i >= 0)
351 read_byte(f_in_name);
352 } while (read_int(batch_gen_fd) != -1);
353 }
354 next_gen_i = -1;
355 }
356
357 if (phase)
358 break;
359
360 phase = 1;
361 csum_length = SUM_LENGTH;
362 if (verbose > 2)
363 rprintf(FINFO, "recv_files phase=%d\n", phase);
364 send_msg(MSG_DONE, "", 0);
365 if (keep_partial && !partial_dir)
366 make_backups = 0; /* prevents double backup */
367 continue;
368 }
369
370 if (i < 0 || i >= flist->count) {
371 rprintf(FERROR,"Invalid file index %d in recv_files (count=%d)\n",
372 i, flist->count);
373 exit_cleanup(RERR_PROTOCOL);
374 }
375
376 file = flist->files[i];
377 fname = local_name ? local_name : f_name_to(file, fbuf);
378
379 if (verbose > 2)
380 rprintf(FINFO, "recv_files(%s)\n", safe_fname(fname));
381
382 if (protocol_version >= 29) {
383 iflags = read_shortint(f_in);
384 if (!(iflags & ITEM_UPDATING) || !S_ISREG(file->mode)) {
385 int see_item = itemizing && (iflags || verbose > 1);
386 if (am_server) {
387 if (am_daemon && !dry_run && see_item)
388 log_recv(file, &stats, iflags);
389 } else if (see_item || iflags & ITEM_UPDATING
390 || (S_ISDIR(file->mode)
391 && iflags & ITEM_REPORT_TIME))
392 log_recv(file, &stats, iflags);
393 continue;
394 }
395 } else
396 iflags = ITEM_UPDATING | ITEM_MISSING_DATA;
397
398 if (!S_ISREG(file->mode)) {
399 rprintf(FERROR, "[%s] got index of non-regular file: %d\n",
400 who_am_i(), i);
401 exit_cleanup(RERR_PROTOCOL);
402 }
403
404 stats.current_file_index = i;
405 stats.num_transferred_files++;
406 stats.total_transferred_size += file->length;
407 cleanup_got_literal = 0;
408
409 if (server_filter_list.head
410 && check_filter(&server_filter_list, fname, 0) < 0) {
411 rprintf(FERROR, "attempt to hack rsync failed.\n");
412 exit_cleanup(RERR_PROTOCOL);
413 }
414
415 if (dry_run) { /* log the transfer */
416 if (!am_server && log_format)
417 log_recv(file, &stats, iflags);
418 continue;
419 }
420
421 if (read_batch) {
422 while (i > next_gen_i) {
423 if (f_in_name >= 0 && next_gen_i >= 0)
424 read_byte(f_in_name);
425 next_gen_i = read_int(batch_gen_fd);
426 if (next_gen_i == -1)
427 next_gen_i = flist->count;
428 }
429 if (i < next_gen_i) {
430 rprintf(FINFO, "skipping update for \"%s\"\n",
431 safe_fname(fname));
432 discard_receive_data(f_in, file->length);
433 continue;
434 }
435 next_gen_i = -1;
436 }
437
438 partialptr = partial_dir ? partial_dir_fname(fname) : fname;
439
440 if (f_in_name >= 0) {
441 uchar j;
442 switch (j = read_byte(f_in_name)) {
443 case FNAMECMP_FNAME:
444 fnamecmp = fname;
445 break;
446 case FNAMECMP_PARTIAL_DIR:
447 fnamecmp = partialptr ? partialptr : fname;
448 break;
449 case FNAMECMP_BACKUP:
450 fnamecmp = get_backup_name(fname);
451 break;
452 case FNAMECMP_FUZZY:
453 read_gen_name(f_in_name, file->dirname, fnamecmpbuf);
454 fnamecmp = fnamecmpbuf;
455 break;
456 default:
457 if (j >= basis_dir_cnt) {
458 rprintf(FERROR,
459 "invalid basis_dir index: %d.\n",
460 j);
461 exit_cleanup(RERR_PROTOCOL);
462 }
463 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
464 basis_dir[j], fname);
465 fnamecmp = fnamecmpbuf;
466 break;
467 }
468 } else
469 fnamecmp = fname;
470
471 initial_stats = stats;
472
473 /* open the file */
474 fd1 = do_open(fnamecmp, O_RDONLY, 0);
475
476 if (fd1 != -1 && do_fstat(fd1,&st) != 0) {
477 rsyserr(FERROR, errno, "fstat %s failed",
478 full_fname(fnamecmp));
479 discard_receive_data(f_in, file->length);
480 close(fd1);
481 continue;
482 }
483
484 if (fd1 != -1 && S_ISDIR(st.st_mode) && fnamecmp == fname) {
485 /* this special handling for directories
486 * wouldn't be necessary if robust_rename()
487 * and the underlying robust_unlink could cope
488 * with directories
489 */
490 rprintf(FERROR,"recv_files: %s is a directory\n",
491 full_fname(fnamecmp));
492 discard_receive_data(f_in, file->length);
493 close(fd1);
494 continue;
495 }
496
497 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
498 close(fd1);
499 fd1 = -1;
500 }
501
502 if (fd1 != -1 && !preserve_perms) {
503 /* if the file exists already and we aren't preserving
504 * permissions then act as though the remote end sent
505 * us the file permissions we already have */
506 file->mode = st.st_mode;
507 }
508
509 /* We now check to see if we are writing file "inplace" */
510 if (inplace) {
511 fd2 = do_open(fname, O_WRONLY|O_CREAT, 0);
512 if (fd2 == -1) {
513 rsyserr(FERROR, errno, "open %s failed",
514 full_fname(fname));
515 discard_receive_data(f_in, file->length);
516 if (fd1 != -1)
517 close(fd1);
518 continue;
519 }
520 } else {
521 if (!get_tmpname(fnametmp,fname)) {
522 discard_receive_data(f_in, file->length);
523 if (fd1 != -1)
524 close(fd1);
525 continue;
526 }
527
528 strlcpy(template, fnametmp, sizeof template);
529
530 /* we initially set the perms without the
531 * setuid/setgid bits to ensure that there is no race
532 * condition. They are then correctly updated after
533 * the lchown. Thanks to snabb@epipe.fi for pointing
534 * this out. We also set it initially without group
535 * access because of a similar race condition. */
536 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
537
538 /* in most cases parent directories will already exist
539 * because their information should have been previously
540 * transferred, but that may not be the case with -R */
541 if (fd2 == -1 && relative_paths && errno == ENOENT
542 && create_directory_path(fnametmp, orig_umask) == 0) {
543 strlcpy(fnametmp, template, sizeof fnametmp);
544 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
545 }
546 if (fd2 == -1) {
547 rsyserr(FERROR, errno, "mkstemp %s failed",
548 full_fname(fnametmp));
549 discard_receive_data(f_in, file->length);
550 if (fd1 != -1)
551 close(fd1);
552 continue;
553 }
554
555 if (partialptr)
556 cleanup_set(fnametmp, partialptr, file, fd1, fd2);
557 }
558
559 /* log the transfer */
560 if (log_before_transfer)
561 log_recv(file, &initial_stats, iflags);
562 else if (!am_server && verbose && do_progress)
563 rprintf(FINFO, "%s\n", safe_fname(fname));
564
565 /* recv file data */
566 recv_ok = receive_data(f_in, fnamecmp, fd1, st.st_size,
567 fname, fd2, file->length);
568
569 if (!log_before_transfer)
570 log_recv(file, &initial_stats, iflags);
571
572 if (fd1 != -1)
573 close(fd1);
574 if (close(fd2) < 0) {
575 rsyserr(FERROR, errno, "close failed on %s",
576 full_fname(fnametmp));
577 exit_cleanup(RERR_FILEIO);
578 }
579
580 if ((recv_ok && (!delay_updates || !partialptr)) || inplace) {
581 finish_transfer(fname, fnametmp, file, recv_ok, 1);
582 if (partialptr != fname && fnamecmp == partialptr) {
583 do_unlink(partialptr);
584 handle_partial_dir(partialptr, PDIR_DELETE);
585 }
586 } else if (keep_partial && partialptr
587 && handle_partial_dir(partialptr, PDIR_CREATE)) {
588 finish_transfer(partialptr, fnametmp, file, recv_ok,
589 !partial_dir);
590 if (delay_updates && recv_ok)
591 delayed_bits[i/8] |= 1 << (i % 8);
592 } else {
593 partialptr = NULL;
594 do_unlink(fnametmp);
595 }
596
597 cleanup_disable();
598
599 if (recv_ok) {
600 if (remove_sent_files) {
601 SIVAL(numbuf, 0, i);
602 send_msg(MSG_SUCCESS, numbuf, 4);
603 }
604 } else {
605 int msgtype = csum_length == SUM_LENGTH || read_batch ?
606 FERROR : FINFO;
607 if (msgtype == FERROR || verbose) {
608 char *errstr, *redostr, *keptstr;
609 if (!(keep_partial && partialptr) && !inplace)
610 keptstr = "discarded";
611 else if (partial_dir)
612 keptstr = "put into partial-dir";
613 else
614 keptstr = "retained";
615 if (msgtype == FERROR) {
616 errstr = "ERROR";
617 redostr = "";
618 } else {
619 errstr = "WARNING";
620 redostr = " (will try again)";
621 }
622 rprintf(msgtype,
623 "%s: %s failed verification -- update %s%s.\n",
624 errstr, safe_fname(fname),
625 keptstr, redostr);
626 }
627 if (csum_length != SUM_LENGTH) {
628 SIVAL(numbuf, 0, i);
629 send_msg(MSG_REDO, numbuf, 4);
630 }
631 }
632 }
633 make_backups = save_make_backups;
634
635 if (delay_updates) {
636 for (i = 0; i < flist->count; i++) {
637 struct file_struct *file = flist->files[i];
638 if (!file->basename
639 || !(delayed_bits[i/8] & (1 << (i % 8))))
640 continue;
641 fname = local_name ? local_name : f_name(file);
642 partialptr = partial_dir_fname(fname);
643 if (partialptr) {
644 if (make_backups && !make_backup(fname))
645 continue;
646 if (verbose > 2) {
647 rprintf(FINFO, "renaming %s to %s\n",
648 safe_fname(partialptr),
649 safe_fname(fname));
650 }
651 if (do_rename(partialptr, fname) < 0) {
652 rsyserr(FERROR, errno,
653 "rename failed for %s (from %s)",
654 full_fname(fname),
655 safe_fname(partialptr));
656 } else {
657 handle_partial_dir(partialptr,
658 PDIR_DELETE);
659 }
660 }
661 }
662 }
663
664 if (delete_after && !local_name && flist->count > 0)
665 delete_files(flist);
666
667 if (verbose > 2)
668 rprintf(FINFO,"recv_files finished\n");
669
670 return 0;
671}