2 * Copyright (c) 2017 M. Warner Losh <imp@FreeBSD.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
29 #define _WANT_FREEBSD11_DIRENT
31 #include "namespace.h"
32 #include <sys/param.h>
33 #include <sys/syscall.h>
34 #include "compat-ino64.h"
42 #include "libc_private.h"
45 __cvt_dirents_from11(const char *de11, ssize_t len11, char *de, ssize_t len)
48 const struct freebsd11_dirent *src;
49 const char *edst, *esrc;
52 src = (const struct freebsd11_dirent *)de11;
53 dst = (struct dirent *)de;
56 while ((const char *)src < esrc && (const char *)dst < edst) {
57 rlen = roundup(offsetof(struct dirent, d_name) + src->d_namlen + 1, 8);
58 if ((const char *)dst + rlen >= edst)
60 dst->d_fileno = src->d_fileno;
61 dst->d_off = 0; /* nothing uses it yet, so safe for now */
63 dst->d_type = src->d_type;
65 dst->d_namlen = src->d_namlen;
67 memset(dst->d_name, 0, roundup(src->d_namlen + 1, 8));
68 memcpy(dst->d_name, src->d_name, src->d_namlen);
69 dst = (struct dirent *)((char *)dst + rlen);
70 src = (const struct freebsd11_dirent *)((const char *)src + src->d_reclen);
72 return ((char *)dst - de);
76 __weak_reference(_getdirentries, getdirentries);
78 #pragma weak _getdirentries
80 _getdirentries(int fd, char *buf, size_t nbytes, off_t *basep)
86 if (__getosreldate() >= INO64_FIRST)
87 return (__sys_getdirentries(fd, buf, nbytes, basep));
90 * Because the old system call returns entries that are smaller than the
91 * new, we could wind up in a situation where we have too many to fit in
92 * the buffer with the new encoding. So sacrifice a small bit of
93 * efficiency to ensure that never happens. We pick 1/4 the size round
94 * up to the next DIRBLKSIZ. This will guarnatee enough room exists in
95 * the dst buffer due to changes in efficiency in packing dirent
96 * entries. We don't check against minimum block size to avoid a lot of
97 * stat calls, we'll see if that's wise or not.
98 * TBD: Will this difference matter to lseek?
100 len = roundup(nbytes / 4, DIRBLKSIZ);
101 oldbuf = malloc(len);
102 if (oldbuf == NULL) {
103 errno = EINVAL; /* ENOMEM not in possible list */
106 rv = syscall(SYS_freebsd11_getdirentries, fd, oldbuf, len, basep);
112 rv = __cvt_dirents_from11(oldbuf, rv, buf, nbytes);