Allow WASI to open directories without O_DIRECTORY (#6163)
* Allow WASI to open directories without O_DIRECTORY The `O_DIRECTORY` flag is a request that open should fail if the named path is not a directory. Opening a path which turns out to be a directory is not supposed to fail if this flag is not specified. However, wasi-common required callers to use it when opening directories. With this PR, we always open the path the same way whether or not the `O_DIRECTORY` flag is specified. However, after opening it, we `stat` it to check whether it turned out to be a directory, and determine which operations the file descriptor should support accordingly. In addition, we explicitly check whether the precondition defined by `O_DIRECTORY` is satisfied. Closes #4947 and closes #4967, which were earlier attempts at fixing the same issue, but which had race conditions. prtest:full * Add tests from #4967/#4947 This test was authored by Roman Volosatovs <rvolosatovs@riseup.net> as part of #4947. * Tests: Close FDs before trying to unlink files On Windows, when opening a path which might be a directory using `CreateFile`, cap-primitives also removes the `FILE_SHARE_DELETE` mode. That means that if we implement WASI's `path_open` such that it always uses `CreateFile` on Windows, for both files and directories, then holding an open file handle prevents deletion of that file. So I'm changing these test programs to make sure they've closed the handle before trying to delete the file.
This commit is contained in:
@@ -3,7 +3,7 @@ use std::any::Any;
|
||||
use std::path::PathBuf;
|
||||
use wasi_common::{
|
||||
dir::{ReaddirCursor, ReaddirEntity, WasiDir},
|
||||
file::{FdFlags, Filestat, OFlags, WasiFile},
|
||||
file::{FdFlags, Filestat, OFlags},
|
||||
Error, ErrorExt,
|
||||
};
|
||||
|
||||
@@ -28,18 +28,19 @@ impl WasiDir for Dir {
|
||||
read: bool,
|
||||
write: bool,
|
||||
fdflags: FdFlags,
|
||||
) -> Result<Box<dyn WasiFile>, Error> {
|
||||
) -> Result<wasi_common::dir::OpenResult, Error> {
|
||||
let f = block_on_dummy_executor(move || async move {
|
||||
self.0
|
||||
.open_file_(symlink_follow, path, oflags, read, write, fdflags)
|
||||
})?;
|
||||
Ok(Box::new(File::from_inner(f)))
|
||||
}
|
||||
|
||||
async fn open_dir(&self, symlink_follow: bool, path: &str) -> Result<Box<dyn WasiDir>, Error> {
|
||||
let d =
|
||||
block_on_dummy_executor(move || async move { self.0.open_dir_(symlink_follow, path) })?;
|
||||
Ok(Box::new(Dir(d)))
|
||||
match f {
|
||||
wasi_cap_std_sync::dir::OpenResult::File(f) => Ok(wasi_common::dir::OpenResult::File(
|
||||
Box::new(File::from_inner(f)),
|
||||
)),
|
||||
wasi_cap_std_sync::dir::OpenResult::Dir(d) => {
|
||||
Ok(wasi_common::dir::OpenResult::Dir(Box::new(Dir(d))))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn create_dir(&self, path: &str) -> Result<(), Error> {
|
||||
@@ -127,6 +128,7 @@ impl WasiDir for Dir {
|
||||
mod test {
|
||||
use super::Dir;
|
||||
use cap_std::ambient_authority;
|
||||
use wasi_common::file::{FdFlags, OFlags};
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn scratch_dir() {
|
||||
@@ -137,9 +139,17 @@ mod test {
|
||||
let preopen_dir = cap_std::fs::Dir::open_ambient_dir(tempdir.path(), ambient_authority())
|
||||
.expect("open ambient temporary dir");
|
||||
let preopen_dir = Dir::from_cap_std(preopen_dir);
|
||||
wasi_common::WasiDir::open_dir(&preopen_dir, false, ".")
|
||||
.await
|
||||
.expect("open the same directory via WasiDir abstraction");
|
||||
wasi_common::WasiDir::open_file(
|
||||
&preopen_dir,
|
||||
false,
|
||||
".",
|
||||
OFlags::empty(),
|
||||
false,
|
||||
false,
|
||||
FdFlags::empty(),
|
||||
)
|
||||
.await
|
||||
.expect("open the same directory via WasiDir abstraction");
|
||||
}
|
||||
|
||||
// Readdir does not work on windows, so we won't test it there.
|
||||
|
||||
@@ -2,6 +2,7 @@ use anyhow::{Context, Error};
|
||||
use cap_std::time::Duration;
|
||||
use std::collections::HashMap;
|
||||
use wasi_common::{
|
||||
dir::OpenResult,
|
||||
file::{FdFlags, OFlags},
|
||||
sched::{Poll, RwEventFlags, SubscriptionResult, Userdata},
|
||||
WasiDir, WasiFile,
|
||||
@@ -25,18 +26,26 @@ async fn empty_file_readable() -> Result<(), Error> {
|
||||
.await
|
||||
.context("create writable file f")?;
|
||||
let to_write: Vec<u8> = vec![0];
|
||||
f.write_vectored(&vec![std::io::IoSlice::new(&to_write)])
|
||||
.await
|
||||
.context("write to f")?;
|
||||
if let OpenResult::File(ref f) = f {
|
||||
f.write_vectored(&vec![std::io::IoSlice::new(&to_write)])
|
||||
.await
|
||||
.context("write to f")?;
|
||||
} else {
|
||||
unreachable!();
|
||||
}
|
||||
drop(f);
|
||||
|
||||
let mut f = d
|
||||
let f = d
|
||||
.open_file(false, "f", OFlags::empty(), true, false, FdFlags::empty())
|
||||
.await
|
||||
.context("open f as readable")?;
|
||||
|
||||
let mut poll = Poll::new();
|
||||
poll.subscribe_read(&mut *f, Userdata::from(123));
|
||||
if let OpenResult::File(ref f) = f {
|
||||
poll.subscribe_read(f.as_ref(), Userdata::from(123));
|
||||
} else {
|
||||
unreachable!();
|
||||
}
|
||||
// Timeout bounds time in poll_oneoff
|
||||
let monotonic = &*clocks.monotonic()?.abs_clock;
|
||||
poll.subscribe_monotonic_clock(
|
||||
@@ -73,13 +82,17 @@ async fn empty_file_writable() -> Result<(), Error> {
|
||||
let d = workspace.open_dir("d").context("open dir")?;
|
||||
let d = Dir::from_cap_std(d);
|
||||
|
||||
let mut writable_f = d
|
||||
let writable_f = d
|
||||
.open_file(false, "f", OFlags::CREATE, true, true, FdFlags::empty())
|
||||
.await
|
||||
.context("create writable file")?;
|
||||
|
||||
let mut poll = Poll::new();
|
||||
poll.subscribe_write(&mut *writable_f, Userdata::from(123));
|
||||
if let OpenResult::File(ref writable_f) = writable_f {
|
||||
poll.subscribe_write(writable_f.as_ref(), Userdata::from(123));
|
||||
} else {
|
||||
unreachable!();
|
||||
}
|
||||
// Timeout bounds time in poll_oneoff
|
||||
let monotonic = &*clocks.monotonic()?.abs_clock;
|
||||
poll.subscribe_monotonic_clock(
|
||||
|
||||
Reference in New Issue
Block a user