AMD64 - Refactor uio_resid and size_t assumptions.
[dragonfly.git] / sys / emulation / dragonfly12 / dfbsd12_getdirentries.c
CommitLineData
01f31ab3
JS
1/*-
2 * Copyright (c) 2005 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Joerg Sonnenberger <joerg@bec.de>.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
efda3bd0 34 * $DragonFly: src/sys/emulation/dragonfly12/dfbsd12_getdirentries.c,v 1.3 2006/09/05 00:55:44 dillon Exp $
01f31ab3
JS
35 */
36
37#include "opt_compatdf12.h"
38
39#include <sys/param.h>
40#include <sys/dirent.h>
41#include <sys/errno.h>
42#include <sys/kern_syscall.h>
43#include <sys/systm.h>
44#include <sys/sysproto.h>
45#include <sys/uio.h>
46
47#define PADDED_SIZE(x) \
48 ((sizeof(struct dfbsd12_dirent) + (x) + 1 + 3) & ~3)
49#define MAX_NAMELEN 255
50
51struct dfbsd12_dirent {
52 uint32_t df12_d_fileno;
53 uint16_t df12_d_reclen;
54 uint8_t df12_d_type;
55 uint8_t df12_d_namlen;
56 char df12_d_name[];
57};
58
59static int
60common_getdirentries(long *base, int *result, int fd, char *usr_buf, size_t count)
61{
62 int error, bytes_transfered;
63 char *buf, *outbuf;
64 size_t len;
65 struct dirent *ndp;
66 struct dfbsd12_dirent *destdp;
67
68 if (count > 16384)
69 len = 16384;
70 else
71 len = count;
72
efda3bd0 73 buf = kmalloc(len, M_TEMP, M_WAITOK);
01f31ab3
JS
74
75 error = kern_getdirentries(fd, buf, len,
76 base, &bytes_transfered, UIO_SYSSPACE);
77
78 if (error) {
efda3bd0 79 kfree(buf, M_TEMP);
01f31ab3
JS
80 return(error);
81 }
82
83 ndp = (struct dirent *)buf;
84 outbuf = usr_buf;
efda3bd0 85 destdp = kmalloc(PADDED_SIZE(MAX_NAMELEN), M_TEMP, M_WAITOK);
01f31ab3
JS
86
87 for (; (char *)ndp < buf + bytes_transfered; ndp = _DIRENT_NEXT(ndp)) {
88 if ((char *)_DIRENT_NEXT(ndp) > buf + bytes_transfered)
89 break;
90 if (ndp->d_namlen > MAX_NAMELEN)
91 continue;
92 destdp->df12_d_fileno = ndp->d_ino;
93 destdp->df12_d_type = ndp->d_type;
94 destdp->df12_d_namlen = ndp->d_namlen;
95 destdp->df12_d_reclen = PADDED_SIZE(destdp->df12_d_namlen);
96 if (destdp->df12_d_reclen > len)
97 break; /* XXX can not happen */
98 bcopy(ndp->d_name, destdp->df12_d_name, destdp->df12_d_namlen);
99 bzero(destdp->df12_d_name + destdp->df12_d_namlen,
100 PADDED_SIZE(destdp->df12_d_namlen) - sizeof(*destdp) -
101 destdp->df12_d_namlen);
102 error = copyout(destdp, outbuf,
103 PADDED_SIZE(destdp->df12_d_namlen));
104 if (error)
105 break;
106 outbuf += PADDED_SIZE(destdp->df12_d_namlen);
107 len -= PADDED_SIZE(destdp->df12_d_namlen);
108 }
109
efda3bd0
MD
110 kfree(destdp, M_TEMP);
111 kfree(buf, M_TEMP);
01f31ab3
JS
112 *result = outbuf - usr_buf;
113 return (0);
114}
115
116int
753fd850 117sys_dfbsd12_getdirentries(struct dfbsd12_getdirentries_args *uap)
01f31ab3
JS
118{
119 long base;
120 int error;
121
e54488bb
MD
122 error = common_getdirentries(&base, &uap->sysmsg_iresult, uap->fd,
123 uap->buf, uap->count);
01f31ab3
JS
124
125 if (error == 0)
126 error = copyout(&base, uap->basep, sizeof(*uap->basep));
127 return (error);
128}
129
130int
753fd850 131sys_dfbsd12_getdents(struct dfbsd12_getdents_args *uap)
01f31ab3 132{
e54488bb
MD
133 return(common_getdirentries(NULL, &uap->sysmsg_iresult, uap->fd,
134 uap->buf, uap->count));
01f31ab3 135}