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