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