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