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/>.
24 #include "system/filesys.h"
25 #include "lib/util/sys_rw.h"
28 bool sys_valid_io_range(off_t offset
, size_t length
)
30 uint64_t last_byte_ofs
;
36 if (offset
> INT64_MAX
) {
40 if (length
> UINT32_MAX
) {
44 last_byte_ofs
= (uint64_t)offset
+ (uint64_t)length
;
45 if (last_byte_ofs
> INT64_MAX
) {
52 bool sys_io_ranges_overlap(size_t c1
, off_t o1
,
55 if (c1
== 0 || c2
== 0) {
65 return (o2
+ c2
> o1
);
73 return (o1
+ c1
> o2
);
77 off_t
sys_block_align_truncate(off_t len
, off_t align
)
80 assert(((align
- 1) & align
) == 0);
81 return len
& (~align
+ 1);
84 off_t
sys_block_align(off_t len
, off_t align
)
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
)
100 ret
= read(fd
, buf
, count
);
101 } while (ret
== -1 && (errno
== EINTR
|| errno
== EAGAIN
||
102 errno
== EWOULDBLOCK
));
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
)
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
)
132 ret
= write(fd
, buf
, count
);
133 } while (ret
== -1 && (errno
== EINTR
|| errno
== EAGAIN
||
134 errno
== EWOULDBLOCK
));
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
)
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
)
165 ret
= writev(fd
, iov
, iovcnt
);
166 } while (ret
== -1 && (errno
== EINTR
|| errno
== EAGAIN
||
167 errno
== EWOULDBLOCK
));
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
)
181 ret
= pread(fd
, buf
, count
, off
);
182 } while (ret
== -1 && errno
== EINTR
);
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
;
199 ok
= sys_valid_io_range(off
, count
);
205 while (curr_count
!= 0) {
206 ssize_t ret
= sys_pread(fd
,
219 if (ret
> curr_count
) {
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
)
243 ret
= pwrite(fd
, buf
, count
, off
);
244 } while (ret
== -1 && errno
== EINTR
);
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
;
261 ok
= sys_valid_io_range(off
, count
);
267 while (curr_count
!= 0) {
268 ssize_t ret
= sys_pwrite(fd
,
277 /* Ensure we can never spin. */
282 if (ret
> curr_count
) {
291 total_written
+= ret
;
294 return total_written
;
297 /*******************************************************************
298 A write wrapper that will deal with EINTR and never allow a short
299 write unless the file system returns an error.
300 ********************************************************************/
302 ssize_t
sys_write_full(int fd
, const void *buf
, size_t count
)
304 ssize_t total_written
= 0;
305 const uint8_t *curr_buf
= (const uint8_t *)buf
;
306 size_t curr_count
= count
;
308 while (curr_count
!= 0) {
309 ssize_t ret
= sys_write(fd
,
316 /* Ensure we can never spin. */
321 if (ret
> curr_count
) {
329 total_written
+= ret
;
332 return total_written
;