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