| 206 | #[cfg(feature = "fs")] |
| 207 | #[test] |
| 208 | fn test_pwritev2() { |
| 209 | use rustix::fs::{openat, seek, Mode, OFlags, SeekFrom, CWD}; |
| 210 | use rustix::io::{preadv2, pwritev2, writev, ReadWriteFlags}; |
| 211 | |
| 212 | let tmp = tempfile::tempdir().unwrap(); |
| 213 | let dir = openat(CWD, tmp.path(), OFlags::RDONLY, Mode::empty()).unwrap(); |
| 214 | let file = openat( |
| 215 | &dir, |
| 216 | "file", |
| 217 | OFlags::RDWR | OFlags::CREATE | OFlags::TRUNC, |
| 218 | Mode::RUSR | Mode::WUSR, |
| 219 | ) |
| 220 | .unwrap(); |
| 221 | |
| 222 | writev(&file, &[IoSlice::new(b"hello")]).unwrap(); |
| 223 | seek(&file, SeekFrom::Start(0)).unwrap(); |
| 224 | |
| 225 | // pwritev2 to append with a 0 offset: don't update the current position. |
| 226 | match pwritev2(&file, &[IoSlice::new(b"world")], 0, ReadWriteFlags::APPEND) { |
| 227 | Ok(_) => {} |
| 228 | // Skip the rest of the test if we don't have `pwritev2` and |
| 229 | // `RWF_APPEND`. |
| 230 | Err(rustix::io::Errno::NOSYS | rustix::io::Errno::NOTSUP) => return, |
| 231 | Err(err) => panic!("{:?}", err), |
| 232 | } |
| 233 | assert_eq!(seek(&file, SeekFrom::Current(0)).unwrap(), 0); |
| 234 | |
| 235 | // pwritev2 to append with a !0 offset: do update the current position. |
| 236 | pwritev2(&file, &[IoSlice::new(b"world")], !0, ReadWriteFlags::APPEND).unwrap(); |
| 237 | assert_eq!(seek(&file, SeekFrom::Current(0)).unwrap(), 15); |
| 238 | |
| 239 | seek(&file, SeekFrom::Start(0)).unwrap(); |
| 240 | let mut buf = [0_u8; 5]; |
| 241 | preadv2( |
| 242 | &file, |
| 243 | &mut [IoSliceMut::new(&mut buf)], |
| 244 | 0, |
| 245 | ReadWriteFlags::empty(), |
| 246 | ) |
| 247 | .unwrap(); |
| 248 | assert_eq!(&buf, b"hello"); |
| 249 | preadv2( |
| 250 | &file, |
| 251 | &mut [IoSliceMut::new(&mut buf)], |
| 252 | 5, |
| 253 | ReadWriteFlags::empty(), |
| 254 | ) |
| 255 | .unwrap(); |
| 256 | assert_eq!(&buf, b"world"); |
| 257 | } |
| 258 | |
| 259 | #[cfg(all(linux_raw_dep, not(target_os = "android")))] |
| 260 | #[cfg(all(feature = "net", feature = "pipe"))] |