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 2 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, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 a replacement for opendir/readdir/telldir/seekdir/closedir for BSD systems
24 This is needed because the existing directory handling in FreeBSD
25 and OpenBSD (and possibly NetBSD) doesn't correctly handle unlink()
26 on files in a directory where telldir() has been used. On a block
27 boundary it will occasionally miss a file when seekdir() is used to
28 return to a position previously recorded with telldir().
30 This also fixes a severe performance and memory usage problem with
31 telldir() on BSD systems. Each call to telldir() in BSD adds an
32 entry to a linked list, and those entries are cleaned up on
33 closedir(). This means with a large directory closedir() can take an
34 arbitrary amount of time, causing network timeouts as millions of
35 telldir() entries are freed
37 Note! This replacement code is not portable. It relies on getdents()
38 always leaving the file descriptor at a seek offset that is a
39 multiple of DIR_BUF_SIZE. If the code detects that this doesn't
40 happen then it will abort(). It also does not handle directories
41 with offsets larger than can be stored in a long,
43 This code is available under other free software licenses as
44 well. Contact the author.
47 #include <include/includes.h>
49 void replace_readdir_dummy(void);
50 void replace_readdir_dummy(void) {}
52 #if defined(REPLACE_READDIR)
54 #if defined(PARANOID_MALLOC_CHECKER)
60 #define DIR_BUF_BITS 9
61 #define DIR_BUF_SIZE (1<<DIR_BUF_BITS)
67 char buf
[DIR_BUF_SIZE
];
70 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OPENDIR64)
71 SMB_STRUCT_DIR
*opendir64(const char *dname
)
73 SMB_STRUCT_DIR
*opendir(const char *dname
)
77 d
= malloc(sizeof(*d
));
82 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OPEN64)
83 d
->fd
= open64(dname
, O_RDONLY
);
85 d
->fd
= open(dname
, O_RDONLY
);
95 return (SMB_STRUCT_DIR
*)d
;
98 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_READDIR64)
99 SMB_STRUCT_DIRENT
*readdir64(SMB_STRUCT_DIR
*dir
)
101 SMB_STRUCT_DIRENT
*readdir(SMB_STRUCT_DIR
*dir
)
104 struct dir_buf
*d
= (struct dir_buf
*)dir
;
105 SMB_STRUCT_DIRENT
*de
;
107 if (d
->ofs
>= d
->nbytes
) {
108 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_LSEEK64)
109 d
->seekpos
= lseek64(d
->fd
, 0, SEEK_CUR
);
111 d
->seekpos
= lseek(d
->fd
, 0, SEEK_CUR
);
114 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_GETDENTS64)
115 d
->nbytes
= getdents64(d
->fd
, d
->buf
, DIR_BUF_SIZE
);
117 d
->nbytes
= getdents(d
->fd
, d
->buf
, DIR_BUF_SIZE
);
121 if (d
->ofs
>= d
->nbytes
) {
124 de
= (SMB_STRUCT_DIRENT
*)&d
->buf
[d
->ofs
];
125 d
->ofs
+= de
->d_reclen
;
129 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_TELLDIR64)
130 long telldir64(SMB_STRUCT_DIR
*dir
)
132 long telldir(SMB_STRUCT_DIR
*dir
)
135 struct dir_buf
*d
= (struct dir_buf
*)dir
;
136 if (d
->ofs
>= d
->nbytes
) {
137 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_LSEEK64)
138 d
->seekpos
= lseek64(d
->fd
, 0, SEEK_CUR
);
140 d
->seekpos
= lseek(d
->fd
, 0, SEEK_CUR
);
145 /* this relies on seekpos always being a multiple of
146 DIR_BUF_SIZE. Is that always true on BSD systems? */
147 if (d
->seekpos
& (DIR_BUF_SIZE
-1)) {
150 return d
->seekpos
+ d
->ofs
;
153 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_SEEKDIR64)
154 void seekdir64(SMB_STRUCT_DIR
*dir
, long ofs
)
156 void seekdir(SMB_STRUCT_DIR
*dir
, long ofs
)
159 struct dir_buf
*d
= (struct dir_buf
*)dir
;
160 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_LSEEK64)
161 d
->seekpos
= lseek64(d
->fd
, ofs
& ~(DIR_BUF_SIZE
-1), SEEK_SET
);
163 d
->seekpos
= lseek(d
->fd
, ofs
& ~(DIR_BUF_SIZE
-1), SEEK_SET
);
166 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_GETDENTS64)
167 d
->nbytes
= getdents64(d
->fd
, d
->buf
, DIR_BUF_SIZE
);
169 d
->nbytes
= getdents(d
->fd
, d
->buf
, DIR_BUF_SIZE
);
173 while (d
->ofs
< (ofs
& (DIR_BUF_SIZE
-1))) {
174 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_READDIR64)
175 if (readdir64(dir
) == NULL
) break;
177 if (readdir(dir
) == NULL
) break;
182 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_REWINDDIR64)
183 void rewinddir64(SMB_STRUCT_DIR
*dir
)
185 void rewinddir(SMB_STRUCT_DIR
*dir
)
188 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_SEEKDIR64)
195 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_CLOSEDIR64)
196 int closedir64(SMB_STRUCT_DIR
*dir
)
198 int closedir(SMB_STRUCT_DIR
*dir
)
201 struct dir_buf
*d
= (struct dir_buf
*)dir
;
202 int r
= close(d
->fd
);
211 /* darn, this is a macro on some systems. */
212 int dirfd(SMB_STRUCT_DIR
*dir
)
214 struct dir_buf
*d
= (struct dir_buf
*)dir
;
218 #endif /* REPLACE_READDIR */