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