Prevent tempfile names from overflowing.
[rsync/rsync.git] / generator.c
CommitLineData
ef1aa910 1/* -*- c-file-style: "linux" -*-
91262d5d
MP
2
3 rsync -- fast file replication program
2cda2560
WD
4
5 Copyright (C) 1996-2000 by Andrew Tridgell
2f03f956 6 Copyright (C) Paul Mackerras 1996
91262d5d 7 Copyright (C) 2002 by Martin Pool <mbp@samba.org>
2cda2560 8
2f03f956
AT
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
2cda2560 13
2f03f956
AT
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
2cda2560 18
2f03f956
AT
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22*/
23
24#include "rsync.h"
25
26extern int verbose;
27extern int dry_run;
28extern int relative_paths;
29extern int preserve_links;
30extern int am_root;
31extern int preserve_devices;
32extern int preserve_hard_links;
33extern int update_only;
3d6feada 34extern int opt_ignore_existing;
2f03f956
AT
35extern int block_size;
36extern int csum_length;
37extern int ignore_times;
f83f0548 38extern int size_only;
2f03f956
AT
39extern int io_timeout;
40extern int remote_version;
41extern int always_checksum;
5b56cc19 42extern int modify_window;
60c8d7bc 43extern char *compare_dest;
59c95e42 44extern int link_dest;
2f03f956
AT
45
46
47/* choose whether to skip a particular file */
48static int skip_file(char *fname,
49 struct file_struct *file, STRUCT_STAT *st)
50{
51 if (st->st_size != file->length) {
52 return 0;
53 }
59c95e42
DD
54 if (link_dest) {
55 if((st->st_mode & ~_S_IFMT) != (file->mode & ~_S_IFMT)) {
56 return 0;
57 }
58 if (st->st_uid != file->uid || st->st_gid != file->gid) {
59 return 0;
60 }
61 }
62
2cda2560 63 /* if always checksum is set then we use the checksum instead
2f03f956
AT
64 of the file time to determine whether to sync */
65 if (always_checksum && S_ISREG(st->st_mode)) {
66 char sum[MD4_SUM_LENGTH];
60c8d7bc
DD
67 char fnamecmpdest[MAXPATHLEN];
68
69 if (compare_dest != NULL) {
70 if (access(fname, 0) != 0) {
8950ac03 71 snprintf(fnamecmpdest,MAXPATHLEN,"%s/%s",
2cda2560 72 compare_dest,fname);
60c8d7bc
DD
73 fname = fnamecmpdest;
74 }
75 }
2f03f956 76 file_checksum(fname,sum,st->st_size);
f855a7d0
AT
77 if (remote_version < 21) {
78 return (memcmp(sum,file->sum,2) == 0);
79 } else {
80 return (memcmp(sum,file->sum,MD4_SUM_LENGTH) == 0);
81 }
2f03f956
AT
82 }
83
f83f0548
AT
84 if (size_only) {
85 return 1;
86 }
87
2f03f956
AT
88 if (ignore_times) {
89 return 0;
90 }
91
5b56cc19 92 return (cmp_modtime(st->st_mtime,file->modtime) == 0);
2f03f956
AT
93}
94
95
96/* use a larger block size for really big files */
97static int adapt_block_size(struct file_struct *file, int bsize)
98{
99 int ret;
100
101 if (bsize != BLOCK_SIZE) return bsize;
102
103 ret = file->length / (10000); /* rough heuristic */
104 ret = ret & ~15; /* multiple of 16 */
105 if (ret < bsize) ret = bsize;
106 if (ret > CHUNK_SIZE/2) ret = CHUNK_SIZE/2;
107 return ret;
108}
109
110
111/*
80605142 112 send a header that says "we have no checksums" down the f_out fd
2f03f956 113 */
80605142 114static void send_null_sums(int f_out)
2f03f956 115{
80605142
WD
116 write_int(f_out, 0);
117 write_int(f_out, block_size);
118 write_int(f_out, 0);
2f03f956
AT
119}
120
bceec82f 121
80605142 122
bceec82f
MP
123/**
124 * Perhaps we want to just send an empty checksum set for this file,
125 * which will force the whole thing to be literally transferred.
126 *
127 * When do we do this? If the user's explicitly said they
128 * want the whole thing, or if { they haven't explicitly
129 * requested a delta, and it's local but not batch mode.}
130 *
131 * Whew. */
132static BOOL disable_deltas_p(void)
133{
2cda2560 134 extern int whole_file;
bceec82f
MP
135 extern int local_server;
136 extern int write_batch;
137
2cda2560 138 if (whole_file > 0)
bceec82f 139 return True;
2cda2560 140 if (whole_file == 0 || write_batch)
bceec82f 141 return False;
2cda2560 142 return local_server;
bceec82f
MP
143}
144
145
80605142
WD
146/*
147 * Generate and send a stream of signatures/checksums that describe a buffer
e66dfd18 148 *
80605142
WD
149 * Generate approximately one checksum every block_len bytes.
150 */
151static void generate_and_send_sums(struct map_struct *buf, OFF_T len,
152 int block_len, int f_out)
2f03f956 153{
80605142
WD
154 size_t i;
155 struct sum_struct sum;
2f03f956
AT
156 OFF_T offset = 0;
157
80605142
WD
158 sum.count = (len + (block_len - 1)) / block_len;
159 sum.remainder = (len % block_len);
160 sum.n = block_len;
161 sum.flength = len;
162 /* not needed here sum.sums = NULL; */
2f03f956 163
80605142 164 if (sum.count && verbose > 3) {
67684d03 165 rprintf(FINFO, "count=%ld rem=%ld n=%ld flength=%.0f\n",
80605142
WD
166 (long) sum.count, (long) sum.remainder,
167 (long) sum.n, (double) sum.flength);
67684d03 168 }
e66dfd18 169
80605142
WD
170 write_int(f_out, sum.count);
171 write_int(f_out, sum.n);
172 write_int(f_out, sum.remainder);
2f03f956 173
80605142
WD
174 for (i = 0; i < sum.count; i++) {
175 int n1 = MIN(len, block_len);
e66dfd18 176 char *map = map_ptr(buf, offset, n1);
80605142
WD
177 uint32 sum1 = get_checksum1(map, n1);
178 char sum2[SUM_LENGTH];
2f03f956 179
80605142 180 get_checksum2(map, n1, sum2);
2f03f956 181
80605142 182 if (verbose > 3) {
e66dfd18 183 rprintf(FINFO,
80605142
WD
184 "chunk[%d] offset=%.0f len=%d sum1=%08lx\n",
185 i, (double) offset, n1, (unsigned long) sum1);
186 }
187 write_int(f_out, sum1);
188 write_buf(f_out, sum2, csum_length);
2f03f956
AT
189 len -= n1;
190 offset += n1;
191 }
2f03f956
AT
192}
193
194
ef1aa910 195
420ef2c4
MP
196/**
197 * Acts on file number @p i from @p flist, whose name is @p fname.
ef1aa910
MP
198 *
199 * First fixes up permissions, then generates checksums for the file.
200 *
420ef2c4
MP
201 * @note This comment was added later by mbp who was trying to work it
202 * out. It might be wrong.
2cda2560 203 **/
420ef2c4 204void recv_generator(char *fname, struct file_list *flist, int i, int f_out)
2cda2560 205{
2f03f956
AT
206 int fd;
207 STRUCT_STAT st;
208 struct map_struct *buf;
2f03f956
AT
209 int statret;
210 struct file_struct *file = flist->files[i];
375a4556
DD
211 char *fnamecmp;
212 char fnamecmpbuf[MAXPATHLEN];
213 extern char *compare_dest;
f7632fc6 214 extern int list_only;
4df9f368 215 extern int preserve_perms;
1347d512 216 extern int only_existing;
b35d0d8e 217 extern int orig_umask;
f7632fc6
AT
218
219 if (list_only) return;
2f03f956
AT
220
221 if (verbose > 2)
222 rprintf(FINFO,"recv_generator(%s,%d)\n",fname,i);
223
224 statret = link_stat(fname,&st);
63787382 225
1347d512
AT
226 if (only_existing && statret == -1 && errno == ENOENT) {
227 /* we only want to update existing files */
1bbd10fe 228 if (verbose > 1) rprintf(FINFO, "not creating new file \"%s\"\n",fname);
1347d512
AT
229 return;
230 }
231
2cda2560
WD
232 if (statret == 0 &&
233 !preserve_perms &&
4df9f368
AT
234 (S_ISDIR(st.st_mode) == S_ISDIR(file->mode))) {
235 /* if the file exists already and we aren't perserving
2cda2560
WD
236 * permissions then act as though the remote end sent
237 * us the file permissions we already have */
7e0ca8e2 238 file->mode = (file->mode & _S_IFMT) | (st.st_mode & ~_S_IFMT);
4df9f368
AT
239 }
240
2f03f956 241 if (S_ISDIR(file->mode)) {
2cda2560
WD
242 /* The file to be received is a directory, so we need
243 * to prepare appropriately. If there is already a
244 * file of that name and it is *not* a directory, then
245 * we need to delete it. If it doesn't exist, then
246 * recursively create it. */
247
85d4d142 248 if (dry_run) return; /* XXXX -- might cause inaccuracies?? -- mbp */
2f03f956 249 if (statret == 0 && !S_ISDIR(st.st_mode)) {
c7c11a0d 250 if (robust_unlink(fname) != 0) {
85d4d142
MP
251 rprintf(FERROR, RSYNC_NAME
252 ": recv_generator: unlink \"%s\" to make room for directory: %s\n",
2cda2560 253 fname,strerror(errno));
2f03f956
AT
254 return;
255 }
256 statret = -1;
257 }
258 if (statret != 0 && do_mkdir(fname,file->mode) != 0 && errno != EEXIST) {
2cda2560
WD
259 if (!(relative_paths && errno==ENOENT &&
260 create_directory_path(fname, orig_umask)==0 &&
2f03f956 261 do_mkdir(fname,file->mode)==0)) {
85d4d142 262 rprintf(FERROR, RSYNC_NAME ": recv_generator: mkdir \"%s\": %s (2)\n",
2f03f956
AT
263 fname,strerror(errno));
264 }
265 }
2cda2560 266 /* f_out is set to -1 when doing final directory
de343e3c 267 permission and modification time repair */
2cda2560 268 if (set_perms(fname,file,NULL,0) && verbose && (f_out != -1))
2f03f956
AT
269 rprintf(FINFO,"%s/\n",fname);
270 return;
271 }
272
273 if (preserve_links && S_ISLNK(file->mode)) {
274#if SUPPORT_LINKS
275 char lnk[MAXPATHLEN];
276 int l;
277 extern int safe_symlinks;
278
279 if (safe_symlinks && unsafe_symlink(file->link, fname)) {
280 if (verbose) {
1bbd10fe 281 rprintf(FINFO,"ignoring unsafe symlink \"%s\" -> \"%s\"\n",
2f03f956
AT
282 fname,file->link);
283 }
284 return;
285 }
286 if (statret == 0) {
287 l = readlink(fname,lnk,MAXPATHLEN-1);
288 if (l > 0) {
289 lnk[l] = 0;
85d4d142
MP
290 /* A link already pointing to the
291 * right place -- no further action
292 * required. */
7e0ca8e2 293 if (strcmp(lnk,file->link) == 0) {
2f03f956
AT
294 set_perms(fname,file,&st,1);
295 return;
296 }
2cda2560 297 }
85d4d142
MP
298 /* Not a symlink, so delete whatever's
299 * already there and put a new symlink
2cda2560 300 * in place. */
4b3977bf 301 delete_file(fname);
2f03f956 302 }
2f03f956 303 if (do_symlink(file->link,fname) != 0) {
85d4d142 304 rprintf(FERROR,RSYNC_NAME": symlink \"%s\" -> \"%s\": %s\n",
2f03f956
AT
305 fname,file->link,strerror(errno));
306 } else {
307 set_perms(fname,file,NULL,0);
308 if (verbose) {
1bbd10fe 309 rprintf(FINFO,"%s -> %s\n", fname,file->link);
2f03f956
AT
310 }
311 }
312#endif
313 return;
314 }
315
316#ifdef HAVE_MKNOD
317 if (am_root && preserve_devices && IS_DEVICE(file->mode)) {
2cda2560 318 if (statret != 0 ||
2f03f956 319 st.st_mode != file->mode ||
2cda2560 320 st.st_rdev != file->rdev) {
2f03f956
AT
321 delete_file(fname);
322 if (verbose > 2)
323 rprintf(FINFO,"mknod(%s,0%o,0x%x)\n",
324 fname,(int)file->mode,(int)file->rdev);
325 if (do_mknod(fname,file->mode,file->rdev) != 0) {
326 rprintf(FERROR,"mknod %s : %s\n",fname,strerror(errno));
327 } else {
328 set_perms(fname,file,NULL,0);
329 if (verbose)
330 rprintf(FINFO,"%s\n",fname);
331 }
332 } else {
333 set_perms(fname,file,&st,1);
334 }
335 return;
336 }
337#endif
338
339 if (preserve_hard_links && check_hard_link(file)) {
340 if (verbose > 1)
fba31efb 341 rprintf(FINFO, "recv_generator: \"%s\" is a hard link\n",f_name(file));
2f03f956
AT
342 return;
343 }
344
345 if (!S_ISREG(file->mode)) {
1bbd10fe 346 rprintf(FINFO, "skipping non-regular file \"%s\"\n",fname);
2f03f956
AT
347 return;
348 }
349
375a4556
DD
350 fnamecmp = fname;
351
352 if ((statret == -1) && (compare_dest != NULL)) {
353 /* try the file at compare_dest instead */
354 int saveerrno = errno;
8950ac03 355 snprintf(fnamecmpbuf,MAXPATHLEN,"%s/%s",compare_dest,fname);
375a4556
DD
356 statret = link_stat(fnamecmpbuf,&st);
357 if (!S_ISREG(st.st_mode))
358 statret = -1;
359 if (statret == -1)
360 errno = saveerrno;
59c95e42
DD
361#if HAVE_LINK
362 else if (link_dest && !dry_run) {
363 if (do_link(fnamecmpbuf, fname) != 0) {
364 if (verbose > 0)
365 rprintf(FINFO,"link %s => %s : %s\n",
366 fnamecmpbuf,
367 fname,
368 strerror(errno));
369 }
370 fnamecmp = fnamecmpbuf;
371 }
372#endif
375a4556
DD
373 else
374 fnamecmp = fnamecmpbuf;
375 }
376
2f03f956
AT
377 if (statret == -1) {
378 if (errno == ENOENT) {
379 write_int(f_out,i);
80605142 380 if (!dry_run) send_null_sums(f_out);
2f03f956
AT
381 } else {
382 if (verbose > 1)
fb47591d
MP
383 rprintf(FERROR, RSYNC_NAME
384 ": recv_generator failed to open \"%s\": %s\n",
385 fname, strerror(errno));
2f03f956
AT
386 }
387 return;
388 }
389
390 if (!S_ISREG(st.st_mode)) {
391 if (delete_file(fname) != 0) {
392 return;
393 }
394
395 /* now pretend the file didn't exist */
396 write_int(f_out,i);
80605142 397 if (!dry_run) send_null_sums(f_out);
2f03f956
AT
398 return;
399 }
400
2cda2560 401 if (opt_ignore_existing && fnamecmp == fname) {
3d6feada
MP
402 if (verbose > 1)
403 rprintf(FINFO,"%s exists\n",fname);
404 return;
2cda2560 405 }
3d6feada 406
5b56cc19 407 if (update_only && cmp_modtime(st.st_mtime,file->modtime)>0 && fnamecmp == fname) {
2f03f956
AT
408 if (verbose > 1)
409 rprintf(FINFO,"%s is newer\n",fname);
410 return;
411 }
412
413 if (skip_file(fname, file, &st)) {
bd4ed7f7
DD
414 if (fnamecmp == fname)
415 set_perms(fname,file,&st,1);
2f03f956
AT
416 return;
417 }
418
419 if (dry_run) {
420 write_int(f_out,i);
421 return;
422 }
423
bceec82f 424 if (disable_deltas_p()) {
2f03f956 425 write_int(f_out,i);
80605142 426 send_null_sums(f_out);
2f03f956
AT
427 return;
428 }
429
2cda2560 430 /* open the file */
8c9fd200 431 fd = do_open(fnamecmp, O_RDONLY, 0);
2f03f956
AT
432
433 if (fd == -1) {
85d4d142 434 rprintf(FERROR,RSYNC_NAME": failed to open \"%s\", continuing : %s\n",fnamecmp,strerror(errno));
60be6acf
DD
435 /* pretend the file didn't exist */
436 write_int(f_out,i);
80605142 437 send_null_sums(f_out);
2f03f956
AT
438 return;
439 }
440
441 if (st.st_size > 0) {
442 buf = map_file(fd,st.st_size);
443 } else {
444 buf = NULL;
445 }
446
447 if (verbose > 3)
5f808dfb 448 rprintf(FINFO,"gen mapped %s of size %.0f\n",fnamecmp,(double)st.st_size);
2f03f956 449
2f03f956 450 if (verbose > 2)
80605142 451 rprintf(FINFO, "generating and sending sums for %d\n", i);
2f03f956
AT
452
453 write_int(f_out,i);
80605142
WD
454 generate_and_send_sums(buf, st.st_size,
455 adapt_block_size(file, block_size), f_out);
2f03f956
AT
456
457 close(fd);
458 if (buf) unmap_file(buf);
2f03f956
AT
459}
460
461
462
463void generate_files(int f,struct file_list *flist,char *local_name,int f_recv)
464{
465 int i;
466 int phase=0;
467
468 if (verbose > 2)
469 rprintf(FINFO,"generator starting pid=%d count=%d\n",
470 (int)getpid(),flist->count);
471
3e7053ac
MP
472 if (verbose >= 2) {
473 rprintf(FINFO,
2cda2560 474 disable_deltas_p()
3e7053ac
MP
475 ? "delta-transmission disabled for local transfer or --whole-file\n"
476 : "delta transmission enabled\n");
477 }
2cda2560 478
a57873b7
AT
479 /* we expect to just sit around now, so don't exit on a
480 timeout. If we really get a timeout then the other process should
481 exit */
482 io_timeout = 0;
483
2f03f956
AT
484 for (i = 0; i < flist->count; i++) {
485 struct file_struct *file = flist->files[i];
486 mode_t saved_mode = file->mode;
487 if (!file->basename) continue;
488
489 /* we need to ensure that any directories we create have writeable
490 permissions initially so that we can create the files within
491 them. This is then fixed after the files are transferred */
492 if (!am_root && S_ISDIR(file->mode)) {
493 file->mode |= S_IWUSR; /* user write */
2cda2560
WD
494 /* XXX: Could this be causing a problem on SCO? Perhaps their
495 * handling of permissions is strange? */
2f03f956
AT
496 }
497
2cda2560 498 recv_generator(local_name?local_name:f_name(file), flist,i,f);
2f03f956
AT
499
500 file->mode = saved_mode;
501 }
502
503 phase++;
504 csum_length = SUM_LENGTH;
505 ignore_times=1;
506
507 if (verbose > 2)
508 rprintf(FINFO,"generate_files phase=%d\n",phase);
509
510 write_int(f,-1);
511
2f03f956
AT
512 if (remote_version >= 13) {
513 /* in newer versions of the protocol the files can cycle through
514 the system more than once to catch initial checksum errors */
515 for (i=read_int(f_recv); i != -1; i=read_int(f_recv)) {
516 struct file_struct *file = flist->files[i];
517 recv_generator(local_name?local_name:f_name(file),
2cda2560 518 flist,i,f);
2f03f956
AT
519 }
520
521 phase++;
522 if (verbose > 2)
523 rprintf(FINFO,"generate_files phase=%d\n",phase);
524
525 write_int(f,-1);
526 }
527}