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