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