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