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