a7050aebefdc413cd4445b43b45aaed19ff3ef2c
[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  * @sa http://lists.samba.org/pipermail/rsync/2000-June/002351.html
25  *
26  **/
27
28 #include "rsync.h"
29
30 extern struct stats stats;
31
32 extern int verbose;
33 extern int do_progress;
34 extern int am_server;
35 extern int always_checksum;
36 extern int module_id;
37 extern int ignore_errors;
38
39 extern int cvs_exclude;
40
41 extern int recurse;
42 extern char curr_dir[MAXPATHLEN];
43 extern char *files_from;
44 extern int filesfrom_fd;
45
46 extern int one_file_system;
47 extern int make_backups;
48 extern int preserve_links;
49 extern int preserve_hard_links;
50 extern int preserve_perms;
51 extern int preserve_devices;
52 extern int preserve_uid;
53 extern int preserve_gid;
54 extern int preserve_times;
55 extern int relative_paths;
56 extern int implied_dirs;
57 extern int copy_links;
58 extern int copy_unsafe_links;
59 extern int protocol_version;
60 extern int sanitize_paths;
61
62 extern int read_batch;
63 extern int write_batch;
64
65 extern struct exclude_struct **exclude_list;
66 extern struct exclude_struct **server_exclude_list;
67 extern struct exclude_struct **local_exclude_list;
68
69 int io_error;
70
71 static char empty_sum[MD4_SUM_LENGTH];
72
73 static void clean_flist(struct file_list *flist, int strip_root, int no_dups);
74
75
76 static int show_filelist_p(void)
77 {
78         return verbose && (recurse || files_from) && !am_server;
79 }
80
81 static void start_filelist_progress(char *kind)
82 {
83         rprintf(FINFO, "%s ... ", kind);
84         if ((verbose > 1) || do_progress)
85                 rprintf(FINFO, "\n");
86         rflush(FINFO);
87 }
88
89
90 static void emit_filelist_progress(const struct file_list *flist)
91 {
92         rprintf(FINFO, " %d files...\r", flist->count);
93 }
94
95
96 static void maybe_emit_filelist_progress(const struct file_list *flist)
97 {
98         if (do_progress && show_filelist_p() && ((flist->count % 100) == 0))
99                 emit_filelist_progress(flist);
100 }
101
102
103 static void finish_filelist_progress(const struct file_list *flist)
104 {
105         if (do_progress) {
106                 /* This overwrites the progress line */
107                 rprintf(FINFO, "%d file%sto consider\n",
108                         flist->count, flist->count == 1 ? " " : "s ");
109         } else
110                 rprintf(FINFO, "done\n");
111 }
112
113 void show_flist_stats(void)
114 {
115         /* Nothing yet */
116 }
117
118
119 static void list_file_entry(struct file_struct *f)
120 {
121         char perms[11];
122
123         if (!f->basename)
124                 /* this can happen if duplicate names were removed */
125                 return;
126
127         permstring(perms, f->mode);
128
129 #if SUPPORT_LINKS
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->u.link);
135         } else
136 #endif
137                 rprintf(FINFO, "%s %11.0f %s %s\n",
138                         perms,
139                         (double) f->length, timestring(f->modtime),
140                         f_name(f));
141 }
142
143
144 /**
145  * Stat either a symlink or its referent, depending on the settings of
146  * copy_links, copy_unsafe_links, etc.
147  *
148  * @retval -1 on error
149  *
150  * @retval 0 for success
151  *
152  * @post If @p path is a symlink, then @p linkbuf (of size @c
153  * MAXPATHLEN) contains the symlink target.
154  *
155  * @post @p buffer contains information about the link or the
156  * referrent as appropriate, if they exist.
157  **/
158 int readlink_stat(const char *path, STRUCT_STAT *buffer, char *linkbuf)
159 {
160 #if SUPPORT_LINKS
161         if (copy_links)
162                 return do_stat(path, buffer);
163         if (do_lstat(path, buffer) == -1)
164                 return -1;
165         if (S_ISLNK(buffer->st_mode)) {
166                 int l = readlink((char *) path, linkbuf, MAXPATHLEN - 1);
167                 if (l == -1)
168                         return -1;
169                 linkbuf[l] = 0;
170                 if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
171                         if (verbose > 1) {
172                                 rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
173                                         path, linkbuf);
174                         }
175                         return do_stat(path, buffer);
176                 }
177         }
178         return 0;
179 #else
180         return do_stat(path, buffer);
181 #endif
182 }
183
184 int link_stat(const char *path, STRUCT_STAT * buffer)
185 {
186 #if SUPPORT_LINKS
187         if (copy_links)
188                 return do_stat(path, buffer);
189         return do_lstat(path, buffer);
190 #else
191         return do_stat(path, buffer);
192 #endif
193 }
194
195 /*
196  * This function is used to check if a file should be included/excluded
197  * from the list of files based on its name and type etc.  The value of
198  * exclude_level is set to either SERVER_EXCLUDES or ALL_EXCLUDES.
199  */
200 static int check_exclude_file(char *fname, int is_dir, int exclude_level)
201 {
202 #if 0 /* This currently never happens, so avoid a useless compare. */
203         if (exclude_level == NO_EXCLUDES)
204                 return 0;
205 #endif
206         if (fname) {
207                 /* never exclude '.', even if somebody does --exclude '*' */
208                 if (fname[0] == '.' && !fname[1])
209                         return 0;
210                 /* Handle the -R version of the '.' dir. */
211                 if (fname[0] == '/') {
212                         int len = strlen(fname);
213                         if (fname[len-1] == '.' && fname[len-2] == '/')
214                                 return 0;
215                 }
216         }
217         if (server_exclude_list
218          && check_exclude(server_exclude_list, fname, is_dir))
219                 return 1;
220         if (exclude_level != ALL_EXCLUDES)
221                 return 0;
222         if (exclude_list && check_exclude(exclude_list, fname, is_dir))
223                 return 1;
224         if (local_exclude_list
225          && check_exclude(local_exclude_list, fname, is_dir))
226                 return 1;
227         return 0;
228 }
229
230 /* used by the one_file_system code */
231 static dev_t filesystem_dev;
232
233 static void set_filesystem(char *fname)
234 {
235         STRUCT_STAT st;
236         if (link_stat(fname, &st) != 0)
237                 return;
238         filesystem_dev = st.st_dev;
239 }
240
241
242 static int to_wire_mode(mode_t mode)
243 {
244 #if SUPPORT_LINKS
245         if (S_ISLNK(mode) && (_S_IFLNK != 0120000))
246                 return (mode & ~(_S_IFMT)) | 0120000;
247 #endif
248         return (int) mode;
249 }
250
251 static mode_t from_wire_mode(int mode)
252 {
253         if ((mode & (_S_IFMT)) == 0120000 && (_S_IFLNK != 0120000))
254                 return (mode & ~(_S_IFMT)) | _S_IFLNK;
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 static int flist_dir_len;
263
264
265 /**
266  * Make sure @p flist is big enough to hold at least @p flist->count
267  * entries.
268  **/
269 static void flist_expand(struct file_list *flist)
270 {
271         if (flist->count >= flist->malloced) {
272                 void *new_ptr;
273
274                 if (flist->malloced < 1000)
275                         flist->malloced += 1000;
276                 else
277                         flist->malloced *= 2;
278
279                 if (flist->files) {
280                         new_ptr = realloc_array(flist->files,
281                                                 struct file_struct *,
282                                                 flist->malloced);
283                 } else {
284                         new_ptr = new_array(struct file_struct *,
285                                             flist->malloced);
286                 }
287
288                 if (verbose >= 2) {
289                         rprintf(FINFO, "[%s] expand file_list to %.0f bytes, did%s move\n",
290                                 who_am_i(),
291                                 (double)sizeof(flist->files[0])
292                                 * flist->malloced,
293                                 (new_ptr == flist->files) ? " not" : "");
294                 }
295
296                 flist->files = (struct file_struct **) new_ptr;
297
298                 if (!flist->files)
299                         out_of_memory("flist_expand");
300         }
301 }
302
303 void send_file_entry(struct file_struct *file, int f, unsigned short base_flags)
304 {
305         unsigned short flags;
306         static time_t modtime;
307         static mode_t mode;
308         static DEV64_T rdev, rdev_high;
309         static DEV64_T dev;
310         static uid_t uid;
311         static gid_t gid;
312         static char lastname[MAXPATHLEN];
313         char *fname, fbuf[MAXPATHLEN];
314         int l1, l2;
315
316         if (f == -1)
317                 return;
318
319         if (!file) {
320                 write_byte(f, 0);
321                 modtime = 0, mode = 0;
322                 rdev = 0, rdev_high = 0, dev = 0;
323                 uid = 0, gid = 0;
324                 *lastname = '\0';
325                 return;
326         }
327
328         io_write_phase = "send_file_entry";
329
330         fname = f_name_to(file, fbuf);
331
332         flags = base_flags;
333
334         if (file->mode == mode)
335                 flags |= XMIT_SAME_MODE;
336         else
337                 mode = file->mode;
338         if (preserve_devices) {
339                 if (protocol_version < 28) {
340                         if (IS_DEVICE(mode)) {
341                                 if (file->u.rdev == rdev) {
342                                         /* Set both flags to simplify the test
343                                          * when writing the data. */
344                                         flags |= XMIT_SAME_RDEV_pre28
345                                                | XMIT_SAME_HIGH_RDEV;
346                                 } else
347                                         rdev = file->u.rdev;
348                         } else
349                                 rdev = 0;
350                 } else if (IS_DEVICE(mode)) {
351                         if ((file->u.rdev & ~0xFF) == rdev_high)
352                                 flags |= XMIT_SAME_HIGH_RDEV;
353                         else {
354                                 rdev = file->u.rdev;
355                                 rdev_high = rdev & ~0xFF;
356                         }
357                 }
358         }
359         if (file->uid == uid)
360                 flags |= XMIT_SAME_UID;
361         else
362                 uid = file->uid;
363         if (file->gid == gid)
364                 flags |= XMIT_SAME_GID;
365         else
366                 gid = file->gid;
367         if (file->modtime == modtime)
368                 flags |= XMIT_SAME_TIME;
369         else
370                 modtime = file->modtime;
371
372 #if SUPPORT_HARD_LINKS
373         if (file->link_u.idev) {
374                 if (file->F_DEV == dev) {
375                         if (protocol_version >= 28)
376                                 flags |= XMIT_SAME_DEV;
377                 } else
378                         dev = file->F_DEV;
379                 flags |= XMIT_HAS_IDEV_DATA;
380         }
381 #endif
382
383         for (l1 = 0;
384              lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
385              l1++) {}
386         l2 = strlen(fname+l1);
387
388         if (l1 > 0)
389                 flags |= XMIT_SAME_NAME;
390         if (l2 > 255)
391                 flags |= XMIT_LONG_NAME;
392
393         /* We must make sure we don't send a zero flag byte or the
394          * other end will terminate the flist transfer.  Note that
395          * the use of XMIT_TOP_DIR on a non-dir has no meaning, so
396          * it's harmless way to add a bit to the first flag byte. */
397         if (protocol_version >= 28) {
398                 if (!flags && !S_ISDIR(mode))
399                         flags |= XMIT_TOP_DIR;
400                 if ((flags & 0xFF00) || !flags) {
401                         flags |= XMIT_EXTENDED_FLAGS;
402                         write_byte(f, flags);
403                         write_byte(f, flags >> 8);
404                 } else
405                         write_byte(f, flags);
406         } else {
407                 if (!(flags & 0xFF) && !S_ISDIR(mode))
408                         flags |= XMIT_TOP_DIR;
409                 if (!(flags & 0xFF))
410                         flags |= XMIT_LONG_NAME;
411                 write_byte(f, flags);
412         }
413         if (flags & XMIT_SAME_NAME)
414                 write_byte(f, l1);
415         if (flags & XMIT_LONG_NAME)
416                 write_int(f, l2);
417         else
418                 write_byte(f, l2);
419         write_buf(f, fname + l1, l2);
420
421         write_longint(f, file->length);
422         if (!(flags & XMIT_SAME_TIME))
423                 write_int(f, modtime);
424         if (!(flags & XMIT_SAME_MODE))
425                 write_int(f, to_wire_mode(mode));
426         if (preserve_uid && !(flags & XMIT_SAME_UID)) {
427                 add_uid(uid);
428                 write_int(f, uid);
429         }
430         if (preserve_gid && !(flags & XMIT_SAME_GID)) {
431                 add_gid(gid);
432                 write_int(f, gid);
433         }
434         if (preserve_devices && IS_DEVICE(mode)) {
435                 /* If XMIT_SAME_HIGH_RDEV is off, XMIT_SAME_RDEV_pre28 is
436                  * also off. */
437                 if (!(flags & XMIT_SAME_HIGH_RDEV))
438                         write_int(f, rdev);
439                 else if (protocol_version >= 28)
440                         write_byte(f, rdev);
441         }
442
443 #if SUPPORT_LINKS
444         if (preserve_links && S_ISLNK(mode)) {
445                 int len = strlen(file->u.link);
446                 write_int(f, len);
447                 write_buf(f, file->u.link, len);
448         }
449 #endif
450
451 #if SUPPORT_HARD_LINKS
452         if (flags & XMIT_HAS_IDEV_DATA) {
453                 if (protocol_version < 26) {
454                         /* 32-bit dev_t and ino_t */
455                         write_int(f, dev);
456                         write_int(f, file->F_INODE);
457                 } else {
458                         /* 64-bit dev_t and ino_t */
459                         if (!(flags & XMIT_SAME_DEV))
460                                 write_longint(f, dev);
461                         write_longint(f, file->F_INODE);
462                 }
463         }
464 #endif
465
466         if (always_checksum) {
467                 char *sum;
468                 if (S_ISREG(mode))
469                         sum = file->u.sum;
470                 else if (protocol_version < 28) {
471                         /* Prior to 28, we sent a useless set of nulls. */
472                         sum = empty_sum;
473                 } else
474                         sum = NULL;
475                 if (sum) {
476                         write_buf(f, sum, protocol_version < 21? 2
477                                                         : MD4_SUM_LENGTH);
478                 }
479         }
480
481         strlcpy(lastname, fname, MAXPATHLEN);
482
483         io_write_phase = "unknown";
484 }
485
486
487
488 void receive_file_entry(struct file_struct **fptr, unsigned short flags, int f)
489 {
490         static time_t modtime;
491         static mode_t mode;
492         static DEV64_T rdev, rdev_high;
493         static DEV64_T dev;
494         static uid_t uid;
495         static gid_t gid;
496         static char lastname[MAXPATHLEN], *lastdir;
497         static int lastdir_len = -1;
498         char thisname[MAXPATHLEN];
499         unsigned int l1 = 0, l2 = 0;
500         int alloc_len, basename_len, dirname_len, linkname_len, sum_len, idev_len;
501         OFF_T file_length;
502         char *basename, *dirname, *bp;
503         struct file_struct *file;
504
505         if (!fptr) {
506                 modtime = 0, mode = 0;
507                 rdev = 0, rdev_high = 0, dev = 0;
508                 uid = 0, gid = 0;
509                 *lastname = '\0';
510                 return;
511         }
512
513         if (flags & XMIT_SAME_NAME)
514                 l1 = read_byte(f);
515
516         if (flags & XMIT_LONG_NAME)
517                 l2 = read_int(f);
518         else
519                 l2 = read_byte(f);
520
521         if (l2 >= MAXPATHLEN - l1) {
522                 rprintf(FERROR,
523                         "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n",
524                         flags, l1, l2, lastname);
525                 overflow("receive_file_entry");
526         }
527
528         strlcpy(thisname, lastname, l1 + 1);
529         read_sbuf(f, &thisname[l1], l2);
530         thisname[l1 + l2] = 0;
531
532         strlcpy(lastname, thisname, MAXPATHLEN);
533
534         clean_fname(thisname);
535
536         if (sanitize_paths)
537                 sanitize_path(thisname, NULL);
538
539         if ((basename = strrchr(thisname, '/')) != NULL) {
540                 dirname_len = ++basename - thisname; /* counts future '\0' */
541                 if (lastdir_len == dirname_len - 1
542                     && strncmp(thisname, lastdir, lastdir_len) == 0) {
543                         dirname = lastdir;
544                         dirname_len = 0; /* indicates no copy is needed */
545                 } else
546                         dirname = thisname;
547         } else {
548                 basename = thisname;
549                 dirname = NULL;
550                 dirname_len = 0;
551         }
552         basename_len = strlen(basename) + 1; /* count the '\0' */
553
554         file_length = read_longint(f);
555         if (!(flags & XMIT_SAME_TIME))
556                 modtime = (time_t)read_int(f);
557         if (!(flags & XMIT_SAME_MODE))
558                 mode = from_wire_mode(read_int(f));
559
560         if (preserve_uid && !(flags & XMIT_SAME_UID))
561                 uid = (uid_t)read_int(f);
562         if (preserve_gid && !(flags & XMIT_SAME_GID))
563                 gid = (gid_t)read_int(f);
564
565         if (preserve_devices) {
566                 if (protocol_version < 28) {
567                         if (IS_DEVICE(mode)) {
568                                 if (!(flags & XMIT_SAME_RDEV_pre28))
569                                         rdev = (DEV64_T)read_int(f);
570                         } else
571                                 rdev = 0;
572                 } else if (IS_DEVICE(mode)) {
573                         if (!(flags & XMIT_SAME_HIGH_RDEV)) {
574                                 rdev = (DEV64_T)read_int(f);
575                                 rdev_high = rdev & ~0xFF;
576                         } else
577                                 rdev = rdev_high | (DEV64_T)read_byte(f);
578                 }
579         }
580
581 #if SUPPORT_LINKS
582         if (preserve_links && S_ISLNK(mode)) {
583                 linkname_len = read_int(f) + 1; /* count the '\0' */
584                 if (linkname_len <= 0 || linkname_len > MAXPATHLEN) {
585                         rprintf(FERROR, "overflow: linkname_len=%d\n",
586                                 linkname_len - 1);
587                         overflow("receive_file_entry");
588                 }
589         }
590         else
591 #endif
592                 linkname_len = 0;
593
594 #if SUPPORT_HARD_LINKS
595         if (preserve_hard_links && protocol_version < 28 && S_ISREG(mode))
596                 flags |= XMIT_HAS_IDEV_DATA;
597         if (flags & XMIT_HAS_IDEV_DATA)
598                 idev_len = sizeof (struct idev);
599         else
600 #endif
601                 idev_len = 0;
602
603         sum_len = always_checksum && S_ISREG(mode) ? MD4_SUM_LENGTH : 0;
604
605         alloc_len = sizeof file[0] + dirname_len + basename_len
606                   + linkname_len + sum_len + idev_len;
607         if (!(bp = new_array(char, alloc_len)))
608                 out_of_memory("receive_file_entry");
609         file = *fptr = (struct file_struct *)bp;
610         memset(bp, 0, sizeof file[0]);
611         bp += sizeof file[0];
612
613         file->flags = flags & XMIT_TOP_DIR ? FLAG_TOP_DIR : 0;
614         file->modtime = modtime;
615         file->length = file_length;
616         file->mode = mode;
617         file->uid = uid;
618         file->gid = gid;
619
620         if (dirname_len) {
621                 file->dirname = lastdir = bp;
622                 lastdir_len = dirname_len - 1;
623                 memcpy(bp, dirname, dirname_len - 1);
624                 bp += dirname_len;
625                 bp[-1] = '\0';
626         } else if (dirname)
627                 file->dirname = dirname;
628
629         file->basename = bp;
630         memcpy(bp, basename, basename_len);
631         bp += basename_len;
632
633         if (preserve_devices && IS_DEVICE(mode))
634                 file->u.rdev = rdev;
635
636 #if SUPPORT_LINKS
637         if (linkname_len) {
638                 file->u.link = bp;
639                 read_sbuf(f, bp, linkname_len - 1);
640                 if (sanitize_paths)
641                         sanitize_path(bp, lastdir);
642                 bp += linkname_len;
643         }
644 #endif
645
646 #if SUPPORT_HARD_LINKS
647         if (idev_len) {
648                 file->link_u.idev = (struct idev *)bp;
649                 bp += idev_len;
650                 if (protocol_version < 26) {
651                         dev = read_int(f);
652                         file->F_INODE = read_int(f);
653                 } else {
654                         if (!(flags & XMIT_SAME_DEV))
655                                 dev = read_longint(f);
656                         file->F_INODE = read_longint(f);
657                 }
658                 file->F_DEV = dev;
659         }
660 #endif
661
662         if (always_checksum) {
663                 char *sum;
664                 if (sum_len) {
665                         file->u.sum = sum = bp;
666                         /*bp += sum_len;*/
667                 } else if (protocol_version < 28) {
668                         /* Prior to 28, we get a useless set of nulls. */
669                         sum = empty_sum;
670                 } else
671                         sum = NULL;
672                 if (sum) {
673                         read_buf(f, sum, protocol_version < 21? 2
674                                                 : MD4_SUM_LENGTH);
675                 }
676         }
677
678         if (!preserve_perms) {
679                 extern int orig_umask;
680                 /* set an appropriate set of permissions based on original
681                  * permissions and umask. This emulates what GNU cp does */
682                 file->mode &= ~orig_umask;
683         }
684 }
685
686
687 /**
688  * Create a file_struct for a named file by reading its stat()
689  * information and performing extensive checks against global
690  * options.
691  *
692  * @return the new file, or NULL if there was an error or this file
693  * should be excluded.
694  *
695  * @todo There is a small optimization opportunity here to avoid
696  * stat()ing the file in some circumstances, which has a certain cost.
697  * We are called immediately after doing readdir(), and so we may
698  * already know the d_type of the file.  We could for example avoid
699  * statting directories if we're not recursing, but this is not a very
700  * important case.  Some systems may not have d_type.
701  **/
702 struct file_struct *make_file(char *fname, int exclude_level)
703 {
704         static char *lastdir;
705         static int lastdir_len = -1;
706         struct file_struct *file;
707         STRUCT_STAT st;
708         char sum[SUM_LENGTH];
709         char thisname[MAXPATHLEN];
710         char linkname[MAXPATHLEN];
711         int alloc_len, basename_len, dirname_len, linkname_len, sum_len, idev_len;
712         char *basename, *dirname, *bp;
713         unsigned short flags = 0;
714
715         if (strlcpy(thisname, fname, sizeof thisname)
716             >= sizeof thisname - flist_dir_len) {
717                 rprintf(FINFO, "skipping overly long name: %s\n", fname);
718                 return NULL;
719         }
720         clean_fname(thisname);
721         if (sanitize_paths)
722                 sanitize_path(thisname, NULL);
723
724         memset(sum, 0, SUM_LENGTH);
725
726         if (readlink_stat(thisname, &st, linkname) != 0) {
727                 int save_errno = errno;
728                 if (errno == ENOENT && exclude_level != NO_EXCLUDES) {
729                         /* either symlink pointing nowhere or file that
730                          * was removed during rsync run; see if excluded
731                          * before reporting an error */
732                         if (check_exclude_file(thisname, 0, exclude_level)) {
733                                 /* file is excluded anyway, ignore silently */
734                                 return NULL;
735                         }
736                 }
737                 io_error |= IOERR_GENERAL;
738                 rprintf(FERROR, "readlink %s failed: %s\n",
739                         full_fname(thisname), strerror(save_errno));
740                 return NULL;
741         }
742
743         /* backup.c calls us with exclude_level set to NO_EXCLUDES. */
744         if (exclude_level == NO_EXCLUDES)
745                 goto skip_excludes;
746
747         if (S_ISDIR(st.st_mode) && !recurse && !files_from) {
748                 rprintf(FINFO, "skipping directory %s\n", thisname);
749                 return NULL;
750         }
751
752         if (one_file_system && st.st_dev != filesystem_dev) {
753                 /* We allow a directory though to preserve the mount point.
754                  * However, flag it so that we don't recurse. */
755                 if (!S_ISDIR(st.st_mode))
756                         return NULL;
757                 flags |= FLAG_MOUNT_POINT;
758         }
759
760         if (check_exclude_file(thisname, S_ISDIR(st.st_mode) != 0, exclude_level))
761                 return NULL;
762
763         if (lp_ignore_nonreadable(module_id) && access(thisname, R_OK) != 0)
764                 return NULL;
765
766       skip_excludes:
767
768         if (verbose > 2) {
769                 rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
770                         who_am_i(), thisname, exclude_level);
771         }
772
773         if ((basename = strrchr(thisname, '/')) != NULL) {
774                 dirname_len = ++basename - thisname; /* counts future '\0' */
775                 if (lastdir_len == dirname_len - 1
776                     && strncmp(thisname, lastdir, lastdir_len) == 0) {
777                         dirname = lastdir;
778                         dirname_len = 0; /* indicates no copy is needed */
779                 } else
780                         dirname = thisname;
781         } else {
782                 basename = thisname;
783                 dirname = NULL;
784                 dirname_len = 0;
785         }
786         basename_len = strlen(basename) + 1; /* count the '\0' */
787
788 #if SUPPORT_LINKS
789         linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
790 #else
791         linkname_len = 0;
792 #endif
793
794 #if SUPPORT_HARD_LINKS
795         if (preserve_hard_links) {
796                 idev_len = (protocol_version < 28 ? S_ISREG(st.st_mode)
797                           : !S_ISDIR(st.st_mode) && st.st_nlink > 1)
798                          ? sizeof (struct idev) : 0;
799         } else
800 #endif
801                 idev_len = 0;
802
803         sum_len = always_checksum && S_ISREG(st.st_mode) ? MD4_SUM_LENGTH : 0;
804
805         alloc_len = sizeof file[0] + dirname_len + basename_len
806                   + linkname_len + sum_len + idev_len;
807         if (!(bp = new_array(char, alloc_len)))
808                 out_of_memory("receive_file_entry");
809         file = (struct file_struct *)bp;
810         memset(bp, 0, sizeof file[0]);
811         bp += sizeof file[0];
812
813         file->flags = flags;
814         file->modtime = st.st_mtime;
815         file->length = st.st_size;
816         file->mode = st.st_mode;
817         file->uid = st.st_uid;
818         file->gid = st.st_gid;
819
820         if (dirname_len) {
821                 file->dirname = lastdir = bp;
822                 lastdir_len = dirname_len - 1;
823                 memcpy(bp, dirname, dirname_len - 1);
824                 bp += dirname_len;
825                 bp[-1] = '\0';
826         } else if (dirname)
827                 file->dirname = dirname;
828
829         file->basename = bp;
830         memcpy(bp, basename, basename_len);
831         bp += basename_len;
832
833 #ifdef HAVE_STRUCT_STAT_ST_RDEV
834         if (preserve_devices && IS_DEVICE(st.st_mode))
835                 file->u.rdev = st.st_rdev;
836 #endif
837
838 #if SUPPORT_LINKS
839         if (linkname_len) {
840                 file->u.link = bp;
841                 memcpy(bp, linkname, linkname_len);
842                 bp += linkname_len;
843         }
844 #endif
845
846 #if SUPPORT_HARD_LINKS
847         if (idev_len) {
848                 file->link_u.idev = (struct idev *)bp;
849                 bp += idev_len;
850                 file->F_DEV = st.st_dev;
851                 file->F_INODE = st.st_ino;
852         }
853 #endif
854
855         if (sum_len) {
856                 file->u.sum = bp;
857                 file_checksum(thisname, bp, st.st_size);
858                 /*bp += sum_len;*/
859         }
860
861         file->basedir = flist_dir;
862
863         if (!S_ISDIR(st.st_mode))
864                 stats.total_size += st.st_size;
865
866         return file;
867 }
868
869
870 void send_file_name(int f, struct file_list *flist, char *fname,
871                     int recursive, unsigned short base_flags)
872 {
873         struct file_struct *file;
874         char fbuf[MAXPATHLEN];
875         extern int delete_excluded;
876
877         /* f is set to -1 when calculating deletion file list */
878         file = make_file(fname,
879                          f == -1 && delete_excluded? SERVER_EXCLUDES
880                                                    : ALL_EXCLUDES);
881
882         if (!file)
883                 return;
884
885         maybe_emit_filelist_progress(flist);
886
887         flist_expand(flist);
888
889         if (write_batch)
890                 file->flags |= FLAG_TOP_DIR;
891
892         if (file->basename[0]) {
893                 flist->files[flist->count++] = file;
894                 send_file_entry(file, f, base_flags);
895         }
896
897         if (recursive && S_ISDIR(file->mode)
898             && !(file->flags & FLAG_MOUNT_POINT)) {
899                 struct exclude_struct **last_exclude_list = local_exclude_list;
900                 send_directory(f, flist, f_name_to(file, fbuf));
901                 local_exclude_list = last_exclude_list;
902                 return;
903         }
904 }
905
906
907 static void send_directory(int f, struct file_list *flist, char *dir)
908 {
909         DIR *d;
910         struct dirent *di;
911         char fname[MAXPATHLEN];
912         unsigned int offset;
913         char *p;
914
915         d = opendir(dir);
916         if (!d) {
917                 io_error |= IOERR_GENERAL;
918                 rprintf(FERROR, "opendir %s failed: %s\n",
919                         full_fname(dir), strerror(errno));
920                 return;
921         }
922
923         offset = strlcpy(fname, dir, MAXPATHLEN);
924         p = fname + offset;
925         if (offset >= MAXPATHLEN || p[-1] != '/') {
926                 if (offset >= MAXPATHLEN - 1) {
927                         io_error |= IOERR_GENERAL;
928                         rprintf(FERROR, "skipping long-named directory: %s\n",
929                                 full_fname(fname));
930                         closedir(d);
931                         return;
932                 }
933                 *p++ = '/';
934                 offset++;
935         }
936
937         local_exclude_list = NULL;
938
939         if (cvs_exclude) {
940                 if (strlcpy(p, ".cvsignore", MAXPATHLEN - offset)
941                     < MAXPATHLEN - offset)
942                         add_exclude_file(&local_exclude_list,fname,MISSING_OK,ADD_EXCLUDE);
943                 else {
944                         io_error |= IOERR_GENERAL;
945                         rprintf(FINFO,
946                                 "cannot cvs-exclude in long-named directory %s\n",
947                                 full_fname(fname));
948                 }
949         }
950
951         for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
952                 char *dname = d_name(di);
953                 if (dname[0] == '.' && (dname[1] == '\0'
954                     || (dname[1] == '.' && dname[2] == '\0')))
955                         continue;
956                 if (strlcpy(p, dname, MAXPATHLEN - offset) < MAXPATHLEN - offset)
957                         send_file_name(f, flist, fname, recurse, 0);
958                 else {
959                         io_error |= IOERR_GENERAL;
960                         rprintf(FINFO,
961                                 "cannot send long-named file %s\n",
962                                 full_fname(fname));
963                 }
964         }
965         if (errno) {
966                 io_error |= IOERR_GENERAL;
967                 rprintf(FERROR, "readdir(%s): (%d) %s\n",
968                         dir, errno, strerror(errno));
969         }
970
971         if (local_exclude_list)
972                 free_exclude_list(&local_exclude_list); /* Zeros pointer too */
973
974         closedir(d);
975 }
976
977
978 /**
979  * The delete_files() function in receiver.c sets f to -1 so that we just
980  * construct the file list in memory without sending it over the wire.  It
981  * also has the side-effect of ignoring user-excludes if delete_excluded
982  * is set (so that the delete list includes user-excluded files).
983  **/
984 struct file_list *send_file_list(int f, int argc, char *argv[])
985 {
986         int l;
987         STRUCT_STAT st;
988         char *p, *dir, olddir[sizeof curr_dir];
989         char lastpath[MAXPATHLEN] = "";
990         struct file_list *flist;
991         int64 start_write;
992         int use_ff_fd = 0;
993
994         if (show_filelist_p() && f != -1)
995                 start_filelist_progress("building file list");
996
997         start_write = stats.total_written;
998
999         flist = flist_new();
1000
1001         if (f != -1) {
1002                 io_start_buffering_out(f);
1003                 if (filesfrom_fd >= 0) {
1004                         if (argv[0] && !push_dir(argv[0])) {
1005                                 rprintf(FERROR, "push_dir %s failed: %s\n",
1006                                         full_fname(argv[0]), strerror(errno));
1007                                 exit_cleanup(RERR_FILESELECT);
1008                         }
1009                         use_ff_fd = 1;
1010                 }
1011         }
1012
1013         while (1) {
1014                 char fname2[MAXPATHLEN];
1015                 char *fname = fname2;
1016
1017                 if (use_ff_fd) {
1018                         if (read_filesfrom_line(filesfrom_fd, fname) == 0)
1019                                 break;
1020                         sanitize_path(fname, NULL);
1021                 } else {
1022                         if (argc-- == 0)
1023                                 break;
1024                         strlcpy(fname, *argv++, MAXPATHLEN);
1025                         if (sanitize_paths)
1026                                 sanitize_path(fname, NULL);
1027                 }
1028
1029                 l = strlen(fname);
1030                 if (fname[l - 1] == '/') {
1031                         if (l == 2 && fname[0] == '.') {
1032                                 /* Turn "./" into just "." rather than "./." */
1033                                 fname[1] = '\0';
1034                         } else if (l < MAXPATHLEN) {
1035                                 fname[l++] = '.';
1036                                 fname[l] = '\0';
1037                         }
1038                 }
1039
1040                 if (link_stat(fname, &st) != 0) {
1041                         if (f != -1) {
1042                                 io_error |= IOERR_GENERAL;
1043                                 rprintf(FERROR, "link_stat %s failed: %s\n",
1044                                         full_fname(fname), strerror(errno));
1045                         }
1046                         continue;
1047                 }
1048
1049                 if (S_ISDIR(st.st_mode) && !recurse && !files_from) {
1050                         rprintf(FINFO, "skipping directory %s\n", fname);
1051                         continue;
1052                 }
1053
1054                 dir = NULL;
1055                 olddir[0] = '\0';
1056
1057                 if (!relative_paths) {
1058                         p = strrchr(fname, '/');
1059                         if (p) {
1060                                 *p = 0;
1061                                 if (p == fname)
1062                                         dir = "/";
1063                                 else
1064                                         dir = fname;
1065                                 fname = p + 1;
1066                         }
1067                 } else if (f != -1 && implied_dirs && (p=strrchr(fname,'/')) && p != fname) {
1068                         /* this ensures we send the intermediate directories,
1069                            thus getting their permissions right */
1070                         char *lp = lastpath, *fn = fname, *slash = fname;
1071                         *p = 0;
1072                         /* Skip any initial directories in our path that we
1073                          * have in common with lastpath. */
1074                         while (*fn && *lp == *fn) {
1075                                 if (*fn == '/')
1076                                         slash = fn;
1077                                 lp++, fn++;
1078                         }
1079                         *p = '/';
1080                         if (fn != p || (*lp && *lp != '/')) {
1081                                 int copy_links_saved = copy_links;
1082                                 int recurse_saved = recurse;
1083                                 copy_links = copy_unsafe_links;
1084                                 /* set recurse to 1 to prevent make_file
1085                                  * from ignoring directory, but still
1086                                  * turn off the recursive parameter to
1087                                  * send_file_name */
1088                                 recurse = 1;
1089                                 while ((slash = strchr(slash+1, '/')) != 0) {
1090                                         *slash = 0;
1091                                         send_file_name(f, flist, fname, 0, 0);
1092                                         *slash = '/';
1093                                 }
1094                                 copy_links = copy_links_saved;
1095                                 recurse = recurse_saved;
1096                                 *p = 0;
1097                                 strlcpy(lastpath, fname, sizeof lastpath);
1098                                 *p = '/';
1099                         }
1100                 }
1101
1102                 if (!*fname)
1103                         fname = ".";
1104
1105                 if (dir && *dir) {
1106                         static char *lastdir;
1107                         static int lastdir_len;
1108
1109                         strcpy(olddir, curr_dir); /* can't overflow */
1110
1111                         if (!push_dir(dir)) {
1112                                 io_error |= IOERR_GENERAL;
1113                                 rprintf(FERROR, "push_dir %s failed: %s\n",
1114                                         full_fname(dir), strerror(errno));
1115                                 continue;
1116                         }
1117
1118                         if (lastdir && strcmp(lastdir, dir) == 0) {
1119                                 flist_dir = lastdir;
1120                                 flist_dir_len = lastdir_len;
1121                         } else {
1122                                 flist_dir = lastdir = strdup(dir);
1123                                 flist_dir_len = lastdir_len = strlen(dir);
1124                         }
1125                 }
1126
1127                 if (one_file_system)
1128                         set_filesystem(fname);
1129
1130                 send_file_name(f, flist, fname, recurse, XMIT_TOP_DIR);
1131
1132                 if (olddir[0]) {
1133                         flist_dir = NULL;
1134                         flist_dir_len = 0;
1135                         if (!pop_dir(olddir)) {
1136                                 rprintf(FERROR, "pop_dir %s failed: %s\n",
1137                                         full_fname(dir), strerror(errno));
1138                                 exit_cleanup(RERR_FILESELECT);
1139                         }
1140                 }
1141         }
1142
1143         if (f != -1) {
1144                 send_file_entry(NULL, f, 0);
1145
1146                 if (show_filelist_p())
1147                         finish_filelist_progress(flist);
1148         }
1149
1150         clean_flist(flist, 0, 0);
1151
1152         if (f != -1) {
1153                 /* Now send the uid/gid list. This was introduced in
1154                  * protocol version 15 */
1155                 send_uid_list(f);
1156
1157                 /* send the io_error flag */
1158                 write_int(f, lp_ignore_errors(module_id) ? 0 : io_error);
1159
1160                 io_end_buffering();
1161                 stats.flist_size = stats.total_written - start_write;
1162                 stats.num_files = flist->count;
1163                 if (write_batch)
1164                         write_batch_flist_info(flist->count, flist->files);
1165         }
1166
1167         if (verbose > 2)
1168                 rprintf(FINFO, "send_file_list done\n");
1169
1170         return flist;
1171 }
1172
1173
1174 struct file_list *recv_file_list(int f)
1175 {
1176         struct file_list *flist;
1177         unsigned short flags;
1178         int64 start_read;
1179         extern int list_only;
1180
1181         if (show_filelist_p())
1182                 start_filelist_progress("receiving file list");
1183
1184         start_read = stats.total_read;
1185
1186         flist = new(struct file_list);
1187         if (!flist)
1188                 goto oom;
1189
1190         flist->count = 0;
1191         flist->malloced = 1000;
1192         flist->files = new_array(struct file_struct *, flist->malloced);
1193         if (!flist->files)
1194                 goto oom;
1195
1196
1197         while ((flags = read_byte(f)) != 0) {
1198                 int i = flist->count;
1199
1200                 flist_expand(flist);
1201
1202                 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
1203                         flags |= read_byte(f) << 8;
1204                 receive_file_entry(&flist->files[i], flags, f);
1205
1206                 if (S_ISREG(flist->files[i]->mode))
1207                         stats.total_size += flist->files[i]->length;
1208
1209                 flist->count++;
1210
1211                 maybe_emit_filelist_progress(flist);
1212
1213                 if (verbose > 2) {
1214                         rprintf(FINFO, "recv_file_name(%s)\n",
1215                                 f_name(flist->files[i]));
1216                 }
1217         }
1218         receive_file_entry(NULL, 0, 0); /* Signal that we're done. */
1219
1220         if (verbose > 2)
1221                 rprintf(FINFO, "received %d names\n", flist->count);
1222
1223         if (show_filelist_p())
1224                 finish_filelist_progress(flist);
1225
1226         clean_flist(flist, relative_paths, 1);
1227
1228         if (f != -1) {
1229                 /* Now send the uid/gid list. This was introduced in
1230                  * protocol version 15 */
1231                 recv_uid_list(f, flist);
1232
1233                 if (!read_batch) {
1234                         /* Recv the io_error flag */
1235                         if (lp_ignore_errors(module_id) || ignore_errors)
1236                                 read_int(f);
1237                         else
1238                                 io_error |= read_int(f);
1239                 }
1240         }
1241
1242         if (list_only) {
1243                 int i;
1244                 for (i = 0; i < flist->count; i++)
1245                         list_file_entry(flist->files[i]);
1246         }
1247
1248         if (verbose > 2)
1249                 rprintf(FINFO, "recv_file_list done\n");
1250
1251         stats.flist_size = stats.total_read - start_read;
1252         stats.num_files = flist->count;
1253
1254         return flist;
1255
1256       oom:
1257         out_of_memory("recv_file_list");
1258         return NULL;            /* not reached */
1259 }
1260
1261
1262 int file_compare(struct file_struct **file1, struct file_struct **file2)
1263 {
1264         struct file_struct *f1 = *file1;
1265         struct file_struct *f2 = *file2;
1266
1267         if (!f1->basename && !f2->basename)
1268                 return 0;
1269         if (!f1->basename)
1270                 return -1;
1271         if (!f2->basename)
1272                 return 1;
1273         if (f1->dirname == f2->dirname)
1274                 return u_strcmp(f1->basename, f2->basename);
1275         return f_name_cmp(f1, f2);
1276 }
1277
1278
1279 int flist_find(struct file_list *flist, struct file_struct *f)
1280 {
1281         int low = 0, high = flist->count - 1;
1282
1283         while (high >= 0 && !flist->files[high]->basename) high--;
1284
1285         if (high < 0)
1286                 return -1;
1287
1288         while (low != high) {
1289                 int mid = (low + high) / 2;
1290                 int ret = file_compare(&flist->files[flist_up(flist, mid)],&f);
1291                 if (ret == 0)
1292                         return flist_up(flist, mid);
1293                 if (ret > 0)
1294                         high = mid;
1295                 else
1296                         low = mid + 1;
1297         }
1298
1299         if (file_compare(&flist->files[flist_up(flist, low)], &f) == 0)
1300                 return flist_up(flist, low);
1301         return -1;
1302 }
1303
1304
1305 /*
1306  * Free up any resources a file_struct has allocated, and optionally free
1307  * it up as well.
1308  */
1309 void free_file(struct file_struct *file, int free_the_struct)
1310 {
1311         if (free_the_struct)
1312                 free(file);
1313         else
1314                 memset(file, 0, sizeof file[0]);
1315 }
1316
1317
1318 /*
1319  * allocate a new file list
1320  */
1321 struct file_list *flist_new(void)
1322 {
1323         struct file_list *flist;
1324
1325         flist = new(struct file_list);
1326         if (!flist)
1327                 out_of_memory("send_file_list");
1328
1329         flist->count = 0;
1330         flist->malloced = 0;
1331         flist->files = NULL;
1332
1333         return flist;
1334 }
1335
1336 /*
1337  * free up all elements in a flist
1338  */
1339 void flist_free(struct file_list *flist)
1340 {
1341         int i;
1342         for (i = 1; i < flist->count; i++)
1343                 free_file(flist->files[i], FREE_STRUCT);
1344         free(flist->files);
1345         free(flist);
1346 }
1347
1348
1349 /*
1350  * This routine ensures we don't have any duplicate names in our file list.
1351  * duplicate names can cause corruption because of the pipelining
1352  */
1353 static void clean_flist(struct file_list *flist, int strip_root, int no_dups)
1354 {
1355         int i, prev_i = 0;
1356
1357         if (!flist || flist->count == 0)
1358                 return;
1359
1360         qsort(flist->files, flist->count,
1361               sizeof(flist->files[0]), (int (*)()) file_compare);
1362
1363         for (i = no_dups? 0 : flist->count; i < flist->count; i++) {
1364                 if (flist->files[i]->basename) {
1365                         prev_i = i;
1366                         break;
1367                 }
1368         }
1369         while (++i < flist->count) {
1370                 if (!flist->files[i]->basename)
1371                         continue;
1372                 if (f_name_cmp(flist->files[i], flist->files[prev_i]) == 0) {
1373                         if (verbose > 1 && !am_server) {
1374                                 rprintf(FINFO,
1375                                         "removing duplicate name %s from file list %d\n",
1376                                         f_name(flist->files[i]), i);
1377                         }
1378                         /* Make sure that if we unduplicate '.', that we don't
1379                          * lose track of a user-specified starting point (or
1380                          * else deletions will mysteriously fail with -R). */
1381                         if (flist->files[i]->flags & FLAG_TOP_DIR)
1382                                 flist->files[prev_i]->flags |= FLAG_TOP_DIR;
1383                         free_file(flist->files[i], CLEAR_STRUCT);
1384                 } else
1385                         prev_i = i;
1386         }
1387
1388         if (strip_root) {
1389                 /* we need to strip off the root directory in the case
1390                    of relative paths, but this must be done _after_
1391                    the sorting phase */
1392                 for (i = 0; i < flist->count; i++) {
1393                         if (flist->files[i]->dirname &&
1394                             flist->files[i]->dirname[0] == '/') {
1395                                 memmove(&flist->files[i]->dirname[0],
1396                                         &flist->files[i]->dirname[1],
1397                                         strlen(flist->files[i]->dirname));
1398                         }
1399
1400                         if (flist->files[i]->dirname &&
1401                             !flist->files[i]->dirname[0]) {
1402                                 flist->files[i]->dirname = NULL;
1403                         }
1404                 }
1405         }
1406
1407         if (verbose <= 3)
1408                 return;
1409
1410         for (i = 0; i < flist->count; i++) {
1411                 rprintf(FINFO, "[%s] i=%d %s %s %s mode=0%o len=%.0f\n",
1412                         who_am_i(), i,
1413                         NS(flist->files[i]->basedir),
1414                         NS(flist->files[i]->dirname),
1415                         NS(flist->files[i]->basename),
1416                         (int) flist->files[i]->mode,
1417                         (double) flist->files[i]->length);
1418         }
1419 }
1420
1421
1422 enum fnc_state { fnc_DIR, fnc_SLASH, fnc_BASE };
1423
1424 /* Compare the names of two file_struct entities, just like strcmp()
1425  * would do if it were operating on the joined strings.  We assume
1426  * that there are no 0-length strings.
1427  */
1428 int f_name_cmp(struct file_struct *f1, struct file_struct *f2)
1429 {
1430         int dif;
1431         const uchar *c1, *c2;
1432         enum fnc_state state1, state2;
1433
1434         if (!f1 || !f1->basename) {
1435                 if (!f2 || !f2->basename)
1436                         return 0;
1437                 return -1;
1438         }
1439         if (!f2 || !f2->basename)
1440                 return 1;
1441
1442         if (!(c1 = (uchar*)f1->dirname)) {
1443                 state1 = fnc_BASE;
1444                 c1 = (uchar*)f1->basename;
1445         } else
1446                 state1 = fnc_DIR;
1447         if (!(c2 = (uchar*)f2->dirname)) {
1448                 state2 = fnc_BASE;
1449                 c2 = (uchar*)f2->basename;
1450         } else
1451                 state2 = fnc_DIR;
1452
1453         while (1) {
1454                 if ((dif = (int)*c1 - (int)*c2) != 0)
1455                         break;
1456                 if (!*++c1) {
1457                         switch (state1) {
1458                         case fnc_DIR:
1459                                 state1 = fnc_SLASH;
1460                                 c1 = (uchar*)"/";
1461                                 break;
1462                         case fnc_SLASH:
1463                                 state1 = fnc_BASE;
1464                                 c1 = (uchar*)f1->basename;
1465                                 break;
1466                         case fnc_BASE:
1467                                 break;
1468                         }
1469                 }
1470                 if (!*++c2) {
1471                         switch (state2) {
1472                         case fnc_DIR:
1473                                 state2 = fnc_SLASH;
1474                                 c2 = (uchar*)"/";
1475                                 break;
1476                         case fnc_SLASH:
1477                                 state2 = fnc_BASE;
1478                                 c2 = (uchar*)f2->basename;
1479                                 break;
1480                         case fnc_BASE:
1481                                 if (!*c1)
1482                                         return 0;
1483                                 break;
1484                         }
1485                 }
1486         }
1487
1488         return dif;
1489 }
1490
1491
1492 /* Return a copy of the full filename of a flist entry, using the indicated
1493  * buffer.  No size-checking is done because we checked the size when creating
1494  * the file_struct entry.
1495  */
1496 char *f_name_to(struct file_struct *f, char *fbuf)
1497 {
1498         if (!f || !f->basename)
1499                 return NULL;
1500
1501         if (f->dirname) {
1502                 int len = strlen(f->dirname);
1503                 memcpy(fbuf, f->dirname, len);
1504                 fbuf[len] = '/';
1505                 strcpy(fbuf + len + 1, f->basename);
1506         } else
1507                 strcpy(fbuf, f->basename);
1508         return fbuf;
1509 }
1510
1511
1512 /* Like f_name_to(), but we rotate through 5 static buffers of our own.
1513  */
1514 char *f_name(struct file_struct *f)
1515 {
1516         static char names[5][MAXPATHLEN];
1517         static unsigned int n;
1518
1519         n = (n + 1) % (sizeof names / sizeof names[0]);
1520
1521         return f_name_to(f, names[n]);
1522 }