if the file changes during transfer then don't loop forever trying to
[rsync/rsync.git] / flist.c
CommitLineData
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
24extern int csum_length;
25
c627d613
AT
26extern int verbose;
27extern int am_server;
28extern int always_checksum;
29extern off_t total_size;
30
31extern int cvs_exclude;
32
a06d19e3
AT
33extern int recurse;
34
c627d613
AT
35extern int one_file_system;
36extern int make_backups;
37extern int preserve_links;
dc5ddbcc 38extern int preserve_hard_links;
c627d613
AT
39extern int preserve_perms;
40extern int preserve_devices;
41extern int preserve_uid;
42extern int preserve_gid;
43extern int preserve_times;
6574b4f7 44extern int relative_paths;
82306bf6 45extern int copy_links;
f6c34742 46extern int remote_version;
c627d613
AT
47
48static char **local_exclude_list = NULL;
49
82306bf6
AT
50int link_stat(const char *Path, struct stat *Buffer)
51{
52#if SUPPORT_LINKS
53 if (copy_links) {
54 return stat(Path, Buffer);
55 } else {
56 return lstat(Path, Buffer);
57 }
58#else
59 return stat(Path, Buffer);
60#endif
61}
62
c627d613
AT
63/*
64 This function is used to check if a file should be included/excluded
65 from the list of files based on its name and type etc
66 */
67static int match_file_name(char *fname,struct stat *st)
68{
69 if (check_exclude(fname,local_exclude_list)) {
70 if (verbose > 2)
dc5ddbcc 71 fprintf(FERROR,"excluding file %s\n",fname);
c627d613
AT
72 return 0;
73 }
74 return 1;
75}
76
77/* used by the one_file_system code */
78static dev_t filesystem_dev;
79
80static void set_filesystem(char *fname)
81{
82 struct stat st;
82306bf6 83 if (link_stat(fname,&st) != 0) return;
c627d613
AT
84 filesystem_dev = st.st_dev;
85}
86
87
88static void send_directory(int f,struct file_list *flist,char *dir);
89
90static char *flist_dir = NULL;
91
3ec4dd97
AT
92static void clean_fname(char *name)
93{
94 char *p;
95 int l;
96 int modified = 1;
97
98 if (!name) return;
99
100 while (modified) {
101 modified = 0;
102
103 if ((p=strstr(name,"/./"))) {
104 modified = 1;
105 while (*p) {
106 p[0] = p[2];
107 p++;
108 }
109 }
110
111 if ((p=strstr(name,"//"))) {
112 modified = 1;
113 while (*p) {
114 p[0] = p[1];
115 p++;
116 }
117 }
4fe159a8 118
3ec4dd97
AT
119 if (strncmp(p=name,"./",2) == 0) {
120 modified = 1;
121 do {
122 p[0] = p[2];
123 } while (*p++);
124 }
4fe159a8 125
3ec4dd97
AT
126 l = strlen(p=name);
127 if (l > 1 && p[l-1] == '/') {
128 modified = 1;
129 p[l-1] = 0;
130 }
131 }
132}
133
134
135
136void send_file_entry(struct file_struct *file,int f)
c627d613 137{
182dca5c 138 unsigned char flags;
4fe159a8 139 static time_t last_time=0;
182dca5c 140 static mode_t last_mode=0;
dc5ddbcc 141 static dev_t last_rdev=0;
182dca5c
AT
142 static uid_t last_uid=0;
143 static gid_t last_gid=0;
4fe159a8 144 static char lastname[MAXPATHLEN]="";
3ec4dd97 145 char *fname;
4fe159a8 146 int l1,l2;
182dca5c 147
c627d613
AT
148 if (f == -1) return;
149
182dca5c
AT
150 if (!file) {
151 write_byte(f,0);
152 return;
153 }
154
3ec4dd97
AT
155 fname = f_name(file);
156
182dca5c
AT
157 flags = FILE_VALID;
158
159 if (file->mode == last_mode) flags |= SAME_MODE;
dc5ddbcc 160 if (file->rdev == last_rdev) flags |= SAME_RDEV;
182dca5c
AT
161 if (file->uid == last_uid) flags |= SAME_UID;
162 if (file->gid == last_gid) flags |= SAME_GID;
4fe159a8 163 if (file->modtime == last_time) flags |= SAME_TIME;
182dca5c 164
3ec4dd97
AT
165 for (l1=0;lastname[l1] && fname[l1] == lastname[l1];l1++) ;
166 l2 = strlen(fname) - l1;
4fe159a8
AT
167
168 if (l1 > 0) flags |= SAME_NAME;
169 if (l2 > 255) flags |= LONG_NAME;
d89322c4 170
4fe159a8
AT
171 write_byte(f,flags);
172 if (flags & SAME_NAME)
173 write_byte(f,l1);
174 if (flags & LONG_NAME)
175 write_int(f,l2);
9f3541e6 176 else
4fe159a8 177 write_byte(f,l2);
3ec4dd97 178 write_buf(f,fname+l1,l2);
4fe159a8 179
c627d613 180 write_int(f,(int)file->length);
4fe159a8
AT
181 if (!(flags & SAME_TIME))
182 write_int(f,(int)file->modtime);
182dca5c
AT
183 if (!(flags & SAME_MODE))
184 write_int(f,(int)file->mode);
f6c34742
AT
185 if (preserve_uid && !(flags & SAME_UID)) {
186 add_uid(file->uid);
187 write_int(f,(int)file->uid);
188 }
189 if (preserve_gid && !(flags & SAME_GID)) {
190 add_gid(file->gid);
191 write_int(f,(int)file->gid);
192 }
dc5ddbcc
AT
193 if (preserve_devices && IS_DEVICE(file->mode) && !(flags & SAME_RDEV))
194 write_int(f,(int)file->rdev);
c627d613
AT
195
196#if SUPPORT_LINKS
197 if (preserve_links && S_ISLNK(file->mode)) {
198 write_int(f,strlen(file->link));
199 write_buf(f,file->link,strlen(file->link));
200 }
201#endif
202
dc5ddbcc
AT
203#if SUPPORT_HARD_LINKS
204 if (preserve_hard_links && S_ISREG(file->mode)) {
205 write_int(f,file->dev);
206 write_int(f,file->inode);
207 }
208#endif
209
c627d613 210 if (always_checksum) {
43a481dc 211 write_buf(f,file->sum,csum_length);
c627d613 212 }
182dca5c
AT
213
214 last_mode = file->mode;
dc5ddbcc 215 last_rdev = file->rdev;
182dca5c
AT
216 last_uid = file->uid;
217 last_gid = file->gid;
4fe159a8
AT
218 last_time = file->modtime;
219
3ec4dd97 220 strncpy(lastname,fname,MAXPATHLEN-1);
13a1f792 221 lastname[MAXPATHLEN-1] = 0;
182dca5c
AT
222}
223
224
225
3ec4dd97
AT
226void receive_file_entry(struct file_struct **fptr,
227 unsigned char flags,int f)
182dca5c 228{
d89322c4 229 static time_t last_time=0;
182dca5c 230 static mode_t last_mode=0;
dc5ddbcc 231 static dev_t last_rdev=0;
182dca5c
AT
232 static uid_t last_uid=0;
233 static gid_t last_gid=0;
3ec4dd97
AT
234 static char lastname[MAXPATHLEN];
235 char thisname[MAXPATHLEN];
4fe159a8 236 int l1=0,l2=0;
3ec4dd97
AT
237 char *p;
238 struct file_struct *file;
182dca5c 239
4fe159a8 240 if (flags & SAME_NAME)
9f3541e6 241 l1 = read_byte(f);
4fe159a8
AT
242
243 if (flags & LONG_NAME)
244 l2 = read_int(f);
245 else
246 l2 = read_byte(f);
182dca5c 247
3ec4dd97
AT
248 file = (struct file_struct *)malloc(sizeof(*file));
249 if (!file) out_of_memory("receive_file_entry");
dc5ddbcc 250 bzero((char *)file,sizeof(*file));
3ec4dd97
AT
251 (*fptr) = file;
252
253 strncpy(thisname,lastname,l1);
254 read_buf(f,&thisname[l1],l2);
255 thisname[l1+l2] = 0;
256
257 strncpy(lastname,thisname,MAXPATHLEN-1);
258 lastname[MAXPATHLEN-1] = 0;
259
260 clean_fname(thisname);
261
262 if ((p = strrchr(thisname,'/'))) {
263 static char *lastdir;
264 *p = 0;
265 if (lastdir && strcmp(thisname, lastdir)==0) {
266 file->dirname = lastdir;
267 } else {
268 file->dirname = strdup(thisname);
269 lastdir = file->dirname;
270 }
271 file->basename = strdup(p+1);
272 } else {
273 file->dirname = NULL;
274 file->basename = strdup(thisname);
275 }
dc5ddbcc 276
3ec4dd97 277 if (!file->basename) out_of_memory("receive_file_entry 1");
182dca5c 278
182dca5c 279
182dca5c 280 file->length = (off_t)read_int(f);
4fe159a8 281 file->modtime = (flags & SAME_TIME) ? last_time : (time_t)read_int(f);
182dca5c
AT
282 file->mode = (flags & SAME_MODE) ? last_mode : (mode_t)read_int(f);
283 if (preserve_uid)
284 file->uid = (flags & SAME_UID) ? last_uid : (uid_t)read_int(f);
285 if (preserve_gid)
286 file->gid = (flags & SAME_GID) ? last_gid : (gid_t)read_int(f);
287 if (preserve_devices && IS_DEVICE(file->mode))
dc5ddbcc 288 file->rdev = (flags & SAME_RDEV) ? last_rdev : (dev_t)read_int(f);
182dca5c 289
182dca5c
AT
290 if (preserve_links && S_ISLNK(file->mode)) {
291 int l = read_int(f);
292 file->link = (char *)malloc(l+1);
6dd1782c 293 if (!file->link) out_of_memory("receive_file_entry 2");
182dca5c
AT
294 read_buf(f,file->link,l);
295 file->link[l] = 0;
296 }
dc5ddbcc
AT
297
298#if SUPPORT_HARD_LINKS
299 if (preserve_hard_links && S_ISREG(file->mode)) {
300 file->dev = read_int(f);
301 file->inode = read_int(f);
302 }
303#endif
182dca5c
AT
304
305 if (always_checksum)
43a481dc 306 read_buf(f,file->sum,csum_length);
182dca5c
AT
307
308 last_mode = file->mode;
dc5ddbcc 309 last_rdev = file->rdev;
182dca5c
AT
310 last_uid = file->uid;
311 last_gid = file->gid;
4fe159a8 312 last_time = file->modtime;
c627d613
AT
313}
314
315
4fe159a8 316
a06d19e3 317static struct file_struct *make_file(char *fname)
c627d613 318{
3ec4dd97
AT
319 struct file_struct *file;
320 struct stat st;
321 char sum[SUM_LENGTH];
322 char *p;
323 char cleaned_name[MAXPATHLEN];
324
325 strncpy(cleaned_name, fname, MAXPATHLEN-1);
326 cleaned_name[MAXPATHLEN-1] = 0;
327 clean_fname(cleaned_name);
328 fname = cleaned_name;
329
330 bzero(sum,SUM_LENGTH);
331
332 if (link_stat(fname,&st) != 0) {
333 fprintf(FERROR,"%s: %s\n",
334 fname,strerror(errno));
335 return NULL;
336 }
c627d613 337
3ec4dd97
AT
338 if (S_ISDIR(st.st_mode) && !recurse) {
339 fprintf(FERROR,"skipping directory %s\n",fname);
340 return NULL;
341 }
342
343 if (one_file_system && st.st_dev != filesystem_dev)
344 return NULL;
345
346 if (!match_file_name(fname,&st))
347 return NULL;
348
349 if (verbose > 2)
350 fprintf(FERROR,"make_file(%s)\n",fname);
351
352 file = (struct file_struct *)malloc(sizeof(*file));
353 if (!file) out_of_memory("make_file");
354 bzero((char *)file,sizeof(*file));
355
356 if ((p = strrchr(fname,'/'))) {
357 static char *lastdir;
358 *p = 0;
359 if (lastdir && strcmp(fname, lastdir)==0) {
360 file->dirname = lastdir;
361 } else {
362 file->dirname = strdup(fname);
363 lastdir = file->dirname;
364 }
365 file->basename = strdup(p+1);
366 *p = '/';
367 } else {
368 file->dirname = NULL;
369 file->basename = strdup(fname);
370 }
c627d613 371
3ec4dd97
AT
372 file->modtime = st.st_mtime;
373 file->length = st.st_size;
374 file->mode = st.st_mode;
375 file->uid = st.st_uid;
376 file->gid = st.st_gid;
377 file->dev = st.st_dev;
378 file->inode = st.st_ino;
c627d613 379#ifdef HAVE_ST_RDEV
3ec4dd97 380 file->rdev = st.st_rdev;
c627d613
AT
381#endif
382
383#if SUPPORT_LINKS
3ec4dd97
AT
384 if (S_ISLNK(st.st_mode)) {
385 int l;
386 char lnk[MAXPATHLEN];
387 if ((l=readlink(fname,lnk,MAXPATHLEN-1)) == -1) {
388 fprintf(FERROR,"readlink %s : %s\n",
389 fname,strerror(errno));
390 return NULL;
391 }
392 lnk[l] = 0;
393 file->link = strdup(lnk);
394 }
c627d613
AT
395#endif
396
3ec4dd97
AT
397 if (always_checksum && S_ISREG(st.st_mode)) {
398 file_checksum(fname,file->sum,st.st_size);
399 }
c627d613 400
3ec4dd97
AT
401 if (flist_dir) {
402 static char *lastdir;
403 if (lastdir && strcmp(lastdir, flist_dir)==0) {
404 file->basedir = lastdir;
405 } else {
406 file->basedir = strdup(flist_dir);
407 lastdir = file->basedir;
408 }
409 } else {
410 file->basedir = NULL;
411 }
c627d613 412
3ec4dd97
AT
413 if (!S_ISDIR(st.st_mode))
414 total_size += st.st_size;
c627d613 415
3ec4dd97 416 return file;
c627d613
AT
417}
418
419
420
a06d19e3 421static void send_file_name(int f,struct file_list *flist,char *fname)
c627d613
AT
422{
423 struct file_struct *file;
424
a06d19e3 425 file = make_file(fname);
c627d613
AT
426
427 if (!file) return;
428
429 if (flist->count >= flist->malloced) {
3ec4dd97
AT
430 if (flist->malloced < 1000)
431 flist->malloced += 1000;
f9c51620 432 else
3ec4dd97
AT
433 flist->malloced *= 2;
434 flist->files = (struct file_struct **)realloc(flist->files,
435 sizeof(flist->files[0])*
436 flist->malloced);
f9c51620
AT
437 if (!flist->files)
438 out_of_memory("send_file_name");
c627d613
AT
439 }
440
3ec4dd97
AT
441 if (strcmp(file->basename,"")) {
442 flist->files[flist->count++] = file;
c627d613
AT
443 send_file_entry(file,f);
444 }
445
446 if (S_ISDIR(file->mode) && recurse) {
447 char **last_exclude_list = local_exclude_list;
3ec4dd97 448 send_directory(f,flist,f_name(file));
c627d613
AT
449 local_exclude_list = last_exclude_list;
450 return;
451 }
452}
453
454
455
456static void send_directory(int f,struct file_list *flist,char *dir)
457{
3ec4dd97
AT
458 DIR *d;
459 struct dirent *di;
460 char fname[MAXPATHLEN];
461 int l;
462 char *p;
463
464 d = opendir(dir);
465 if (!d) {
466 fprintf(FERROR,"%s: %s\n",
467 dir,strerror(errno));
468 return;
469 }
c627d613 470
3ec4dd97
AT
471 strncpy(fname,dir,MAXPATHLEN-1);
472 fname[MAXPATHLEN-1]=0;
473 l = strlen(fname);
474 if (fname[l-1] != '/') {
475 if (l == MAXPATHLEN-1) {
476 fprintf(FERROR,"skipping long-named directory %s\n",fname);
477 closedir(d);
478 return;
479 }
480 strcat(fname,"/");
481 l++;
482 }
483 p = fname + strlen(fname);
c627d613 484
3ec4dd97
AT
485 if (cvs_exclude) {
486 if (strlen(fname) + strlen(".cvsignore") <= MAXPATHLEN-1) {
487 strcpy(p,".cvsignore");
488 local_exclude_list = make_exclude_list(fname,NULL,0);
489 } else {
490 fprintf(FERROR,"cannot cvs-exclude in long-named directory %s\n",fname);
491 }
492 }
493
494 for (di=readdir(d); di; di=readdir(d)) {
495 if (strcmp(di->d_name,".")==0 ||
496 strcmp(di->d_name,"..")==0)
497 continue;
498 strncpy(p,di->d_name,MAXPATHLEN-(l+1));
499 send_file_name(f,flist,fname);
500 }
c627d613 501
3ec4dd97 502 closedir(d);
c627d613
AT
503}
504
505
506
a06d19e3 507struct file_list *send_file_list(int f,int argc,char *argv[])
c627d613
AT
508{
509 int i,l;
510 struct stat st;
511 char *p,*dir;
512 char dbuf[MAXPATHLEN];
513 struct file_list *flist;
514
95a38e86 515 if (verbose && recurse && !am_server && f != -1) {
dc5ddbcc
AT
516 fprintf(FINFO,"building file list ... ");
517 fflush(FINFO);
c627d613
AT
518 }
519
520 flist = (struct file_list *)malloc(sizeof(flist[0]));
521 if (!flist) out_of_memory("send_file_list");
522
523 flist->count=0;
3ec4dd97
AT
524 flist->malloced = 1000;
525 flist->files = (struct file_struct **)malloc(sizeof(flist->files[0])*
526 flist->malloced);
c627d613
AT
527 if (!flist->files) out_of_memory("send_file_list");
528
529 for (i=0;i<argc;i++) {
530 char fname2[MAXPATHLEN];
531 char *fname = fname2;
532
13a1f792
AT
533 strncpy(fname,argv[i],MAXPATHLEN-1);
534 fname[MAXPATHLEN-1] = 0;
c627d613
AT
535
536 l = strlen(fname);
537 if (l != 1 && fname[l-1] == '/') {
538 strcat(fname,".");
539 }
540
82306bf6 541 if (link_stat(fname,&st) != 0) {
dc5ddbcc 542 fprintf(FERROR,"%s : %s\n",fname,strerror(errno));
c627d613
AT
543 continue;
544 }
545
546 if (S_ISDIR(st.st_mode) && !recurse) {
dc5ddbcc 547 fprintf(FERROR,"skipping directory %s\n",fname);
c627d613
AT
548 continue;
549 }
550
551 dir = NULL;
6574b4f7
AT
552
553 if (!relative_paths) {
554 p = strrchr(fname,'/');
555 if (p) {
556 *p = 0;
557 if (p == fname)
558 dir = "/";
559 else
560 dir = fname;
561 fname = p+1;
562 }
c627d613 563 }
6574b4f7 564
c627d613
AT
565 if (!*fname)
566 fname = ".";
567
568 if (dir && *dir) {
569 if (getcwd(dbuf,MAXPATHLEN-1) == NULL) {
dc5ddbcc 570 fprintf(FERROR,"getwd : %s\n",strerror(errno));
34ccb63e 571 exit_cleanup(1);
c627d613
AT
572 }
573 if (chdir(dir) != 0) {
dc5ddbcc 574 fprintf(FERROR,"chdir %s : %s\n",dir,strerror(errno));
c627d613
AT
575 continue;
576 }
577 flist_dir = dir;
578 if (one_file_system)
579 set_filesystem(fname);
a06d19e3 580 send_file_name(f,flist,fname);
c627d613
AT
581 flist_dir = NULL;
582 if (chdir(dbuf) != 0) {
dc5ddbcc 583 fprintf(FERROR,"chdir %s : %s\n",dbuf,strerror(errno));
34ccb63e 584 exit_cleanup(1);
c627d613
AT
585 }
586 continue;
587 }
588
589 if (one_file_system)
590 set_filesystem(fname);
a06d19e3 591 send_file_name(f,flist,fname);
c627d613
AT
592 }
593
594 if (f != -1) {
182dca5c 595 send_file_entry(NULL,f);
c627d613
AT
596 write_flush(f);
597 }
598
95a38e86 599 if (verbose && recurse && !am_server && f != -1)
dc5ddbcc
AT
600 fprintf(FINFO,"done\n");
601
602 clean_flist(flist);
c627d613 603
f6c34742
AT
604 /* now send the uid/gid list. This was introduced in protocol version 15 */
605 if (f != -1 && remote_version >= 15) {
606 send_uid_list(f);
607 }
608
c627d613
AT
609 return flist;
610}
611
612
613struct file_list *recv_file_list(int f)
614{
c627d613 615 struct file_list *flist;
182dca5c 616 unsigned char flags;
c627d613 617
a06d19e3
AT
618 if (verbose && recurse && !am_server) {
619 fprintf(FINFO,"receiving file list ... ");
620 fflush(FINFO);
621 }
c627d613
AT
622
623 flist = (struct file_list *)malloc(sizeof(flist[0]));
624 if (!flist)
625 goto oom;
626
627 flist->count=0;
3ec4dd97
AT
628 flist->malloced=1000;
629 flist->files = (struct file_struct **)malloc(sizeof(flist->files[0])*
630 flist->malloced);
c627d613
AT
631 if (!flist->files)
632 goto oom;
633
634
182dca5c 635 for (flags=read_byte(f); flags; flags=read_byte(f)) {
c627d613
AT
636 int i = flist->count;
637
638 if (i >= flist->malloced) {
3ec4dd97
AT
639 if (flist->malloced < 1000)
640 flist->malloced += 1000;
f9c51620 641 else
3ec4dd97
AT
642 flist->malloced *= 2;
643 flist->files =(struct file_struct **)realloc(flist->files,
644 sizeof(flist->files[0])*
645 flist->malloced);
f9c51620
AT
646 if (!flist->files)
647 goto oom;
c627d613
AT
648 }
649
182dca5c 650 receive_file_entry(&flist->files[i],flags,f);
c627d613 651
3ec4dd97
AT
652 if (S_ISREG(flist->files[i]->mode))
653 total_size += flist->files[i]->length;
c627d613
AT
654
655 flist->count++;
656
657 if (verbose > 2)
3ec4dd97 658 fprintf(FERROR,"recv_file_name(%s)\n",f_name(flist->files[i]));
c627d613
AT
659 }
660
661
662 if (verbose > 2)
dc5ddbcc 663 fprintf(FERROR,"received %d names\n",flist->count);
c627d613
AT
664
665 clean_flist(flist);
666
a06d19e3
AT
667 if (verbose && recurse && !am_server) {
668 fprintf(FINFO,"done\n");
669 }
670
f6c34742
AT
671 /* now recv the uid/gid list. This was introduced in protocol version 15 */
672 if (f != -1 && remote_version >= 15) {
673 recv_uid_list(f, flist);
674 }
675
c627d613
AT
676 return flist;
677
678oom:
679 out_of_memory("recv_file_list");
680 return NULL; /* not reached */
681}
682
683
3ec4dd97 684int file_compare(struct file_struct **f1,struct file_struct **f2)
c627d613 685{
3ec4dd97
AT
686 if (!(*f1)->basename && !(*f2)->basename) return 0;
687 if (!(*f1)->basename) return -1;
688 if (!(*f2)->basename) return 1;
689 if ((*f1)->dirname == (*f2)->dirname)
690 return strcmp((*f1)->basename, (*f2)->basename);
691 return strcmp(f_name(*f1),f_name(*f2));
c627d613
AT
692}
693
694
695int flist_find(struct file_list *flist,struct file_struct *f)
696{
d966ee25
AT
697 int low=0,high=flist->count-1;
698
699 if (flist->count <= 0) return -1;
700
701 while (low != high) {
702 int mid = (low+high)/2;
3ec4dd97 703 int ret = file_compare(&flist->files[flist_up(flist, mid)],&f);
d966ee25
AT
704 if (ret == 0) return flist_up(flist, mid);
705 if (ret > 0) {
706 high=mid;
707 } else {
708 low=mid+1;
709 }
710 }
711
3ec4dd97 712 if (file_compare(&flist->files[flist_up(flist,low)],&f) == 0)
d966ee25
AT
713 return flist_up(flist,low);
714 return -1;
c627d613
AT
715}
716
717
3ec4dd97
AT
718/*
719 * free up one file
720 */
721static void free_file(struct file_struct *file)
c627d613 722{
3ec4dd97
AT
723 if (!file) return;
724 if (file->basename) free(file->basename);
725 if (file->link) free(file->link);
726 bzero((char *)file, sizeof(*file));
727 free(file);
728}
c627d613 729
c627d613 730
3ec4dd97
AT
731/*
732 * free up all elements in a flist
733 */
734void flist_free(struct file_list *flist)
735{
736 int i;
737 for (i=1;i<flist->count;i++) {
738 free_file(flist->files[i]);
739 }
740 bzero((char *)flist->files, sizeof(flist->files[0])*flist->count);
741 free(flist->files);
742 bzero((char *)flist, sizeof(*flist));
743 free(flist);
c627d613
AT
744}
745
746
747/*
748 * This routine ensures we don't have any duplicate names in our file list.
749 * duplicate names can cause corruption because of the pipelining
750 */
751void clean_flist(struct file_list *flist)
752{
3ec4dd97 753 int i;
c627d613 754
3ec4dd97
AT
755 if (!flist || flist->count == 0)
756 return;
c627d613 757
3ec4dd97
AT
758 qsort(flist->files,flist->count,
759 sizeof(flist->files[0]),
760 (int (*)())file_compare);
761
762 for (i=1;i<flist->count;i++) {
763 if (flist->files[i]->basename &&
764 strcmp(f_name(flist->files[i]),
765 f_name(flist->files[i-1])) == 0) {
766 if (verbose > 1 && !am_server)
767 fprintf(FERROR,"removing duplicate name %s from file list %d\n",
768 f_name(flist->files[i-1]),i-1);
769 free_file(flist->files[i]);
770 flist->files[i] = NULL;
771 }
772 }
773}
774
775
776/*
777 * return the full filename of a flist entry
778 */
779char *f_name(struct file_struct *f)
780{
781 static char names[10][MAXPATHLEN];
782 static int n;
783 char *p = names[n];
784
785 if (!f) return NULL;
786
787 n = (n+1)%10;
788
789 if (f->dirname) {
790 sprintf(p, "%s/%s", f->dirname, f->basename);
791 } else {
792 strcpy(p, f->basename);
793 }
794
795 return p;
c627d613
AT
796}
797