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