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