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