1 /* $NetBSD: initdir.c,v 1.3 2012/03/13 21:13:36 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)
34 __RCSID("$NetBSD: initdir.c,v 1.3 2012/03/13 21:13:36 christos Exp $");
35 #endif /* LIBC_SCCS and not lint */
37 #include "namespace.h"
38 #include "reentrant.h"
41 #include <sys/param.h>
51 #include "dirent_private.h"
53 #define MAXITERATIONS 100
56 _initdir(DIR *dirp
, int fd
, const char *name
)
58 int flags
= dirp
->dd_flags
;
63 * If the machine's page size is an exact multiple of DIRBLKSIZ,
64 * use a buffer that is cluster boundary aligned.
65 * Hopefully this can be a big win someday by allowing page trades
66 * to user space to be done by getdents()
68 if (((pagesz
= getpagesize()) % DIRBLKSIZ
) == 0)
73 if ((flags
& DTF_REWIND
) && name
== NULL
) {
76 if ((flags
& __DTF_READALL
) != 0) {
87 * The strategy here for directories on top of a union stack
88 * is to read all the directory entries into a buffer, sort
89 * the buffer, and remove duplicate entries by setting the
90 * inode number to zero.
92 * For directories on an NFS mounted filesystem, we try
93 * to get a consistent snapshot by trying until we have
94 * successfully read all of the directory without errors
95 * (i.e. 'bad cookie' errors from the server because
96 * the directory was modified). These errors should not
97 * happen often, but need to be dealt with.
108 * Always make at least DIRBLKSIZ bytes
109 * available to getdents
111 if (space
< DIRBLKSIZ
) {
114 nbuf
= realloc(buf
, len
);
120 ddptr
= buf
+ (len
- space
);
123 dirp
->dd_seek
= lseek(fd
, (off_t
)0, SEEK_CUR
);
124 n
= getdents(fd
, ddptr
, space
);
126 * For NFS: EINVAL means a bad cookie error
127 * from the server. Keep trying to get a
128 * consistent view, in this case this means
129 * starting all over again.
131 if (n
== -1 && errno
== EINVAL
&&
132 (flags
& __DTF_RETRY_ON_BADCOOKIE
) != 0) {
134 lseek(fd
, (off_t
)0, SEEK_SET
);
135 if (++i
> MAXITERATIONS
)
148 * Re-open the directory.
149 * This has the effect of rewinding back to the
150 * top of the union stack and is needed by
151 * programs which plan to fchdir to a descriptor
152 * which has also been read -- see fts.c.
154 if (flags
& DTF_REWIND
) {
156 if ((fd
= open(name
, O_RDONLY
| O_CLOEXEC
)) == -1) {
163 * There is now a buffer full of (possibly) duplicate
169 * Go round this loop twice...
171 * Scan through the buffer, counting entries.
172 * On the second pass, save pointers to each one.
173 * Then sort the pointers and remove duplicate names.
175 if ((flags
& DTF_NODUP
) != 0) {
177 for (n
= 0, ddptr
= buf
; ddptr
< ddeptr
;) {
180 dp
= (struct dirent
*)(void *)ddptr
;
181 if ((long)dp
& _DIRENT_ALIGN(dp
))
184 * d_reclen is unsigned,
185 * so no need to compare <= 0
187 if (dp
->d_reclen
> (ddeptr
+ 1 - ddptr
))
189 ddptr
+= dp
->d_reclen
;
201 * This sort must be stable.
203 mergesort(dpv
, (size_t)n
, sizeof(*dpv
),
210 * Scan through the buffer in sort
211 * order, zapping the inode number
212 * of any duplicate names.
214 for (n
= 0; dpv
[n
]; n
++) {
215 struct dirent
*dp
= dpv
[n
];
223 #if !defined(__minix)
224 if (dp
->d_type
== DT_WHT
&&
227 #endif /* !defined(__minix) */
233 dpv
= malloc((n
+ 1) *
234 sizeof(struct dirent
*));
241 _DIAGASSERT(__type_fit(int, len
));
242 dirp
->dd_len
= (int)len
;
243 dirp
->dd_size
= ddptr
- dirp
->dd_buf
;
246 dirp
->dd_buf
= malloc((size_t)dirp
->dd_len
);
247 if (dirp
->dd_buf
== NULL
)
250 flags
&= ~DTF_REWIND
;
254 dirp
->dd_flags
= flags
;
256 * Set up seek point for rewinddir.
258 (void)_telldir_unlocked(dirp
);
265 struct dirpos
*poslist
;
269 /* free seekdir/telldir storage */
270 for (poslist
= dirp
->dd_internal
; poslist
; ) {
271 struct dirpos
*nextpos
= poslist
->dp_next
;
275 dirp
->dd_internal
= NULL
;