1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
use {
anyhow::{anyhow, Context as _, Error},
fidl_fuchsia_io as fio,
fidl_fuchsia_io_test::{
self as io_test, Io1Config, Io1HarnessRequest, Io1HarnessRequestStream,
},
fuchsia_component::server::ServiceFs,
fuchsia_zircon as zx,
futures::prelude::*,
std::sync::Arc,
tracing::error,
vfs::{
directory::{
entry::DirectoryEntry,
helper::DirectlyMutable,
mutable::{connection::io1::MutableConnection, simple},
simple::Simple,
},
execution_scope::ExecutionScope,
file::vmo,
path::Path,
remote::remote_dir,
},
};
struct Harness(Io1HarnessRequestStream);
const HARNESS_EXEC_PATH: &'static str = "/pkg/bin/io_conformance_harness_rustvfs";
fn new_vmo_file(vmo: zx::Vmo) -> Result<Arc<dyn DirectoryEntry>, Error> {
Ok(vmo::VmoFile::new(
vmo, true, true, false,
))
}
fn new_executable_file() -> Result<Arc<dyn DirectoryEntry>, Error> {
let file = fdio::open_fd(
HARNESS_EXEC_PATH,
fio::OpenFlags::RIGHT_READABLE | fio::OpenFlags::RIGHT_EXECUTABLE,
)?;
let exec_vmo = fdio::get_vmo_exec_from_file(&file)?;
let exec_file = vmo::VmoFile::new(
exec_vmo, true, false, true,
);
Ok(exec_file)
}
fn add_entry(
entry: io_test::DirectoryEntry,
dest: &Arc<Simple<MutableConnection>>,
) -> Result<(), Error> {
match entry {
io_test::DirectoryEntry::Directory(io_test::Directory { name, entries, .. }) => {
let name = name.expect("Directory must have name");
let new_dir = simple();
if let Some(entries) = entries {
for entry in entries {
let entry = *entry.expect("Directory entries must not be null");
add_entry(entry, &new_dir)?;
}
}
dest.add_entry(name, new_dir)?;
}
io_test::DirectoryEntry::RemoteDirectory(io_test::RemoteDirectory {
name,
remote_client,
..
}) => {
let name = name.expect("RemoteDirectory must have name");
let dir_proxy =
remote_client.expect("RemoteDirectory must have a remote client").into_proxy()?;
dest.add_entry(name, remote_dir(dir_proxy))?;
}
io_test::DirectoryEntry::File(io_test::File { name, contents, .. }) => {
let name = name.expect("File must have name");
let contents = contents.expect("File must have contents");
let new_file = vmo::read_write(contents, Some(100));
dest.add_entry(name, new_file)?;
}
io_test::DirectoryEntry::VmoFile(io_test::VmoFile { name, vmo, .. }) => {
let name = name.expect("VMO file must have a name");
let vmo = vmo.expect("VMO file must have a VMO");
let vmo_file = new_vmo_file(vmo)?;
dest.add_entry(name, vmo_file)?;
}
io_test::DirectoryEntry::ExecutableFile(io_test::ExecutableFile { name, .. }) => {
let name = name.expect("Executable file must have a name");
let executable_file = new_executable_file()?;
dest.add_entry(name, executable_file)?;
}
}
Ok(())
}
async fn run(mut stream: Io1HarnessRequestStream) -> Result<(), Error> {
while let Some(request) = stream.try_next().await.context("error running harness server")? {
let (dir, flags, directory_request) = match request {
Io1HarnessRequest::GetConfig { responder } => {
let config = Io1Config {
mutable_file: Some(true),
supports_create: Some(true),
supports_executable_file: Some(true),
supports_vmo_file: Some(true),
supports_remote_dir: Some(true),
supports_get_backing_memory: Some(true),
supports_rename: Some(true),
supports_get_token: Some(true),
conformant_path_handling: Some(true),
supports_unlink: Some(true),
supports_link: Some(false), supports_set_attr: Some(false),
..Io1Config::EMPTY
};
responder.send(config)?;
continue;
}
Io1HarnessRequest::GetDirectory {
root,
flags,
directory_request,
control_handle: _,
} => {
let dir = simple();
if let Some(entries) = root.entries {
for entry in entries {
let entry = entry.expect("Directory entries must not be null");
add_entry(*entry, &dir)?;
}
}
(dir, flags, directory_request)
}
};
let scope = ExecutionScope::build()
.entry_constructor(simple::tree_constructor(|_parent, _filename| {
Ok(vmo::read_write("", Some(100)))
}))
.new();
dir.open(scope, flags, Path::dot(), directory_request.into_channel().into());
}
Ok(())
}
#[fuchsia::main]
async fn main() -> Result<(), Error> {
let mut fs = ServiceFs::new_local();
fs.dir("svc").add_fidl_service(Harness);
fs.take_and_serve_directory_handle()?;
let fut = fs.for_each_concurrent(10_000, |Harness(stream)| {
run(stream).unwrap_or_else(|e| error!("Error processing request: {:?}", anyhow!(e)))
});
fut.await;
Ok(())
}