Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix readv_at on device nodes #13

Merged
merged 1 commit into from
Feb 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## [Unreleased] - ReleaseDate
### Fixed
- Fixed `readv_at` on device nodes with unaligned buffers
(#[13](https://github.com/asomers/tokio-file/pull/13))


## [0.5.1] - 2019-09-05

### Changed
Expand Down
18 changes: 15 additions & 3 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,15 +179,27 @@ impl Future for LioFut {
})
});
let mut last_inner_result: Option<LioResult> = None;
let mut last_inner_ofs = 0;
let iter = obuf_iter.map(move |mut obuf_ref| {
if let Some((buf_ref, mc)) = obuf_ref.take() {
if let Some((mut buf_ref, mc)) = obuf_ref.take() {
if mc {
last_inner_result = Some(inner_iter.next().unwrap());
last_inner_ofs = 0;
}
let lir = last_inner_result.as_ref().unwrap();
if lir.value.is_ok() {
let len = buf_ref.len().unwrap() as isize;
LioResult{value: Ok(len), buf: buf_ref}
let len = buf_ref.len().unwrap();
if let BufRef::BoxedMutSlice(ref mut bms) = buf_ref {
(**bms).borrow_mut().copy_from_slice(
&match &lir.buf {
BufRef::BoxedMutSlice(bms) => (**bms).borrow(),
BufRef::BoxedSlice(bs) => (**bs).borrow(),
BufRef::None => unreachable!()
}[last_inner_ofs..(last_inner_ofs + len)]
);
last_inner_ofs += len;
}
LioResult{value: Ok(len as isize), buf: buf_ref}
} else {
let r = lir.value;
LioResult{value: r, buf: buf_ref}
Expand Down
23 changes: 15 additions & 8 deletions tests/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,15 @@ test_suite! {
Box::new(dbs.try_mut().unwrap()) as Box<dyn BorrowMut<[u8]>>
}).collect::<Vec<_>>();
let mut rt = current_thread::Runtime::new().unwrap();
let file = t!(File::open(&path));

fs::OpenOptions::new()
.write(true)
.open(&path)
.expect("open failed")
.write_all(&orig)
.expect("write failed");

let file = t!(File::open(&path));
let mut ri = rt.block_on(lazy(|| {
file.readv_at(rbufs, 0).expect("readv_at failed early")
})).unwrap();
Expand All @@ -339,13 +346,13 @@ test_suite! {
assert!(ri.next().is_none());
drop(file);

assert_eq!(&vec![0u8; 100][..], &orig[0..100]);
assert_eq!(&vec![1u8; 412][..], &orig[100..512]);
assert_eq!(&vec![2u8; 512][..], &orig[512..1024]);
assert_eq!(&vec![3u8; 100][..], &orig[1024..1124]);
assert_eq!(&vec![4u8; 100][..], &orig[1124..1224]);
assert_eq!(&vec![5u8; 312][..], &orig[1224..1536]);
assert_eq!(&vec![6u8; 512][..], &orig[1536..2048]);
assert_eq!(&dbses[0].try_const().unwrap()[..], &orig[0..100]);
assert_eq!(&dbses[1].try_const().unwrap()[..], &orig[100..512]);
assert_eq!(&dbses[2].try_const().unwrap()[..], &orig[512..1024]);
assert_eq!(&dbses[3].try_const().unwrap()[..], &orig[1024..1124]);
assert_eq!(&dbses[4].try_const().unwrap()[..], &orig[1124..1224]);
assert_eq!(&dbses[5].try_const().unwrap()[..], &orig[1224..1536]);
assert_eq!(&dbses[6].try_const().unwrap()[..], &orig[1536..2048]);
} else {
println!("This test requires root privileges");
}
Expand Down