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