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