Do not close file on invalid seek mode (#2004)

This commit is contained in:
Kevin (Kun) "Kassimo" Qian 2019-03-26 19:47:17 -07:00 committed by Ryan Dahl
parent d78b4112c6
commit 5c4189a3b8
2 changed files with 18 additions and 11 deletions

View file

@ -449,17 +449,6 @@ pub fn seek(
match maybe_repr {
None => panic!("bad rid"),
Some(Repr::FsFile(f)) => {
let seek_from = match whence {
0 => SeekFrom::Start(offset as u64),
1 => SeekFrom::Current(i64::from(offset)),
2 => SeekFrom::End(i64::from(offset)),
_ => {
return Box::new(futures::future::err(errors::new(
errors::ErrorKind::InvalidSeekMode,
format!("Invalid seek mode: {}", whence),
)));
}
};
// Trait Clone not implemented on tokio::fs::File,
// so convert to std File first.
let std_file = f.into_std();
@ -475,6 +464,18 @@ pub fn seek(
resource.rid,
Repr::FsFile(tokio_fs::File::from_std(std_file)),
);
// Translate seek mode to Rust repr.
let seek_from = match whence {
0 => SeekFrom::Start(offset as u64),
1 => SeekFrom::Current(i64::from(offset)),
2 => SeekFrom::End(i64::from(offset)),
_ => {
return Box::new(futures::future::err(errors::new(
errors::ErrorKind::InvalidSeekMode,
format!("Invalid seek mode: {}", whence),
)));
}
};
if maybe_std_file_copy.is_err() {
return Box::new(futures::future::err(DenoError::from(
maybe_std_file_copy.unwrap_err(),

View file

@ -198,4 +198,10 @@ testPerm({ read: true }, async function seekMode() {
assert(!!err);
assertEquals(err.kind, Deno.ErrorKind.InvalidSeekMode);
assertEquals(err.name, "InvalidSeekMode");
// We should still be able to read the file
// since it is still open.
let buf = new Uint8Array(1);
await file.read(buf); // "H"
assertEquals(new TextDecoder().decode(buf), "H");
});