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