1 | /* $Id: fileio-sg-at-posix.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - File I/O, RTFileSgReadAt & RTFileSgWriteAt, posixy.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | /*
|
---|
42 | * Determin whether we've got preadv and pwritev.
|
---|
43 | */
|
---|
44 | #include <iprt/cdefs.h>
|
---|
45 | #ifdef RT_OS_LINUX
|
---|
46 | /* Linux has these since glibc 2.10 and Linux 2.6.30: */
|
---|
47 | # include <features.h>
|
---|
48 | # ifdef __GLIBC_PREREQ
|
---|
49 | # if __GLIBC_PREREQ(2,10)
|
---|
50 | # define HAVE_PREADV_AND_PWRITEV 1
|
---|
51 | #else
|
---|
52 | # endif
|
---|
53 | # endif
|
---|
54 |
|
---|
55 | #elif defined(RT_OS_FREEBSD)
|
---|
56 | /* FreeBSD has these since 6.0: */
|
---|
57 | # include <osreldate.h>
|
---|
58 | # ifdef __FreeBSD_version
|
---|
59 | # if __FreeBSD_version >= 600000
|
---|
60 | # define HAVE_PREADV_AND_PWRITEV 1
|
---|
61 | # endif
|
---|
62 | # endif
|
---|
63 |
|
---|
64 | #endif
|
---|
65 |
|
---|
66 | #ifndef HAVE_PREADV_AND_PWRITEV
|
---|
67 |
|
---|
68 | # include "../../generic/fileio-sg-at-generic.cpp"
|
---|
69 |
|
---|
70 | #else /* HAVE_PREADV_AND_PWRITEV - rest of the file */
|
---|
71 |
|
---|
72 | # include <errno.h>
|
---|
73 | # include <sys/types.h>
|
---|
74 | # include <sys/uio.h>
|
---|
75 | # include <unistd.h>
|
---|
76 | # include <limits.h>
|
---|
77 | # if defined(RT_OS_DARWIN) || defined(RT_OS_FREEBSD) || defined(RT_OS_NETBSD) || defined(RT_OS_OPENBSD)
|
---|
78 | # include <sys/syslimits.h>
|
---|
79 | # endif
|
---|
80 |
|
---|
81 | # include "internal/iprt.h"
|
---|
82 | # include <iprt/file.h>
|
---|
83 |
|
---|
84 | # include <iprt/assert.h>
|
---|
85 | # include <iprt/err.h>
|
---|
86 | # include <iprt/log.h>
|
---|
87 |
|
---|
88 | # ifndef UIO_MAXIOV
|
---|
89 | # ifdef IOV_MAX
|
---|
90 | # define UIO_MAXIOV IOV_MAX
|
---|
91 | # else
|
---|
92 | # error "UIO_MAXIOV and IOV_MAX are undefined"
|
---|
93 | # endif
|
---|
94 | # endif
|
---|
95 |
|
---|
96 |
|
---|
97 | /* These assumptions simplifies things a lot here. */
|
---|
98 | AssertCompileMembersSameSizeAndOffset(struct iovec, iov_base, RTSGSEG, pvSeg);
|
---|
99 | AssertCompileMembersSameSizeAndOffset(struct iovec, iov_len, RTSGSEG, cbSeg);
|
---|
100 |
|
---|
101 |
|
---|
102 | RTDECL(int) RTFileSgReadAt(RTFILE hFile, RTFOFF off, PRTSGBUF pSgBuf, size_t cbToRead, size_t *pcbRead)
|
---|
103 | {
|
---|
104 | /*
|
---|
105 | * Make sure we set pcbRead.
|
---|
106 | */
|
---|
107 | if (pcbRead)
|
---|
108 | *pcbRead = 0;
|
---|
109 |
|
---|
110 | /*
|
---|
111 | * Special case: Zero read == seek.
|
---|
112 | */
|
---|
113 | if (cbToRead == 0)
|
---|
114 | return RTFileSeek(hFile, off, RTFILE_SEEK_BEGIN, NULL);
|
---|
115 |
|
---|
116 | /*
|
---|
117 | * We can use the segment array directly if we're at the start of the
|
---|
118 | * current S/G segment and cbToRead matches the remainder exactly.
|
---|
119 | */
|
---|
120 | size_t cbTotalRead = 0;
|
---|
121 |
|
---|
122 | size_t const cbSgBufLeft = RTSgBufCalcLengthLeft(pSgBuf);
|
---|
123 | AssertMsgReturn(cbSgBufLeft >= cbToRead, ("%#zx vs %#zx\n", cbSgBufLeft, cbToRead), VERR_INVALID_PARAMETER);
|
---|
124 |
|
---|
125 | if (cbToRead == cbSgBufLeft)
|
---|
126 | while (RTSgBufIsAtStartOfSegment(pSgBuf))
|
---|
127 | {
|
---|
128 | size_t const cSegsLeft = pSgBuf->cSegs - pSgBuf->idxSeg;
|
---|
129 | ssize_t cbThisRead = preadv(RTFileToNative(hFile), (const struct iovec *)&pSgBuf->paSegs[pSgBuf->idxSeg],
|
---|
130 | RT_MIN(cSegsLeft, UIO_MAXIOV), off);
|
---|
131 | if (cbThisRead >= 0)
|
---|
132 | {
|
---|
133 | AssertStmt((size_t)cbThisRead <= cbToRead, cbThisRead = cbToRead);
|
---|
134 |
|
---|
135 | RTSgBufAdvance(pSgBuf, cbThisRead);
|
---|
136 | cbTotalRead += cbThisRead;
|
---|
137 | cbToRead -= cbThisRead;
|
---|
138 | if (cbToRead == 0)
|
---|
139 | {
|
---|
140 | if (pcbRead)
|
---|
141 | *pcbRead = cbTotalRead;
|
---|
142 | return VINF_SUCCESS;
|
---|
143 | }
|
---|
144 |
|
---|
145 | if ( pcbRead
|
---|
146 | && ( cSegsLeft <= UIO_MAXIOV
|
---|
147 | || cbThisRead == 0 /* typically EOF */ ))
|
---|
148 | {
|
---|
149 | *pcbRead = cbTotalRead;
|
---|
150 | return VINF_SUCCESS;
|
---|
151 | }
|
---|
152 | if (cbThisRead == 0)
|
---|
153 | return VERR_EOF;
|
---|
154 |
|
---|
155 | off += cbThisRead;
|
---|
156 | }
|
---|
157 | else if (cbTotalRead > 0 && pcbRead)
|
---|
158 | {
|
---|
159 | *pcbRead = cbTotalRead;
|
---|
160 | return VINF_SUCCESS;
|
---|
161 | }
|
---|
162 | else
|
---|
163 | return RTErrConvertFromErrno(errno);
|
---|
164 | }
|
---|
165 |
|
---|
166 | /*
|
---|
167 | * Unaligned start or not reading the whole buffer. For reasons of
|
---|
168 | * simplicity, we work the input segment by segment like the generic code.
|
---|
169 | */
|
---|
170 | int rc = VINF_SUCCESS;
|
---|
171 | while (cbToRead > 0)
|
---|
172 | {
|
---|
173 | size_t cbSeg;
|
---|
174 | void *pvSeg = RTSgBufGetCurrentSegment(pSgBuf, cbToRead, &cbSeg);
|
---|
175 | size_t cbThisRead = cbSeg;
|
---|
176 | rc = RTFileReadAt(hFile, off, pvSeg, cbSeg, pcbRead ? &cbThisRead : NULL);
|
---|
177 | if (RT_SUCCESS(rc))
|
---|
178 | {
|
---|
179 | RTSgBufAdvance(pSgBuf, cbThisRead);
|
---|
180 | cbTotalRead += cbThisRead;
|
---|
181 | }
|
---|
182 | else
|
---|
183 | break;
|
---|
184 | if ((size_t)cbThisRead < cbSeg)
|
---|
185 | {
|
---|
186 | AssertStmt(pcbRead, rc = VERR_INTERNAL_ERROR_2);
|
---|
187 | break;
|
---|
188 | }
|
---|
189 |
|
---|
190 | Assert(cbSeg == cbThisRead);
|
---|
191 | cbToRead -= cbSeg;
|
---|
192 | off += cbSeg;
|
---|
193 | }
|
---|
194 | if (pcbRead)
|
---|
195 | *pcbRead = cbTotalRead;
|
---|
196 | return rc;
|
---|
197 | }
|
---|
198 |
|
---|
199 |
|
---|
200 | RTDECL(int) RTFileSgWriteAt(RTFILE hFile, RTFOFF off, PRTSGBUF pSgBuf, size_t cbToWrite, size_t *pcbWritten)
|
---|
201 | {
|
---|
202 | /*
|
---|
203 | * Make sure we set pcbWritten.
|
---|
204 | */
|
---|
205 | if (pcbWritten)
|
---|
206 | *pcbWritten = 0;
|
---|
207 |
|
---|
208 | /*
|
---|
209 | * Special case: Zero write == seek.
|
---|
210 | */
|
---|
211 | if (cbToWrite == 0)
|
---|
212 | return RTFileSeek(hFile, off, RTFILE_SEEK_BEGIN, NULL);
|
---|
213 |
|
---|
214 | /*
|
---|
215 | * We can use the segment array directly if we're at the start of the
|
---|
216 | * current S/G segment and cbToWrite matches the remainder exactly.
|
---|
217 | */
|
---|
218 | size_t cbTotalWritten = 0;
|
---|
219 |
|
---|
220 | size_t const cbSgBufLeft = RTSgBufCalcLengthLeft(pSgBuf);
|
---|
221 | AssertMsgReturn(cbSgBufLeft >= cbToWrite, ("%#zx vs %#zx\n", cbSgBufLeft, cbToWrite), VERR_INVALID_PARAMETER);
|
---|
222 |
|
---|
223 | if (cbToWrite == cbSgBufLeft)
|
---|
224 | while (RTSgBufIsAtStartOfSegment(pSgBuf))
|
---|
225 | {
|
---|
226 | size_t const cSegsLeft = pSgBuf->cSegs - pSgBuf->idxSeg;
|
---|
227 | ssize_t cbThisWritten = pwritev(RTFileToNative(hFile), (const struct iovec *)&pSgBuf->paSegs[pSgBuf->idxSeg],
|
---|
228 | RT_MIN(cSegsLeft, UIO_MAXIOV), off);
|
---|
229 | if (cbThisWritten >= 0)
|
---|
230 | {
|
---|
231 | AssertStmt((size_t)cbThisWritten <= cbToWrite, cbThisWritten = cbToWrite);
|
---|
232 |
|
---|
233 | RTSgBufAdvance(pSgBuf, cbThisWritten);
|
---|
234 | cbTotalWritten += cbThisWritten;
|
---|
235 | cbToWrite -= cbThisWritten;
|
---|
236 | if (cbToWrite == 0)
|
---|
237 | {
|
---|
238 | if (pcbWritten)
|
---|
239 | *pcbWritten = cbTotalWritten;
|
---|
240 | return VINF_SUCCESS;
|
---|
241 | }
|
---|
242 |
|
---|
243 | if ( pcbWritten
|
---|
244 | && ( cSegsLeft <= UIO_MAXIOV
|
---|
245 | || cbThisWritten == 0 /* non-file, full buffer/whatever */ ))
|
---|
246 | {
|
---|
247 | *pcbWritten = cbTotalWritten;
|
---|
248 | return VINF_SUCCESS;
|
---|
249 | }
|
---|
250 | if (cbThisWritten == 0)
|
---|
251 | return VERR_TRY_AGAIN;
|
---|
252 |
|
---|
253 | off += cbThisWritten;
|
---|
254 | }
|
---|
255 | else if (cbTotalWritten > 0 && pcbWritten)
|
---|
256 | {
|
---|
257 | *pcbWritten = cbTotalWritten;
|
---|
258 | return VINF_SUCCESS;
|
---|
259 | }
|
---|
260 | else
|
---|
261 | return RTErrConvertFromErrno(errno);
|
---|
262 | }
|
---|
263 |
|
---|
264 | /*
|
---|
265 | * Unaligned start or not writing the whole buffer. For reasons of
|
---|
266 | * simplicity, we work the input segment by segment like the generic code.
|
---|
267 | */
|
---|
268 | int rc = VINF_SUCCESS;
|
---|
269 | while (cbToWrite > 0)
|
---|
270 | {
|
---|
271 | size_t cbSeg;
|
---|
272 | void *pvSeg = RTSgBufGetCurrentSegment(pSgBuf, cbToWrite, &cbSeg);
|
---|
273 | size_t cbThisWritten = cbSeg;
|
---|
274 | rc = RTFileWriteAt(hFile, off, pvSeg, cbSeg, pcbWritten ? &cbThisWritten : NULL);
|
---|
275 | if (RT_SUCCESS(rc))
|
---|
276 | {
|
---|
277 | RTSgBufAdvance(pSgBuf, cbThisWritten);
|
---|
278 | cbTotalWritten += cbThisWritten;
|
---|
279 | }
|
---|
280 | else
|
---|
281 | break;
|
---|
282 | if ((size_t)cbThisWritten < cbSeg)
|
---|
283 | {
|
---|
284 | AssertStmt(pcbWritten, rc = VERR_INTERNAL_ERROR_2);
|
---|
285 | break;
|
---|
286 | }
|
---|
287 |
|
---|
288 | Assert(cbSeg == cbThisWritten);
|
---|
289 | cbToWrite -= cbSeg;
|
---|
290 | off += cbSeg;
|
---|
291 | }
|
---|
292 | if (pcbWritten)
|
---|
293 | *pcbWritten = cbTotalWritten;
|
---|
294 | return rc;
|
---|
295 | }
|
---|
296 |
|
---|
297 | #endif /* HAVE_PREADV_AND_PWRITEV */
|
---|
298 |
|
---|