Commit | Line | Data |
---|---|---|
c627d613 AT |
1 | /* |
2 | Copyright (C) Andrew Tridgell 1996 | |
3 | Copyright (C) Paul Mackerras 1996 | |
4 | ||
5 | This program is free software; you can redistribute it and/or modify | |
6 | it under the terms of the GNU General Public License as published by | |
7 | the Free Software Foundation; either version 2 of the License, or | |
8 | (at your option) any later version. | |
9 | ||
10 | This program is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | GNU General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU General Public License | |
16 | along with this program; if not, write to the Free Software | |
17 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
18 | */ | |
19 | ||
20 | #include "rsync.h" | |
21 | ||
43a481dc AT |
22 | extern int csum_length; |
23 | ||
c627d613 AT |
24 | extern int verbose; |
25 | extern int am_server; | |
26 | extern int always_checksum; | |
27 | extern time_t starttime; | |
28 | ||
c6e7fcb4 AT |
29 | extern int remote_version; |
30 | ||
c627d613 AT |
31 | extern char *backup_suffix; |
32 | ||
33 | extern int block_size; | |
34 | extern int update_only; | |
35 | extern int make_backups; | |
36 | extern int preserve_links; | |
dc5ddbcc | 37 | extern int preserve_hard_links; |
c627d613 AT |
38 | extern int preserve_perms; |
39 | extern int preserve_devices; | |
40 | extern int preserve_uid; | |
41 | extern int preserve_gid; | |
42 | extern int preserve_times; | |
43 | extern int dry_run; | |
44 | extern int ignore_times; | |
45 | extern int recurse; | |
46 | extern int delete_mode; | |
47 | extern int cvs_exclude; | |
7b8356d0 | 48 | extern int am_root; |
6574b4f7 | 49 | extern int relative_paths; |
c627d613 AT |
50 | |
51 | /* | |
52 | free a sums struct | |
53 | */ | |
54 | static void free_sums(struct sum_struct *s) | |
55 | { | |
56 | if (s->sums) free(s->sums); | |
57 | free(s); | |
58 | } | |
59 | ||
60 | ||
61 | ||
62 | /* | |
63 | send a sums struct down a fd | |
64 | */ | |
65 | static void send_sums(struct sum_struct *s,int f_out) | |
66 | { | |
67 | int i; | |
68 | ||
69 | /* tell the other guy how many we are going to be doing and how many | |
70 | bytes there are in the last chunk */ | |
71 | write_int(f_out,s?s->count:0); | |
72 | write_int(f_out,s?s->n:block_size); | |
73 | write_int(f_out,s?s->remainder:0); | |
74 | if (s) | |
75 | for (i=0;i<s->count;i++) { | |
76 | write_int(f_out,s->sums[i].sum1); | |
43a481dc | 77 | write_buf(f_out,s->sums[i].sum2,csum_length); |
c627d613 AT |
78 | } |
79 | write_flush(f_out); | |
80 | } | |
81 | ||
82 | ||
83 | /* | |
84 | generate a stream of signatures/checksums that describe a buffer | |
85 | ||
86 | generate approximately one checksum every n bytes | |
87 | */ | |
c6e7fcb4 | 88 | static struct sum_struct *generate_sums(struct map_struct *buf,off_t len,int n) |
c627d613 AT |
89 | { |
90 | int i; | |
91 | struct sum_struct *s; | |
92 | int count; | |
93 | int block_len = n; | |
94 | int remainder = (len%block_len); | |
95 | off_t offset = 0; | |
96 | ||
97 | count = (len+(block_len-1))/block_len; | |
98 | ||
99 | s = (struct sum_struct *)malloc(sizeof(*s)); | |
100 | if (!s) out_of_memory("generate_sums"); | |
101 | ||
102 | s->count = count; | |
103 | s->remainder = remainder; | |
104 | s->n = n; | |
105 | s->flength = len; | |
106 | ||
107 | if (count==0) { | |
108 | s->sums = NULL; | |
109 | return s; | |
110 | } | |
111 | ||
112 | if (verbose > 3) | |
dc5ddbcc | 113 | fprintf(FERROR,"count=%d rem=%d n=%d flength=%d\n", |
c627d613 AT |
114 | s->count,s->remainder,s->n,(int)s->flength); |
115 | ||
116 | s->sums = (struct sum_buf *)malloc(sizeof(s->sums[0])*s->count); | |
117 | if (!s->sums) out_of_memory("generate_sums"); | |
118 | ||
119 | for (i=0;i<count;i++) { | |
120 | int n1 = MIN(len,n); | |
d9bea2dd | 121 | char *map = map_ptr(buf,offset,n1); |
c627d613 | 122 | |
d9bea2dd AT |
123 | s->sums[i].sum1 = get_checksum1(map,n1); |
124 | get_checksum2(map,n1,s->sums[i].sum2); | |
c627d613 AT |
125 | |
126 | s->sums[i].offset = offset; | |
127 | s->sums[i].len = n1; | |
128 | s->sums[i].i = i; | |
129 | ||
130 | if (verbose > 3) | |
dc5ddbcc | 131 | fprintf(FERROR,"chunk[%d] offset=%d len=%d sum1=%08x\n", |
c627d613 AT |
132 | i,(int)s->sums[i].offset,s->sums[i].len,s->sums[i].sum1); |
133 | ||
134 | len -= n1; | |
c627d613 AT |
135 | offset += n1; |
136 | } | |
137 | ||
138 | return s; | |
139 | } | |
140 | ||
141 | ||
142 | /* | |
143 | receive the checksums for a buffer | |
144 | */ | |
145 | static struct sum_struct *receive_sums(int f) | |
146 | { | |
147 | struct sum_struct *s; | |
148 | int i; | |
149 | off_t offset = 0; | |
150 | int block_len; | |
151 | ||
152 | s = (struct sum_struct *)malloc(sizeof(*s)); | |
153 | if (!s) out_of_memory("receive_sums"); | |
154 | ||
155 | s->count = read_int(f); | |
156 | s->n = read_int(f); | |
157 | s->remainder = read_int(f); | |
158 | s->sums = NULL; | |
159 | ||
160 | if (verbose > 3) | |
dc5ddbcc | 161 | fprintf(FERROR,"count=%d n=%d rem=%d\n", |
c627d613 AT |
162 | s->count,s->n,s->remainder); |
163 | ||
164 | block_len = s->n; | |
165 | ||
166 | if (s->count == 0) | |
167 | return(s); | |
168 | ||
169 | s->sums = (struct sum_buf *)malloc(sizeof(s->sums[0])*s->count); | |
170 | if (!s->sums) out_of_memory("receive_sums"); | |
171 | ||
172 | for (i=0;i<s->count;i++) { | |
173 | s->sums[i].sum1 = read_int(f); | |
43a481dc | 174 | read_buf(f,s->sums[i].sum2,csum_length); |
c627d613 AT |
175 | |
176 | s->sums[i].offset = offset; | |
177 | s->sums[i].i = i; | |
178 | ||
179 | if (i == s->count-1 && s->remainder != 0) { | |
180 | s->sums[i].len = s->remainder; | |
181 | } else { | |
182 | s->sums[i].len = s->n; | |
183 | } | |
184 | offset += s->sums[i].len; | |
185 | ||
186 | if (verbose > 3) | |
dc5ddbcc | 187 | fprintf(FERROR,"chunk[%d] len=%d offset=%d sum1=%08x\n", |
c627d613 AT |
188 | i,s->sums[i].len,(int)s->sums[i].offset,s->sums[i].sum1); |
189 | } | |
190 | ||
191 | s->flength = offset; | |
192 | ||
193 | return s; | |
194 | } | |
195 | ||
196 | ||
7b8356d0 AT |
197 | static int set_perms(char *fname,struct file_struct *file,struct stat *st, |
198 | int report) | |
c627d613 AT |
199 | { |
200 | int updated = 0; | |
201 | struct stat st2; | |
202 | ||
7b8356d0 | 203 | if (dry_run) return 0; |
c627d613 AT |
204 | |
205 | if (!st) { | |
7b8356d0 | 206 | if (lstat(fname,&st2) != 0) { |
dc5ddbcc | 207 | fprintf(FERROR,"stat %s : %s\n",fname,strerror(errno)); |
7b8356d0 | 208 | return 0; |
c627d613 AT |
209 | } |
210 | st = &st2; | |
211 | } | |
212 | ||
7bec6a5c AT |
213 | if (preserve_times && !S_ISLNK(st->st_mode) && |
214 | st->st_mtime != file->modtime) { | |
c627d613 AT |
215 | updated = 1; |
216 | if (set_modtime(fname,file->modtime) != 0) { | |
dc5ddbcc | 217 | fprintf(FERROR,"failed to set times on %s : %s\n", |
c627d613 | 218 | fname,strerror(errno)); |
7b8356d0 | 219 | return 0; |
c627d613 AT |
220 | } |
221 | } | |
222 | ||
223 | #ifdef HAVE_CHMOD | |
7bec6a5c AT |
224 | if (preserve_perms && !S_ISLNK(st->st_mode) && |
225 | st->st_mode != file->mode) { | |
c627d613 AT |
226 | updated = 1; |
227 | if (chmod(fname,file->mode) != 0) { | |
dc5ddbcc | 228 | fprintf(FERROR,"failed to set permissions on %s : %s\n", |
c627d613 | 229 | fname,strerror(errno)); |
7b8356d0 | 230 | return 0; |
c627d613 AT |
231 | } |
232 | } | |
233 | #endif | |
234 | ||
7b8356d0 | 235 | if ((am_root && preserve_uid && st->st_uid != file->uid) || |
c627d613 AT |
236 | (preserve_gid && st->st_gid != file->gid)) { |
237 | updated = 1; | |
8bf73749 AT |
238 | if (lchown(fname, |
239 | (am_root&&preserve_uid)?file->uid:-1, | |
240 | preserve_gid?file->gid:-1) != 0) { | |
c627d613 | 241 | if (verbose>1 || preserve_uid) |
dc5ddbcc | 242 | fprintf(FERROR,"chown %s : %s\n",fname,strerror(errno)); |
7b8356d0 | 243 | return updated; |
c627d613 AT |
244 | } |
245 | } | |
246 | ||
247 | if (verbose > 1 && report) { | |
248 | if (updated) | |
dc5ddbcc | 249 | fprintf(FINFO,"%s\n",fname); |
c627d613 | 250 | else |
dc5ddbcc | 251 | fprintf(FINFO,"%s is uptodate\n",fname); |
c627d613 | 252 | } |
7b8356d0 | 253 | return updated; |
c627d613 AT |
254 | } |
255 | ||
256 | ||
257 | void recv_generator(char *fname,struct file_list *flist,int i,int f_out) | |
258 | { | |
259 | int fd; | |
260 | struct stat st; | |
c6e7fcb4 | 261 | struct map_struct *buf; |
c627d613 | 262 | struct sum_struct *s; |
ebb0a6f6 | 263 | char sum[MD4_SUM_LENGTH]; |
c627d613 | 264 | int statret; |
dc5ddbcc | 265 | struct file_struct *file = &flist->files[i]; |
c627d613 AT |
266 | |
267 | if (verbose > 2) | |
9e31c482 | 268 | fprintf(FERROR,"recv_generator(%s,%d)\n",fname,i); |
c627d613 AT |
269 | |
270 | statret = lstat(fname,&st); | |
271 | ||
7b8356d0 AT |
272 | if (S_ISDIR(file->mode)) { |
273 | if (dry_run) return; | |
274 | if (statret == 0 && !S_ISDIR(st.st_mode)) { | |
275 | if (unlink(fname) != 0) { | |
276 | fprintf(FERROR,"unlink %s : %s\n",fname,strerror(errno)); | |
277 | return; | |
278 | } | |
279 | statret = -1; | |
280 | } | |
6574b4f7 AT |
281 | if (statret != 0 && mkdir(fname,file->mode) != 0 && errno != EEXIST) { |
282 | if (!(relative_paths && errno==ENOENT && | |
283 | create_directory_path(fname)==0 && | |
284 | mkdir(fname,file->mode)==0)) { | |
285 | fprintf(FERROR,"mkdir %s : %s (2)\n", | |
286 | fname,strerror(errno)); | |
287 | } | |
288 | } | |
7b8356d0 AT |
289 | if (set_perms(fname,file,NULL,0) && verbose) |
290 | fprintf(FINFO,"%s/\n",fname); | |
291 | return; | |
292 | } | |
293 | ||
c627d613 | 294 | #if SUPPORT_LINKS |
dc5ddbcc | 295 | if (preserve_links && S_ISLNK(file->mode)) { |
c627d613 AT |
296 | char lnk[MAXPATHLEN]; |
297 | int l; | |
298 | if (statret == 0) { | |
299 | l = readlink(fname,lnk,MAXPATHLEN-1); | |
300 | if (l > 0) { | |
301 | lnk[l] = 0; | |
dc5ddbcc AT |
302 | if (strcmp(lnk,file->link) == 0) { |
303 | set_perms(fname,file,&st,1); | |
c627d613 AT |
304 | return; |
305 | } | |
306 | } | |
307 | } | |
308 | if (!dry_run) unlink(fname); | |
dc5ddbcc AT |
309 | if (!dry_run && symlink(file->link,fname) != 0) { |
310 | fprintf(FERROR,"link %s -> %s : %s\n", | |
311 | fname,file->link,strerror(errno)); | |
c627d613 | 312 | } else { |
dc5ddbcc | 313 | set_perms(fname,file,NULL,0); |
c627d613 | 314 | if (verbose) |
dc5ddbcc AT |
315 | fprintf(FINFO,"%s -> %s\n", |
316 | fname,file->link); | |
c627d613 AT |
317 | } |
318 | return; | |
319 | } | |
320 | #endif | |
321 | ||
322 | #ifdef HAVE_MKNOD | |
7b8356d0 | 323 | if (am_root && preserve_devices && IS_DEVICE(file->mode)) { |
c627d613 | 324 | if (statret != 0 || |
dc5ddbcc AT |
325 | st.st_mode != file->mode || |
326 | st.st_rdev != file->rdev) { | |
c627d613 AT |
327 | if (!dry_run) unlink(fname); |
328 | if (verbose > 2) | |
dc5ddbcc AT |
329 | fprintf(FERROR,"mknod(%s,0%o,0x%x)\n", |
330 | fname,(int)file->mode,(int)file->rdev); | |
c627d613 | 331 | if (!dry_run && |
dc5ddbcc AT |
332 | mknod(fname,file->mode,file->rdev) != 0) { |
333 | fprintf(FERROR,"mknod %s : %s\n",fname,strerror(errno)); | |
c627d613 | 334 | } else { |
dc5ddbcc | 335 | set_perms(fname,file,NULL,0); |
c627d613 | 336 | if (verbose) |
dc5ddbcc | 337 | fprintf(FINFO,"%s\n",fname); |
c627d613 AT |
338 | } |
339 | } else { | |
dc5ddbcc | 340 | set_perms(fname,file,&st,1); |
c627d613 AT |
341 | } |
342 | return; | |
343 | } | |
344 | #endif | |
345 | ||
dc5ddbcc AT |
346 | if (preserve_hard_links && check_hard_link(file)) { |
347 | if (verbose > 1) | |
348 | fprintf(FINFO,"%s is a hard link\n",file->name); | |
349 | return; | |
350 | } | |
351 | ||
352 | if (!S_ISREG(file->mode)) { | |
353 | fprintf(FERROR,"skipping non-regular file %s\n",fname); | |
c627d613 AT |
354 | return; |
355 | } | |
356 | ||
357 | if (statret == -1) { | |
358 | if (errno == ENOENT) { | |
359 | write_int(f_out,i); | |
360 | if (!dry_run) send_sums(NULL,f_out); | |
361 | } else { | |
362 | if (verbose > 1) | |
dc5ddbcc | 363 | fprintf(FERROR,"recv_generator failed to open %s\n",fname); |
c627d613 AT |
364 | } |
365 | return; | |
366 | } | |
367 | ||
368 | if (!S_ISREG(st.st_mode)) { | |
7b8356d0 AT |
369 | /* its not a regular file on the receiving end, but it is on the |
370 | sending end. If its a directory then skip it (too dangerous to | |
371 | do a recursive deletion??) otherwise try to unlink it */ | |
372 | if (S_ISDIR(st.st_mode)) { | |
373 | fprintf(FERROR,"ERROR: %s is a directory\n",fname); | |
374 | return; | |
375 | } | |
376 | if (unlink(fname) != 0) { | |
377 | fprintf(FERROR,"%s : not a regular file (generator)\n",fname); | |
378 | return; | |
379 | } | |
380 | ||
381 | /* now pretend the file didn't exist */ | |
382 | write_int(f_out,i); | |
383 | if (!dry_run) send_sums(NULL,f_out); | |
c627d613 AT |
384 | return; |
385 | } | |
386 | ||
dc5ddbcc | 387 | if (update_only && st.st_mtime >= file->modtime) { |
c627d613 | 388 | if (verbose > 1) |
dc5ddbcc | 389 | fprintf(FERROR,"%s is newer\n",fname); |
c627d613 AT |
390 | return; |
391 | } | |
392 | ||
393 | if (always_checksum && S_ISREG(st.st_mode)) { | |
394 | file_checksum(fname,sum,st.st_size); | |
395 | } | |
396 | ||
dc5ddbcc AT |
397 | if (st.st_size == file->length && |
398 | ((!ignore_times && st.st_mtime == file->modtime) || | |
c627d613 | 399 | (always_checksum && S_ISREG(st.st_mode) && |
dc5ddbcc AT |
400 | memcmp(sum,file->sum,csum_length) == 0))) { |
401 | set_perms(fname,file,&st,1); | |
c627d613 AT |
402 | return; |
403 | } | |
404 | ||
405 | if (dry_run) { | |
406 | write_int(f_out,i); | |
407 | return; | |
408 | } | |
409 | ||
410 | /* open the file */ | |
411 | fd = open(fname,O_RDONLY); | |
412 | ||
413 | if (fd == -1) { | |
dc5ddbcc | 414 | fprintf(FERROR,"failed to open %s : %s\n",fname,strerror(errno)); |
c627d613 AT |
415 | return; |
416 | } | |
417 | ||
418 | if (st.st_size > 0) { | |
419 | buf = map_file(fd,st.st_size); | |
c627d613 AT |
420 | } else { |
421 | buf = NULL; | |
422 | } | |
423 | ||
424 | if (verbose > 3) | |
9e31c482 | 425 | fprintf(FERROR,"gen mapped %s of size %d\n",fname,(int)st.st_size); |
c627d613 AT |
426 | |
427 | s = generate_sums(buf,st.st_size,block_size); | |
428 | ||
9e31c482 AT |
429 | if (verbose > 2) |
430 | fprintf(FERROR,"sending sums for %d\n",i); | |
431 | ||
c627d613 AT |
432 | write_int(f_out,i); |
433 | send_sums(s,f_out); | |
434 | write_flush(f_out); | |
435 | ||
436 | close(fd); | |
1cdc8b50 | 437 | if (buf) unmap_file(buf); |
c627d613 AT |
438 | |
439 | free_sums(s); | |
440 | } | |
441 | ||
442 | ||
443 | ||
9e31c482 | 444 | static int receive_data(int f_in,struct map_struct *buf,int fd,char *fname) |
c627d613 AT |
445 | { |
446 | int i,n,remainder,len,count; | |
447 | off_t offset = 0; | |
448 | off_t offset2; | |
70d794dc | 449 | char *data; |
ebb0a6f6 AT |
450 | static char file_sum1[MD4_SUM_LENGTH]; |
451 | static char file_sum2[MD4_SUM_LENGTH]; | |
9e31c482 | 452 | char *map=NULL; |
c627d613 AT |
453 | |
454 | count = read_int(f_in); | |
455 | n = read_int(f_in); | |
456 | remainder = read_int(f_in); | |
457 | ||
9e31c482 AT |
458 | sum_init(); |
459 | ||
70d794dc | 460 | for (i=recv_token(f_in,&data); i != 0; i=recv_token(f_in,&data)) { |
c627d613 AT |
461 | if (i > 0) { |
462 | if (verbose > 3) | |
dc5ddbcc | 463 | fprintf(FERROR,"data recv %d at %d\n",i,(int)offset); |
c627d613 | 464 | |
9e31c482 AT |
465 | sum_update(data,i); |
466 | ||
7b8356d0 | 467 | if (fd != -1 && write_sparse(fd,data,i) != i) { |
dc5ddbcc | 468 | fprintf(FERROR,"write failed on %s : %s\n",fname,strerror(errno)); |
34ccb63e | 469 | exit_cleanup(1); |
c627d613 AT |
470 | } |
471 | offset += i; | |
472 | } else { | |
473 | i = -(i+1); | |
474 | offset2 = i*n; | |
475 | len = n; | |
476 | if (i == count-1 && remainder != 0) | |
477 | len = remainder; | |
478 | ||
479 | if (verbose > 3) | |
dc5ddbcc | 480 | fprintf(FERROR,"chunk[%d] of size %d at %d offset=%d\n", |
c627d613 AT |
481 | i,len,(int)offset2,(int)offset); |
482 | ||
9e31c482 AT |
483 | map = map_ptr(buf,offset2,len); |
484 | ||
861c20b4 | 485 | see_token(map, len); |
9e31c482 AT |
486 | sum_update(map,len); |
487 | ||
7b8356d0 | 488 | if (fd != -1 && write_sparse(fd,map,len) != len) { |
dc5ddbcc | 489 | fprintf(FERROR,"write failed on %s : %s\n",fname,strerror(errno)); |
34ccb63e | 490 | exit_cleanup(1); |
c627d613 AT |
491 | } |
492 | offset += len; | |
493 | } | |
494 | } | |
7bec6a5c | 495 | |
7b8356d0 | 496 | if (fd != -1 && offset > 0 && sparse_end(fd) != 0) { |
dc5ddbcc | 497 | fprintf(FERROR,"write failed on %s : %s\n",fname,strerror(errno)); |
34ccb63e | 498 | exit_cleanup(1); |
7bec6a5c | 499 | } |
9e31c482 AT |
500 | |
501 | sum_end(file_sum1); | |
502 | ||
503 | if (remote_version >= 14) { | |
ebb0a6f6 | 504 | read_buf(f_in,file_sum2,MD4_SUM_LENGTH); |
9e31c482 AT |
505 | if (verbose > 2) |
506 | fprintf(FERROR,"got file_sum\n"); | |
7b8356d0 | 507 | if (fd != -1 && memcmp(file_sum1,file_sum2,MD4_SUM_LENGTH) != 0) |
9e31c482 AT |
508 | return 0; |
509 | } | |
510 | return 1; | |
c627d613 AT |
511 | } |
512 | ||
513 | ||
514 | static void delete_one(struct file_struct *f) | |
515 | { | |
516 | if (!S_ISDIR(f->mode)) { | |
517 | if (!dry_run && unlink(f->name) != 0) { | |
dc5ddbcc | 518 | fprintf(FERROR,"unlink %s : %s\n",f->name,strerror(errno)); |
c627d613 | 519 | } else if (verbose) { |
dc5ddbcc | 520 | fprintf(FERROR,"deleting %s\n",f->name); |
c627d613 AT |
521 | } |
522 | } else { | |
523 | if (!dry_run && rmdir(f->name) != 0) { | |
524 | if (errno != ENOTEMPTY) | |
dc5ddbcc | 525 | fprintf(FERROR,"rmdir %s : %s\n",f->name,strerror(errno)); |
c627d613 | 526 | } else if (verbose) { |
dc5ddbcc | 527 | fprintf(FERROR,"deleting directory %s\n",f->name); |
c627d613 AT |
528 | } |
529 | } | |
530 | } | |
531 | ||
532 | ||
533 | static void delete_files(struct file_list *flist) | |
534 | { | |
535 | struct file_list *local_file_list; | |
536 | char *dot="."; | |
537 | int i; | |
538 | ||
4fe159a8 | 539 | if (cvs_exclude) |
79fbb6f5 | 540 | add_cvs_excludes(); |
4fe159a8 | 541 | |
a06d19e3 | 542 | if (!(local_file_list = send_file_list(-1,1,&dot))) |
c627d613 AT |
543 | return; |
544 | ||
774ef68f | 545 | for (i=local_file_list->count-1;i>=0;i--) { |
c627d613 AT |
546 | if (!local_file_list->files[i].name) continue; |
547 | if (-1 == flist_find(flist,&local_file_list->files[i])) { | |
548 | delete_one(&local_file_list->files[i]); | |
549 | } | |
550 | } | |
551 | } | |
552 | ||
553 | static char *cleanup_fname = NULL; | |
554 | ||
34ccb63e | 555 | void exit_cleanup(int code) |
c627d613 AT |
556 | { |
557 | if (cleanup_fname) | |
558 | unlink(cleanup_fname); | |
34ccb63e AT |
559 | exit(code); |
560 | } | |
561 | ||
562 | void sig_int(void) | |
563 | { | |
564 | exit_cleanup(1); | |
c627d613 AT |
565 | } |
566 | ||
567 | ||
c6e7fcb4 | 568 | int recv_files(int f_in,struct file_list *flist,char *local_name,int f_gen) |
c627d613 AT |
569 | { |
570 | int fd1,fd2; | |
571 | struct stat st; | |
572 | char *fname; | |
573 | char fnametmp[MAXPATHLEN]; | |
c6e7fcb4 | 574 | struct map_struct *buf; |
c627d613 | 575 | int i; |
dc5ddbcc | 576 | struct file_struct *file; |
c6e7fcb4 | 577 | int phase=0; |
9e31c482 | 578 | int recv_ok; |
c627d613 | 579 | |
981791bd | 580 | if (verbose > 2) { |
dc5ddbcc | 581 | fprintf(FERROR,"recv_files(%d) starting\n",flist->count); |
981791bd | 582 | } |
c627d613 AT |
583 | |
584 | if (recurse && delete_mode && !local_name && flist->count>0) { | |
585 | delete_files(flist); | |
586 | } | |
587 | ||
588 | while (1) | |
9e31c482 | 589 | { |
c627d613 | 590 | i = read_int(f_in); |
c6e7fcb4 AT |
591 | if (i == -1) { |
592 | if (phase==0 && remote_version >= 13) { | |
593 | phase++; | |
9e31c482 AT |
594 | csum_length = SUM_LENGTH; |
595 | if (verbose > 2) | |
596 | fprintf(FERROR,"recv_files phase=%d\n",phase); | |
c6e7fcb4 AT |
597 | write_int(f_gen,-1); |
598 | write_flush(f_gen); | |
599 | continue; | |
600 | } | |
601 | break; | |
602 | } | |
c627d613 | 603 | |
dc5ddbcc AT |
604 | file = &flist->files[i]; |
605 | fname = file->name; | |
c627d613 AT |
606 | |
607 | if (local_name) | |
608 | fname = local_name; | |
609 | ||
610 | if (dry_run) { | |
611 | if (!am_server && verbose) | |
612 | printf("%s\n",fname); | |
613 | continue; | |
614 | } | |
615 | ||
616 | if (verbose > 2) | |
dc5ddbcc | 617 | fprintf(FERROR,"recv_files(%s)\n",fname); |
c627d613 AT |
618 | |
619 | /* open the file */ | |
ac1eb754 | 620 | fd1 = open(fname,O_RDONLY); |
c627d613 | 621 | |
ac1eb754 | 622 | if (fd1 != -1 && fstat(fd1,&st) != 0) { |
dc5ddbcc | 623 | fprintf(FERROR,"fstat %s : %s\n",fname,strerror(errno)); |
7b8356d0 | 624 | receive_data(f_in,NULL,-1,NULL); |
c627d613 | 625 | close(fd1); |
7b8356d0 | 626 | continue; |
c627d613 AT |
627 | } |
628 | ||
ac1eb754 | 629 | if (fd1 != -1 && !S_ISREG(st.st_mode)) { |
7b8356d0 AT |
630 | fprintf(FERROR,"%s : not a regular file (recv_files)\n",fname); |
631 | receive_data(f_in,NULL,-1,NULL); | |
c627d613 | 632 | close(fd1); |
7b8356d0 | 633 | continue; |
c627d613 AT |
634 | } |
635 | ||
ac1eb754 | 636 | if (fd1 != -1 && st.st_size > 0) { |
c627d613 | 637 | buf = map_file(fd1,st.st_size); |
9e31c482 AT |
638 | if (verbose > 2) |
639 | fprintf(FERROR,"recv mapped %s of size %d\n",fname,(int)st.st_size); | |
c627d613 AT |
640 | } else { |
641 | buf = NULL; | |
642 | } | |
643 | ||
c627d613 AT |
644 | /* open tmp file */ |
645 | sprintf(fnametmp,"%s.XXXXXX",fname); | |
646 | if (NULL == mktemp(fnametmp)) { | |
dc5ddbcc | 647 | fprintf(FERROR,"mktemp %s failed\n",fnametmp); |
7b8356d0 AT |
648 | receive_data(f_in,buf,-1,NULL); |
649 | if (buf) unmap_file(buf); | |
650 | close(fd1); | |
651 | continue; | |
c627d613 | 652 | } |
dc5ddbcc | 653 | fd2 = open(fnametmp,O_WRONLY|O_CREAT,file->mode); |
6574b4f7 AT |
654 | if (relative_paths && errno == ENOENT && |
655 | create_directory_path(fnametmp) == 0) { | |
656 | fd2 = open(fnametmp,O_WRONLY|O_CREAT,file->mode); | |
657 | } | |
c627d613 | 658 | if (fd2 == -1) { |
dc5ddbcc | 659 | fprintf(FERROR,"open %s : %s\n",fnametmp,strerror(errno)); |
7b8356d0 AT |
660 | receive_data(f_in,buf,-1,NULL); |
661 | if (buf) unmap_file(buf); | |
662 | close(fd1); | |
663 | continue; | |
c627d613 AT |
664 | } |
665 | ||
666 | cleanup_fname = fnametmp; | |
667 | ||
668 | if (!am_server && verbose) | |
669 | printf("%s\n",fname); | |
670 | ||
671 | /* recv file data */ | |
9e31c482 | 672 | recv_ok = receive_data(f_in,buf,fd2,fname); |
c627d613 | 673 | |
981791bd | 674 | if (fd1 != -1) { |
1cdc8b50 | 675 | if (buf) unmap_file(buf); |
981791bd AT |
676 | close(fd1); |
677 | } | |
c627d613 AT |
678 | close(fd2); |
679 | ||
680 | if (verbose > 2) | |
dc5ddbcc | 681 | fprintf(FERROR,"renaming %s to %s\n",fnametmp,fname); |
c627d613 AT |
682 | |
683 | if (make_backups) { | |
684 | char fnamebak[MAXPATHLEN]; | |
685 | sprintf(fnamebak,"%s%s",fname,backup_suffix); | |
ac1eb754 | 686 | if (rename(fname,fnamebak) != 0 && errno != ENOENT) { |
dc5ddbcc | 687 | fprintf(FERROR,"rename %s %s : %s\n",fname,fnamebak,strerror(errno)); |
7b8356d0 | 688 | continue; |
c627d613 AT |
689 | } |
690 | } | |
691 | ||
692 | /* move tmp file over real file */ | |
693 | if (rename(fnametmp,fname) != 0) { | |
dc5ddbcc | 694 | fprintf(FERROR,"rename %s -> %s : %s\n", |
c627d613 | 695 | fnametmp,fname,strerror(errno)); |
7b8356d0 | 696 | unlink(fnametmp); |
c627d613 AT |
697 | } |
698 | ||
699 | cleanup_fname = NULL; | |
700 | ||
dc5ddbcc | 701 | set_perms(fname,file,NULL,0); |
9e31c482 AT |
702 | |
703 | if (!recv_ok) { | |
704 | if (verbose > 1) | |
705 | fprintf(FERROR,"redoing %s(%d)\n",fname,i); | |
52296954 AT |
706 | if (csum_length == SUM_LENGTH) |
707 | fprintf(FERROR,"ERROR: file corruption in %s\n",fname); | |
9e31c482 AT |
708 | write_int(f_gen,i); |
709 | } | |
c627d613 AT |
710 | } |
711 | ||
7b8356d0 AT |
712 | /* now we need to fix any directory permissions that were |
713 | modified during the transfer */ | |
714 | if (!am_root) { | |
715 | for (i = 0; i < flist->count; i++) { | |
716 | struct file_struct *file = &flist->files[i]; | |
717 | if (!file->name || !S_ISDIR(file->mode)) continue; | |
718 | recv_generator(file->name,flist,i,-1); | |
719 | } | |
720 | } | |
721 | ||
722 | ||
c627d613 | 723 | if (verbose > 2) |
dc5ddbcc | 724 | fprintf(FERROR,"recv_files finished\n"); |
c627d613 AT |
725 | |
726 | return 0; | |
727 | } | |
728 | ||
729 | ||
730 | ||
731 | off_t send_files(struct file_list *flist,int f_out,int f_in) | |
732 | { | |
733 | int fd; | |
734 | struct sum_struct *s; | |
c6e7fcb4 | 735 | struct map_struct *buf; |
c627d613 AT |
736 | struct stat st; |
737 | char fname[MAXPATHLEN]; | |
738 | off_t total=0; | |
739 | int i; | |
dc5ddbcc | 740 | struct file_struct *file; |
c6e7fcb4 | 741 | int phase = 0; |
c627d613 AT |
742 | |
743 | if (verbose > 2) | |
dc5ddbcc | 744 | fprintf(FERROR,"send_files starting\n"); |
c627d613 | 745 | |
720b47f2 AT |
746 | setup_nonblocking(f_in,f_out); |
747 | ||
c627d613 AT |
748 | while (1) |
749 | { | |
750 | i = read_int(f_in); | |
c6e7fcb4 AT |
751 | if (i == -1) { |
752 | if (phase==0 && remote_version >= 13) { | |
753 | phase++; | |
9e31c482 | 754 | csum_length = SUM_LENGTH; |
c6e7fcb4 AT |
755 | write_int(f_out,-1); |
756 | write_flush(f_out); | |
9e31c482 AT |
757 | if (verbose > 2) |
758 | fprintf(FERROR,"send_files phase=%d\n",phase); | |
c6e7fcb4 AT |
759 | continue; |
760 | } | |
761 | break; | |
762 | } | |
c627d613 | 763 | |
dc5ddbcc AT |
764 | file = &flist->files[i]; |
765 | ||
c627d613 | 766 | fname[0] = 0; |
dc5ddbcc AT |
767 | if (file->dir) { |
768 | strcpy(fname,file->dir); | |
c627d613 AT |
769 | strcat(fname,"/"); |
770 | } | |
dc5ddbcc | 771 | strcat(fname,file->name); |
c627d613 AT |
772 | |
773 | if (verbose > 2) | |
dc5ddbcc | 774 | fprintf(FERROR,"send_files(%d,%s)\n",i,fname); |
c627d613 AT |
775 | |
776 | if (dry_run) { | |
777 | if (!am_server && verbose) | |
778 | printf("%s\n",fname); | |
779 | write_int(f_out,i); | |
780 | continue; | |
781 | } | |
782 | ||
783 | s = receive_sums(f_in); | |
784 | if (!s) { | |
dc5ddbcc | 785 | fprintf(FERROR,"receive_sums failed\n"); |
c627d613 AT |
786 | return -1; |
787 | } | |
788 | ||
789 | fd = open(fname,O_RDONLY); | |
790 | if (fd == -1) { | |
dc5ddbcc | 791 | fprintf(FERROR,"send_files failed to open %s: %s\n", |
c627d613 AT |
792 | fname,strerror(errno)); |
793 | continue; | |
794 | } | |
795 | ||
796 | /* map the local file */ | |
797 | if (fstat(fd,&st) != 0) { | |
dc5ddbcc | 798 | fprintf(FERROR,"fstat failed : %s\n",strerror(errno)); |
c627d613 AT |
799 | return -1; |
800 | } | |
801 | ||
802 | if (st.st_size > 0) { | |
803 | buf = map_file(fd,st.st_size); | |
c627d613 AT |
804 | } else { |
805 | buf = NULL; | |
806 | } | |
807 | ||
808 | if (verbose > 2) | |
dc5ddbcc | 809 | fprintf(FERROR,"send_files mapped %s of size %d\n", |
c627d613 AT |
810 | fname,(int)st.st_size); |
811 | ||
812 | write_int(f_out,i); | |
813 | ||
814 | write_int(f_out,s->count); | |
815 | write_int(f_out,s->n); | |
816 | write_int(f_out,s->remainder); | |
817 | ||
818 | if (verbose > 2) | |
dc5ddbcc | 819 | fprintf(FERROR,"calling match_sums %s\n",fname); |
c627d613 AT |
820 | |
821 | if (!am_server && verbose) | |
822 | printf("%s\n",fname); | |
823 | ||
720b47f2 | 824 | match_sums(f_out,s,buf,st.st_size); |
c627d613 AT |
825 | write_flush(f_out); |
826 | ||
1cdc8b50 | 827 | if (buf) unmap_file(buf); |
c627d613 AT |
828 | close(fd); |
829 | ||
830 | free_sums(s); | |
831 | ||
832 | if (verbose > 2) | |
dc5ddbcc | 833 | fprintf(FERROR,"sender finished %s\n",fname); |
c627d613 AT |
834 | |
835 | total += st.st_size; | |
836 | } | |
837 | ||
dc5ddbcc AT |
838 | if (verbose > 2) |
839 | fprintf(FERROR,"send files finished\n"); | |
840 | ||
c627d613 AT |
841 | match_report(); |
842 | ||
843 | write_int(f_out,-1); | |
844 | write_flush(f_out); | |
845 | ||
846 | return total; | |
847 | } | |
848 | ||
849 | ||
850 | ||
c6e7fcb4 | 851 | void generate_files(int f,struct file_list *flist,char *local_name,int f_recv) |
c627d613 AT |
852 | { |
853 | int i; | |
9e31c482 | 854 | int phase=0; |
c627d613 AT |
855 | |
856 | if (verbose > 2) | |
dc5ddbcc | 857 | fprintf(FERROR,"generator starting pid=%d count=%d\n", |
c627d613 AT |
858 | (int)getpid(),flist->count); |
859 | ||
860 | for (i = 0; i < flist->count; i++) { | |
dc5ddbcc | 861 | struct file_struct *file = &flist->files[i]; |
7b8356d0 | 862 | mode_t saved_mode = file->mode; |
dc5ddbcc | 863 | if (!file->name) continue; |
7b8356d0 AT |
864 | |
865 | /* we need to ensure that any directories we create have writeable | |
866 | permissions initially so that we can create the files within | |
867 | them. This is then fixed after the files are transferred */ | |
868 | if (!am_root && S_ISDIR(file->mode)) { | |
869 | file->mode |= S_IWUSR; /* user write */ | |
c627d613 | 870 | } |
7b8356d0 | 871 | |
dc5ddbcc | 872 | recv_generator(local_name?local_name:file->name, |
c627d613 | 873 | flist,i,f); |
7b8356d0 AT |
874 | |
875 | file->mode = saved_mode; | |
c627d613 | 876 | } |
c6e7fcb4 | 877 | |
9e31c482 AT |
878 | phase++; |
879 | csum_length = SUM_LENGTH; | |
880 | ignore_times=1; | |
881 | ||
882 | if (verbose > 2) | |
883 | fprintf(FERROR,"generate_files phase=%d\n",phase); | |
884 | ||
c627d613 AT |
885 | write_int(f,-1); |
886 | write_flush(f); | |
c6e7fcb4 AT |
887 | |
888 | if (remote_version >= 13) { | |
7b8356d0 AT |
889 | /* in newer versions of the protocol the files can cycle through |
890 | the system more than once to catch initial checksum errors */ | |
c6e7fcb4 AT |
891 | for (i=read_int(f_recv); i != -1; i=read_int(f_recv)) { |
892 | struct file_struct *file = &flist->files[i]; | |
893 | recv_generator(local_name?local_name:file->name, | |
894 | flist,i,f); | |
895 | } | |
896 | ||
9e31c482 AT |
897 | phase++; |
898 | if (verbose > 2) | |
899 | fprintf(FERROR,"generate_files phase=%d\n",phase); | |
900 | ||
c6e7fcb4 AT |
901 | write_int(f,-1); |
902 | write_flush(f); | |
903 | } | |
904 | ||
905 | ||
c627d613 | 906 | if (verbose > 2) |
dc5ddbcc | 907 | fprintf(FERROR,"generator wrote %d\n",write_total()); |
c627d613 | 908 | } |
dc5ddbcc AT |
909 | |
910 |