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