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