1 /* $NetBSD: opendir.c,v 1.33.12.1 2009/01/04 17:02:19 christos Exp $ */
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 #if defined(LIBC_SCCS) && !defined(lint)
35 static char sccsid
[] = "@(#)opendir.c 8.7 (Berkeley) 12/10/94";
37 __RCSID("$NetBSD: opendir.c,v 1.33.12.1 2009/01/04 17:02:19 christos Exp $");
39 #endif /* LIBC_SCCS and not lint */
41 #include "namespace.h"
42 #include "reentrant.h"
45 #include <sys/param.h>
46 #include <sys/mount.h>
57 #include "dirent_private.h"
59 #define MAXITERATIONS 100
61 static DIR *__opendir_common(int, const char *, int);
63 __weak_alias(fdopendir
,_fdopendir
)
69 opendir(const char *name
)
72 _DIAGASSERT(name
!= NULL
);
74 return (__opendir2(name
, DTF_HIDEW
|DTF_NODUP
));
78 __opendir2(const char *name
, int flags
)
82 if ((fd
= open(name
, O_RDONLY
| O_NONBLOCK
)) == -1)
84 return __opendir_common(fd
, name
, flags
);
87 #ifndef __LIBC12_SOURCE__
92 return __opendir_common(fd
, NULL
, DTF_HIDEW
|DTF_NODUP
);
97 __opendir_common(int fd
, const char *name
, int flags
)
104 int unionstack
, nfsdir
;
107 _DIAGASSERT(name
!= NULL
);
109 if (fcntl(fd
, F_SETFD
, FD_CLOEXEC
) == -1)
111 if (fstat(fd
, &sb
) || !S_ISDIR(sb
.st_mode
)) {
115 if ((dirp
= (DIR *)malloc(sizeof(DIR))) == NULL
)
120 * If the machine's page size is an exact multiple of DIRBLKSIZ,
121 * use a buffer that is cluster boundary aligned.
122 * Hopefully this can be a big win someday by allowing page trades
123 * to user space to be done by getdirentries()
125 if (((pagesz
= getpagesize()) % DIRBLKSIZ
) == 0)
131 * Determine whether this directory is the top of a union stack.
134 if (fstatvfs1(fd
, &sfb
, ST_NOWAIT
) < 0)
137 if (flags
& DTF_NODUP
)
138 unionstack
= !(strncmp(sfb
.f_fstypename
, MOUNT_UNION
,
139 sizeof(sfb
.f_fstypename
))) || (sfb
.f_flag
& MNT_UNION
);
143 nfsdir
= !(strncmp(sfb
.f_fstypename
, MOUNT_NFS
, sizeof(sfb
.f_fstypename
)));
145 if (unionstack
|| nfsdir
) {
156 * The strategy here for directories on top of a union stack
157 * is to read all the directory entries into a buffer, sort
158 * the buffer, and remove duplicate entries by setting the
159 * inode number to zero.
161 * For directories on an NFS mounted filesystem, we try
162 * to get a consistent snapshot by trying until we have
163 * successfully read all of the directory without errors
164 * (i.e. 'bad cookie' errors from the server because
165 * the directory was modified). These errors should not
166 * happen often, but need to be dealt with.
177 * Always make at least DIRBLKSIZ bytes
178 * available to getdirentries
180 if (space
< DIRBLKSIZ
) {
183 nbuf
= realloc(buf
, len
);
189 ddptr
= buf
+ (len
- space
);
192 dirp
->dd_seek
= lseek(fd
, (off_t
)0, SEEK_CUR
);
193 n
= getdents(fd
, ddptr
, space
);
195 * For NFS: EINVAL means a bad cookie error
196 * from the server. Keep trying to get a
197 * consistent view, in this case this means
198 * starting all over again.
200 if (n
== -1 && errno
== EINVAL
&& nfsdir
) {
202 lseek(fd
, (off_t
)0, SEEK_SET
);
203 if (++i
> MAXITERATIONS
)
214 flags
|= __DTF_READALL
;
217 * Re-open the directory.
218 * This has the effect of rewinding back to the
219 * top of the union stack and is needed by
220 * programs which plan to fchdir to a descriptor
221 * which has also been read -- see fts.c.
223 if (flags
& DTF_REWIND
) {
225 if ((fd
= open(name
, O_RDONLY
)) == -1 ||
226 fcntl(fd
, F_SETFD
, FD_CLOEXEC
) == -1) {
233 * There is now a buffer full of (possibly) duplicate
239 * Go round this loop twice...
241 * Scan through the buffer, counting entries.
242 * On the second pass, save pointers to each one.
243 * Then sort the pointers and remove duplicate names.
247 for (n
= 0, ddptr
= buf
; ddptr
< ddeptr
;) {
250 dp
= (struct dirent
*)(void *)ddptr
;
251 if ((long)dp
& _DIRENT_ALIGN(dp
))
254 * d_reclen is unsigned,
255 * so no need to compare <= 0
257 if (dp
->d_reclen
> (ddeptr
+ 1 - ddptr
))
259 ddptr
+= dp
->d_reclen
;
271 * This sort must be stable.
273 mergesort(dpv
, (size_t)n
, sizeof(*dpv
),
280 * Scan through the buffer in sort
281 * order, zapping the inode number
282 * of any duplicate names.
284 for (n
= 0; dpv
[n
]; n
++) {
285 struct dirent
*dp
= dpv
[n
];
293 if (dp
->d_type
== DT_WHT
&&
301 dpv
= malloc((n
+ 1) *
302 sizeof(struct dirent
*));
310 dirp
->dd_size
= ddptr
- dirp
->dd_buf
;
313 dirp
->dd_buf
= malloc((size_t)dirp
->dd_len
);
314 if (dirp
->dd_buf
== NULL
)
317 flags
&= ~DTF_REWIND
;
322 dirp
->dd_flags
= flags
;
325 * Set up seek point for rewinddir.
329 if ((dirp
->dd_lock
= malloc(sizeof(mutex_t
))) == NULL
)
331 mutex_init((mutex_t
*)dirp
->dd_lock
, NULL
);
334 dirp
->dd_internal
= NULL
;
335 (void)_telldir_unlocked(dirp
);
339 if (dirp
&& dirp
->dd_buf
)