2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 2005
6 ** NOTE! The following LGPL license applies to the replace
7 ** library. This does NOT imply that all of Samba is released
10 This library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Lesser General Public
12 License as published by the Free Software Foundation; either
13 version 2 of the License, or (at your option) any later version.
15 This library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
20 You should have received a copy of the GNU Lesser General Public
21 License along with this library; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 a replacement for opendir/readdir/telldir/seekdir/closedir for BSD
26 systems using getdirentries
28 This is needed because the existing directory handling in FreeBSD
29 and OpenBSD (and possibly NetBSD) doesn't correctly handle unlink()
30 on files in a directory where telldir() has been used. On a block
31 boundary it will occasionally miss a file when seekdir() is used to
32 return to a position previously recorded with telldir().
34 This also fixes a severe performance and memory usage problem with
35 telldir() on BSD systems. Each call to telldir() in BSD adds an
36 entry to a linked list, and those entries are cleaned up on
37 closedir(). This means with a large directory closedir() can take an
38 arbitrary amount of time, causing network timeouts as millions of
39 telldir() entries are freed
41 Note! This replacement code is not portable. It relies on
42 getdirentries() always leaving the file descriptor at a seek offset
43 that is a multiple of DIR_BUF_SIZE. If the code detects that this
44 doesn't happen then it will abort(). It also does not handle
45 directories with offsets larger than can be stored in a long,
47 This code is available under other free software licenses as
48 well. Contact the author.
55 #include <sys/types.h>
60 #define DIR_BUF_BITS 9
61 #define DIR_BUF_SIZE (1<<DIR_BUF_BITS)
67 char buf[DIR_BUF_SIZE];
70 DIR *opendir(const char *dname)
73 d = malloc(sizeof(*d));
78 d->fd = open(dname, O_RDONLY);
89 struct dirent *readdir(DIR *dir)
91 struct dir_buf *d = (struct dir_buf *)dir;
94 if (d->ofs >= d->nbytes) {
96 d->nbytes = getdirentries(d->fd, d->buf, DIR_BUF_SIZE, &pos);
100 if (d->ofs >= d->nbytes) {
103 de = (struct dirent *)&d->buf[d->ofs];
104 d->ofs += de->d_reclen;
108 #ifdef TELLDIR_TAKES_CONST_DIR
109 long telldir(const DIR *dir)
111 long telldir(DIR *dir)
114 struct dir_buf *d = (struct dir_buf *)dir;
115 if (d->ofs >= d->nbytes) {
116 d->seekpos = lseek(d->fd, 0, SEEK_CUR);
120 /* this relies on seekpos always being a multiple of
121 DIR_BUF_SIZE. Is that always true on BSD systems? */
122 if (d->seekpos & (DIR_BUF_SIZE-1)) {
125 return d->seekpos + d->ofs;
128 #ifdef SEEKDIR_RETURNS_INT
129 int seekdir(DIR *dir, long ofs)
131 void seekdir(DIR *dir, long ofs)
134 struct dir_buf *d = (struct dir_buf *)dir;
136 d->seekpos = lseek(d->fd, ofs & ~(DIR_BUF_SIZE-1), SEEK_SET);
137 d->nbytes = getdirentries(d->fd, d->buf, DIR_BUF_SIZE, &pos);
139 while (d->ofs < (ofs & (DIR_BUF_SIZE-1))) {
140 if (readdir(dir) == NULL) break;
142 #ifdef SEEKDIR_RETURNS_INT
147 void rewinddir(DIR *dir)
152 int closedir(DIR *dir)
154 struct dir_buf *d = (struct dir_buf *)dir;
155 int r = close(d->fd);
164 /* darn, this is a macro on some systems. */
167 struct dir_buf *d = (struct dir_buf *)dir;