2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 2005
5 Updated for Samba3 64-bit cleanliness (C) Jeremy Allison 2006
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 a replacement for opendir/readdir/telldir/seekdir/closedir for BSD systems
23 This is needed because the existing directory handling in FreeBSD
24 and OpenBSD (and possibly NetBSD) doesn't correctly handle unlink()
25 on files in a directory where telldir() has been used. On a block
26 boundary it will occasionally miss a file when seekdir() is used to
27 return to a position previously recorded with telldir().
29 This also fixes a severe performance and memory usage problem with
30 telldir() on BSD systems. Each call to telldir() in BSD adds an
31 entry to a linked list, and those entries are cleaned up on
32 closedir(). This means with a large directory closedir() can take an
33 arbitrary amount of time, causing network timeouts as millions of
34 telldir() entries are freed
36 Note! This replacement code is not portable. It relies on getdents()
37 always leaving the file descriptor at a seek offset that is a
38 multiple of DIR_BUF_SIZE. If the code detects that this doesn't
39 happen then it will abort(). It also does not handle directories
40 with offsets larger than can be stored in a long,
42 This code is available under other free software licenses as
43 well. Contact the author.
46 #include <include/includes.h>
48 void replace_readdir_dummy(void);
49 void replace_readdir_dummy(void) {}
51 #if defined(REPLACE_READDIR)
53 #if defined(PARANOID_MALLOC_CHECKER)
59 #define DIR_BUF_BITS 9
60 #define DIR_BUF_SIZE (1<<DIR_BUF_BITS)
66 char buf[DIR_BUF_SIZE];
69 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OPENDIR64)
70 SMB_STRUCT_DIR *opendir64(const char *dname)
72 SMB_STRUCT_DIR *opendir(const char *dname)
76 d = malloc(sizeof(*d));
81 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OPEN64)
82 d->fd = open64(dname, O_RDONLY);
84 d->fd = open(dname, O_RDONLY);
94 return (SMB_STRUCT_DIR *)d;
97 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_READDIR64)
98 SMB_STRUCT_DIRENT *readdir64(SMB_STRUCT_DIR *dir)
100 SMB_STRUCT_DIRENT *readdir(SMB_STRUCT_DIR *dir)
103 struct dir_buf *d = (struct dir_buf *)dir;
104 SMB_STRUCT_DIRENT *de;
106 if (d->ofs >= d->nbytes) {
107 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_LSEEK64)
108 d->seekpos = lseek64(d->fd, 0, SEEK_CUR);
110 d->seekpos = lseek(d->fd, 0, SEEK_CUR);
113 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_GETDENTS64)
114 d->nbytes = getdents64(d->fd, d->buf, DIR_BUF_SIZE);
116 d->nbytes = getdents(d->fd, d->buf, DIR_BUF_SIZE);
120 if (d->ofs >= d->nbytes) {
123 de = (SMB_STRUCT_DIRENT *)&d->buf[d->ofs];
124 d->ofs += de->d_reclen;
128 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_TELLDIR64)
129 long telldir64(SMB_STRUCT_DIR *dir)
131 long telldir(SMB_STRUCT_DIR *dir)
134 struct dir_buf *d = (struct dir_buf *)dir;
135 if (d->ofs >= d->nbytes) {
136 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_LSEEK64)
137 d->seekpos = lseek64(d->fd, 0, SEEK_CUR);
139 d->seekpos = lseek(d->fd, 0, SEEK_CUR);
144 /* this relies on seekpos always being a multiple of
145 DIR_BUF_SIZE. Is that always true on BSD systems? */
146 if (d->seekpos & (DIR_BUF_SIZE-1)) {
149 return d->seekpos + d->ofs;
152 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_SEEKDIR64)
153 void seekdir64(SMB_STRUCT_DIR *dir, long ofs)
155 void seekdir(SMB_STRUCT_DIR *dir, long ofs)
158 struct dir_buf *d = (struct dir_buf *)dir;
159 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_LSEEK64)
160 d->seekpos = lseek64(d->fd, ofs & ~(DIR_BUF_SIZE-1), SEEK_SET);
162 d->seekpos = lseek(d->fd, ofs & ~(DIR_BUF_SIZE-1), SEEK_SET);
165 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_GETDENTS64)
166 d->nbytes = getdents64(d->fd, d->buf, DIR_BUF_SIZE);
168 d->nbytes = getdents(d->fd, d->buf, DIR_BUF_SIZE);
172 while (d->ofs < (ofs & (DIR_BUF_SIZE-1))) {
173 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_READDIR64)
174 if (readdir64(dir) == NULL) break;
176 if (readdir(dir) == NULL) break;
181 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_REWINDDIR64)
182 void rewinddir64(SMB_STRUCT_DIR *dir)
184 void rewinddir(SMB_STRUCT_DIR *dir)
187 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_SEEKDIR64)
194 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_CLOSEDIR64)
195 int closedir64(SMB_STRUCT_DIR *dir)
197 int closedir(SMB_STRUCT_DIR *dir)
200 struct dir_buf *d = (struct dir_buf *)dir;
201 int r = close(d->fd);
210 /* darn, this is a macro on some systems. */
211 int dirfd(SMB_STRUCT_DIR *dir)
213 struct dir_buf *d = (struct dir_buf *)dir;
217 #endif /* REPLACE_READDIR */