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