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