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