6b115c4c4fbb9680c58e1dfbb67bdf57414fcee6
[samba.git] / source / lib / replace / repdir_getdents.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 systems
26
27   This is needed because the existing directory handling in FreeBSD
28   and OpenBSD (and possibly NetBSD) doesn't correctly handle unlink()
29   on files in a directory where telldir() has been used. On a block
30   boundary it will occasionally miss a file when seekdir() is used to
31   return to a position previously recorded with telldir().
32
33   This also fixes a severe performance and memory usage problem with
34   telldir() on BSD systems. Each call to telldir() in BSD adds an
35   entry to a linked list, and those entries are cleaned up on
36   closedir(). This means with a large directory closedir() can take an
37   arbitrary amount of time, causing network timeouts as millions of
38   telldir() entries are freed
39
40   Note! This replacement code is not portable. It relies on getdents()
41   always leaving the file descriptor at a seek offset that is a
42   multiple of DIR_BUF_SIZE. If the code detects that this doesn't
43   happen then it will abort(). It also does not handle directories
44   with offsets larger than can be stored in a long,
45
46   This code is available under other free software licenses as
47   well. Contact the author.
48 */
49
50 #include <stdlib.h>
51 #include <sys/stat.h>
52 #include <unistd.h>
53 #include <sys/types.h>
54 #include <errno.h>
55 #include <fcntl.h>
56 #include <dirent.h>
57
58 #define DIR_BUF_BITS 9
59 #define DIR_BUF_SIZE (1<<DIR_BUF_BITS)
60
61 struct dir_buf {
62         int fd;
63         int nbytes, ofs;
64         off_t seekpos;
65         char buf[DIR_BUF_SIZE];
66 };
67
68 DIR *opendir(const char *dname)
69 {
70         struct dir_buf *d;
71         struct stat sb;
72         d = malloc(sizeof(*d));
73         if (d == NULL) {
74                 errno = ENOMEM;
75                 return NULL;
76         }
77         d->fd = open(dname, O_RDONLY);
78         if (d->fd == -1) {
79                 free(d);
80                 return NULL;
81         }
82         if (fstat(d->fd, &sb) < 0) {
83                 close(d->fd);
84                 free(d);
85                 return NULL;
86         }
87         if (!S_ISDIR(sb.st_mode)) {
88                 close(d->fd);
89                 free(d);   
90                 errno = ENOTDIR;
91                 return NULL;
92         }
93         d->ofs = 0;
94         d->seekpos = 0;
95         d->nbytes = 0;
96         return (DIR *)d;
97 }
98
99 struct dirent *readdir(DIR *dir)
100 {
101         struct dir_buf *d = (struct dir_buf *)dir;
102         struct dirent *de;
103
104         if (d->ofs >= d->nbytes) {
105                 d->seekpos = lseek(d->fd, 0, SEEK_CUR);
106                 d->nbytes = getdents(d->fd, d->buf, DIR_BUF_SIZE);
107                 d->ofs = 0;
108         }
109         if (d->ofs >= d->nbytes) {
110                 return NULL;
111         }
112         de = (struct dirent *)&d->buf[d->ofs];
113         d->ofs += de->d_reclen;
114         return de;
115 }
116
117 long telldir(DIR *dir)
118 {
119         struct dir_buf *d = (struct dir_buf *)dir;
120         if (d->ofs >= d->nbytes) {
121                 d->seekpos = lseek(d->fd, 0, SEEK_CUR);
122                 d->ofs = 0;
123                 d->nbytes = 0;
124         }
125         /* this relies on seekpos always being a multiple of
126            DIR_BUF_SIZE. Is that always true on BSD systems? */
127         if (d->seekpos & (DIR_BUF_SIZE-1)) {
128                 abort();
129         }
130         return d->seekpos + d->ofs;
131 }
132
133 void seekdir(DIR *dir, long ofs)
134 {
135         struct dir_buf *d = (struct dir_buf *)dir;
136         d->seekpos = lseek(d->fd, ofs & ~(DIR_BUF_SIZE-1), SEEK_SET);
137         d->nbytes = getdents(d->fd, d->buf, DIR_BUF_SIZE);
138         d->ofs = 0;
139         while (d->ofs < (ofs & (DIR_BUF_SIZE-1))) {
140                 if (readdir(dir) == NULL) break;
141         }
142 }
143
144 void rewinddir(DIR *dir)
145 {
146         seekdir(dir, 0);
147 }
148
149 int closedir(DIR *dir)
150 {
151         struct dir_buf *d = (struct dir_buf *)dir;
152         int r = close(d->fd);
153         if (r != 0) {
154                 return r;
155         }
156         free(d);
157         return 0;
158 }
159
160 #ifndef dirfd
161 /* darn, this is a macro on some systems. */
162 int dirfd(DIR *dir)
163 {
164         struct dir_buf *d = (struct dir_buf *)dir;
165         return d->fd;
166 }
167 #endif