1 | /* $Id: fileio-sg-posix.cpp 82968 2020-02-04 10:35:17Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - File I/O, RTFileSgRead & RTFileSgWrite, posixy.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2020 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #include <iprt/cdefs.h>
|
---|
32 | #include <errno.h>
|
---|
33 | #include <sys/types.h>
|
---|
34 | #include <sys/uio.h>
|
---|
35 | #include <unistd.h>
|
---|
36 | #include <limits.h>
|
---|
37 | #if defined(RT_OS_DARWIN) || defined(RT_OS_FREEBSD) || defined(RT_OS_NETBSD) || defined(RT_OS_OPENBSD)
|
---|
38 | # include <sys/syslimits.h>
|
---|
39 | #endif
|
---|
40 |
|
---|
41 | #include "internal/iprt.h"
|
---|
42 | #include <iprt/file.h>
|
---|
43 |
|
---|
44 | #include <iprt/assert.h>
|
---|
45 | #include <iprt/err.h>
|
---|
46 | #include <iprt/log.h>
|
---|
47 |
|
---|
48 | #ifndef UIO_MAXIOV
|
---|
49 | # ifdef IOV_MAX
|
---|
50 | # define UIO_MAXIOV IOV_MAX
|
---|
51 | # else
|
---|
52 | # error "UIO_MAXIOV and IOV_MAX are undefined"
|
---|
53 | # endif
|
---|
54 | #endif
|
---|
55 |
|
---|
56 |
|
---|
57 | /* These assumptions simplifies things a lot here. */
|
---|
58 | AssertCompileMembersSameSizeAndOffset(struct iovec, iov_base, RTSGSEG, pvSeg);
|
---|
59 | AssertCompileMembersSameSizeAndOffset(struct iovec, iov_len, RTSGSEG, cbSeg);
|
---|
60 |
|
---|
61 |
|
---|
62 | RTDECL(int) RTFileSgRead(RTFILE hFile, PRTSGBUF pSgBuf, size_t cbToRead, size_t *pcbRead)
|
---|
63 | {
|
---|
64 | /*
|
---|
65 | * Make sure we set pcbRead.
|
---|
66 | */
|
---|
67 | if (pcbRead)
|
---|
68 | *pcbRead = 0;
|
---|
69 |
|
---|
70 | /*
|
---|
71 | * Special case: Zero read == nop.
|
---|
72 | */
|
---|
73 | if (cbToRead == 0)
|
---|
74 | return VINF_SUCCESS;
|
---|
75 |
|
---|
76 | /*
|
---|
77 | * We can use the segment array directly if we're at the start of the
|
---|
78 | * current S/G segment and cbToRead matches the remainder exactly.
|
---|
79 | */
|
---|
80 | size_t cbTotalRead = 0;
|
---|
81 |
|
---|
82 | size_t const cbSgBufLeft = RTSgBufCalcLengthLeft(pSgBuf);
|
---|
83 | AssertMsgReturn(cbSgBufLeft >= cbToRead, ("%#zx vs %#zx\n", cbSgBufLeft, cbToRead), VERR_INVALID_PARAMETER);
|
---|
84 |
|
---|
85 | if (cbToRead == cbSgBufLeft)
|
---|
86 | while (RTSgBufIsAtStartOfSegment(pSgBuf))
|
---|
87 | {
|
---|
88 | size_t const cSegsLeft = pSgBuf->cSegs - pSgBuf->idxSeg;
|
---|
89 | ssize_t cbThisRead = readv(RTFileToNative(hFile), (const struct iovec *)&pSgBuf->paSegs[pSgBuf->idxSeg],
|
---|
90 | RT_MIN(cSegsLeft, UIO_MAXIOV));
|
---|
91 | if (cbThisRead >= 0)
|
---|
92 | {
|
---|
93 | AssertStmt((size_t)cbThisRead <= cbToRead, cbThisRead = cbToRead);
|
---|
94 |
|
---|
95 | RTSgBufAdvance(pSgBuf, cbThisRead);
|
---|
96 | cbTotalRead += cbThisRead;
|
---|
97 | cbToRead -= cbThisRead;
|
---|
98 | if (cbToRead == 0)
|
---|
99 | {
|
---|
100 | if (pcbRead)
|
---|
101 | *pcbRead = cbTotalRead;
|
---|
102 | return VINF_SUCCESS;
|
---|
103 | }
|
---|
104 |
|
---|
105 | if ( pcbRead
|
---|
106 | && ( cSegsLeft <= UIO_MAXIOV
|
---|
107 | || cbThisRead == 0 /* typically EOF */ ))
|
---|
108 | {
|
---|
109 | *pcbRead = cbTotalRead;
|
---|
110 | return VINF_SUCCESS;
|
---|
111 | }
|
---|
112 | if (cbThisRead == 0)
|
---|
113 | return VERR_EOF;
|
---|
114 | }
|
---|
115 | else if (cbTotalRead > 0 && pcbRead)
|
---|
116 | {
|
---|
117 | *pcbRead = cbTotalRead;
|
---|
118 | return VINF_SUCCESS;
|
---|
119 | }
|
---|
120 | else
|
---|
121 | return RTErrConvertFromErrno(errno);
|
---|
122 | }
|
---|
123 |
|
---|
124 | /*
|
---|
125 | * Unaligned start or not reading the whole buffer. For reasons of
|
---|
126 | * simplicity, we work the input segment by segment like the generic code.
|
---|
127 | */
|
---|
128 | int rc = VINF_SUCCESS;
|
---|
129 | while (cbToRead > 0)
|
---|
130 | {
|
---|
131 | size_t cbSeg;
|
---|
132 | void *pvSeg = RTSgBufGetCurrentSegment(pSgBuf, cbToRead, &cbSeg);
|
---|
133 | size_t cbThisRead = cbSeg;
|
---|
134 | rc = RTFileRead(hFile, pvSeg, cbSeg, pcbRead ? &cbThisRead : NULL);
|
---|
135 | if (RT_SUCCESS(rc))
|
---|
136 | {
|
---|
137 | RTSgBufAdvance(pSgBuf, cbThisRead);
|
---|
138 | cbTotalRead += cbThisRead;
|
---|
139 | }
|
---|
140 | else
|
---|
141 | break;
|
---|
142 | if ((size_t)cbThisRead < cbSeg)
|
---|
143 | {
|
---|
144 | AssertStmt(pcbRead, rc = VERR_INTERNAL_ERROR_2);
|
---|
145 | break;
|
---|
146 | }
|
---|
147 |
|
---|
148 | Assert(cbSeg == cbThisRead);
|
---|
149 | cbToRead -= cbSeg;
|
---|
150 | }
|
---|
151 | if (pcbRead)
|
---|
152 | *pcbRead = cbTotalRead;
|
---|
153 | return rc;
|
---|
154 | }
|
---|
155 |
|
---|
156 |
|
---|
157 | RTDECL(int) RTFileSgWrite(RTFILE hFile, PRTSGBUF pSgBuf, size_t cbToWrite, size_t *pcbWritten)
|
---|
158 | {
|
---|
159 | /*
|
---|
160 | * Make sure we set pcbWritten.
|
---|
161 | */
|
---|
162 | if (pcbWritten)
|
---|
163 | *pcbWritten = 0;
|
---|
164 |
|
---|
165 | /*
|
---|
166 | * Special case: Zero write == nop.
|
---|
167 | */
|
---|
168 | if (cbToWrite == 0)
|
---|
169 | return VINF_SUCCESS;
|
---|
170 |
|
---|
171 | /*
|
---|
172 | * We can use the segment array directly if we're at the start of the
|
---|
173 | * current S/G segment and cbToWrite matches the remainder exactly.
|
---|
174 | */
|
---|
175 | size_t cbTotalWritten = 0;
|
---|
176 |
|
---|
177 | size_t const cbSgBufLeft = RTSgBufCalcLengthLeft(pSgBuf);
|
---|
178 | AssertMsgReturn(cbSgBufLeft >= cbToWrite, ("%#zx vs %#zx\n", cbSgBufLeft, cbToWrite), VERR_INVALID_PARAMETER);
|
---|
179 |
|
---|
180 | if (cbToWrite == cbSgBufLeft)
|
---|
181 | while (RTSgBufIsAtStartOfSegment(pSgBuf))
|
---|
182 | {
|
---|
183 | size_t const cSegsLeft = pSgBuf->cSegs - pSgBuf->idxSeg;
|
---|
184 | ssize_t cbThisWritten = writev(RTFileToNative(hFile), (const struct iovec *)&pSgBuf->paSegs[pSgBuf->idxSeg],
|
---|
185 | RT_MIN(cSegsLeft, UIO_MAXIOV));
|
---|
186 | if (cbThisWritten >= 0)
|
---|
187 | {
|
---|
188 | AssertStmt((size_t)cbThisWritten <= cbToWrite, cbThisWritten = cbToWrite);
|
---|
189 |
|
---|
190 | RTSgBufAdvance(pSgBuf, cbThisWritten);
|
---|
191 | cbTotalWritten += cbThisWritten;
|
---|
192 | cbToWrite -= cbThisWritten;
|
---|
193 | if (cbToWrite == 0)
|
---|
194 | {
|
---|
195 | if (pcbWritten)
|
---|
196 | *pcbWritten = cbTotalWritten;
|
---|
197 | return VINF_SUCCESS;
|
---|
198 | }
|
---|
199 |
|
---|
200 | if ( pcbWritten
|
---|
201 | && ( cSegsLeft <= UIO_MAXIOV
|
---|
202 | || cbThisWritten == 0 /* non-file, full buffer/whatever */ ))
|
---|
203 | {
|
---|
204 | *pcbWritten = cbTotalWritten;
|
---|
205 | return VINF_SUCCESS;
|
---|
206 | }
|
---|
207 | if (cbThisWritten == 0)
|
---|
208 | return VERR_TRY_AGAIN;
|
---|
209 | }
|
---|
210 | else if (cbTotalWritten > 0 && pcbWritten)
|
---|
211 | {
|
---|
212 | *pcbWritten = cbTotalWritten;
|
---|
213 | return VINF_SUCCESS;
|
---|
214 | }
|
---|
215 | else
|
---|
216 | return RTErrConvertFromErrno(errno);
|
---|
217 | }
|
---|
218 |
|
---|
219 | /*
|
---|
220 | * Unaligned start or not writing the whole buffer. For reasons of
|
---|
221 | * simplicity, we work the input segment by segment like the generic code.
|
---|
222 | */
|
---|
223 | int rc = VINF_SUCCESS;
|
---|
224 | while (cbToWrite > 0)
|
---|
225 | {
|
---|
226 | size_t cbSeg;
|
---|
227 | void *pvSeg = RTSgBufGetCurrentSegment(pSgBuf, cbToWrite, &cbSeg);
|
---|
228 | size_t cbThisWritten = cbSeg;
|
---|
229 | rc = RTFileWrite(hFile, pvSeg, cbSeg, pcbWritten ? &cbThisWritten : NULL);
|
---|
230 | if (RT_SUCCESS(rc))
|
---|
231 | {
|
---|
232 | RTSgBufAdvance(pSgBuf, cbThisWritten);
|
---|
233 | cbTotalWritten += cbThisWritten;
|
---|
234 | }
|
---|
235 | else
|
---|
236 | break;
|
---|
237 | if ((size_t)cbThisWritten < cbSeg)
|
---|
238 | {
|
---|
239 | AssertStmt(pcbWritten, rc = VERR_INTERNAL_ERROR_2);
|
---|
240 | break;
|
---|
241 | }
|
---|
242 |
|
---|
243 | Assert(cbSeg == cbThisWritten);
|
---|
244 | cbToWrite -= cbSeg;
|
---|
245 | }
|
---|
246 | if (pcbWritten)
|
---|
247 | *pcbWritten = cbTotalWritten;
|
---|
248 | return rc;
|
---|
249 | }
|
---|
250 |
|
---|