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