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