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