s3: smbd: Move check_fsp_open() and check_fsp() to smb1_reply.c
[Samba.git] / lib / util / sys_rw.c
blobd25a42b7082c555b808685eac5393bc1511119c6
1 /*
2 * Unix SMB/CIFS implementation.
3 * Samba system utilities
4 * Copyright (C) Andrew Tridgell 1992-1998
5 * Copyright (C) Jeremy Allison 1998-2005
6 * Copyright (C) Timur Bakeyev 2005
7 * Copyright (C) Bjoern Jacke 2006-2007
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "replace.h"
24 #include "system/filesys.h"
25 #include "lib/util/sys_rw.h"
26 #include <assert.h>
28 bool sys_valid_io_range(off_t offset, size_t length)
30 uint64_t last_byte_ofs;
32 if (offset < 0) {
33 return false;
36 if (offset > INT64_MAX) {
37 return false;
40 if (length > UINT32_MAX) {
41 return false;
44 last_byte_ofs = (uint64_t)offset + (uint64_t)length;
45 if (last_byte_ofs > INT64_MAX) {
46 return false;
49 return true;
52 bool sys_io_ranges_overlap(size_t c1, off_t o1,
53 size_t c2, off_t o2)
55 if (c1 == 0 || c2 == 0) {
56 return false;
58 if (o2 < o1) {
60 * o1
61 * |····c1····|
62 * o2
63 * |····c2···| ?
65 return (o2 + c2 > o1);
66 } else {
68 * o1
69 * |····c1···|
70 * o2
71 * |····c2····| ?
73 return (o1 + c1 > o2);
77 off_t sys_block_align_truncate(off_t len, off_t align)
79 assert(align > 1);
80 assert(((align - 1) & align) == 0);
81 return len & (~align + 1);
84 off_t sys_block_align(off_t len, off_t align)
86 assert(align > 1);
87 assert(((align - 1) & align) == 0);
88 return (len + (align - 1)) & ~(align - 1);
91 /*******************************************************************
92 A read wrapper that will deal with EINTR/EWOULDBLOCK
93 ********************************************************************/
95 ssize_t sys_read(int fd, void *buf, size_t count)
97 ssize_t ret;
99 do {
100 ret = read(fd, buf, count);
101 } while (ret == -1 && (errno == EINTR || errno == EAGAIN ||
102 errno == EWOULDBLOCK));
104 return ret;
108 * read wrapper, void variant:
109 * This is intended to be used as a void variant of
110 * read in situations where the caller wants to ignore
111 * the result. Hence not checking for EAGAIN|EWOULDBLOCK.
113 void sys_read_v(int fd, void *buf, size_t count)
115 ssize_t ret;
117 do {
118 ret = read(fd, buf, count);
119 } while (ret == -1 && errno == EINTR);
123 /*******************************************************************
124 A write wrapper that will deal with EINTR/EWOULDBLOCK.
125 ********************************************************************/
127 ssize_t sys_write(int fd, const void *buf, size_t count)
129 ssize_t ret;
131 do {
132 ret = write(fd, buf, count);
133 } while (ret == -1 && (errno == EINTR || errno == EAGAIN ||
134 errno == EWOULDBLOCK));
136 return ret;
140 * write wrapper to deal with EINTR and friends.
141 * void-variant that ignores the number of bytes written.
142 * This is intended to be used as a void variant of
143 * write in situations where the caller wants to ignore
144 * the result. Hence not checking for EAGAIN|EWOULDBLOCK.
146 void sys_write_v(int fd, const void *buf, size_t count)
148 ssize_t ret;
150 do {
151 ret = write(fd, buf, count);
152 } while (ret == -1 && errno == EINTR);
156 /*******************************************************************
157 A writev wrapper that will deal with EINTR.
158 ********************************************************************/
160 ssize_t sys_writev(int fd, const struct iovec *iov, int iovcnt)
162 ssize_t ret;
164 do {
165 ret = writev(fd, iov, iovcnt);
166 } while (ret == -1 && (errno == EINTR || errno == EAGAIN ||
167 errno == EWOULDBLOCK));
169 return ret;
172 /*******************************************************************
173 A pread wrapper that will deal with EINTR
174 ********************************************************************/
176 ssize_t sys_pread(int fd, void *buf, size_t count, off_t off)
178 ssize_t ret;
180 do {
181 ret = pread(fd, buf, count, off);
182 } while (ret == -1 && errno == EINTR);
183 return ret;
186 /*******************************************************************
187 A pread wrapper that will deal with EINTR and never return a short
188 read unless pread returns zero meaning EOF.
189 ********************************************************************/
191 ssize_t sys_pread_full(int fd, void *buf, size_t count, off_t off)
193 ssize_t total_read = 0;
194 uint8_t *curr_buf = (uint8_t *)buf;
195 size_t curr_count = count;
196 off_t curr_off = off;
197 bool ok;
199 ok = sys_valid_io_range(off, count);
200 if (!ok) {
201 errno = EINVAL;
202 return -1;
205 while (curr_count != 0) {
206 ssize_t ret = sys_pread(fd,
207 curr_buf,
208 curr_count,
209 curr_off);
211 if (ret == -1) {
212 return -1;
214 if (ret == 0) {
215 /* EOF */
216 break;
219 if (ret > curr_count) {
220 errno = EIO;
221 return -1;
224 curr_buf += ret;
225 curr_count -= ret;
226 curr_off += ret;
228 total_read += ret;
231 return total_read;
234 /*******************************************************************
235 A write wrapper that will deal with EINTR
236 ********************************************************************/
238 ssize_t sys_pwrite(int fd, const void *buf, size_t count, off_t off)
240 ssize_t ret;
242 do {
243 ret = pwrite(fd, buf, count, off);
244 } while (ret == -1 && errno == EINTR);
245 return ret;
248 /*******************************************************************
249 A pwrite wrapper that will deal with EINTR and never allow a short
250 write unless the file system returns an error.
251 ********************************************************************/
253 ssize_t sys_pwrite_full(int fd, const void *buf, size_t count, off_t off)
255 ssize_t total_written = 0;
256 const uint8_t *curr_buf = (const uint8_t *)buf;
257 size_t curr_count = count;
258 off_t curr_off = off;
259 bool ok;
261 ok = sys_valid_io_range(off, count);
262 if (!ok) {
263 errno = EINVAL;
264 return -1;
267 while (curr_count != 0) {
268 ssize_t ret = sys_pwrite(fd,
269 curr_buf,
270 curr_count,
271 curr_off);
273 if (ret == -1) {
274 return -1;
276 if (ret == 0) {
277 /* Ensure we can never spin. */
278 errno = ENOSPC;
279 return -1;
282 if (ret > curr_count) {
283 errno = EIO;
284 return -1;
287 curr_buf += ret;
288 curr_count -= ret;
289 curr_off += ret;
291 total_written += ret;
294 return total_written;