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