Skip to content

Commit

Permalink
Add a tips for writing 0
Browse files Browse the repository at this point in the history
  • Loading branch information
mohanson committed Nov 18, 2024
1 parent 2b350fb commit 6b764b8
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 deletions.
7 changes: 4 additions & 3 deletions src/syscalls/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -735,12 +735,13 @@ pub fn read(fd: u64, buffer: &mut [u8]) -> Result<usize, SysError> {

/// This syscall writes data to a pipe via a file descriptor. The syscall Write writes up to value pointed by length
/// bytes from the buffer, and the actual length of data written is returned.
///
/// If buffer is empty and fd is avaliable, then write() can still succeed: A data with a length of 0 is written to the
/// pipe. The peer needs to use a read() syscall to consume the empty data, and read() will returns Ok(0).
///
/// Note: available after ckb 2nd hardfork.
pub fn write(fd: u64, buffer: &[u8]) -> Result<usize, SysError> {
let mut l: u64 = buffer.len() as u64;
if l == 0 {
return Ok(0);
}
let ret = unsafe {
syscall(
fd,
Expand Down
3 changes: 0 additions & 3 deletions src/syscalls/simulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,9 +345,6 @@ pub fn read(fd: u64, buffer: &mut [u8]) -> Result<usize, SysError> {

pub fn write(fd: u64, buffer: &[u8]) -> Result<usize, SysError> {
let mut l = buffer.len();
if l == 0 {
return Ok(0);
}
let ret = sim::ckb_write(fd, buffer.as_ptr() as *mut c_void, &mut l as *mut usize) as u64;
match ret {
0 => Ok(l as usize),
Expand Down

0 comments on commit 6b764b8

Please sign in to comment.