1 /* $NetBSD: initdir.c,v 1.1 2010/09/26 02:26:59 yamt 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)
34 __RCSID("$NetBSD: initdir.c,v 1.1 2010/09/26 02:26:59 yamt Exp $");
35 #endif /* LIBC_SCCS and not lint */
37 #include "namespace.h"
40 /* NetBSD BUG on !_REENTRANT */
41 #include <sys/cdefs.h>
42 #include <sys/featuretest.h>
43 #include <sys/types.h>
46 #include "reentrant.h"
49 #include <sys/param.h>
59 #include "dirent_private.h"
61 #define MAXITERATIONS 100
64 _initdir(DIR *dirp
, int fd
, const char *name
)
66 int flags
= dirp
->dd_flags
;
71 * If the machine's page size is an exact multiple of DIRBLKSIZ,
72 * use a buffer that is cluster boundary aligned.
73 * Hopefully this can be a big win someday by allowing page trades
74 * to user space to be done by getdents()
76 if (((pagesz
= getpagesize()) % DIRBLKSIZ
) == 0)
81 if ((flags
& DTF_REWIND
) && name
== NULL
) {
84 if ((flags
& __DTF_READALL
) != 0) {
95 * The strategy here for directories on top of a union stack
96 * is to read all the directory entries into a buffer, sort
97 * the buffer, and remove duplicate entries by setting the
98 * inode number to zero.
100 * For directories on an NFS mounted filesystem, we try
101 * to get a consistent snapshot by trying until we have
102 * successfully read all of the directory without errors
103 * (i.e. 'bad cookie' errors from the server because
104 * the directory was modified). These errors should not
105 * happen often, but need to be dealt with.
116 * Always make at least DIRBLKSIZ bytes
117 * available to getdents
119 if (space
< DIRBLKSIZ
) {
122 nbuf
= realloc(buf
, len
);
128 ddptr
= buf
+ (len
- space
);
131 dirp
->dd_seek
= lseek(fd
, (off_t
)0, SEEK_CUR
);
132 n
= getdents(fd
, ddptr
, space
);
134 * For NFS: EINVAL means a bad cookie error
135 * from the server. Keep trying to get a
136 * consistent view, in this case this means
137 * starting all over again.
139 if (n
== -1 && errno
== EINVAL
&&
140 (flags
& __DTF_RETRY_ON_BADCOOKIE
) != 0) {
142 lseek(fd
, (off_t
)0, SEEK_SET
);
143 if (++i
> MAXITERATIONS
)
156 * Re-open the directory.
157 * This has the effect of rewinding back to the
158 * top of the union stack and is needed by
159 * programs which plan to fchdir to a descriptor
160 * which has also been read -- see fts.c.
162 if (flags
& DTF_REWIND
) {
164 if ((fd
= open(name
, O_RDONLY
)) == -1 ||
165 fcntl(fd
, F_SETFD
, FD_CLOEXEC
) == -1) {
172 * There is now a buffer full of (possibly) duplicate
178 * Go round this loop twice...
180 * Scan through the buffer, counting entries.
181 * On the second pass, save pointers to each one.
182 * Then sort the pointers and remove duplicate names.
184 if ((flags
& DTF_NODUP
) != 0) {
186 for (n
= 0, ddptr
= buf
; ddptr
< ddeptr
;) {
189 dp
= (struct dirent
*)(void *)ddptr
;
190 if ((long)dp
& _DIRENT_ALIGN(dp
))
193 * d_reclen is unsigned,
194 * so no need to compare <= 0
196 if (dp
->d_reclen
> (ddeptr
+ 1 - ddptr
))
198 ddptr
+= dp
->d_reclen
;
210 * This sort must be stable.
212 mergesort(dpv
, (size_t)n
, sizeof(*dpv
),
219 * Scan through the buffer in sort
220 * order, zapping the inode number
221 * of any duplicate names.
223 for (n
= 0; dpv
[n
]; n
++) {
224 struct dirent
*dp
= dpv
[n
];
233 if (dp
->d_type
== DT_WHT
&&
242 dpv
= malloc((n
+ 1) *
243 sizeof(struct dirent
*));
251 dirp
->dd_size
= ddptr
- dirp
->dd_buf
;
254 dirp
->dd_buf
= malloc((size_t)dirp
->dd_len
);
255 if (dirp
->dd_buf
== NULL
)
258 flags
&= ~DTF_REWIND
;
262 dirp
->dd_flags
= flags
;
264 * Set up seek point for rewinddir.
266 (void)_telldir_unlocked(dirp
);
273 struct dirpos
*poslist
;
277 /* free seekdir/telldir storage */
278 for (poslist
= dirp
->dd_internal
; poslist
; ) {
279 struct dirpos
*nextpos
= poslist
->dp_next
;
283 dirp
->dd_internal
= NULL
;