1740209916fed5ae252b62af09c5bcd524c0a3ae
[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) {
303                 rprintf(FERROR,"overflow: flags=0x%x l1=%d l2=%d lastname=%s\n",
304                         flags, l1, l2, lastname);
305                 overflow("receive_file_entry");
306         }
307
308         strlcpy(thisname,lastname,l1+1);
309         read_sbuf(f,&thisname[l1],l2);
310         thisname[l1+l2] = 0;
311
312         strlcpy(lastname,thisname,MAXPATHLEN);
313         lastname[MAXPATHLEN-1] = 0;
314
315         clean_fname(thisname);
316
317         if (sanitize_paths) {
318                 sanitize_path(thisname, NULL);
319         }
320
321         if ((p = strrchr(thisname,'/'))) {
322                 static char *lastdir;
323                 *p = 0;
324                 if (lastdir && strcmp(thisname, lastdir)==0) {
325                         file->dirname = lastdir;
326                 } else {
327                         file->dirname = strdup(thisname);
328                         lastdir = file->dirname;
329                 }
330                 file->basename = strdup(p+1);
331         } else {
332                 file->dirname = NULL;
333                 file->basename = strdup(thisname);
334         }
335
336         if (!file->basename) out_of_memory("receive_file_entry 1");
337
338
339         file->flags = flags;
340         file->length = read_longint(f);
341         file->modtime = (flags & SAME_TIME) ? last_time : (time_t)read_int(f);
342         file->mode = (flags & SAME_MODE) ? last_mode : from_wire_mode(read_int(f));
343         if (preserve_uid)
344                 file->uid = (flags & SAME_UID) ? last_uid : (uid_t)read_int(f);
345         if (preserve_gid)
346                 file->gid = (flags & SAME_GID) ? last_gid : (gid_t)read_int(f);
347         if (preserve_devices && IS_DEVICE(file->mode))
348                 file->rdev = (flags & SAME_RDEV) ? last_rdev : (dev_t)read_int(f);
349
350         if (preserve_links && S_ISLNK(file->mode)) {
351                 int l = read_int(f);
352                 file->link = (char *)malloc(l+1);
353                 if (!file->link) out_of_memory("receive_file_entry 2");
354                 read_sbuf(f,file->link,l);
355                 if (sanitize_paths) {
356                         sanitize_path(file->link, file->dirname);
357                 }
358         }
359
360 #if SUPPORT_HARD_LINKS
361         if (preserve_hard_links && S_ISREG(file->mode)) {
362                 file->dev = read_int(f);
363                 file->inode = read_int(f);
364         }
365 #endif
366   
367         if (always_checksum) {
368                 file->sum = (char *)malloc(MD4_SUM_LENGTH);
369                 if (!file->sum) out_of_memory("md4 sum");
370                 if (remote_version < 21) {
371                         read_buf(f,file->sum,2);
372                 } else {
373                         read_buf(f,file->sum,MD4_SUM_LENGTH);
374                 }
375         }
376   
377         last_mode = file->mode;
378         last_rdev = file->rdev;
379         last_uid = file->uid;
380         last_gid = file->gid;
381         last_time = file->modtime;
382
383         if (!preserve_perms) {
384                 extern int orig_umask;
385                 /* set an appropriate set of permissions based on original
386                    permissions and umask. This emulates what GNU cp does */
387                 file->mode &= ~orig_umask;
388         }
389 }
390
391
392 /* determine if a file in a different filesstem should be skipped
393    when one_file_system is set. We bascally only want to include
394    the mount points - but they can be hard to find! */
395 static int skip_filesystem(char *fname, STRUCT_STAT *st)
396 {
397         STRUCT_STAT st2;
398         char *p = strrchr(fname, '/');
399
400         /* skip all but directories */
401         if (!S_ISDIR(st->st_mode)) return 1;
402
403         /* if its not a subdirectory then allow */
404         if (!p) return 0;
405
406         *p = 0;
407         if (link_stat(fname, &st2)) {
408                 *p = '/';
409                 return 0;
410         }
411         *p = '/';
412         
413         return (st2.st_dev != filesystem_dev);
414 }
415
416 /* create a file_struct for a named file */
417 struct file_struct *make_file(int f, char *fname)
418 {
419         struct file_struct *file;
420         STRUCT_STAT st;
421         char sum[SUM_LENGTH];
422         char *p;
423         char cleaned_name[MAXPATHLEN];
424         char linkbuf[MAXPATHLEN];
425         extern int delete_excluded;
426
427         strlcpy(cleaned_name, fname, MAXPATHLEN);
428         cleaned_name[MAXPATHLEN-1] = 0;
429         clean_fname(cleaned_name);
430         if (sanitize_paths) {
431                 sanitize_path(cleaned_name, NULL);
432         }
433         fname = cleaned_name;
434
435         memset(sum,0,SUM_LENGTH);
436
437         if (readlink_stat(fname,&st,linkbuf) != 0) {
438                 io_error = 1;
439                 rprintf(FERROR,"readlink %s: %s\n",
440                         fname,strerror(errno));
441                 return NULL;
442         }
443
444         if (S_ISDIR(st.st_mode) && !recurse) {
445                 rprintf(FINFO,"skipping directory %s\n",fname);
446                 return NULL;
447         }
448         
449         if (one_file_system && st.st_dev != filesystem_dev) {
450                 if (skip_filesystem(fname, &st))
451                         return NULL;
452         }
453         
454         /* f is set to -1 when calculating deletion file list */
455         if (((f != -1) || !delete_excluded) && !match_file_name(fname,&st))
456                 return NULL;
457         
458         if (verbose > 2)
459                 rprintf(FINFO,"make_file(%d,%s)\n",f,fname);
460         
461         file = (struct file_struct *)malloc(sizeof(*file));
462         if (!file) out_of_memory("make_file");
463         memset((char *)file,0,sizeof(*file));
464
465         if ((p = strrchr(fname,'/'))) {
466                 static char *lastdir;
467                 *p = 0;
468                 if (lastdir && strcmp(fname, lastdir)==0) {
469                         file->dirname = lastdir;
470                 } else {
471                         file->dirname = strdup(fname);
472                         lastdir = file->dirname;
473                 }
474                 file->basename = strdup(p+1);
475                 *p = '/';
476         } else {
477                 file->dirname = NULL;
478                 file->basename = strdup(fname);
479         }
480
481         file->modtime = st.st_mtime;
482         file->length = st.st_size;
483         file->mode = st.st_mode;
484         file->uid = st.st_uid;
485         file->gid = st.st_gid;
486         file->dev = st.st_dev;
487         file->inode = st.st_ino;
488 #ifdef HAVE_ST_RDEV
489         file->rdev = st.st_rdev;
490 #endif
491
492 #if SUPPORT_LINKS
493         if (S_ISLNK(st.st_mode)) {
494                 file->link = strdup(linkbuf);
495         }
496 #endif
497
498         if (always_checksum) {
499                 file->sum = (char *)malloc(MD4_SUM_LENGTH);
500                 if (!file->sum) out_of_memory("md4 sum");
501                 /* drat. we have to provide a null checksum for non-regular
502                    files in order to be compatible with earlier versions
503                    of rsync */
504                 if (S_ISREG(st.st_mode)) {
505                         file_checksum(fname,file->sum,st.st_size);
506                 } else {
507                         memset(file->sum, 0, MD4_SUM_LENGTH);
508                 }
509         }       
510
511         if (flist_dir) {
512                 static char *lastdir;
513                 if (lastdir && strcmp(lastdir, flist_dir)==0) {
514                         file->basedir = lastdir;
515                 } else {
516                         file->basedir = strdup(flist_dir);
517                         lastdir = file->basedir;
518                 }
519         } else {
520                 file->basedir = NULL;
521         }
522
523         if (!S_ISDIR(st.st_mode))
524                 stats.total_size += st.st_size;
525
526         return file;
527 }
528
529
530
531 void send_file_name(int f,struct file_list *flist,char *fname,
532                            int recursive, unsigned base_flags)
533 {
534   struct file_struct *file;
535
536   file = make_file(f,fname);
537
538   if (!file) return;  
539   
540   if (flist->count >= flist->malloced) {
541           if (flist->malloced < 1000)
542                   flist->malloced += 1000;
543           else
544                   flist->malloced *= 2;
545           flist->files = (struct file_struct **)realloc(flist->files,
546                                                         sizeof(flist->files[0])*
547                                                         flist->malloced);
548           if (!flist->files)
549                   out_of_memory("send_file_name");
550   }
551
552   if (strcmp(file->basename,"")) {
553     flist->files[flist->count++] = file;
554     send_file_entry(file,f,base_flags);
555   }
556
557   if (S_ISDIR(file->mode) && recursive) {
558           struct exclude_struct **last_exclude_list = local_exclude_list;
559           send_directory(f,flist,f_name(file));
560           local_exclude_list = last_exclude_list;
561           return;
562   }
563 }
564
565
566
567 static void send_directory(int f,struct file_list *flist,char *dir)
568 {
569         DIR *d;
570         struct dirent *di;
571         char fname[MAXPATHLEN];
572         int l;
573         char *p;
574
575         d = opendir(dir);
576         if (!d) {
577                 io_error = 1;
578                 rprintf(FERROR,"opendir(%s): %s\n",
579                         dir,strerror(errno));
580                 return;
581         }
582
583         strlcpy(fname,dir,MAXPATHLEN);
584         l = strlen(fname);
585         if (fname[l-1] != '/') {
586                 if (l == MAXPATHLEN-1) {
587                         io_error = 1;
588                         rprintf(FERROR,"skipping long-named directory %s\n",fname);
589                         closedir(d);
590                         return;
591                 }
592                 strlcat(fname,"/", MAXPATHLEN);
593                 l++;
594         }
595         p = fname + strlen(fname);
596
597         local_exclude_list = NULL;
598
599         if (cvs_exclude) {
600                 if (strlen(fname) + strlen(".cvsignore") <= MAXPATHLEN-1) {
601                         strcpy(p,".cvsignore");
602                         local_exclude_list = make_exclude_list(fname,NULL,0,0);
603                 } else {
604                         io_error = 1;
605                         rprintf(FINFO,"cannot cvs-exclude in long-named directory %s\n",fname);
606                 }
607         }  
608         
609         for (di=readdir(d); di; di=readdir(d)) {
610                 char *dname = d_name(di);
611                 if (strcmp(dname,".")==0 ||
612                     strcmp(dname,"..")==0)
613                         continue;
614                 strlcpy(p,dname,MAXPATHLEN-l);
615                 send_file_name(f,flist,fname,recurse,0);
616         }
617
618         if (local_exclude_list) {
619                 add_exclude_list("!", &local_exclude_list, 0);
620         }
621
622         closedir(d);
623 }
624
625
626
627 struct file_list *send_file_list(int f,int argc,char *argv[])
628 {
629         int i,l;
630         STRUCT_STAT st;
631         char *p,*dir,*olddir;
632         char lastpath[MAXPATHLEN]="";
633         struct file_list *flist;
634         int64 start_write;
635
636         if (verbose && recurse && !am_server && f != -1) {
637                 rprintf(FINFO,"building file list ... ");
638                 rflush(FINFO);
639         }
640
641         start_write = stats.total_written;
642
643         flist = (struct file_list *)malloc(sizeof(flist[0]));
644         if (!flist) out_of_memory("send_file_list");
645
646         flist->count=0;
647         flist->malloced = 1000;
648         flist->files = (struct file_struct **)malloc(sizeof(flist->files[0])*
649                                                      flist->malloced);
650         if (!flist->files) out_of_memory("send_file_list");
651
652         if (f != -1) {
653                 io_start_buffering(f);
654         }
655
656         for (i=0;i<argc;i++) {
657                 char *fname = topsrcname;
658
659                 strlcpy(fname,argv[i],MAXPATHLEN);
660
661                 l = strlen(fname);
662                 if (l != 1 && fname[l-1] == '/') {
663                         if ((l == 2) && (fname[0] == '.')) {
664                                 /*  Turn ./ into just . rather than ./.
665                                     This was put in to avoid a problem with
666                                       rsync -aR --delete from ./
667                                     The send_file_name() below of ./ was
668                                     mysteriously preventing deletes */
669                                 fname[1] = 0;
670                         } else {
671                                 strlcat(fname,".",MAXPATHLEN);
672                         }
673                 }
674
675                 if (link_stat(fname,&st) != 0) {
676                         if (f != -1) {
677                                 io_error=1;
678                                 rprintf(FERROR,"link_stat %s : %s\n",fname,strerror(errno));
679                         }
680                         continue;
681                 }
682
683                 if (S_ISDIR(st.st_mode) && !recurse) {
684                         rprintf(FINFO,"skipping directory %s\n",fname);
685                         continue;
686                 }
687
688                 dir = NULL;
689                 olddir = NULL;
690
691                 if (!relative_paths) {
692                         p = strrchr(fname,'/');
693                         if (p) {
694                                 *p = 0;
695                                 if (p == fname) 
696                                         dir = "/";
697                                 else
698                                         dir = fname;      
699                                 fname = p+1;      
700                         }
701                 } else if (f != -1 && (p=strrchr(fname,'/'))) {
702                         /* this ensures we send the intermediate directories,
703                            thus getting their permissions right */
704                         *p = 0;
705                         if (strcmp(lastpath,fname)) {
706                                 strlcpy(lastpath, fname, sizeof(lastpath));
707                                 *p = '/';
708                                 for (p=fname+1; (p=strchr(p,'/')); p++) {
709                                         int copy_links_saved = copy_links;
710                                         int recurse_saved = recurse;
711                                         *p = 0;
712                                         copy_links = copy_unsafe_links;
713                                         /* set recurse to 1 to prevent make_file
714                                            from ignoring directory, but still
715                                            turn off the recursive parameter to
716                                            send_file_name */
717                                         recurse = 1;
718                                         send_file_name(f, flist, fname, 0, 0);
719                                         copy_links = copy_links_saved;
720                                         recurse = recurse_saved;
721                                         *p = '/';
722                                 }
723                         } else {
724                                 *p = '/';
725                         }
726                 }
727                 
728                 if (!*fname)
729                         fname = ".";
730                 
731                 if (dir && *dir) {
732                         olddir = push_dir(dir, 1);
733
734                         if (!olddir) {
735                                 io_error=1;
736                                 rprintf(FERROR,"push_dir %s : %s\n",
737                                         dir,strerror(errno));
738                                 continue;
739                         }
740
741                         flist_dir = dir;
742                 }
743                 
744                 if (one_file_system)
745                         set_filesystem(fname);
746
747                 send_file_name(f,flist,fname,recurse,FLAG_DELETE);
748
749                 if (olddir != NULL) {
750                         flist_dir = NULL;
751                         if (pop_dir(olddir) != 0) {
752                                 rprintf(FERROR,"pop_dir %s : %s\n",
753                                         dir,strerror(errno));
754                                 exit_cleanup(RERR_FILESELECT);
755                         }
756                 }
757         }
758
759         topsrcname[0] = '\0';
760
761         if (f != -1) {
762                 send_file_entry(NULL,f,0);
763         }
764
765         if (verbose && recurse && !am_server && f != -1)
766                 rprintf(FINFO,"done\n");
767         
768         clean_flist(flist, 0);
769         
770         /* now send the uid/gid list. This was introduced in protocol
771            version 15 */
772         if (f != -1 && remote_version >= 15) {
773                 send_uid_list(f);
774         }
775
776         /* if protocol version is >= 17 then send the io_error flag */
777         if (f != -1 && remote_version >= 17) {
778                 extern int module_id;
779                 write_int(f, lp_ignore_errors(module_id)? 0 : io_error);
780         }
781
782         if (f != -1) {
783                 io_end_buffering(f);
784                 stats.flist_size = stats.total_written - start_write;
785                 stats.num_files = flist->count;
786         }
787
788         if (verbose > 2)
789                 rprintf(FINFO,"send_file_list done\n");
790
791         return flist;
792 }
793
794
795 struct file_list *recv_file_list(int f)
796 {
797   struct file_list *flist;
798   unsigned char flags;
799   int64 start_read;
800   extern int list_only;
801
802   if (verbose && recurse && !am_server) {
803     rprintf(FINFO,"receiving file list ... ");
804     rflush(FINFO);
805   }
806
807   start_read = stats.total_read;
808
809   flist = (struct file_list *)malloc(sizeof(flist[0]));
810   if (!flist)
811     goto oom;
812
813   flist->count=0;
814   flist->malloced=1000;
815   flist->files = (struct file_struct **)malloc(sizeof(flist->files[0])*
816                                                flist->malloced);
817   if (!flist->files)
818     goto oom;
819
820
821   for (flags=read_byte(f); flags; flags=read_byte(f)) {
822     int i = flist->count;
823
824     if (i >= flist->malloced) {
825           if (flist->malloced < 1000)
826                   flist->malloced += 1000;
827           else
828                   flist->malloced *= 2;
829           flist->files =(struct file_struct **)realloc(flist->files,
830                                                        sizeof(flist->files[0])*
831                                                        flist->malloced);
832           if (!flist->files)
833                   goto oom;
834     }
835
836     receive_file_entry(&flist->files[i],flags,f);
837
838     if (S_ISREG(flist->files[i]->mode))
839             stats.total_size += flist->files[i]->length;
840
841     flist->count++;
842
843     if (verbose > 2)
844       rprintf(FINFO,"recv_file_name(%s)\n",f_name(flist->files[i]));
845   }
846
847
848   if (verbose > 2)
849     rprintf(FINFO,"received %d names\n",flist->count);
850
851   clean_flist(flist, relative_paths);
852
853   if (verbose && recurse && !am_server) {
854     rprintf(FINFO,"done\n");
855   }
856
857   /* now recv the uid/gid list. This was introduced in protocol version 15 */
858   if (f != -1 && remote_version >= 15) {
859           recv_uid_list(f, flist);
860   }
861
862   /* if protocol version is >= 17 then recv the io_error flag */
863   if (f != -1 && remote_version >= 17) {
864           extern int module_id;
865           if (lp_ignore_errors(module_id)) {
866                   read_int(f);
867           } else {
868                   io_error |= read_int(f);
869           }
870   }
871
872   if (list_only) {
873           int i;
874           for (i=0;i<flist->count;i++) {
875                   list_file_entry(flist->files[i]);
876           }
877   }
878
879
880   if (verbose > 2)
881     rprintf(FINFO,"recv_file_list done\n");
882
883   stats.flist_size = stats.total_read - start_read;
884   stats.num_files = flist->count;
885
886   return flist;
887
888 oom:
889     out_of_memory("recv_file_list");
890     return NULL; /* not reached */
891 }
892
893
894 int file_compare(struct file_struct **f1,struct file_struct **f2)
895 {
896         if (!(*f1)->basename && !(*f2)->basename) return 0;
897         if (!(*f1)->basename) return -1;
898         if (!(*f2)->basename) return 1;
899         if ((*f1)->dirname == (*f2)->dirname)
900                 return u_strcmp((*f1)->basename, (*f2)->basename);
901         return u_strcmp(f_name(*f1),f_name(*f2));
902 }
903
904
905 int flist_find(struct file_list *flist,struct file_struct *f)
906 {
907         int low=0,high=flist->count-1;
908
909         if (flist->count <= 0) return -1;
910
911         while (low != high) {
912                 int mid = (low+high)/2;
913                 int ret = file_compare(&flist->files[flist_up(flist, mid)],&f);
914                 if (ret == 0) return flist_up(flist, mid);
915                 if (ret > 0) {
916                         high=mid;
917                 } else {
918                         low=mid+1;
919                 }
920         }
921
922         if (file_compare(&flist->files[flist_up(flist,low)],&f) == 0)
923                 return flist_up(flist,low);
924         return -1;
925 }
926
927
928 /*
929  * free up one file
930  */
931 void free_file(struct file_struct *file)
932 {
933         if (!file) return;
934         if (file->basename) free(file->basename);
935         if (file->link) free(file->link);
936         if (file->sum) free(file->sum);
937         memset((char *)file, 0, sizeof(*file));
938 }
939
940
941 /*
942  * free up all elements in a flist
943  */
944 void flist_free(struct file_list *flist)
945 {
946         int i;
947         for (i=1;i<flist->count;i++) {
948                 free_file(flist->files[i]);
949                 free(flist->files[i]);
950         }       
951         memset((char *)flist->files, 0, sizeof(flist->files[0])*flist->count);
952         free(flist->files);
953         memset((char *)flist, 0, sizeof(*flist));
954         free(flist);
955 }
956
957
958 /*
959  * This routine ensures we don't have any duplicate names in our file list.
960  * duplicate names can cause corruption because of the pipelining 
961  */
962 static void clean_flist(struct file_list *flist, int strip_root)
963 {
964         int i;
965
966         if (!flist || flist->count == 0) 
967                 return;
968   
969         qsort(flist->files,flist->count,
970               sizeof(flist->files[0]),
971               (int (*)())file_compare);
972
973         for (i=1;i<flist->count;i++) {
974                 if (flist->files[i]->basename &&
975                     flist->files[i-1]->basename &&
976                     strcmp(f_name(flist->files[i]),
977                            f_name(flist->files[i-1])) == 0) {
978                         if (verbose > 1 && !am_server)
979                                 rprintf(FINFO,"removing duplicate name %s from file list %d\n",
980                                         f_name(flist->files[i-1]),i-1);
981                         free_file(flist->files[i]);
982                 } 
983         }
984
985         if (strip_root) {
986                 /* we need to strip off the root directory in the case
987                    of relative paths, but this must be done _after_
988                    the sorting phase */
989                 for (i=0;i<flist->count;i++) {
990                         if (flist->files[i]->dirname &&
991                             flist->files[i]->dirname[0] == '/') {
992                                 memmove(&flist->files[i]->dirname[0],
993                                         &flist->files[i]->dirname[1],
994                                         strlen(flist->files[i]->dirname));
995                         }
996                         
997                         if (flist->files[i]->dirname && 
998                             !flist->files[i]->dirname[0]) {
999                                 flist->files[i]->dirname = NULL;
1000                         }
1001                 }
1002         }
1003
1004
1005         if (verbose <= 3) return;
1006
1007         for (i=0;i<flist->count;i++) {
1008                 rprintf(FINFO,"[%d] i=%d %s %s mode=0%o len=%.0f\n",
1009                         getpid(), i, 
1010                         NS(flist->files[i]->dirname),
1011                         NS(flist->files[i]->basename),
1012                         flist->files[i]->mode,
1013                         (double)flist->files[i]->length);
1014         }
1015 }
1016
1017
1018 /*
1019  * return the full filename of a flist entry
1020  */
1021 char *f_name(struct file_struct *f)
1022 {
1023         static char names[10][MAXPATHLEN];
1024         static int n;
1025         char *p = names[n];
1026
1027         if (!f || !f->basename) return NULL;
1028
1029         n = (n+1)%10;
1030
1031         if (f->dirname) {
1032                 strlcpy(p, f->dirname, MAXPATHLEN);
1033                 strlcat(p, "/", MAXPATHLEN);
1034                 strlcat(p, f->basename, MAXPATHLEN);
1035         } else {
1036                 strlcpy(p, f->basename, MAXPATHLEN);
1037         }
1038
1039         return p;
1040 }
1041