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