pub fn sys_read(_fd: u64, _buf: &mut [u8]) -> SyscallResult<usize>Expand description
Read data from an open file descriptor.
Reads up to buf.len() bytes from the file descriptor into the
provided buffer, starting at the file’s current offset. The file offset
is advanced by the number of bytes read. Partial reads are possible and
normal: the kernel may return fewer bytes than requested if fewer are
available (e.g., near end-of-file or for device files).
A return value of Ok(0) indicates end-of-file (EOF): the current offset
is at or past the end of the file and no more data can be read.
For special file descriptors: fd 0 (stdin) reads from the process’s standard input source (terminal or pipe).
§Arguments
fd- File descriptor returned by a prior call tosys_open. Must be opened with at leastO_RDONLYorO_RDWRaccess.buf- Mutable buffer to read data into. The kernel reads at mostbuf.len()bytes. Must be non-empty.
§Returns
Ok(bytes_read)- The number of bytes actually read, which may be less thanbuf.len(). ReturnsOk(0)at end-of-file.
§Errors
SyscallError::BadDescriptor-fdis not a valid open file descriptor.SyscallError::PermissionDenied- The file descriptor was not opened for reading.SyscallError::InvalidArgument-bufhas zero length.SyscallError::IoError- An I/O error occurred reading from the underlying storage or device.
§Examples
use veridian_kernel::pkg::sdk::syscall_api::{sys_close, sys_open, sys_read};
let fd = sys_open("/etc/hostname", 0x000).expect("open failed");
let mut buf = [0u8; 256];
let bytes = sys_read(fd, &mut buf).expect("read failed");
if bytes == 0 {
// End of file reached
}
let data = &buf[..bytes];
sys_close(fd).expect("close failed");