r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text
[abartlet/samba.git/.git] / source3 / lib / repdir.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Copyright (C) Andrew Tridgell 2005
5    Updated for Samba3 64-bit cleanliness (C) Jeremy Allison 2006
6    
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.
11    
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.
16    
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/>.
19 */
20 /*
21   a replacement for opendir/readdir/telldir/seekdir/closedir for BSD systems
22
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().
28
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
35
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,
41
42   This code is available under other free software licenses as
43   well. Contact the author.
44 */
45
46 #include <include/includes.h>
47
48  void replace_readdir_dummy(void);
49  void replace_readdir_dummy(void) {}
50
51 #if defined(REPLACE_READDIR)
52
53 #if defined(PARANOID_MALLOC_CHECKER)
54 #ifdef malloc
55 #undef malloc
56 #endif
57 #endif
58
59 #define DIR_BUF_BITS 9
60 #define DIR_BUF_SIZE (1<<DIR_BUF_BITS)
61
62 struct dir_buf {
63         int fd;
64         int nbytes, ofs;
65         SMB_OFF_T seekpos;
66         char buf[DIR_BUF_SIZE];
67 };
68
69 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OPENDIR64)
70  SMB_STRUCT_DIR *opendir64(const char *dname)
71 #else
72  SMB_STRUCT_DIR *opendir(const char *dname)
73 #endif
74 {
75         struct dir_buf *d;
76         d = malloc(sizeof(*d));
77         if (d == NULL) {
78                 errno = ENOMEM;
79                 return NULL;
80         }
81 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OPEN64)
82         d->fd = open64(dname, O_RDONLY);
83 #else
84         d->fd = open(dname, O_RDONLY);
85 #endif
86
87         if (d->fd == -1) {
88                 free(d);
89                 return NULL;
90         }
91         d->ofs = 0;
92         d->seekpos = 0;
93         d->nbytes = 0;
94         return (SMB_STRUCT_DIR *)d;
95 }
96
97 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_READDIR64)
98  SMB_STRUCT_DIRENT *readdir64(SMB_STRUCT_DIR *dir)
99 #else
100  SMB_STRUCT_DIRENT *readdir(SMB_STRUCT_DIR *dir)
101 #endif
102 {
103         struct dir_buf *d = (struct dir_buf *)dir;
104         SMB_STRUCT_DIRENT *de;
105
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);
109 #else
110                 d->seekpos = lseek(d->fd, 0, SEEK_CUR);
111 #endif
112
113 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_GETDENTS64)
114                 d->nbytes = getdents64(d->fd, d->buf, DIR_BUF_SIZE);
115 #else
116                 d->nbytes = getdents(d->fd, d->buf, DIR_BUF_SIZE);
117 #endif
118                 d->ofs = 0;
119         }
120         if (d->ofs >= d->nbytes) {
121                 return NULL;
122         }
123         de = (SMB_STRUCT_DIRENT *)&d->buf[d->ofs];
124         d->ofs += de->d_reclen;
125         return de;
126 }
127
128 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_TELLDIR64)
129  long telldir64(SMB_STRUCT_DIR *dir)
130 #else
131  long telldir(SMB_STRUCT_DIR *dir)
132 #endif
133 {
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);
138 #else
139                 d->seekpos = lseek(d->fd, 0, SEEK_CUR);
140 #endif
141                 d->ofs = 0;
142                 d->nbytes = 0;
143         }
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)) {
147                 abort();
148         }
149         return d->seekpos + d->ofs;
150 }
151
152 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_SEEKDIR64)
153  void seekdir64(SMB_STRUCT_DIR *dir, long ofs)
154 #else
155  void seekdir(SMB_STRUCT_DIR *dir, long ofs)
156 #endif
157 {
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);
161 #else
162         d->seekpos = lseek(d->fd, ofs & ~(DIR_BUF_SIZE-1), SEEK_SET);
163 #endif
164
165 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_GETDENTS64)
166         d->nbytes = getdents64(d->fd, d->buf, DIR_BUF_SIZE);
167 #else
168         d->nbytes = getdents(d->fd, d->buf, DIR_BUF_SIZE);
169 #endif
170
171         d->ofs = 0;
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;
175 #else
176                 if (readdir(dir) == NULL) break;
177 #endif
178         }
179 }
180
181 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_REWINDDIR64)
182  void rewinddir64(SMB_STRUCT_DIR *dir)
183 #else
184  void rewinddir(SMB_STRUCT_DIR *dir)
185 #endif
186 {
187 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_SEEKDIR64)
188         seekdir64(dir, 0);
189 #else
190         seekdir(dir, 0);
191 #endif
192 }
193
194 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_CLOSEDIR64)
195  int closedir64(SMB_STRUCT_DIR *dir)
196 #else
197  int closedir(SMB_STRUCT_DIR *dir)
198 #endif
199 {
200         struct dir_buf *d = (struct dir_buf *)dir;
201         int r = close(d->fd);
202         if (r != 0) {
203                 return r;
204         }
205         free(d);
206         return 0;
207 }
208
209 #ifndef dirfd
210 /* darn, this is a macro on some systems. */
211  int dirfd(SMB_STRUCT_DIR *dir)
212 {
213         struct dir_buf *d = (struct dir_buf *)dir;
214         return d->fd;
215 }
216 #endif
217 #endif /* REPLACE_READDIR */