Use unsigned return values in read()-like Pal fns.
Returning a negative number of bytes makes absolutely no sense, besides the "-1 and errno" pattern, which is now converted to Result<_, Errno>.
This commit is contained in:
@@ -62,7 +62,7 @@ impl File {
|
||||
|
||||
impl io::Read for &File {
|
||||
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
||||
match Sys::read(self.fd, buf).or_minus_one_errno() /* TODO */ {
|
||||
match Sys::read(self.fd, buf).map(|read| read as ssize_t).or_minus_one_errno() /* TODO */ {
|
||||
-1 => Err(io::last_os_error()),
|
||||
ok => Ok(ok as usize),
|
||||
}
|
||||
@@ -71,7 +71,10 @@ impl io::Read for &File {
|
||||
|
||||
impl io::Write for &File {
|
||||
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
||||
match Sys::write(self.fd, buf).or_minus_one_errno() {
|
||||
match Sys::write(self.fd, buf)
|
||||
.map(|read| read as ssize_t)
|
||||
.or_minus_one_errno()
|
||||
{
|
||||
-1 => Err(io::last_os_error()),
|
||||
ok => Ok(ok as usize),
|
||||
}
|
||||
|
||||
@@ -12,7 +12,9 @@ pub(super) unsafe fn openpty(name: &mut [u8]) -> Result<(c_int, c_int), ()> {
|
||||
}
|
||||
|
||||
// TODO: better error handling
|
||||
let count = Sys::fpath(master, name).or_minus_one_errno();
|
||||
let count = Sys::fpath(master, name)
|
||||
.map(|u| u as ssize_t)
|
||||
.or_minus_one_errno();
|
||||
if count < 0 {
|
||||
unistd::close(master);
|
||||
return Err(());
|
||||
|
||||
@@ -986,7 +986,9 @@ pub unsafe extern "C" fn realpath(pathname: *const c_char, resolved: *mut c_char
|
||||
|
||||
let len = out.len();
|
||||
// TODO: better error handling
|
||||
let read = Sys::fpath(*file, &mut out[..len - 1]).or_minus_one_errno();
|
||||
let read = Sys::fpath(*file, &mut out[..len - 1])
|
||||
.map(|read| read as ssize_t)
|
||||
.or_minus_one_errno();
|
||||
if read < 0 {
|
||||
return ptr::null_mut();
|
||||
}
|
||||
|
||||
@@ -14,5 +14,6 @@ pub unsafe extern "C" fn getrandom(buf: *mut c_void, buflen: size_t, flags: c_ui
|
||||
slice::from_raw_parts_mut(buf as *mut u8, buflen as usize),
|
||||
flags,
|
||||
)
|
||||
.map(|read| read as ssize_t)
|
||||
.or_minus_one_errno()
|
||||
}
|
||||
|
||||
@@ -647,6 +647,7 @@ pub unsafe extern "C" fn pread(
|
||||
slice::from_raw_parts_mut(buf.cast::<u8>(), nbyte),
|
||||
offset,
|
||||
)
|
||||
.map(|read| read as ssize_t)
|
||||
.or_minus_one_errno()
|
||||
}
|
||||
|
||||
@@ -681,6 +682,7 @@ pub unsafe extern "C" fn pwrite(
|
||||
slice::from_raw_parts(buf.cast::<u8>(), nbyte),
|
||||
offset,
|
||||
)
|
||||
.map(|read| read as ssize_t)
|
||||
.or_minus_one_errno()
|
||||
}
|
||||
|
||||
@@ -688,7 +690,9 @@ pub unsafe extern "C" fn pwrite(
|
||||
pub unsafe extern "C" fn read(fildes: c_int, buf: *const c_void, nbyte: size_t) -> ssize_t {
|
||||
let buf = unsafe { slice::from_raw_parts_mut(buf as *mut u8, nbyte as usize) };
|
||||
trace_expr!(
|
||||
Sys::read(fildes, buf).or_minus_one_errno(),
|
||||
Sys::read(fildes, buf)
|
||||
.map(|read| read as ssize_t)
|
||||
.or_minus_one_errno(),
|
||||
"read({}, {:p}, {})",
|
||||
fildes,
|
||||
buf,
|
||||
@@ -704,7 +708,9 @@ pub unsafe extern "C" fn readlink(
|
||||
) -> ssize_t {
|
||||
let path = CStr::from_ptr(path);
|
||||
let buf = slice::from_raw_parts_mut(buf as *mut u8, bufsize as usize);
|
||||
Sys::readlink(path, buf).or_minus_one_errno()
|
||||
Sys::readlink(path, buf)
|
||||
.map(|read| read as ssize_t)
|
||||
.or_minus_one_errno()
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
@@ -862,7 +868,9 @@ pub extern "C" fn ttyname_r(fildes: c_int, name: *mut c_char, namesize: size_t)
|
||||
return errno::ERANGE;
|
||||
}
|
||||
|
||||
let len = Sys::fpath(fildes, &mut name[..namesize - 1]).or_minus_one_errno();
|
||||
let len = Sys::fpath(fildes, &mut name[..namesize - 1])
|
||||
.map(|read| read as ssize_t)
|
||||
.or_minus_one_errno();
|
||||
if len < 0 {
|
||||
return -platform::ERRNO.get();
|
||||
}
|
||||
@@ -920,5 +928,7 @@ pub extern "C" fn vfork() -> pid_t {
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn write(fildes: c_int, buf: *const c_void, nbyte: size_t) -> ssize_t {
|
||||
let buf = slice::from_raw_parts(buf as *const u8, nbyte as usize);
|
||||
Sys::write(fildes, buf).or_minus_one_errno()
|
||||
Sys::write(fildes, buf)
|
||||
.map(|bytes| bytes as ssize_t)
|
||||
.or_minus_one_errno()
|
||||
}
|
||||
|
||||
+12
-16
@@ -226,7 +226,7 @@ impl Pal for Sys {
|
||||
Ok(e_raw(unsafe { syscall!(CLONE, SIGCHLD, 0, 0, 0, 0) })? as pid_t)
|
||||
}
|
||||
|
||||
fn fpath(fildes: c_int, out: &mut [u8]) -> Result<ssize_t> {
|
||||
fn fpath(fildes: c_int, out: &mut [u8]) -> Result<usize> {
|
||||
let mut proc_path = b"/proc/self/fd/".to_vec();
|
||||
write!(proc_path, "{}", fildes).unwrap();
|
||||
proc_path.push(0);
|
||||
@@ -331,8 +331,8 @@ impl Pal for Sys {
|
||||
Ok(e_raw(unsafe { syscall!(GETPRIORITY, which, who) })? as c_int)
|
||||
}
|
||||
|
||||
fn getrandom(buf: &mut [u8], flags: c_uint) -> Result<ssize_t> {
|
||||
Ok(e_raw(unsafe { syscall!(GETRANDOM, buf.as_mut_ptr(), buf.len(), flags) })? as ssize_t)
|
||||
fn getrandom(buf: &mut [u8], flags: c_uint) -> Result<usize> {
|
||||
e_raw(unsafe { syscall!(GETRANDOM, buf.as_mut_ptr(), buf.len(), flags) })
|
||||
}
|
||||
|
||||
unsafe fn getrlimit(resource: c_int, rlim: *mut rlimit) -> Result<()> {
|
||||
@@ -540,17 +540,14 @@ impl Pal for Sys {
|
||||
}
|
||||
}
|
||||
|
||||
fn read(fildes: c_int, buf: &mut [u8]) -> Result<ssize_t> {
|
||||
Ok(e_raw(unsafe { syscall!(READ, fildes, buf.as_mut_ptr(), buf.len()) })? as ssize_t)
|
||||
fn read(fildes: c_int, buf: &mut [u8]) -> Result<usize> {
|
||||
e_raw(unsafe { syscall!(READ, fildes, buf.as_mut_ptr(), buf.len()) })
|
||||
}
|
||||
fn pread(fildes: c_int, buf: &mut [u8], off: off_t) -> Result<ssize_t> {
|
||||
Ok(
|
||||
e_raw(unsafe { syscall!(PREAD64, fildes, buf.as_mut_ptr(), buf.len(), off) })?
|
||||
as ssize_t,
|
||||
)
|
||||
fn pread(fildes: c_int, buf: &mut [u8], off: off_t) -> Result<usize> {
|
||||
e_raw(unsafe { syscall!(PREAD64, fildes, buf.as_mut_ptr(), buf.len(), off) })
|
||||
}
|
||||
|
||||
fn readlink(pathname: CStr, out: &mut [u8]) -> Result<ssize_t> {
|
||||
fn readlink(pathname: CStr, out: &mut [u8]) -> Result<usize> {
|
||||
e_raw(unsafe {
|
||||
syscall!(
|
||||
READLINKAT,
|
||||
@@ -560,7 +557,6 @@ impl Pal for Sys {
|
||||
out.len()
|
||||
)
|
||||
})
|
||||
.map(|b| b as ssize_t)
|
||||
}
|
||||
|
||||
fn rename(old: CStr, new: CStr) -> Result<()> {
|
||||
@@ -624,11 +620,11 @@ impl Pal for Sys {
|
||||
e_raw(unsafe { syscall!(WAIT4, pid, stat_loc, options, 0) }).map(|p| p as pid_t)
|
||||
}
|
||||
|
||||
fn write(fildes: c_int, buf: &[u8]) -> Result<ssize_t> {
|
||||
Ok(e_raw(unsafe { syscall!(WRITE, fildes, buf.as_ptr(), buf.len()) })? as ssize_t)
|
||||
fn write(fildes: c_int, buf: &[u8]) -> Result<usize> {
|
||||
e_raw(unsafe { syscall!(WRITE, fildes, buf.as_ptr(), buf.len()) })
|
||||
}
|
||||
fn pwrite(fildes: c_int, buf: &[u8], off: off_t) -> Result<ssize_t> {
|
||||
Ok(e_raw(unsafe { syscall!(PWRITE64, fildes, buf.as_ptr(), buf.len(), off) })? as ssize_t)
|
||||
fn pwrite(fildes: c_int, buf: &[u8], off: off_t) -> Result<usize> {
|
||||
e_raw(unsafe { syscall!(PWRITE64, fildes, buf.as_ptr(), buf.len(), off) })
|
||||
}
|
||||
|
||||
fn verify() -> bool {
|
||||
|
||||
+10
-3
@@ -86,7 +86,9 @@ pub struct FileWriter(pub c_int);
|
||||
|
||||
impl FileWriter {
|
||||
pub fn write(&mut self, buf: &[u8]) -> isize {
|
||||
Sys::write(self.0, buf).or_minus_one_errno()
|
||||
Sys::write(self.0, buf)
|
||||
.map(|u| u as isize)
|
||||
.or_minus_one_errno()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -107,14 +109,19 @@ impl WriteByte for FileWriter {
|
||||
pub struct FileReader(pub c_int);
|
||||
|
||||
impl FileReader {
|
||||
// TODO: This is a bad interface. Rustify
|
||||
pub fn read(&mut self, buf: &mut [u8]) -> isize {
|
||||
Sys::read(self.0, buf).or_minus_one_errno()
|
||||
Sys::read(self.0, buf)
|
||||
.map(|u| u as isize)
|
||||
.or_minus_one_errno()
|
||||
}
|
||||
}
|
||||
|
||||
impl Read for FileReader {
|
||||
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
||||
let i = Sys::read(self.0, buf).or_minus_one_errno(); // TODO
|
||||
let i = Sys::read(self.0, buf)
|
||||
.map(|u| u as isize)
|
||||
.or_minus_one_errno(); // TODO
|
||||
if i >= 0 {
|
||||
Ok(i as usize)
|
||||
} else {
|
||||
|
||||
@@ -79,7 +79,7 @@ pub trait Pal {
|
||||
|
||||
unsafe fn fork() -> Result<pid_t>;
|
||||
|
||||
fn fpath(fildes: c_int, out: &mut [u8]) -> Result<ssize_t>;
|
||||
fn fpath(fildes: c_int, out: &mut [u8]) -> Result<usize>;
|
||||
|
||||
fn fsync(fildes: c_int) -> Result<()>;
|
||||
|
||||
@@ -128,7 +128,7 @@ pub trait Pal {
|
||||
|
||||
fn getpriority(which: c_int, who: id_t) -> Result<c_int>;
|
||||
|
||||
fn getrandom(buf: &mut [u8], flags: c_uint) -> Result<ssize_t>;
|
||||
fn getrandom(buf: &mut [u8], flags: c_uint) -> Result<usize>;
|
||||
|
||||
unsafe fn getrlimit(resource: c_int, rlim: *mut rlimit) -> Result<()>;
|
||||
|
||||
@@ -203,10 +203,10 @@ pub trait Pal {
|
||||
|
||||
fn current_os_tid() -> pthread::OsTid;
|
||||
|
||||
fn read(fildes: c_int, buf: &mut [u8]) -> Result<ssize_t>;
|
||||
fn pread(fildes: c_int, buf: &mut [u8], offset: off_t) -> Result<ssize_t>;
|
||||
fn read(fildes: c_int, buf: &mut [u8]) -> Result<usize>;
|
||||
fn pread(fildes: c_int, buf: &mut [u8], offset: off_t) -> Result<usize>;
|
||||
|
||||
fn readlink(pathname: CStr, out: &mut [u8]) -> Result<ssize_t>;
|
||||
fn readlink(pathname: CStr, out: &mut [u8]) -> Result<usize>;
|
||||
|
||||
fn rename(old: CStr, new: CStr) -> Result<()>;
|
||||
|
||||
@@ -239,8 +239,8 @@ pub trait Pal {
|
||||
|
||||
unsafe fn waitpid(pid: pid_t, stat_loc: *mut c_int, options: c_int) -> Result<pid_t>;
|
||||
|
||||
fn write(fildes: c_int, buf: &[u8]) -> Result<ssize_t>;
|
||||
fn pwrite(fildes: c_int, buf: &[u8], offset: off_t) -> Result<ssize_t>;
|
||||
fn write(fildes: c_int, buf: &[u8]) -> Result<usize>;
|
||||
fn pwrite(fildes: c_int, buf: &[u8], offset: off_t) -> Result<usize>;
|
||||
|
||||
fn verify() -> bool;
|
||||
}
|
||||
|
||||
@@ -69,6 +69,7 @@ impl PalEpoll for Sys {
|
||||
data: unsafe { (*event).data.u64 as usize },
|
||||
},
|
||||
)
|
||||
.map(|u| u as ssize_t)
|
||||
.or_minus_one_errno()
|
||||
< 0
|
||||
{
|
||||
@@ -87,6 +88,7 @@ impl PalEpoll for Sys {
|
||||
data: 0,
|
||||
},
|
||||
)
|
||||
.map(|u| u as ssize_t)
|
||||
.or_minus_one_errno()
|
||||
< 0
|
||||
{
|
||||
@@ -129,6 +131,7 @@ impl PalEpoll for Sys {
|
||||
data: 0,
|
||||
},
|
||||
)
|
||||
.map(|u| u as ssize_t)
|
||||
.or_minus_one_errno()
|
||||
== -1
|
||||
{
|
||||
@@ -158,6 +161,7 @@ impl PalEpoll for Sys {
|
||||
maxevents as usize * mem::size_of::<syscall::Event>(),
|
||||
)
|
||||
})
|
||||
.map(|u| u as ssize_t)
|
||||
.or_minus_one_errno(); // TODO
|
||||
if bytes_read == -1 {
|
||||
return -1;
|
||||
|
||||
+13
-13
@@ -446,7 +446,7 @@ impl Pal for Sys {
|
||||
Err(Errno(ENOSYS))
|
||||
}
|
||||
|
||||
fn getrandom(buf: &mut [u8], flags: c_uint) -> Result<ssize_t> {
|
||||
fn getrandom(buf: &mut [u8], flags: c_uint) -> Result<usize> {
|
||||
let path = if flags & sys_random::GRND_RANDOM != 0 {
|
||||
//TODO: /dev/random equivalent
|
||||
"/scheme/rand"
|
||||
@@ -461,7 +461,7 @@ impl Pal for Sys {
|
||||
|
||||
//TODO: store fd internally
|
||||
let fd = FdGuard::new(syscall::open(path, open_flags)?);
|
||||
Ok(syscall::read(*fd, buf)? as ssize_t)
|
||||
Ok(syscall::read(*fd, buf)?)
|
||||
}
|
||||
|
||||
unsafe fn getrlimit(resource: c_int, rlim: *mut rlimit) -> Result<()> {
|
||||
@@ -735,11 +735,11 @@ impl Pal for Sys {
|
||||
}
|
||||
}
|
||||
|
||||
fn read(fd: c_int, buf: &mut [u8]) -> Result<ssize_t> {
|
||||
fn read(fd: c_int, buf: &mut [u8]) -> Result<usize> {
|
||||
let fd = usize::try_from(fd).map_err(|_| Errno(EBADF))?;
|
||||
Ok(redox_rt::sys::posix_read(fd, buf)? as ssize_t)
|
||||
Ok(redox_rt::sys::posix_read(fd, buf)?)
|
||||
}
|
||||
fn pread(fd: c_int, buf: &mut [u8], offset: off_t) -> Result<ssize_t> {
|
||||
fn pread(fd: c_int, buf: &mut [u8], offset: off_t) -> Result<usize> {
|
||||
unsafe {
|
||||
Ok(syscall::syscall5(
|
||||
syscall::SYS_READ2,
|
||||
@@ -748,11 +748,11 @@ impl Pal for Sys {
|
||||
buf.len(),
|
||||
offset as usize,
|
||||
!0,
|
||||
)? as ssize_t)
|
||||
)?)
|
||||
}
|
||||
}
|
||||
|
||||
fn fpath(fildes: c_int, out: &mut [u8]) -> Result<ssize_t> {
|
||||
fn fpath(fildes: c_int, out: &mut [u8]) -> Result<usize> {
|
||||
// Since this is used by realpath, it converts from the old format to the new one for
|
||||
// compatibility reasons
|
||||
let mut buf = [0; limits::PATH_MAX];
|
||||
@@ -776,12 +776,12 @@ impl Pal for Sys {
|
||||
),
|
||||
};
|
||||
match res {
|
||||
Ok(()) => Ok(cursor.position() as ssize_t),
|
||||
Ok(()) => Ok(cursor.position() as usize),
|
||||
Err(_err) => Err(Errno(ENAMETOOLONG)),
|
||||
}
|
||||
}
|
||||
|
||||
fn readlink(pathname: CStr, out: &mut [u8]) -> Result<ssize_t> {
|
||||
fn readlink(pathname: CStr, out: &mut [u8]) -> Result<usize> {
|
||||
let file = File::open(
|
||||
pathname,
|
||||
fcntl::O_RDONLY | fcntl::O_SYMLINK | fcntl::O_CLOEXEC,
|
||||
@@ -1017,11 +1017,11 @@ impl Pal for Sys {
|
||||
Ok(res? as pid_t)
|
||||
}
|
||||
|
||||
fn write(fd: c_int, buf: &[u8]) -> Result<ssize_t> {
|
||||
fn write(fd: c_int, buf: &[u8]) -> Result<usize> {
|
||||
let fd = usize::try_from(fd).map_err(|_| Errno(EBADFD))?;
|
||||
Ok(redox_rt::sys::posix_write(fd, buf)? as ssize_t)
|
||||
Ok(redox_rt::sys::posix_write(fd, buf)?)
|
||||
}
|
||||
fn pwrite(fd: c_int, buf: &[u8], offset: off_t) -> Result<ssize_t> {
|
||||
fn pwrite(fd: c_int, buf: &[u8], offset: off_t) -> Result<usize> {
|
||||
unsafe {
|
||||
Ok(syscall::syscall5(
|
||||
syscall::SYS_WRITE2,
|
||||
@@ -1030,7 +1030,7 @@ impl Pal for Sys {
|
||||
buf.len(),
|
||||
offset as usize,
|
||||
!0,
|
||||
)? as ssize_t)
|
||||
)?)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -306,7 +306,9 @@ impl PalSocket for Sys {
|
||||
return -1;
|
||||
}
|
||||
if address == ptr::null_mut() || address_len == ptr::null_mut() {
|
||||
Self::read(socket, slice::from_raw_parts_mut(buf as *mut u8, len)).or_minus_one_errno()
|
||||
Self::read(socket, slice::from_raw_parts_mut(buf as *mut u8, len))
|
||||
.map(|u| u as ssize_t)
|
||||
.or_minus_one_errno()
|
||||
} else {
|
||||
let fd = e(syscall::dup(socket as usize, b"listen"));
|
||||
if fd == !0 {
|
||||
@@ -318,6 +320,7 @@ impl PalSocket for Sys {
|
||||
}
|
||||
|
||||
let ret = Self::read(fd as c_int, slice::from_raw_parts_mut(buf as *mut u8, len))
|
||||
.map(|u| u as ssize_t)
|
||||
.or_minus_one_errno();
|
||||
let _ = syscall::close(fd);
|
||||
ret
|
||||
@@ -351,10 +354,13 @@ impl PalSocket for Sys {
|
||||
return -1;
|
||||
}
|
||||
if dest_addr == ptr::null() || dest_len == 0 {
|
||||
Self::write(socket, slice::from_raw_parts(buf as *const u8, len)).or_minus_one_errno()
|
||||
Self::write(socket, slice::from_raw_parts(buf as *const u8, len))
|
||||
.map(|u| u as ssize_t)
|
||||
.or_minus_one_errno()
|
||||
} else {
|
||||
let fd = bind_or_connect!(connect copy, socket, dest_addr, dest_len);
|
||||
let ret = Self::write(fd as c_int, slice::from_raw_parts(buf as *const u8, len))
|
||||
.map(|u| u as ssize_t)
|
||||
.or_minus_one_errno();
|
||||
let _ = syscall::close(fd);
|
||||
ret
|
||||
@@ -389,7 +395,9 @@ impl PalSocket for Sys {
|
||||
tv_nsec: timeval.tv_usec * 1000,
|
||||
};
|
||||
|
||||
let ret = Self::write(fd as c_int, ×pec).or_minus_one_errno();
|
||||
let ret = Self::write(fd as c_int, ×pec)
|
||||
.map(|u| u as ssize_t)
|
||||
.or_minus_one_errno();
|
||||
|
||||
let _ = syscall::close(fd);
|
||||
|
||||
|
||||
+3
-1
@@ -61,7 +61,9 @@ impl RawLineBuffer {
|
||||
self.buf.set_len(capacity);
|
||||
}
|
||||
|
||||
let read = Sys::read(self.fd, &mut self.buf[len..]).or_minus_one_errno();
|
||||
let read = Sys::read(self.fd, &mut self.buf[len..])
|
||||
.map(|u| u as isize)
|
||||
.or_minus_one_errno();
|
||||
|
||||
let read_usize = read.max(0) as usize;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user