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"
27 bool sys_valid_io_range(off_t offset
, size_t length
)
29 uint64_t last_byte_ofs
;
35 if (offset
> INT64_MAX
) {
39 if (length
> UINT32_MAX
) {
43 last_byte_ofs
= (uint64_t)offset
+ (uint64_t)length
;
44 if (last_byte_ofs
> INT64_MAX
) {
51 /*******************************************************************
52 A read wrapper that will deal with EINTR/EWOULDBLOCK
53 ********************************************************************/
55 ssize_t
sys_read(int fd
, void *buf
, size_t count
)
60 ret
= read(fd
, buf
, count
);
61 } while (ret
== -1 && (errno
== EINTR
|| errno
== EAGAIN
||
62 errno
== EWOULDBLOCK
));
68 * read wrapper, void variant:
69 * This is intended to be used as a void variant of
70 * read in situations where the caller wants to ignore
71 * the result. Hence not checking for EAGAIN|EWOULDBLOCK.
73 void sys_read_v(int fd
, void *buf
, size_t count
)
78 ret
= read(fd
, buf
, count
);
79 } while (ret
== -1 && errno
== EINTR
);
83 /*******************************************************************
84 A write wrapper that will deal with EINTR/EWOULDBLOCK.
85 ********************************************************************/
87 ssize_t
sys_write(int fd
, const void *buf
, size_t count
)
92 ret
= write(fd
, buf
, count
);
93 } while (ret
== -1 && (errno
== EINTR
|| errno
== EAGAIN
||
94 errno
== EWOULDBLOCK
));
100 * write wrapper to deal with EINTR and friends.
101 * void-variant that ignores the number of bytes written.
102 * This is intended to be used as a void variant of
103 * write in situations where the caller wants to ignore
104 * the result. Hence not checking for EAGAIN|EWOULDBLOCK.
106 void sys_write_v(int fd
, const void *buf
, size_t count
)
111 ret
= write(fd
, buf
, count
);
112 } while (ret
== -1 && errno
== EINTR
);
116 /*******************************************************************
117 A writev wrapper that will deal with EINTR.
118 ********************************************************************/
120 ssize_t
sys_writev(int fd
, const struct iovec
*iov
, int iovcnt
)
125 ret
= writev(fd
, iov
, iovcnt
);
126 } while (ret
== -1 && (errno
== EINTR
|| errno
== EAGAIN
||
127 errno
== EWOULDBLOCK
));
132 /*******************************************************************
133 A pread wrapper that will deal with EINTR
134 ********************************************************************/
136 ssize_t
sys_pread(int fd
, void *buf
, size_t count
, off_t off
)
141 ret
= pread(fd
, buf
, count
, off
);
142 } while (ret
== -1 && errno
== EINTR
);
146 /*******************************************************************
147 A pread wrapper that will deal with EINTR and never return a short
148 read unless pread returns zero meaning EOF.
149 ********************************************************************/
151 ssize_t
sys_pread_full(int fd
, void *buf
, size_t count
, off_t off
)
153 ssize_t total_read
= 0;
154 uint8_t *curr_buf
= (uint8_t *)buf
;
155 size_t curr_count
= count
;
156 off_t curr_off
= off
;
159 ok
= sys_valid_io_range(off
, count
);
165 while (curr_count
!= 0) {
166 ssize_t ret
= sys_pread(fd
,
179 if (ret
> curr_count
) {
194 /*******************************************************************
195 A write wrapper that will deal with EINTR
196 ********************************************************************/
198 ssize_t
sys_pwrite(int fd
, const void *buf
, size_t count
, off_t off
)
203 ret
= pwrite(fd
, buf
, count
, off
);
204 } while (ret
== -1 && errno
== EINTR
);
208 /*******************************************************************
209 A pwrite wrapper that will deal with EINTR and never allow a short
210 write unless the file system returns an error.
211 ********************************************************************/
213 ssize_t
sys_pwrite_full(int fd
, const void *buf
, size_t count
, off_t off
)
215 ssize_t total_written
= 0;
216 const uint8_t *curr_buf
= (const uint8_t *)buf
;
217 size_t curr_count
= count
;
218 off_t curr_off
= off
;
221 ok
= sys_valid_io_range(off
, count
);
227 while (curr_count
!= 0) {
228 ssize_t ret
= sys_pwrite(fd
,
237 /* Ensure we can never spin. */
242 if (ret
> curr_count
) {
251 total_written
+= ret
;
254 return total_written
;