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