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