]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/sys/getdirentries.c
MFV r329552: less v530.
[FreeBSD/FreeBSD.git] / lib / libc / sys / getdirentries.c
1 /*-
2  * Copyright (c) 2017 M. Warner Losh <imp@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #define _WANT_FREEBSD11_DIRENT
31
32 #include "namespace.h"
33 #include <sys/param.h>
34 #include <sys/syscall.h>
35 #include "compat-ino64.h"
36 #include <dirent.h>
37 #include <errno.h>
38 #include <stddef.h>
39 #include <stdlib.h>
40 #include <stdio.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include "libc_private.h"
44
45 static ssize_t
46 __cvt_dirents_from11(const char *de11, ssize_t len11, char *de, ssize_t len)
47 {
48         struct dirent *dst;
49         const struct freebsd11_dirent *src;
50         const char *edst, *esrc;
51         ssize_t rlen;
52
53         src = (const struct freebsd11_dirent *)de11;
54         dst = (struct dirent *)de;
55         esrc = de11 + len11;
56         edst = de + len;
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)
60                         break;
61                 dst->d_fileno = src->d_fileno;
62                 dst->d_off = 0;                 /* nothing uses it yet, so safe for now */
63                 dst->d_reclen = rlen;
64                 dst->d_type = src->d_type;
65                 dst->d_pad0 = 0;
66                 dst->d_namlen = src->d_namlen;
67                 dst->d_pad1 = 0;
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);
72         }
73         return ((char *)dst - de);
74 }
75
76 #undef getdirentries
77 __weak_reference(_getdirentries, getdirentries);
78
79 #pragma weak _getdirentries
80 ssize_t
81 _getdirentries(int fd, char *buf, size_t nbytes, off_t *basep)
82 {
83         char *oldbuf;
84         size_t len;
85         ssize_t rv;
86
87         if (__getosreldate() >= INO64_FIRST)
88                 return (__sys_getdirentries(fd, buf, nbytes, basep));
89
90         /*
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?
100          */
101         len = roundup(nbytes / 4, DIRBLKSIZ);
102         oldbuf = malloc(len);
103         if (oldbuf == NULL) {
104                 errno = EINVAL;         /* ENOMEM not in possible list */
105                 return (-1);
106         }
107         rv = syscall(SYS_freebsd11_getdirentries, fd, oldbuf, len, basep);
108         if (rv == -1) {
109                 free(oldbuf);
110                 return (rv);
111         }
112         if (rv > 0)
113                 rv = __cvt_dirents_from11(oldbuf, rv, buf, nbytes);
114         free(oldbuf);
115
116         return (rv);
117 }