Don't free lastdir! It is still needed by the flist basedir pointers.
[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 |= XMIT_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 to simplify the test
398                                          * when writing the data. */
399                                         flags |= XMIT_SAME_RDEV_pre28
400                                                | XMIT_SAME_HIGH_RDEV;
401                                 } else
402                                         rdev = file->u.rdev;
403                         } else
404                                 rdev = 0;
405                 } else if (IS_DEVICE(mode)) {
406                         if ((file->u.rdev & ~0xFF) == rdev)
407                                 flags |= XMIT_SAME_HIGH_RDEV;
408                         else
409                                 rdev = file->u.rdev & ~0xFF;
410                 }
411         }
412         if (file->uid == uid)
413                 flags |= XMIT_SAME_UID;
414         else
415                 uid = file->uid;
416         if (file->gid == gid)
417                 flags |= XMIT_SAME_GID;
418         else
419                 gid = file->gid;
420         if (file->modtime == modtime)
421                 flags |= XMIT_SAME_TIME;
422         else
423                 modtime = file->modtime;
424         if (file->link_u.idev) {
425                 if (file->F_DEV == dev) {
426                         if (protocol_version >= 28)
427                                 flags |= XMIT_SAME_DEV;
428                 } else
429                         dev = file->F_DEV;
430                 flags |= XMIT_HAS_IDEV_DATA;
431         }
432
433         for (l1 = 0;
434              lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
435              l1++) {}
436         l2 = strlen(fname+l1);
437
438         if (l1 > 0)
439                 flags |= XMIT_SAME_NAME;
440         if (l2 > 255)
441                 flags |= XMIT_LONG_NAME;
442
443         /* We must make sure we don't send a zero flags byte or
444          * the other end will terminate the flist transfer. */
445         if (flags == 0 && !S_ISDIR(mode))
446                 flags |= XMIT_TOP_DIR; /* NOTE: no meaning for non-dir */
447         if (protocol_version >= 28) {
448                 if ((flags & 0xFF00) || flags == 0) {
449                         flags |= XMIT_EXTENDED_FLAGS;
450                         write_byte(f, flags);
451                         write_byte(f, flags >> 8);
452                 } else
453                         write_byte(f, flags);
454         } else {
455                 if (flags == 0)
456                         flags |= XMIT_LONG_NAME;
457                 write_byte(f, flags);
458         }
459         if (flags & XMIT_SAME_NAME)
460                 write_byte(f, l1);
461         if (flags & XMIT_LONG_NAME)
462                 write_int(f, l2);
463         else
464                 write_byte(f, l2);
465         write_buf(f, fname + l1, l2);
466
467         write_longint(f, file->length);
468         if (!(flags & XMIT_SAME_TIME))
469                 write_int(f, modtime);
470         if (!(flags & XMIT_SAME_MODE))
471                 write_int(f, to_wire_mode(mode));
472         if (preserve_uid && !(flags & XMIT_SAME_UID)) {
473                 add_uid(uid);
474                 write_int(f, uid);
475         }
476         if (preserve_gid && !(flags & XMIT_SAME_GID)) {
477                 add_gid(gid);
478                 write_int(f, gid);
479         }
480         if (preserve_devices && IS_DEVICE(mode)) {
481                 /* If XMIT_SAME_HIGH_RDEV is off, XMIT_SAME_RDEV_pre28 is
482                  * also off.  Also, avoid using "rdev" because it may be
483                  * incomplete. */
484                 if (!(flags & XMIT_SAME_HIGH_RDEV))
485                         write_int(f, file->u.rdev);
486                 else if (protocol_version >= 28)
487                         write_byte(f, file->u.rdev);
488         }
489
490 #if SUPPORT_LINKS
491         if (preserve_links && S_ISLNK(mode)) {
492                 write_int(f, strlen(file->u.link));
493                 write_buf(f, file->u.link, strlen(file->u.link));
494         }
495 #endif
496
497 #if SUPPORT_HARD_LINKS
498         if (flags & XMIT_HAS_IDEV_DATA) {
499                 if (protocol_version < 26) {
500                         /* 32-bit dev_t and ino_t */
501                         write_int(f, dev);
502                         write_int(f, file->F_INODE);
503                 } else {
504                         /* 64-bit dev_t and ino_t */
505                         if (!(flags & XMIT_SAME_DEV))
506                                 write_longint(f, dev);
507                         write_longint(f, file->F_INODE);
508                 }
509         }
510 #endif
511
512         if (always_checksum) {
513                 char *sum;
514                 if (S_ISREG(mode))
515                         sum = file->u.sum;
516                 else if (protocol_version < 28) {
517                         /* Prior to 28, we sent a useless set of nulls. */
518                         sum = empty_sum;
519                 } else
520                         sum = NULL;
521                 if (sum) {
522                         write_buf(f, sum, protocol_version < 21? 2
523                                                         : MD4_SUM_LENGTH);
524                 }
525         }
526
527         strlcpy(lastname, fname, MAXPATHLEN);
528
529         io_write_phase = "unknown";
530 }
531
532
533
534 void receive_file_entry(struct file_struct **fptr, unsigned short flags, int f)
535 {
536         static time_t modtime;
537         static mode_t mode;
538         static DEV64_T rdev;    /* just high bytes in p28 onward */
539         static DEV64_T dev;
540         static uid_t uid;
541         static gid_t gid;
542         static char lastname[MAXPATHLEN];
543         char thisname[MAXPATHLEN];
544         unsigned int l1 = 0, l2 = 0;
545         char *p;
546         struct file_struct *file;
547
548         if (!fptr) {
549                 modtime = 0, mode = 0;
550                 rdev = 0, dev = 0;
551                 uid = 0, gid = 0;
552                 *lastname = '\0';
553                 return;
554         }
555
556         if (flags & XMIT_SAME_NAME)
557                 l1 = read_byte(f);
558
559         if (flags & XMIT_LONG_NAME)
560                 l2 = read_int(f);
561         else
562                 l2 = read_byte(f);
563
564         file = new(struct file_struct);
565         if (!file)
566                 out_of_memory("receive_file_entry");
567         memset((char *) file, 0, sizeof(*file));
568         (*fptr) = file;
569
570         if (l2 >= MAXPATHLEN - l1) {
571                 rprintf(FERROR,
572                         "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n",
573                         flags, l1, l2, lastname);
574                 overflow("receive_file_entry");
575         }
576
577         strlcpy(thisname, lastname, l1 + 1);
578         read_sbuf(f, &thisname[l1], l2);
579         thisname[l1 + l2] = 0;
580
581         strlcpy(lastname, thisname, MAXPATHLEN);
582         lastname[MAXPATHLEN - 1] = 0;
583
584         clean_fname(thisname);
585
586         if (sanitize_paths) {
587                 sanitize_path(thisname, NULL);
588         }
589
590         if ((p = strrchr(thisname, '/'))) {
591                 static char *lastdir;
592                 *p = 0;
593                 if (lastdir && strcmp(thisname, lastdir) == 0)
594                         file->dirname = lastdir;
595                 else {
596                         file->dirname = strdup(thisname);
597                         lastdir = file->dirname;
598                 }
599                 file->basename = strdup(p + 1);
600         } else {
601                 file->dirname = NULL;
602                 file->basename = strdup(thisname);
603         }
604
605         if (!file->basename)
606                 out_of_memory("receive_file_entry 1");
607
608         file->flags = flags & XMIT_TOP_DIR ? FLAG_TOP_DIR : 0;
609         file->length = read_longint(f);
610         if (!(flags & XMIT_SAME_TIME))
611                 modtime = (time_t)read_int(f);
612         file->modtime = modtime;
613         if (!(flags & XMIT_SAME_MODE))
614                 mode = from_wire_mode(read_int(f));
615         file->mode = mode;
616
617         if (preserve_uid) {
618                 if (!(flags & XMIT_SAME_UID))
619                         uid = (uid_t)read_int(f);
620                 file->uid = uid;
621         }
622         if (preserve_gid) {
623                 if (!(flags & XMIT_SAME_GID))
624                         gid = (gid_t)read_int(f);
625                 file->gid = gid;
626         }
627         if (preserve_devices) {
628                 if (protocol_version < 28) {
629                         if (IS_DEVICE(mode)) {
630                                 if (!(flags & XMIT_SAME_RDEV_pre28))
631                                         rdev = (DEV64_T)read_int(f);
632                                 file->u.rdev = rdev;
633                         } else
634                                 rdev = 0;
635                 } else if (IS_DEVICE(mode)) {
636                         if (!(flags & XMIT_SAME_HIGH_RDEV)) {
637                                 file->u.rdev = (DEV64_T)read_int(f);
638                                 rdev = file->u.rdev & ~0xFF;
639                         } else
640                                 file->u.rdev = rdev | (DEV64_T)read_byte(f);
641                 }
642         }
643
644         if (preserve_links && S_ISLNK(mode)) {
645                 int l = read_int(f);
646                 if (l < 0) {
647                         rprintf(FERROR, "overflow: l=%d\n", l);
648                         overflow("receive_file_entry");
649                 }
650                 if (!(file->u.link = new_array(char, l + 1)))
651                         out_of_memory("receive_file_entry 2");
652                 read_sbuf(f, file->u.link, l);
653                 if (sanitize_paths)
654                         sanitize_path(file->u.link, file->dirname);
655         }
656 #if SUPPORT_HARD_LINKS
657         if (preserve_hard_links && protocol_version < 28 && S_ISREG(mode))
658                 flags |= XMIT_HAS_IDEV_DATA;
659         if (flags & XMIT_HAS_IDEV_DATA) {
660                 if (!(file->link_u.idev = new(struct idev)))
661                         out_of_memory("file inode data");
662                 if (protocol_version < 26) {
663                         dev = read_int(f);
664                         file->F_INODE = read_int(f);
665                 } else {
666                         if (!(flags & XMIT_SAME_DEV))
667                                 dev = read_longint(f);
668                         file->F_INODE = read_longint(f);
669                 }
670                 file->F_DEV = dev;
671         }
672 #endif
673
674         if (always_checksum) {
675                 char *sum;
676                 if (S_ISREG(mode)) {
677                         sum = file->u.sum = new_array(char, MD4_SUM_LENGTH);
678                         if (!sum)
679                                 out_of_memory("md4 sum");
680                 } else if (protocol_version < 28) {
681                         /* Prior to 28, we get a useless set of nulls. */
682                         sum = empty_sum;
683                 } else
684                         sum = NULL;
685                 if (sum) {
686                         read_buf(f, sum, protocol_version < 21? 2
687                                                 : MD4_SUM_LENGTH);
688                 }
689         }
690
691         if (!preserve_perms) {
692                 extern int orig_umask;
693                 /* set an appropriate set of permissions based on original
694                  * permissions and umask. This emulates what GNU cp does */
695                 file->mode &= ~orig_umask;
696         }
697 }
698
699
700 #define STRDUP(ap, p)   (ap ? string_area_strdup(ap, p) : strdup(p))
701 /* IRIX cc cares that the operands to the ternary have the same type. */
702 #define MALLOC(ap, i)   (ap ? (void*) string_area_malloc(ap, i) : malloc(i))
703
704 /**
705  * Create a file_struct for a named file by reading its stat()
706  * information and performing extensive checks against global
707  * options.
708  *
709  * @return the new file, or NULL if there was an error or this file
710  * should be excluded.
711  *
712  * @todo There is a small optimization opportunity here to avoid
713  * stat()ing the file in some circumstances, which has a certain cost.
714  * We are called immediately after doing readdir(), and so we may
715  * already know the d_type of the file.  We could for example avoid
716  * statting directories if we're not recursing, but this is not a very
717  * important case.  Some systems may not have d_type.
718  **/
719 struct file_struct *make_file(char *fname, struct string_area **ap,
720                               int exclude_level)
721 {
722         struct file_struct *file;
723         STRUCT_STAT st;
724         char sum[SUM_LENGTH];
725         char *p;
726         char cleaned_name[MAXPATHLEN];
727         char linkbuf[MAXPATHLEN];
728         unsigned short flags = 0;
729
730         if (strlcpy(cleaned_name, fname, sizeof cleaned_name)
731             >= sizeof cleaned_name - flist_dir_len) {
732                 rprintf(FINFO, "skipping overly long name: %s\n", fname);
733                 return NULL;
734         }
735         clean_fname(cleaned_name);
736         if (sanitize_paths)
737                 sanitize_path(cleaned_name, NULL);
738         fname = cleaned_name;
739
740         memset(sum, 0, SUM_LENGTH);
741
742         if (readlink_stat(fname, &st, linkbuf) != 0) {
743                 int save_errno = errno;
744                 if (errno == ENOENT && exclude_level != NO_EXCLUDES) {
745                         /* either symlink pointing nowhere or file that
746                          * was removed during rsync run; see if excluded
747                          * before reporting an error */
748                         if (check_exclude_file(fname, 0, exclude_level)) {
749                                 /* file is excluded anyway, ignore silently */
750                                 return NULL;
751                         }
752                 }
753                 io_error |= IOERR_GENERAL;
754                 rprintf(FERROR, "readlink %s failed: %s\n",
755                         full_fname(fname), strerror(save_errno));
756                 return NULL;
757         }
758
759         /* backup.c calls us with exclude_level set to NO_EXCLUDES. */
760         if (exclude_level == NO_EXCLUDES)
761                 goto skip_excludes;
762
763         if (S_ISDIR(st.st_mode) && !recurse && !files_from) {
764                 rprintf(FINFO, "skipping directory %s\n", fname);
765                 return NULL;
766         }
767
768         if (one_file_system && st.st_dev != filesystem_dev) {
769                 /* We allow a directory though to preserve the mount point.
770                  * However, flag it so that we don't recurse. */
771                 if (!S_ISDIR(st.st_mode))
772                         return NULL;
773                 flags |= FLAG_MOUNT_POINT;
774         }
775
776         if (check_exclude_file(fname, S_ISDIR(st.st_mode) != 0, exclude_level))
777                 return NULL;
778
779         if (lp_ignore_nonreadable(module_id) && access(fname, R_OK) != 0)
780                 return NULL;
781
782       skip_excludes:
783
784         if (verbose > 2)
785                 rprintf(FINFO, "make_file(%s,*,%d)\n", fname, exclude_level);
786
787         file = new(struct file_struct);
788         if (!file)
789                 out_of_memory("make_file");
790         memset((char *) file, 0, sizeof(*file));
791         file->flags = flags;
792
793         if ((p = strrchr(fname, '/'))) {
794                 static char *lastdir;
795                 *p = 0;
796                 if (lastdir && strcmp(fname, lastdir) == 0)
797                         file->dirname = lastdir;
798                 else {
799                         file->dirname = strdup(fname);
800                         lastdir = file->dirname;
801                 }
802                 file->basename = STRDUP(ap, p + 1);
803                 *p = '/';
804         } else {
805                 file->dirname = NULL;
806                 file->basename = STRDUP(ap, fname);
807         }
808
809         file->modtime = st.st_mtime;
810         file->length = st.st_size;
811         file->mode = st.st_mode;
812         file->uid = st.st_uid;
813         file->gid = st.st_gid;
814         if (preserve_hard_links) {
815                 if (protocol_version < 28 ? S_ISREG(st.st_mode)
816                     : !S_ISDIR(st.st_mode) && st.st_nlink > 1) {
817                         if (!(file->link_u.idev = new(struct idev)))
818                                 out_of_memory("file inode data");
819                         file->F_DEV = st.st_dev;
820                         file->F_INODE = st.st_ino;
821                 }
822         }
823 #ifdef HAVE_STRUCT_STAT_ST_RDEV
824         if (IS_DEVICE(st.st_mode))
825                 file->u.rdev = st.st_rdev;
826 #endif
827
828 #if SUPPORT_LINKS
829         if (S_ISLNK(st.st_mode))
830                 file->u.link = STRDUP(ap, linkbuf);
831 #endif
832
833         if (always_checksum && S_ISREG(st.st_mode)) {
834                 if (!(file->u.sum = (char*)MALLOC(ap, MD4_SUM_LENGTH)))
835                         out_of_memory("md4 sum");
836                 file_checksum(fname, file->u.sum, st.st_size);
837         }
838
839         file->basedir = flist_dir;
840
841         if (!S_ISDIR(st.st_mode))
842                 stats.total_size += st.st_size;
843
844         return file;
845 }
846
847
848 void send_file_name(int f, struct file_list *flist, char *fname,
849                     int recursive, unsigned short base_flags)
850 {
851         struct file_struct *file;
852         char fbuf[MAXPATHLEN];
853         extern int delete_excluded;
854
855         /* f is set to -1 when calculating deletion file list */
856         file = make_file(fname, &flist->string_area,
857                          f == -1 && delete_excluded? SERVER_EXCLUDES
858                                                    : ALL_EXCLUDES);
859
860         if (!file)
861                 return;
862
863         maybe_emit_filelist_progress(flist);
864
865         flist_expand(flist);
866
867         if (write_batch)
868                 file->flags |= FLAG_TOP_DIR;
869
870         if (file->basename[0]) {
871                 flist->files[flist->count++] = file;
872                 send_file_entry(file, f, base_flags);
873         }
874
875         if (recursive && S_ISDIR(file->mode)
876             && !(file->flags & FLAG_MOUNT_POINT)) {
877                 struct exclude_struct **last_exclude_list = local_exclude_list;
878                 send_directory(f, flist, f_name_to(file, fbuf));
879                 local_exclude_list = last_exclude_list;
880                 return;
881         }
882 }
883
884
885 static void send_directory(int f, struct file_list *flist, char *dir)
886 {
887         DIR *d;
888         struct dirent *di;
889         char fname[MAXPATHLEN];
890         unsigned int offset;
891         char *p;
892
893         d = opendir(dir);
894         if (!d) {
895                 io_error |= IOERR_GENERAL;
896                 rprintf(FERROR, "opendir %s failed: %s\n",
897                         full_fname(dir), strerror(errno));
898                 return;
899         }
900
901         offset = strlcpy(fname, dir, MAXPATHLEN);
902         p = fname + offset;
903         if (offset >= MAXPATHLEN || p[-1] != '/') {
904                 if (offset >= MAXPATHLEN - 1) {
905                         io_error |= IOERR_GENERAL;
906                         rprintf(FERROR, "skipping long-named directory: %s\n",
907                                 full_fname(fname));
908                         closedir(d);
909                         return;
910                 }
911                 *p++ = '/';
912                 offset++;
913         }
914
915         local_exclude_list = NULL;
916
917         if (cvs_exclude) {
918                 if (strlcpy(p, ".cvsignore", MAXPATHLEN - offset)
919                     < MAXPATHLEN - offset)
920                         add_exclude_file(&local_exclude_list,fname,MISSING_OK,ADD_EXCLUDE);
921                 else {
922                         io_error |= IOERR_GENERAL;
923                         rprintf(FINFO,
924                                 "cannot cvs-exclude in long-named directory %s\n",
925                                 full_fname(fname));
926                 }
927         }
928
929         for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
930                 char *dname = d_name(di);
931                 if (dname[0] == '.' && (dname[1] == '\0'
932                     || (dname[1] == '.' && dname[2] == '\0')))
933                         continue;
934                 if (strlcpy(p, dname, MAXPATHLEN - offset) < MAXPATHLEN - offset)
935                         send_file_name(f, flist, fname, recurse, 0);
936                 else {
937                         io_error |= IOERR_GENERAL;
938                         rprintf(FINFO,
939                                 "cannot send long-named file %s\n",
940                                 full_fname(fname));
941                 }
942         }
943         if (errno) {
944                 io_error |= IOERR_GENERAL;
945                 rprintf(FERROR, "readdir(%s): (%d) %s\n",
946                         dir, errno, strerror(errno));
947         }
948
949         if (local_exclude_list)
950                 free_exclude_list(&local_exclude_list); /* Zeros pointer too */
951
952         closedir(d);
953 }
954
955
956 /**
957  * The delete_files() function in receiver.c sets f to -1 so that we just
958  * construct the file list in memory without sending it over the wire.  It
959  * also has the side-effect of ignoring user-excludes if delete_excluded
960  * is set (so that the delete list includes user-excluded files).
961  **/
962 struct file_list *send_file_list(int f, int argc, char *argv[])
963 {
964         int l;
965         STRUCT_STAT st;
966         char *p, *dir, olddir[sizeof curr_dir];
967         char lastpath[MAXPATHLEN] = "";
968         struct file_list *flist;
969         int64 start_write;
970         int use_ff_fd = 0;
971
972         if (show_filelist_p() && f != -1)
973                 start_filelist_progress("building file list");
974
975         start_write = stats.total_written;
976
977         flist = flist_new();
978
979         if (f != -1) {
980                 io_start_buffering_out(f);
981                 if (filesfrom_fd >= 0) {
982                         if (argv[0] && !push_dir(argv[0])) {
983                                 rprintf(FERROR, "push_dir %s failed: %s\n",
984                                         full_fname(argv[0]), strerror(errno));
985                                 exit_cleanup(RERR_FILESELECT);
986                         }
987                         use_ff_fd = 1;
988                 }
989         }
990
991         while (1) {
992                 char fname2[MAXPATHLEN];
993                 char *fname = fname2;
994
995                 if (use_ff_fd) {
996                         if (read_filesfrom_line(filesfrom_fd, fname) == 0)
997                                 break;
998                         sanitize_path(fname, NULL);
999                 } else {
1000                         if (argc-- == 0)
1001                                 break;
1002                         strlcpy(fname, *argv++, MAXPATHLEN);
1003                         if (sanitize_paths)
1004                                 sanitize_path(fname, NULL);
1005                 }
1006
1007                 l = strlen(fname);
1008                 if (fname[l - 1] == '/') {
1009                         if (l == 2 && fname[0] == '.') {
1010                                 /* Turn "./" into just "." rather than "./." */
1011                                 fname[1] = '\0';
1012                         } else if (l < MAXPATHLEN) {
1013                                 fname[l++] = '.';
1014                                 fname[l] = '\0';
1015                         }
1016                 }
1017
1018                 if (link_stat(fname, &st) != 0) {
1019                         if (f != -1) {
1020                                 io_error |= IOERR_GENERAL;
1021                                 rprintf(FERROR, "link_stat %s failed: %s\n",
1022                                         full_fname(fname), strerror(errno));
1023                         }
1024                         continue;
1025                 }
1026
1027                 if (S_ISDIR(st.st_mode) && !recurse && !files_from) {
1028                         rprintf(FINFO, "skipping directory %s\n", fname);
1029                         continue;
1030                 }
1031
1032                 dir = NULL;
1033                 olddir[0] = '\0';
1034
1035                 if (!relative_paths) {
1036                         p = strrchr(fname, '/');
1037                         if (p) {
1038                                 *p = 0;
1039                                 if (p == fname)
1040                                         dir = "/";
1041                                 else
1042                                         dir = fname;
1043                                 fname = p + 1;
1044                         }
1045                 } else if (f != -1 && implied_dirs && (p=strrchr(fname,'/')) && p != fname) {
1046                         /* this ensures we send the intermediate directories,
1047                            thus getting their permissions right */
1048                         char *lp = lastpath, *fn = fname, *slash = fname;
1049                         *p = 0;
1050                         /* Skip any initial directories in our path that we
1051                          * have in common with lastpath. */
1052                         while (*fn && *lp == *fn) {
1053                                 if (*fn == '/')
1054                                         slash = fn;
1055                                 lp++, fn++;
1056                         }
1057                         *p = '/';
1058                         if (fn != p || (*lp && *lp != '/')) {
1059                                 int copy_links_saved = copy_links;
1060                                 int recurse_saved = recurse;
1061                                 copy_links = copy_unsafe_links;
1062                                 /* set recurse to 1 to prevent make_file
1063                                  * from ignoring directory, but still
1064                                  * turn off the recursive parameter to
1065                                  * send_file_name */
1066                                 recurse = 1;
1067                                 while ((slash = strchr(slash+1, '/')) != 0) {
1068                                         *slash = 0;
1069                                         send_file_name(f, flist, fname, 0, 0);
1070                                         *slash = '/';
1071                                 }
1072                                 copy_links = copy_links_saved;
1073                                 recurse = recurse_saved;
1074                                 *p = 0;
1075                                 strlcpy(lastpath, fname, sizeof lastpath);
1076                                 *p = '/';
1077                         }
1078                 }
1079
1080                 if (!*fname)
1081                         fname = ".";
1082
1083                 if (dir && *dir) {
1084                         static char *lastdir;
1085                         static int lastdir_len;
1086
1087                         strcpy(olddir, curr_dir); /* can't overflow */
1088
1089                         if (!push_dir(dir)) {
1090                                 io_error |= IOERR_GENERAL;
1091                                 rprintf(FERROR, "push_dir %s failed: %s\n",
1092                                         full_fname(dir), strerror(errno));
1093                                 continue;
1094                         }
1095
1096                         if (lastdir && strcmp(lastdir, dir) == 0) {
1097                                 flist_dir = lastdir;
1098                                 flist_dir_len = lastdir_len;
1099                         } else {
1100                                 flist_dir = lastdir = strdup(dir);
1101                                 flist_dir_len = lastdir_len = strlen(dir);
1102                         }
1103                 }
1104
1105                 if (one_file_system)
1106                         set_filesystem(fname);
1107
1108                 send_file_name(f, flist, fname, recurse, XMIT_TOP_DIR);
1109
1110                 if (olddir[0]) {
1111                         flist_dir = NULL;
1112                         flist_dir_len = 0;
1113                         if (!pop_dir(olddir)) {
1114                                 rprintf(FERROR, "pop_dir %s failed: %s\n",
1115                                         full_fname(dir), strerror(errno));
1116                                 exit_cleanup(RERR_FILESELECT);
1117                         }
1118                 }
1119         }
1120
1121         if (f != -1) {
1122                 send_file_entry(NULL, f, 0);
1123
1124                 if (show_filelist_p())
1125                         finish_filelist_progress(flist);
1126         }
1127
1128         clean_flist(flist, 0, 0);
1129
1130         if (f != -1) {
1131                 /* Now send the uid/gid list. This was introduced in
1132                  * protocol version 15 */
1133                 send_uid_list(f);
1134
1135                 /* send the io_error flag */
1136                 write_int(f, lp_ignore_errors(module_id) ? 0 : io_error);
1137
1138                 io_end_buffering();
1139                 stats.flist_size = stats.total_written - start_write;
1140                 stats.num_files = flist->count;
1141                 if (write_batch)
1142                         write_batch_flist_info(flist->count, flist->files);
1143         }
1144
1145         if (verbose > 2)
1146                 rprintf(FINFO, "send_file_list done\n");
1147
1148         return flist;
1149 }
1150
1151
1152 struct file_list *recv_file_list(int f)
1153 {
1154         struct file_list *flist;
1155         unsigned short flags;
1156         int64 start_read;
1157         extern int list_only;
1158
1159         if (show_filelist_p())
1160                 start_filelist_progress("receiving file list");
1161
1162         start_read = stats.total_read;
1163
1164         flist = new(struct file_list);
1165         if (!flist)
1166                 goto oom;
1167
1168         flist->count = 0;
1169         flist->malloced = 1000;
1170         flist->files = new_array(struct file_struct *, flist->malloced);
1171         if (!flist->files)
1172                 goto oom;
1173
1174
1175         while ((flags = read_byte(f)) != 0) {
1176                 int i = flist->count;
1177
1178                 flist_expand(flist);
1179
1180                 if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
1181                         flags |= read_byte(f) << 8;
1182                 receive_file_entry(&flist->files[i], flags, f);
1183
1184                 if (S_ISREG(flist->files[i]->mode))
1185                         stats.total_size += flist->files[i]->length;
1186
1187                 flist->count++;
1188
1189                 maybe_emit_filelist_progress(flist);
1190
1191                 if (verbose > 2) {
1192                         rprintf(FINFO, "recv_file_name(%s)\n",
1193                                 f_name(flist->files[i]));
1194                 }
1195         }
1196         receive_file_entry(NULL, 0, 0); /* Signal that we're done. */
1197
1198         if (verbose > 2)
1199                 rprintf(FINFO, "received %d names\n", flist->count);
1200
1201         if (show_filelist_p())
1202                 finish_filelist_progress(flist);
1203
1204         clean_flist(flist, relative_paths, 1);
1205
1206         if (f != -1) {
1207                 /* Now send the uid/gid list. This was introduced in
1208                  * protocol version 15 */
1209                 recv_uid_list(f, flist);
1210
1211                 if (!read_batch) {
1212                         /* Recv the io_error flag */
1213                         if (lp_ignore_errors(module_id) || ignore_errors)
1214                                 read_int(f);
1215                         else
1216                                 io_error |= read_int(f);
1217                 }
1218         }
1219
1220         if (list_only) {
1221                 int i;
1222                 for (i = 0; i < flist->count; i++)
1223                         list_file_entry(flist->files[i]);
1224         }
1225
1226         if (verbose > 2)
1227                 rprintf(FINFO, "recv_file_list done\n");
1228
1229         stats.flist_size = stats.total_read - start_read;
1230         stats.num_files = flist->count;
1231
1232         return flist;
1233
1234       oom:
1235         out_of_memory("recv_file_list");
1236         return NULL;            /* not reached */
1237 }
1238
1239
1240 int file_compare(struct file_struct **file1, struct file_struct **file2)
1241 {
1242         struct file_struct *f1 = *file1;
1243         struct file_struct *f2 = *file2;
1244
1245         if (!f1->basename && !f2->basename)
1246                 return 0;
1247         if (!f1->basename)
1248                 return -1;
1249         if (!f2->basename)
1250                 return 1;
1251         if (f1->dirname == f2->dirname)
1252                 return u_strcmp(f1->basename, f2->basename);
1253         return f_name_cmp(f1, f2);
1254 }
1255
1256
1257 int flist_find(struct file_list *flist, struct file_struct *f)
1258 {
1259         int low = 0, high = flist->count - 1;
1260
1261         while (high >= 0 && !flist->files[high]->basename) high--;
1262
1263         if (high < 0)
1264                 return -1;
1265
1266         while (low != high) {
1267                 int mid = (low + high) / 2;
1268                 int ret = file_compare(&flist->files[flist_up(flist, mid)],&f);
1269                 if (ret == 0)
1270                         return flist_up(flist, mid);
1271                 if (ret > 0)
1272                         high = mid;
1273                 else
1274                         low = mid + 1;
1275         }
1276
1277         if (file_compare(&flist->files[flist_up(flist, low)], &f) == 0)
1278                 return flist_up(flist, low);
1279         return -1;
1280 }
1281
1282
1283 /*
1284  * free up one file
1285  */
1286 void free_file(struct file_struct *file)
1287 {
1288         if (!file)
1289                 return;
1290         if (file->basename)
1291                 free(file->basename);
1292         if (!IS_DEVICE(file->mode) && file->u.link)
1293                 free(file->u.link); /* Handles u.sum too. */
1294         if (file->link_u.idev)
1295                 free((char*)file->link_u.idev); /* Handles link_u.links too. */
1296         *file = null_file;
1297 }
1298
1299
1300 /*
1301  * allocate a new file list
1302  */
1303 struct file_list *flist_new(void)
1304 {
1305         struct file_list *flist;
1306
1307         flist = new(struct file_list);
1308         if (!flist)
1309                 out_of_memory("send_file_list");
1310
1311         flist->count = 0;
1312         flist->malloced = 0;
1313         flist->files = NULL;
1314
1315 #if ARENA_SIZE > 0
1316         flist->string_area = string_area_new(0);
1317 #else
1318         flist->string_area = NULL;
1319 #endif
1320         return flist;
1321 }
1322
1323 /*
1324  * free up all elements in a flist
1325  */
1326 void flist_free(struct file_list *flist)
1327 {
1328         int i;
1329         for (i = 1; i < flist->count; i++) {
1330                 if (!flist->string_area)
1331                         free_file(flist->files[i]);
1332                 free(flist->files[i]);
1333         }
1334         /* FIXME: I don't think we generally need to blank the flist
1335          * since it's about to be freed.  This will just cause more
1336          * memory traffic.  If you want a freed-memory debugger, you
1337          * know where to get it. */
1338         memset((char *) flist->files, 0,
1339                sizeof(flist->files[0]) * flist->count);
1340         free(flist->files);
1341         if (flist->string_area)
1342                 string_area_free(flist->string_area);
1343         memset((char *) flist, 0, sizeof(*flist));
1344         free(flist);
1345 }
1346
1347
1348 /*
1349  * This routine ensures we don't have any duplicate names in our file list.
1350  * duplicate names can cause corruption because of the pipelining
1351  */
1352 static void clean_flist(struct file_list *flist, int strip_root, int no_dups)
1353 {
1354         int i, prev_i = 0;
1355
1356         if (!flist || flist->count == 0)
1357                 return;
1358
1359         qsort(flist->files, flist->count,
1360               sizeof(flist->files[0]), (int (*)()) file_compare);
1361
1362         for (i = no_dups? 0 : flist->count; i < flist->count; i++) {
1363                 if (flist->files[i]->basename) {
1364                         prev_i = i;
1365                         break;
1366                 }
1367         }
1368         while (++i < flist->count) {
1369                 if (!flist->files[i]->basename)
1370                         continue;
1371                 if (f_name_cmp(flist->files[i], flist->files[prev_i]) == 0) {
1372                         if (verbose > 1 && !am_server) {
1373                                 rprintf(FINFO,
1374                                         "removing duplicate name %s from file list %d\n",
1375                                         f_name(flist->files[i]), i);
1376                         }
1377                         /* Make sure that if we unduplicate '.', that we don't
1378                          * lose track of a user-specified starting point (or
1379                          * else deletions will mysteriously fail with -R). */
1380                         if (flist->files[i]->flags & FLAG_TOP_DIR)
1381                                 flist->files[prev_i]->flags |= FLAG_TOP_DIR;
1382                         /* it's not great that the flist knows the semantics of
1383                          * the file memory usage, but i'd rather not add a flag
1384                          * byte to that struct.
1385                          * XXX can i use a bit in the flags field? */
1386                         if (flist->string_area)
1387                                 flist->files[i][0] = null_file;
1388                         else
1389                                 free_file(flist->files[i]);
1390                 } else
1391                         prev_i = i;
1392         }
1393
1394         if (strip_root) {
1395                 /* we need to strip off the root directory in the case
1396                    of relative paths, but this must be done _after_
1397                    the sorting phase */
1398                 for (i = 0; i < flist->count; i++) {
1399                         if (flist->files[i]->dirname &&
1400                             flist->files[i]->dirname[0] == '/') {
1401                                 memmove(&flist->files[i]->dirname[0],
1402                                         &flist->files[i]->dirname[1],
1403                                         strlen(flist->files[i]->dirname));
1404                         }
1405
1406                         if (flist->files[i]->dirname &&
1407                             !flist->files[i]->dirname[0]) {
1408                                 flist->files[i]->dirname = NULL;
1409                         }
1410                 }
1411         }
1412
1413         if (verbose <= 3)
1414                 return;
1415
1416         for (i = 0; i < flist->count; i++) {
1417                 rprintf(FINFO, "[%ld] i=%d %s %s %s mode=0%o len=%.0f\n",
1418                         (long) getpid(), i,
1419                         NS(flist->files[i]->basedir),
1420                         NS(flist->files[i]->dirname),
1421                         NS(flist->files[i]->basename),
1422                         (int) flist->files[i]->mode,
1423                         (double) flist->files[i]->length);
1424         }
1425 }
1426
1427
1428 enum fnc_state { fnc_DIR, fnc_SLASH, fnc_BASE };
1429
1430 /* Compare the names of two file_struct entities, just like strcmp()
1431  * would do if it were operating on the joined strings.  We assume
1432  * that there are no 0-length strings.
1433  */
1434 int f_name_cmp(struct file_struct *f1, struct file_struct *f2)
1435 {
1436         int dif;
1437         const uchar *c1, *c2;
1438         enum fnc_state state1, state2;
1439
1440         if (!f1 || !f1->basename) {
1441                 if (!f2 || !f2->basename)
1442                         return 0;
1443                 return -1;
1444         }
1445         if (!f2 || !f2->basename)
1446                 return 1;
1447
1448         if (!(c1 = (uchar*)f1->dirname)) {
1449                 state1 = fnc_BASE;
1450                 c1 = (uchar*)f1->basename;
1451         } else
1452                 state1 = fnc_DIR;
1453         if (!(c2 = (uchar*)f2->dirname)) {
1454                 state2 = fnc_BASE;
1455                 c2 = (uchar*)f2->basename;
1456         } else
1457                 state2 = fnc_DIR;
1458
1459         while (1) {
1460                 if ((dif = (int)*c1 - (int)*c2) != 0)
1461                         break;
1462                 if (!*++c1) {
1463                         switch (state1) {
1464                         case fnc_DIR:
1465                                 state1 = fnc_SLASH;
1466                                 c1 = (uchar*)"/";
1467                                 break;
1468                         case fnc_SLASH:
1469                                 state1 = fnc_BASE;
1470                                 c1 = (uchar*)f1->basename;
1471                                 break;
1472                         case fnc_BASE:
1473                                 break;
1474                         }
1475                 }
1476                 if (!*++c2) {
1477                         switch (state2) {
1478                         case fnc_DIR:
1479                                 state2 = fnc_SLASH;
1480                                 c2 = (uchar*)"/";
1481                                 break;
1482                         case fnc_SLASH:
1483                                 state2 = fnc_BASE;
1484                                 c2 = (uchar*)f2->basename;
1485                                 break;
1486                         case fnc_BASE:
1487                                 if (!*c1)
1488                                         return 0;
1489                                 break;
1490                         }
1491                 }
1492         }
1493
1494         return dif;
1495 }
1496
1497
1498 /* Return a copy of the full filename of a flist entry, using the indicated
1499  * buffer.  No size-checking is done because we checked the size when creating
1500  * the file_struct entry.
1501  */
1502 char *f_name_to(struct file_struct *f, char *fbuf)
1503 {
1504         if (!f || !f->basename)
1505                 return NULL;
1506
1507         if (f->dirname) {
1508                 int len = strlen(f->dirname);
1509                 memcpy(fbuf, f->dirname, len);
1510                 fbuf[len] = '/';
1511                 strcpy(fbuf + len + 1, f->basename);
1512         } else
1513                 strcpy(fbuf, f->basename);
1514         return fbuf;
1515 }
1516
1517
1518 /* Like f_name_to(), but we rotate through 5 static buffers of our own.
1519  */
1520 char *f_name(struct file_struct *f)
1521 {
1522         static char names[5][MAXPATHLEN];
1523         static unsigned int n;
1524
1525         n = (n + 1) % (sizeof names / sizeof names[0]);
1526
1527         return f_name_to(f, names[n]);
1528 }