|
1 | 1 | use more_asserts::assert_gt;
|
2 | 2 | use std::{env, process};
|
| 3 | +use std::convert::TryInto; |
3 | 4 | use wasi_tests::open_scratch_directory;
|
4 | 5 |
|
5 | 6 | unsafe fn test_file_pread_pwrite(dir_fd: wasi::Fd) {
|
@@ -38,6 +39,71 @@ unsafe fn test_file_pread_pwrite(dir_fd: wasi::Fd) {
|
38 | 39 | assert_eq!(nread, 4, "nread bytes check");
|
39 | 40 | assert_eq!(contents, &[0u8, 1, 2, 3], "written bytes equal read bytes");
|
40 | 41 |
|
| 42 | + // Write all the data through multiple iovecs. |
| 43 | + // |
| 44 | + // Note that this needs to be done with a loop, because some |
| 45 | + // platforms do not support writing multiple iovecs at once. |
| 46 | + // See https://github.com/rust-lang/rust/issues/74825. |
| 47 | + let contents = &[0u8, 1, 2, 3]; |
| 48 | + let mut offset = 0usize; |
| 49 | + loop { |
| 50 | + let mut ciovecs: Vec<wasi::Ciovec> = Vec::new(); |
| 51 | + let mut remaining = contents.len() - offset; |
| 52 | + if remaining > 2 { |
| 53 | + ciovecs.push( |
| 54 | + wasi::Ciovec { |
| 55 | + buf: contents[offset..].as_ptr() as *const _, |
| 56 | + buf_len: 2, |
| 57 | + }, |
| 58 | + ); |
| 59 | + remaining -= 2; |
| 60 | + } |
| 61 | + ciovecs.push( |
| 62 | + wasi::Ciovec { |
| 63 | + buf: contents[contents.len() - remaining..].as_ptr() as *const _, |
| 64 | + buf_len: remaining |
| 65 | + }, |
| 66 | + ); |
| 67 | + |
| 68 | + nwritten = |
| 69 | + wasi::fd_pwrite(file_fd, ciovecs.as_slice(), offset.try_into().unwrap()).expect("writing bytes at offset 0"); |
| 70 | + |
| 71 | + offset += nwritten; |
| 72 | + if offset == contents.len() { |
| 73 | + break; |
| 74 | + } |
| 75 | + } |
| 76 | + assert_eq!(offset, 4, "nread bytes check"); |
| 77 | + |
| 78 | + // Read all the data through multiple iovecs. |
| 79 | + // |
| 80 | + // Note that this needs to be done with a loop, because some |
| 81 | + // platforms do not support reading multiple iovecs at once. |
| 82 | + // See https://github.com/rust-lang/rust/issues/74825. |
| 83 | + let contents = &mut [0u8; 4]; |
| 84 | + let mut offset = 0usize; |
| 85 | + loop { |
| 86 | + let buffer = &mut [0u8; 4]; |
| 87 | + let iovecs = &[ |
| 88 | + wasi::Iovec { |
| 89 | + buf: buffer.as_mut_ptr() as *mut _, |
| 90 | + buf_len: 2, |
| 91 | + }, |
| 92 | + wasi::Iovec { |
| 93 | + buf: buffer[2..].as_mut_ptr() as *mut _, |
| 94 | + buf_len: 2 |
| 95 | + }, |
| 96 | + ]; |
| 97 | + nread = wasi::fd_pread(file_fd, iovecs, offset as _).expect("reading bytes at offset 0"); |
| 98 | + if nread == 0 { |
| 99 | + break; |
| 100 | + } |
| 101 | + contents[offset..offset+nread].copy_from_slice(&buffer[0..nread]); |
| 102 | + offset += nread; |
| 103 | + } |
| 104 | + assert_eq!(offset, 4, "nread bytes check"); |
| 105 | + assert_eq!(contents, &[0u8, 1, 2, 3], "file cursor was overwritten"); |
| 106 | + |
41 | 107 | let contents = &mut [0u8; 4];
|
42 | 108 | let iovec = wasi::Iovec {
|
43 | 109 | buf: contents.as_mut_ptr() as *mut _,
|
|
0 commit comments