mirror of
https://github.com/denoland/deno.git
synced 2024-11-28 16:20:57 -05:00
Do not close file on invalid seek mode (#2004)
This commit is contained in:
parent
d78b4112c6
commit
5c4189a3b8
2 changed files with 18 additions and 11 deletions
|
@ -449,17 +449,6 @@ pub fn seek(
|
||||||
match maybe_repr {
|
match maybe_repr {
|
||||||
None => panic!("bad rid"),
|
None => panic!("bad rid"),
|
||||||
Some(Repr::FsFile(f)) => {
|
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,
|
// Trait Clone not implemented on tokio::fs::File,
|
||||||
// so convert to std File first.
|
// so convert to std File first.
|
||||||
let std_file = f.into_std();
|
let std_file = f.into_std();
|
||||||
|
@ -475,6 +464,18 @@ pub fn seek(
|
||||||
resource.rid,
|
resource.rid,
|
||||||
Repr::FsFile(tokio_fs::File::from_std(std_file)),
|
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() {
|
if maybe_std_file_copy.is_err() {
|
||||||
return Box::new(futures::future::err(DenoError::from(
|
return Box::new(futures::future::err(DenoError::from(
|
||||||
maybe_std_file_copy.unwrap_err(),
|
maybe_std_file_copy.unwrap_err(),
|
||||||
|
|
|
@ -198,4 +198,10 @@ testPerm({ read: true }, async function seekMode() {
|
||||||
assert(!!err);
|
assert(!!err);
|
||||||
assertEquals(err.kind, Deno.ErrorKind.InvalidSeekMode);
|
assertEquals(err.kind, Deno.ErrorKind.InvalidSeekMode);
|
||||||
assertEquals(err.name, "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");
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue