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