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 | /* generate and receive file lists */ | |
21 | ||
22 | #include "rsync.h" | |
23 | ||
43a481dc AT |
24 | extern int csum_length; |
25 | ||
c627d613 AT |
26 | extern int verbose; |
27 | extern int am_server; | |
28 | extern int always_checksum; | |
29 | extern off_t total_size; | |
30 | ||
31 | extern int cvs_exclude; | |
32 | ||
33 | extern int one_file_system; | |
34 | extern int make_backups; | |
35 | extern int preserve_links; | |
36 | extern int preserve_perms; | |
37 | extern int preserve_devices; | |
38 | extern int preserve_uid; | |
39 | extern int preserve_gid; | |
40 | extern int preserve_times; | |
41 | ||
42 | static char **local_exclude_list = NULL; | |
43 | ||
44 | static void clean_fname(char *name); | |
45 | ||
46 | ||
47 | /* | |
48 | This function is used to check if a file should be included/excluded | |
49 | from the list of files based on its name and type etc | |
50 | */ | |
51 | static int match_file_name(char *fname,struct stat *st) | |
52 | { | |
53 | if (check_exclude(fname,local_exclude_list)) { | |
54 | if (verbose > 2) | |
55 | fprintf(stderr,"excluding file %s\n",fname); | |
56 | return 0; | |
57 | } | |
58 | return 1; | |
59 | } | |
60 | ||
61 | /* used by the one_file_system code */ | |
62 | static dev_t filesystem_dev; | |
63 | ||
64 | static void set_filesystem(char *fname) | |
65 | { | |
66 | struct stat st; | |
67 | if (lstat(fname,&st) != 0) return; | |
68 | filesystem_dev = st.st_dev; | |
69 | } | |
70 | ||
71 | ||
72 | static void send_directory(int f,struct file_list *flist,char *dir); | |
73 | ||
74 | static char *flist_dir = NULL; | |
75 | ||
4fe159a8 AT |
76 | extern void (*send_file_entry)(struct file_struct *file,int f); |
77 | extern void (*receive_file_entry)(struct file_struct *file, | |
78 | unsigned char flags,int f); | |
79 | ||
80 | ||
81 | void send_file_entry_v11(struct file_struct *file,int f) | |
c627d613 | 82 | { |
182dca5c | 83 | unsigned char flags; |
4fe159a8 | 84 | static time_t last_time=0; |
182dca5c AT |
85 | static mode_t last_mode=0; |
86 | static dev_t last_dev=0; | |
87 | static uid_t last_uid=0; | |
88 | static gid_t last_gid=0; | |
4fe159a8 AT |
89 | static char lastname[MAXPATHLEN]=""; |
90 | int l1,l2; | |
182dca5c | 91 | |
c627d613 AT |
92 | if (f == -1) return; |
93 | ||
182dca5c AT |
94 | if (!file) { |
95 | write_byte(f,0); | |
96 | return; | |
97 | } | |
98 | ||
99 | flags = FILE_VALID; | |
100 | ||
101 | if (file->mode == last_mode) flags |= SAME_MODE; | |
102 | if (file->dev == last_dev) flags |= SAME_DEV; | |
103 | if (file->uid == last_uid) flags |= SAME_UID; | |
104 | if (file->gid == last_gid) flags |= SAME_GID; | |
4fe159a8 | 105 | if (file->modtime == last_time) flags |= SAME_TIME; |
182dca5c | 106 | |
4fe159a8 AT |
107 | for (l1=0;lastname[l1] && file->name[l1] == lastname[l1];l1++) ; |
108 | l2 = strlen(file->name) - l1; | |
109 | ||
110 | if (l1 > 0) flags |= SAME_NAME; | |
111 | if (l2 > 255) flags |= LONG_NAME; | |
112 | ||
113 | write_byte(f,flags); | |
114 | if (flags & SAME_NAME) | |
115 | write_byte(f,l1); | |
116 | if (flags & LONG_NAME) | |
117 | write_int(f,l2); | |
9f3541e6 | 118 | else |
4fe159a8 AT |
119 | write_byte(f,l2); |
120 | write_buf(f,file->name+l1,l2); | |
121 | ||
c627d613 | 122 | write_int(f,(int)file->length); |
4fe159a8 AT |
123 | if (!(flags & SAME_TIME)) |
124 | write_int(f,(int)file->modtime); | |
182dca5c AT |
125 | if (!(flags & SAME_MODE)) |
126 | write_int(f,(int)file->mode); | |
127 | if (preserve_uid && !(flags & SAME_UID)) | |
c627d613 | 128 | write_int(f,(int)file->uid); |
182dca5c | 129 | if (preserve_gid && !(flags & SAME_GID)) |
c627d613 | 130 | write_int(f,(int)file->gid); |
182dca5c | 131 | if (preserve_devices && IS_DEVICE(file->mode) && !(flags & SAME_DEV)) |
c627d613 | 132 | write_int(f,(int)file->dev); |
c627d613 AT |
133 | |
134 | #if SUPPORT_LINKS | |
135 | if (preserve_links && S_ISLNK(file->mode)) { | |
136 | write_int(f,strlen(file->link)); | |
137 | write_buf(f,file->link,strlen(file->link)); | |
138 | } | |
139 | #endif | |
140 | ||
141 | if (always_checksum) { | |
43a481dc | 142 | write_buf(f,file->sum,csum_length); |
c627d613 | 143 | } |
182dca5c AT |
144 | |
145 | last_mode = file->mode; | |
146 | last_dev = file->dev; | |
147 | last_uid = file->uid; | |
148 | last_gid = file->gid; | |
4fe159a8 AT |
149 | last_time = file->modtime; |
150 | ||
151 | strcpy(lastname,file->name); | |
152 | lastname[255] = 0; | |
182dca5c AT |
153 | } |
154 | ||
155 | ||
156 | ||
4fe159a8 AT |
157 | void receive_file_entry_v11(struct file_struct *file, |
158 | unsigned char flags,int f) | |
182dca5c | 159 | { |
4fe159a8 | 160 | static mode_t last_time=0; |
182dca5c AT |
161 | static mode_t last_mode=0; |
162 | static dev_t last_dev=0; | |
163 | static uid_t last_uid=0; | |
164 | static gid_t last_gid=0; | |
4fe159a8 AT |
165 | static char lastname[MAXPATHLEN]=""; |
166 | int l1=0,l2=0; | |
182dca5c | 167 | |
4fe159a8 | 168 | if (flags & SAME_NAME) |
9f3541e6 | 169 | l1 = read_byte(f); |
4fe159a8 AT |
170 | |
171 | if (flags & LONG_NAME) | |
172 | l2 = read_int(f); | |
173 | else | |
174 | l2 = read_byte(f); | |
182dca5c AT |
175 | |
176 | file->name = (char *)malloc(l1+l2+1); | |
177 | if (!file->name) out_of_memory("receive_file_entry"); | |
178 | ||
4fe159a8 AT |
179 | strncpy(file->name,lastname,l1); |
180 | read_buf(f,file->name+l1,l2); | |
182dca5c AT |
181 | file->name[l1+l2] = 0; |
182 | ||
182dca5c | 183 | file->length = (off_t)read_int(f); |
4fe159a8 | 184 | file->modtime = (flags & SAME_TIME) ? last_time : (time_t)read_int(f); |
182dca5c AT |
185 | file->mode = (flags & SAME_MODE) ? last_mode : (mode_t)read_int(f); |
186 | if (preserve_uid) | |
187 | file->uid = (flags & SAME_UID) ? last_uid : (uid_t)read_int(f); | |
188 | if (preserve_gid) | |
189 | file->gid = (flags & SAME_GID) ? last_gid : (gid_t)read_int(f); | |
190 | if (preserve_devices && IS_DEVICE(file->mode)) | |
191 | file->dev = (flags & SAME_DEV) ? last_dev : (dev_t)read_int(f); | |
192 | ||
193 | #if SUPPORT_LINKS | |
194 | if (preserve_links && S_ISLNK(file->mode)) { | |
195 | int l = read_int(f); | |
196 | file->link = (char *)malloc(l+1); | |
197 | if (!file->link) out_of_memory("receive_file_entry"); | |
198 | read_buf(f,file->link,l); | |
199 | file->link[l] = 0; | |
200 | } | |
201 | #endif | |
202 | ||
203 | if (always_checksum) | |
43a481dc | 204 | read_buf(f,file->sum,csum_length); |
182dca5c AT |
205 | |
206 | last_mode = file->mode; | |
207 | last_dev = file->dev; | |
208 | last_uid = file->uid; | |
209 | last_gid = file->gid; | |
4fe159a8 AT |
210 | last_time = file->modtime; |
211 | ||
212 | strcpy(lastname,file->name); | |
213 | lastname[255] = 0; | |
c627d613 AT |
214 | } |
215 | ||
216 | ||
4fe159a8 | 217 | |
c627d613 AT |
218 | static struct file_struct *make_file(int recurse,char *fname) |
219 | { | |
220 | static struct file_struct file; | |
221 | struct stat st; | |
222 | char sum[SUM_LENGTH]; | |
223 | ||
224 | bzero(sum,SUM_LENGTH); | |
225 | ||
226 | if (lstat(fname,&st) != 0) { | |
227 | fprintf(stderr,"%s: %s\n", | |
228 | fname,strerror(errno)); | |
229 | return NULL; | |
230 | } | |
231 | ||
232 | if (S_ISDIR(st.st_mode) && !recurse) { | |
233 | fprintf(stderr,"skipping directory %s\n",fname); | |
234 | return NULL; | |
235 | } | |
236 | ||
237 | if (one_file_system && st.st_dev != filesystem_dev) | |
238 | return NULL; | |
239 | ||
240 | if (!match_file_name(fname,&st)) | |
241 | return NULL; | |
242 | ||
243 | if (verbose > 2) | |
244 | fprintf(stderr,"make_file(%s)\n",fname); | |
245 | ||
246 | file.name = strdup(fname); | |
247 | file.modtime = st.st_mtime; | |
248 | file.length = st.st_size; | |
249 | file.mode = st.st_mode; | |
250 | file.uid = st.st_uid; | |
251 | file.gid = st.st_gid; | |
252 | #ifdef HAVE_ST_RDEV | |
253 | file.dev = st.st_rdev; | |
254 | #endif | |
255 | ||
256 | #if SUPPORT_LINKS | |
257 | if (S_ISLNK(st.st_mode)) { | |
258 | int l; | |
259 | char lnk[MAXPATHLEN]; | |
260 | if ((l=readlink(fname,lnk,MAXPATHLEN-1)) == -1) { | |
261 | fprintf(stderr,"readlink %s : %s\n",fname,strerror(errno)); | |
262 | return NULL; | |
263 | } | |
264 | lnk[l] = 0; | |
265 | file.link = strdup(lnk); | |
266 | } | |
267 | #endif | |
268 | ||
269 | if (always_checksum && S_ISREG(st.st_mode)) { | |
270 | file_checksum(fname,file.sum,st.st_size); | |
271 | } | |
272 | ||
273 | if (flist_dir) | |
274 | file.dir = strdup(flist_dir); | |
275 | else | |
276 | file.dir = NULL; | |
277 | ||
278 | if (!S_ISDIR(st.st_mode)) | |
279 | total_size += st.st_size; | |
280 | ||
281 | return &file; | |
282 | } | |
283 | ||
284 | ||
285 | ||
286 | static void send_file_name(int f,struct file_list *flist, | |
287 | int recurse,char *fname) | |
288 | { | |
289 | struct file_struct *file; | |
290 | ||
291 | file = make_file(recurse,fname); | |
292 | ||
293 | if (!file) return; | |
294 | ||
295 | if (flist->count >= flist->malloced) { | |
296 | flist->malloced += 100; | |
297 | flist->files = (struct file_struct *)realloc(flist->files, | |
298 | sizeof(flist->files[0])* | |
299 | flist->malloced); | |
300 | if (!flist->files) | |
301 | out_of_memory("send_file_name"); | |
302 | } | |
303 | ||
304 | if (strcmp(file->name,".") && strcmp(file->name,"/")) { | |
305 | flist->files[flist->count++] = *file; | |
306 | send_file_entry(file,f); | |
307 | } | |
308 | ||
309 | if (S_ISDIR(file->mode) && recurse) { | |
310 | char **last_exclude_list = local_exclude_list; | |
311 | send_directory(f,flist,file->name); | |
312 | local_exclude_list = last_exclude_list; | |
313 | return; | |
314 | } | |
315 | } | |
316 | ||
317 | ||
318 | ||
319 | static void send_directory(int f,struct file_list *flist,char *dir) | |
320 | { | |
321 | DIR *d; | |
322 | struct dirent *di; | |
323 | char fname[MAXPATHLEN]; | |
324 | int l; | |
325 | char *p; | |
326 | ||
327 | d = opendir(dir); | |
328 | if (!d) { | |
329 | fprintf(stderr,"%s: %s\n", | |
330 | dir,strerror(errno)); | |
331 | return; | |
332 | } | |
333 | ||
334 | strcpy(fname,dir); | |
335 | l = strlen(fname); | |
336 | if (fname[l-1] != '/') | |
337 | strcat(fname,"/"); | |
338 | p = fname + strlen(fname); | |
339 | ||
340 | if (cvs_exclude) { | |
341 | strcpy(p,".cvsignore"); | |
342 | local_exclude_list = make_exclude_list(fname,NULL,0); | |
343 | } | |
344 | ||
345 | for (di=readdir(d); di; di=readdir(d)) { | |
346 | if (strcmp(di->d_name,".")==0 || | |
347 | strcmp(di->d_name,"..")==0) | |
348 | continue; | |
349 | strcpy(p,di->d_name); | |
350 | send_file_name(f,flist,1,fname); | |
351 | } | |
352 | ||
353 | closedir(d); | |
354 | } | |
355 | ||
356 | ||
357 | ||
358 | struct file_list *send_file_list(int f,int recurse,int argc,char *argv[]) | |
359 | { | |
360 | int i,l; | |
361 | struct stat st; | |
362 | char *p,*dir; | |
363 | char dbuf[MAXPATHLEN]; | |
364 | struct file_list *flist; | |
365 | ||
366 | if (verbose && recurse) { | |
367 | fprintf(am_server?stderr:stdout,"building file list ... "); | |
368 | fflush(am_server?stderr:stdout); | |
369 | } | |
370 | ||
371 | flist = (struct file_list *)malloc(sizeof(flist[0])); | |
372 | if (!flist) out_of_memory("send_file_list"); | |
373 | ||
374 | flist->count=0; | |
375 | flist->malloced = 100; | |
376 | flist->files = (struct file_struct *)malloc(sizeof(flist->files[0])* | |
377 | flist->malloced); | |
378 | if (!flist->files) out_of_memory("send_file_list"); | |
379 | ||
380 | for (i=0;i<argc;i++) { | |
381 | char fname2[MAXPATHLEN]; | |
382 | char *fname = fname2; | |
383 | ||
384 | strcpy(fname,argv[i]); | |
385 | ||
386 | l = strlen(fname); | |
387 | if (l != 1 && fname[l-1] == '/') { | |
388 | strcat(fname,"."); | |
389 | } | |
390 | ||
391 | if (lstat(fname,&st) != 0) { | |
392 | fprintf(stderr,"%s : %s\n",fname,strerror(errno)); | |
393 | continue; | |
394 | } | |
395 | ||
396 | if (S_ISDIR(st.st_mode) && !recurse) { | |
397 | fprintf(stderr,"skipping directory %s\n",fname); | |
398 | continue; | |
399 | } | |
400 | ||
401 | dir = NULL; | |
402 | p = strrchr(fname,'/'); | |
403 | if (p) { | |
404 | *p = 0; | |
405 | dir = fname; | |
406 | fname = p+1; | |
407 | } | |
408 | if (!*fname) | |
409 | fname = "."; | |
410 | ||
411 | if (dir && *dir) { | |
412 | if (getcwd(dbuf,MAXPATHLEN-1) == NULL) { | |
413 | fprintf(stderr,"getwd : %s\n",strerror(errno)); | |
34ccb63e | 414 | exit_cleanup(1); |
c627d613 AT |
415 | } |
416 | if (chdir(dir) != 0) { | |
417 | fprintf(stderr,"chdir %s : %s\n",dir,strerror(errno)); | |
418 | continue; | |
419 | } | |
420 | flist_dir = dir; | |
421 | if (one_file_system) | |
422 | set_filesystem(fname); | |
423 | send_file_name(f,flist,recurse,fname); | |
424 | flist_dir = NULL; | |
425 | if (chdir(dbuf) != 0) { | |
426 | fprintf(stderr,"chdir %s : %s\n",dbuf,strerror(errno)); | |
34ccb63e | 427 | exit_cleanup(1); |
c627d613 AT |
428 | } |
429 | continue; | |
430 | } | |
431 | ||
432 | if (one_file_system) | |
433 | set_filesystem(fname); | |
434 | send_file_name(f,flist,recurse,fname); | |
435 | } | |
436 | ||
437 | if (f != -1) { | |
182dca5c | 438 | send_file_entry(NULL,f); |
c627d613 AT |
439 | write_flush(f); |
440 | } | |
441 | ||
442 | clean_flist(flist); | |
443 | ||
444 | if (verbose && recurse) | |
445 | fprintf(am_server?stderr:stdout,"done\n"); | |
446 | ||
447 | return flist; | |
448 | } | |
449 | ||
450 | ||
451 | struct file_list *recv_file_list(int f) | |
452 | { | |
c627d613 | 453 | struct file_list *flist; |
182dca5c | 454 | unsigned char flags; |
c627d613 AT |
455 | |
456 | if (verbose > 2) | |
457 | fprintf(stderr,"recv_file_list starting\n"); | |
458 | ||
459 | flist = (struct file_list *)malloc(sizeof(flist[0])); | |
460 | if (!flist) | |
461 | goto oom; | |
462 | ||
463 | flist->count=0; | |
464 | flist->malloced=100; | |
465 | flist->files = (struct file_struct *)malloc(sizeof(flist->files[0])* | |
466 | flist->malloced); | |
467 | if (!flist->files) | |
468 | goto oom; | |
469 | ||
470 | ||
182dca5c | 471 | for (flags=read_byte(f); flags; flags=read_byte(f)) { |
c627d613 AT |
472 | int i = flist->count; |
473 | ||
474 | if (i >= flist->malloced) { | |
475 | flist->malloced += 100; | |
476 | flist->files =(struct file_struct *)realloc(flist->files, | |
477 | sizeof(flist->files[0])* | |
478 | flist->malloced); | |
479 | if (!flist->files) | |
480 | goto oom; | |
481 | } | |
482 | ||
182dca5c | 483 | receive_file_entry(&flist->files[i],flags,f); |
c627d613 AT |
484 | |
485 | if (S_ISREG(flist->files[i].mode)) | |
486 | total_size += flist->files[i].length; | |
487 | ||
488 | flist->count++; | |
489 | ||
490 | if (verbose > 2) | |
491 | fprintf(stderr,"recv_file_name(%s)\n",flist->files[i].name); | |
492 | } | |
493 | ||
494 | ||
495 | if (verbose > 2) | |
496 | fprintf(stderr,"received %d names\n",flist->count); | |
497 | ||
498 | clean_flist(flist); | |
499 | ||
500 | return flist; | |
501 | ||
502 | oom: | |
503 | out_of_memory("recv_file_list"); | |
504 | return NULL; /* not reached */ | |
505 | } | |
506 | ||
507 | ||
508 | static int flist_compare(struct file_struct *f1,struct file_struct *f2) | |
509 | { | |
510 | if (!f1->name && !f2->name) return 0; | |
511 | if (!f1->name) return -1; | |
512 | if (!f2->name) return 1; | |
513 | return strcmp(f1->name,f2->name); | |
514 | } | |
515 | ||
516 | ||
517 | int flist_find(struct file_list *flist,struct file_struct *f) | |
518 | { | |
519 | int low=0,high=flist->count; | |
520 | ||
521 | while (low != high) { | |
522 | int mid = (low+high)/2; | |
523 | int ret = flist_compare(&flist->files[mid],f); | |
524 | if (ret == 0) return mid; | |
525 | if (ret > 0) | |
526 | high=mid; | |
527 | else | |
528 | low=mid+1; | |
529 | } | |
530 | if (flist_compare(&flist->files[low],f) == 0) | |
531 | return low; | |
532 | return -1; | |
533 | } | |
534 | ||
535 | ||
536 | static void clean_fname(char *name) | |
537 | { | |
538 | char *p; | |
539 | int l; | |
540 | int modified = 1; | |
541 | ||
542 | if (!name) return; | |
543 | ||
544 | while (modified) { | |
545 | modified = 0; | |
546 | ||
547 | if ((p=strstr(name,"/./"))) { | |
548 | modified = 1; | |
549 | while (*p) { | |
550 | p[0] = p[2]; | |
551 | p++; | |
552 | } | |
553 | } | |
554 | ||
555 | if ((p=strstr(name,"//"))) { | |
556 | modified = 1; | |
557 | while (*p) { | |
558 | p[0] = p[1]; | |
559 | p++; | |
560 | } | |
561 | } | |
562 | ||
563 | if (strncmp(p=name,"./",2) == 0) { | |
564 | modified = 1; | |
565 | while (*p) { | |
566 | p[0] = p[2]; | |
567 | p++; | |
568 | } | |
569 | } | |
570 | ||
571 | l = strlen(p=name); | |
572 | if (l > 1 && p[l-1] == '/') { | |
573 | modified = 1; | |
574 | p[l-1] = 0; | |
575 | } | |
576 | } | |
577 | } | |
578 | ||
579 | ||
580 | /* | |
581 | * This routine ensures we don't have any duplicate names in our file list. | |
582 | * duplicate names can cause corruption because of the pipelining | |
583 | */ | |
584 | void clean_flist(struct file_list *flist) | |
585 | { | |
586 | int i; | |
587 | ||
588 | if (!flist || flist->count == 0) | |
589 | return; | |
590 | ||
591 | for (i=0;i<flist->count;i++) { | |
592 | clean_fname(flist->files[i].name); | |
593 | } | |
594 | ||
595 | qsort(flist->files,flist->count, | |
596 | sizeof(flist->files[0]), | |
597 | (int (*)())flist_compare); | |
598 | ||
599 | for (i=1;i<flist->count;i++) { | |
600 | if (flist->files[i].name && | |
601 | strcmp(flist->files[i].name,flist->files[i-1].name) == 0) { | |
602 | if (verbose > 1 && !am_server) | |
603 | fprintf(stderr,"removing duplicate name %s from file list\n", | |
604 | flist->files[i].name); | |
605 | free(flist->files[i-1].name); | |
606 | flist->files[i-1].name = NULL; | |
607 | } | |
608 | } | |
609 | } | |
610 |