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