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