r19403: try to fix the crashes in the buildfarm related to timegm
[samba.git] / source4 / lib / replace / repdir_getdirentries.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Copyright (C) Andrew Tridgell 2005
5
6      ** NOTE! The following LGPL license applies to the replace
7      ** library. This does NOT imply that all of Samba is released
8      ** under the LGPL
9    
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.
14
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.
19
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
23 */
24 /*
25   a replacement for opendir/readdir/telldir/seekdir/closedir for BSD
26   systems using getdirentries
27
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().
33
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
40
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,
46
47   This code is available under other free software licenses as
48   well. Contact the author.
49 */
50
51 #include "replace.h"
52 #include <stdlib.h>
53 #include <sys/stat.h>
54 #include <unistd.h>
55 #include <sys/types.h>
56 #include <errno.h>
57 #include <fcntl.h>
58 #include <dirent.h>
59
60 #define DIR_BUF_BITS 9
61 #define DIR_BUF_SIZE (1<<DIR_BUF_BITS)
62
63 struct dir_buf {
64         int fd;
65         int nbytes, ofs;
66         off_t seekpos;
67         char buf[DIR_BUF_SIZE];
68 };
69
70 DIR *opendir(const char *dname)
71 {
72         struct dir_buf *d;
73         struct stat sb;
74         d = malloc(sizeof(*d));
75         if (d == NULL) {
76                 errno = ENOMEM;
77                 return NULL;
78         }
79         d->fd = open(dname, O_RDONLY);
80         if (d->fd == -1) {
81                 free(d);
82                 return NULL;
83         }
84         if (fstat(d->fd, &sb) < 0) {
85                 close(d->fd);
86                 free(d);
87                 return NULL;
88         }
89         if (!S_ISDIR(sb.st_mode)) {
90                 close(d->fd);
91                 free(d);   
92                 errno = ENOTDIR;
93                 return NULL;
94         }
95         d->ofs = 0;
96         d->seekpos = 0;
97         d->nbytes = 0;
98         return (DIR *)d;
99 }
100
101 struct dirent *readdir(DIR *dir)
102 {
103         struct dir_buf *d = (struct dir_buf *)dir;
104         struct dirent *de;
105
106         if (d->ofs >= d->nbytes) {
107                 long pos;
108                 d->nbytes = getdirentries(d->fd, d->buf, DIR_BUF_SIZE, &pos);
109                 d->seekpos = pos;
110                 d->ofs = 0;
111         }
112         if (d->ofs >= d->nbytes) {
113                 return NULL;
114         }
115         de = (struct dirent *)&d->buf[d->ofs];
116         d->ofs += de->d_reclen;
117         return de;
118 }
119
120 #ifdef TELLDIR_TAKES_CONST_DIR
121 long telldir(const DIR *dir)
122 #else
123 long telldir(DIR *dir)
124 #endif
125 {
126         struct dir_buf *d = (struct dir_buf *)dir;
127         if (d->ofs >= d->nbytes) {
128                 d->seekpos = lseek(d->fd, 0, SEEK_CUR);
129                 d->ofs = 0;
130                 d->nbytes = 0;
131         }
132         /* this relies on seekpos always being a multiple of
133            DIR_BUF_SIZE. Is that always true on BSD systems? */
134         if (d->seekpos & (DIR_BUF_SIZE-1)) {
135                 abort();
136         }
137         return d->seekpos + d->ofs;
138 }
139
140 #ifdef SEEKDIR_RETURNS_INT
141 int seekdir(DIR *dir, long ofs)
142 #else
143 void seekdir(DIR *dir, long ofs)
144 #endif
145 {
146         struct dir_buf *d = (struct dir_buf *)dir;
147         long pos;
148         d->seekpos = lseek(d->fd, ofs & ~(DIR_BUF_SIZE-1), SEEK_SET);
149         d->nbytes = getdirentries(d->fd, d->buf, DIR_BUF_SIZE, &pos);
150         d->ofs = 0;
151         while (d->ofs < (ofs & (DIR_BUF_SIZE-1))) {
152                 if (readdir(dir) == NULL) break;
153         }
154 #ifdef SEEKDIR_RETURNS_INT
155         return -1;
156 #endif
157 }
158
159 void rewinddir(DIR *dir)
160 {
161         seekdir(dir, 0);
162 }
163
164 int closedir(DIR *dir)
165 {
166         struct dir_buf *d = (struct dir_buf *)dir;
167         int r = close(d->fd);
168         if (r != 0) {
169                 return r;
170         }
171         free(d);
172         return 0;
173 }
174
175 #ifndef dirfd
176 /* darn, this is a macro on some systems. */
177 int dirfd(DIR *dir)
178 {
179         struct dir_buf *d = (struct dir_buf *)dir;
180         return d->fd;
181 }
182 #endif
183
184