kobako 0.29.0 → 0.31.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.release-please-manifest.json +1 -1
- data/CHANGELOG.md +552 -10
- data/Cargo.lock +595 -139
- data/README.md +126 -564
- data/ROADMAP.md +3 -2
- data/crates/kobako-runtime/CHANGELOG.md +488 -1
- data/crates/kobako-runtime/Cargo.toml +73 -15
- data/crates/kobako-runtime/README.md +11 -4
- data/crates/kobako-runtime/src/cred.rs +124 -0
- data/crates/kobako-runtime/src/dispatch.rs +5 -6
- data/crates/kobako-runtime/src/endpoint/buffer.rs +301 -0
- data/crates/kobako-runtime/src/endpoint/dispatch.rs +292 -0
- data/crates/kobako-runtime/src/endpoint/invoke.rs +181 -0
- data/crates/kobako-runtime/src/endpoint.rs +267 -0
- data/crates/kobako-runtime/src/errno.rs +391 -0
- data/crates/kobako-runtime/src/error.rs +31 -10
- data/crates/kobako-runtime/src/filesystem/anon_inodes.rs +52 -0
- data/crates/kobako-runtime/src/filesystem/bind/fid.rs +82 -0
- data/crates/kobako-runtime/src/filesystem/bind/lookup.rs +274 -0
- data/crates/kobako-runtime/src/filesystem/bind/walk.rs +248 -0
- data/crates/kobako-runtime/src/filesystem/bind.rs +2857 -0
- data/crates/kobako-runtime/src/filesystem/overlay/file.rs +89 -0
- data/crates/kobako-runtime/src/filesystem/overlay/inode.rs +106 -0
- data/crates/kobako-runtime/src/filesystem/overlay/path.rs +263 -0
- data/crates/kobako-runtime/src/filesystem/overlay/stack/copy_up.rs +128 -0
- data/crates/kobako-runtime/src/filesystem/overlay/stack/dir.rs +238 -0
- data/crates/kobako-runtime/src/filesystem/overlay/stack/namei.rs +135 -0
- data/crates/kobako-runtime/src/filesystem/overlay/stack/readdir.rs +54 -0
- data/crates/kobako-runtime/src/filesystem/overlay/stack.rs +316 -739
- data/crates/kobako-runtime/src/filesystem/overlay.rs +2627 -1154
- data/crates/kobako-runtime/src/filesystem/pipe.rs +224 -0
- data/crates/kobako-runtime/src/filesystem/rdir.rs +84 -0
- data/crates/kobako-runtime/src/filesystem/tmpfs/seed.rs +32 -0
- data/crates/kobako-runtime/src/filesystem/tmpfs/tmpfile.rs +32 -0
- data/crates/kobako-runtime/src/filesystem/tmpfs/whiteout.rs +107 -0
- data/crates/kobako-runtime/src/filesystem/tmpfs/xattr.rs +22 -0
- data/crates/kobako-runtime/src/filesystem/tmpfs.rs +1997 -0
- data/crates/kobako-runtime/src/filesystem/wrap.rs +208 -0
- data/crates/kobako-runtime/src/filesystem.rs +25 -388
- data/crates/kobako-runtime/src/fork.rs +96 -0
- data/crates/kobako-runtime/src/grant.rs +3 -0
- data/crates/kobako-runtime/src/hashtable.rs +46 -0
- data/crates/kobako-runtime/src/lib.rs +27 -11
- data/crates/kobako-runtime/src/limits.rs +195 -0
- data/crates/kobako-runtime/src/locking/seqlock.rs +66 -0
- data/crates/kobako-runtime/src/locking.rs +228 -0
- data/crates/kobako-runtime/src/memcg.rs +383 -0
- data/crates/kobako-runtime/src/mount.rs +86 -101
- data/crates/kobako-runtime/src/net/backends.rs +43 -0
- data/crates/kobako-runtime/src/net/connections.rs +179 -0
- data/crates/kobako-runtime/src/net/credential.rs +125 -0
- data/crates/kobako-runtime/src/net/egress.rs +160 -0
- data/crates/kobako-runtime/src/net/family.rs +181 -0
- data/crates/kobako-runtime/src/net/http.rs +2733 -0
- data/crates/kobako-runtime/src/net/lookup.rs +285 -0
- data/crates/kobako-runtime/src/net/mock.rs +251 -0
- data/crates/kobako-runtime/src/net/namespace.rs +1366 -0
- data/crates/kobako-runtime/src/net/reactor.rs +531 -0
- data/crates/kobako-runtime/src/net/resolv/darwin.rs +275 -0
- data/crates/kobako-runtime/src/net/resolv/stub/tests.rs +469 -0
- data/crates/kobako-runtime/src/net/resolv/stub.rs +536 -0
- data/crates/kobako-runtime/src/net/resolv/windows.rs +248 -0
- data/crates/kobako-runtime/src/net/resolv.rs +227 -0
- data/crates/kobako-runtime/src/net/socket.rs +870 -0
- data/crates/kobako-runtime/src/net/tcp.rs +1588 -0
- data/crates/kobako-runtime/src/net/tls.rs +58 -0
- data/crates/kobako-runtime/src/net/transport.rs +279 -0
- data/crates/kobako-runtime/src/net/udp.rs +1003 -0
- data/crates/kobako-runtime/src/net.rs +42 -0
- data/crates/kobako-runtime/src/network/builder.rs +177 -0
- data/crates/kobako-runtime/src/network/ruleset.rs +777 -0
- data/crates/kobako-runtime/src/network.rs +551 -0
- data/crates/kobako-runtime/src/policy.rs +16 -0
- data/crates/kobako-runtime/src/profile.rs +37 -16
- data/crates/kobako-runtime/src/runtime.rs +3 -5
- data/crates/kobako-runtime/src/sched.rs +311 -0
- data/crates/kobako-runtime/src/security/entry_policy.rs +22 -0
- data/crates/kobako-runtime/src/security/hooks.rs +242 -0
- data/crates/kobako-runtime/src/security/mount_policy.rs +158 -0
- data/crates/kobako-runtime/src/security.rs +601 -0
- data/crates/kobako-runtime/src/snapshot.rs +121 -2
- data/crates/kobako-runtime/src/vfs/attr.rs +162 -0
- data/crates/kobako-runtime/src/vfs/dentry.rs +1466 -0
- data/crates/kobako-runtime/src/vfs/file.rs +1911 -0
- data/crates/kobako-runtime/src/vfs/inode.rs +425 -0
- data/crates/kobako-runtime/src/vfs/kern_path.rs +213 -0
- data/crates/kobako-runtime/src/vfs/mount.rs +60 -0
- data/crates/kobako-runtime/src/vfs/namei.rs +5837 -0
- data/crates/kobako-runtime/src/vfs/namespace.rs +162 -0
- data/crates/kobako-runtime/src/vfs/open_files.rs +69 -0
- data/crates/kobako-runtime/src/vfs/openat2.rs +116 -0
- data/crates/kobako-runtime/src/vfs/path.rs +64 -0
- data/crates/kobako-runtime/src/vfs/readdir.rs +163 -0
- data/crates/kobako-runtime/src/vfs/stat.rs +293 -0
- data/crates/kobako-runtime/src/vfs/super_block.rs +126 -0
- data/crates/kobako-runtime/src/vfs/utimes.rs +299 -0
- data/crates/kobako-runtime/src/vfs.rs +63 -0
- data/crates/kobako-runtime/src/wait.rs +356 -0
- data/crates/kobako-transport/CHANGELOG.md +39 -2
- data/crates/kobako-transport/Cargo.toml +3 -3
- data/crates/kobako-transport/README.md +8 -8
- data/crates/kobako-transport/src/abi.rs +78 -72
- data/crates/kobako-transport/src/envelope/bytes.rs +49 -30
- data/crates/kobako-transport/src/envelope/call.rs +37 -9
- data/crates/kobako-transport/src/envelope/compile_reply.rs +2 -2
- data/crates/kobako-transport/src/envelope/error_record.rs +24 -3
- data/crates/kobako-transport/src/envelope/fault.rs +2 -2
- data/crates/kobako-transport/src/envelope/name.rs +99 -0
- data/crates/kobako-transport/src/envelope/outcome.rs +11 -11
- data/crates/kobako-transport/src/envelope/preamble.rs +34 -31
- data/crates/kobako-transport/src/envelope/reply.rs +29 -29
- data/crates/kobako-transport/src/envelope/request.rs +2 -2
- data/crates/kobako-transport/src/envelope/run.rs +1 -1
- data/crates/kobako-transport/src/envelope/unsupported.rs +12 -12
- data/crates/kobako-transport/src/envelope.rs +15 -10
- data/crates/kobako-wasmtime/CHANGELOG.md +315 -8
- data/crates/kobako-wasmtime/Cargo.toml +35 -15
- data/crates/kobako-wasmtime/README.md +11 -6
- data/crates/kobako-wasmtime/build/preview2.rs +1339 -0
- data/crates/kobako-wasmtime/build.rs +16 -0
- data/crates/kobako-wasmtime/src/abi.rs +19 -47
- data/crates/kobako-wasmtime/src/ambient.rs +33 -19
- data/crates/kobako-wasmtime/src/cache.rs +28 -20
- data/crates/kobako-wasmtime/src/config.rs +59 -60
- data/crates/kobako-wasmtime/src/driver.rs +63 -145
- data/crates/kobako-wasmtime/src/exports.rs +147 -32
- data/crates/kobako-wasmtime/src/guest.rs +44 -0
- data/crates/kobako-wasmtime/src/guest_binary.rs +14 -0
- data/crates/kobako-wasmtime/src/imports.rs +48 -18
- data/crates/kobako-wasmtime/src/instance_pre.rs +7 -7
- data/crates/kobako-wasmtime/src/invocation.rs +93 -51
- data/crates/kobako-wasmtime/src/lib.rs +6 -7
- data/crates/kobako-wasmtime/src/limiter.rs +52 -211
- data/crates/kobako-wasmtime/src/preview1/descriptors.rs +309 -0
- data/crates/kobako-wasmtime/src/preview1/differential/mount.rs +70 -57
- data/crates/kobako-wasmtime/src/preview1/differential.rs +137 -27
- data/crates/kobako-wasmtime/src/preview1/errno.rs +90 -233
- data/crates/kobako-wasmtime/src/preview1/fd.rs +1301 -522
- data/crates/kobako-wasmtime/src/preview1/memory.rs +126 -150
- data/crates/kobako-wasmtime/src/preview1/mount_view/wrap.rs +192 -0
- data/crates/kobako-wasmtime/src/preview1/mount_view.rs +1989 -708
- data/crates/kobako-wasmtime/src/preview1/path.rs +181 -365
- data/crates/kobako-wasmtime/src/preview1/poll.rs +345 -173
- data/crates/kobako-wasmtime/src/preview1/sock.rs +10 -25
- data/crates/kobako-wasmtime/src/preview1/testsuite-expected.txt +6 -0
- data/crates/kobako-wasmtime/src/preview1/testsuite.rs +147 -0
- data/crates/kobako-wasmtime/src/preview1.rs +471 -416
- data/crates/kobako-wasmtime/src/preview2/abi.rs +227 -0
- data/crates/kobako-wasmtime/src/preview2/error.rs +118 -0
- data/crates/kobako-wasmtime/src/preview2/filesystem.rs +122 -0
- data/crates/kobako-wasmtime/src/preview2/host.rs +3959 -0
- data/crates/kobako-wasmtime/src/preview2/http.rs +1503 -0
- data/crates/kobako-wasmtime/src/preview2/io.rs +315 -0
- data/crates/kobako-wasmtime/src/preview2/sockets.rs +379 -0
- data/crates/kobako-wasmtime/src/preview2/table.rs +305 -0
- data/crates/kobako-wasmtime/src/preview2.rs +57 -0
- data/crates/kobako-wasmtime/src/trap.rs +31 -16
- data/crates/kobako-wasmtime/src/wasi.rs +238 -93
- data/crates/kobako-wasmtime/wit/deps/cli.wit +233 -0
- data/crates/kobako-wasmtime/wit/deps/clocks.wit +162 -0
- data/crates/kobako-wasmtime/wit/deps/filesystem.wit +589 -0
- data/crates/kobako-wasmtime/wit/deps/http.wit +733 -0
- data/crates/kobako-wasmtime/wit/deps/io.wit +299 -0
- data/crates/kobako-wasmtime/wit/deps/random.wit +92 -0
- data/crates/kobako-wasmtime/wit/deps/sockets.wit +949 -0
- data/crates/kobako-wasmtime/witx/typenames.witx +750 -0
- data/crates/kobako-wasmtime/witx/wasi_snapshot_preview1.witx +441 -0
- data/data/kobako.wasm +0 -0
- data/ext/kobako/Cargo.toml +1 -1
- data/ext/kobako/src/{runtime/errors.rs → errors.rs} +35 -27
- data/ext/kobako/src/escape.rs +98 -0
- data/ext/kobako/src/{runtime/filesystem → filesystem}/grants.rs +36 -16
- data/ext/kobako/src/filesystem/ruby.rs +689 -0
- data/ext/kobako/src/filesystem.rs +217 -0
- data/ext/kobako/src/gvl.rs +306 -0
- data/ext/kobako/src/lib.rs +64 -3
- data/ext/kobako/src/network/mock.rs +187 -0
- data/ext/kobako/src/network.rs +591 -0
- data/ext/kobako/src/runtime/bridge.rs +31 -30
- data/ext/kobako/src/runtime/snapshot.rs +5 -5
- data/ext/kobako/src/runtime.rs +221 -138
- data/ext/kobako/src/wire.rs +42 -0
- data/lib/kobako/catalog/extensions.rb +19 -7
- data/lib/kobako/catalog/flags.rb +4 -6
- data/lib/kobako/catalog/grants.rb +5 -4
- data/lib/kobako/catalog/handles.rb +9 -31
- data/lib/kobako/catalog/mount.rb +12 -12
- data/lib/kobako/catalog/network.rb +35 -0
- data/lib/kobako/catalog/replacements.rb +28 -0
- data/lib/kobako/catalog/services.rb +7 -21
- data/lib/kobako/catalog/snippets.rb +10 -25
- data/lib/kobako/catalog.rb +4 -1
- data/lib/kobako/codec/{utils.rb → boundary.rb} +5 -3
- data/lib/kobako/codec/decoder.rb +12 -12
- data/lib/kobako/codec/ext_types.rb +5 -7
- data/lib/kobako/codec/handle_walk.rb +5 -47
- data/lib/kobako/codec/nesting.rb +7 -7
- data/lib/kobako/codec/value_budget.rb +134 -42
- data/lib/kobako/codec.rb +1 -1
- data/lib/kobako/context.rb +28 -15
- data/lib/kobako/errors.rb +15 -12
- data/lib/kobako/extension.rb +3 -2
- data/lib/kobako/filesystem.rb +53 -15
- data/lib/kobako/handle.rb +2 -2
- data/lib/kobako/names.rb +28 -0
- data/lib/kobako/network.rb +210 -0
- data/lib/kobako/outcome.rb +7 -7
- data/lib/kobako/payload/arguments.rb +2 -2
- data/lib/kobako/pool.rb +47 -25
- data/lib/kobako/runtime.rb +3 -2
- data/lib/kobako/sandbox.rb +113 -56
- data/lib/kobako/sandbox_options.rb +41 -107
- data/lib/kobako/snippet/bytecode.rb +1 -1
- data/lib/kobako/snippet/source.rb +1 -1
- data/lib/kobako/transport/argument_wrap.rb +64 -0
- data/lib/kobako/transport/block_failure.rb +1 -1
- data/lib/kobako/transport/dispatcher.rb +10 -10
- data/lib/kobako/transport/exposure.rb +28 -17
- data/lib/kobako/transport/reflection.rb +28 -11
- data/lib/kobako/transport/run.rb +13 -28
- data/lib/kobako/transport/yielder.rb +3 -10
- data/lib/kobako/transport.rb +1 -0
- data/lib/kobako/version.rb +1 -1
- data/lib/kobako.rb +38 -0
- data/release-please-config.json +16 -0
- data/sig/core/thread.rbs +6 -0
- data/sig/kobako/catalog/extensions.rbs +5 -3
- data/sig/kobako/catalog/grants.rbs +7 -4
- data/sig/kobako/catalog/handles.rbs +2 -4
- data/sig/kobako/catalog/mount.rbs +3 -5
- data/sig/kobako/catalog/network.rbs +20 -0
- data/sig/kobako/catalog/replacements.rbs +14 -0
- data/sig/kobako/catalog/services.rbs +1 -5
- data/sig/kobako/catalog/snippets.rbs +3 -5
- data/sig/kobako/catalog.rbs +1 -1
- data/sig/kobako/codec/boundary.rbs +9 -0
- data/sig/kobako/codec/decoder.rbs +1 -1
- data/sig/kobako/codec/handle_walk.rbs +0 -6
- data/sig/kobako/codec/nesting.rbs +2 -2
- data/sig/kobako/codec/value_budget.rbs +48 -5
- data/sig/kobako/codec.rbs +1 -2
- data/sig/kobako/context.rbs +4 -2
- data/sig/kobako/errors.rbs +4 -4
- data/sig/kobako/filesystem.rbs +38 -15
- data/sig/kobako/guest_yielder.rbs +1 -1
- data/sig/kobako/network.rbs +115 -0
- data/sig/kobako/outcome.rbs +1 -1
- data/sig/kobako/payload/arguments.rbs +1 -1
- data/sig/kobako/pool.rbs +8 -4
- data/sig/kobako/runtime.rbs +32 -3
- data/sig/kobako/sandbox.rbs +22 -7
- data/sig/kobako/sandbox_options.rbs +29 -38
- data/sig/kobako/transport/argument_wrap.rbs +15 -0
- data/sig/kobako/transport/dispatcher.rbs +2 -2
- data/sig/kobako/transport/error.rbs +1 -1
- data/sig/kobako/transport/exposure.rbs +3 -5
- data/sig/kobako/transport/reflection.rbs +12 -2
- data/sig/kobako/transport/run.rbs +3 -5
- data/sig/kobako/transport/yielder.rbs +0 -3
- data/sig/kobako/transport.rbs +2 -2
- data/sig/kobako.rbs +13 -0
- metadata +131 -34
- data/crates/kobako-runtime/src/filesystem/directory/spelling.rs +0 -194
- data/crates/kobako-runtime/src/filesystem/directory.rs +0 -1082
- data/crates/kobako-runtime/src/filesystem/memory.rs +0 -1100
- data/crates/kobako-runtime/src/filesystem/overlay/exclusive.rs +0 -88
- data/crates/kobako-runtime/src/filesystem/path.rs +0 -60
- data/crates/kobako-runtime/src/filesystem/temporary.rs +0 -167
- data/crates/kobako-wasmtime/src/capture.rs +0 -96
- data/crates/kobako-wasmtime/src/descriptors.rs +0 -167
- data/crates/kobako-wasmtime/src/dispatch.rs +0 -125
- data/crates/kobako-wasmtime/src/guest_mem.rs +0 -271
- data/crates/kobako-wasmtime/src/io.rs +0 -89
- data/crates/kobako-wasmtime/src/preview1/args.rs +0 -60
- data/crates/kobako-wasmtime/src/preview1/beneath.rs +0 -188
- data/crates/kobako-wasmtime/tests/compile_posture.rs +0 -43
- data/crates/kobako-wasmtime/tests/guest_file.rs +0 -1054
- data/crates/kobako-wasmtime/tests/preamble_flags.rs +0 -70
- data/crates/kobako-wasmtime/tests/snippet_compile.rs +0 -95
- data/crates/kobako-wasmtime/tests/support/mod.rs +0 -30
- data/crates/kobako-wasmtime/tests/unanswered_call.rs +0 -86
- data/crates/kobako-wasmtime/tests/unsupported_call.rs +0 -51
- data/ext/kobako/src/runtime/escape.rs +0 -53
- data/ext/kobako/src/runtime/filesystem/ruby.rs +0 -400
- data/ext/kobako/src/runtime/filesystem.rs +0 -236
- data/ext/kobako/src/runtime/gvl.rs +0 -112
- data/lib/kobako/constant_name.rb +0 -8
- data/sig/kobako/codec/utils.rbs +0 -9
- data/sig/kobako/constant_name.rbs +0 -3
- /data/ext/kobako/src/{runtime/pinned_value.rs → pinned_value.rs} +0 -0
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
//! The lock an Overlay holds across each call, so a change that touches
|
|
2
|
-
//! several Layers is never seen, nor interleaved with, half made, as the
|
|
3
|
-
//! directory's lock overlayfs's changes run under.
|
|
4
|
-
|
|
5
|
-
use std::sync::{Arc, Condvar, Mutex, MutexGuard};
|
|
6
|
-
use std::thread::{self, ThreadId};
|
|
7
|
-
|
|
8
|
-
use super::super::FilesystemError;
|
|
9
|
-
|
|
10
|
-
/// How a thread waits for a lock another thread holds. A frontend whose
|
|
11
|
-
/// threads share a lock of their own, as Ruby's threads share the GVL,
|
|
12
|
-
/// gives that lock up for the wait, or a holder that needs it to finish
|
|
13
|
-
/// never could.
|
|
14
|
-
pub trait Blocking: Send + Sync {
|
|
15
|
-
/// Run `wait`, which returns once the lock is taken.
|
|
16
|
-
fn wait(&self, wait: &mut dyn FnMut());
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/// Waiting with nothing else to give up.
|
|
20
|
-
pub(super) struct Inline;
|
|
21
|
-
|
|
22
|
-
impl Blocking for Inline {
|
|
23
|
-
fn wait(&self, wait: &mut dyn FnMut()) {
|
|
24
|
-
wait();
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/// Held by one thread at a time. The thread holding it asking again is
|
|
29
|
-
/// refused as a deadlock, as `fcntl` refuses a lock that could never be
|
|
30
|
-
/// granted.
|
|
31
|
-
pub(super) struct Exclusive {
|
|
32
|
-
holder: Mutex<Option<ThreadId>>,
|
|
33
|
-
freed: Condvar,
|
|
34
|
-
blocking: Arc<dyn Blocking>,
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
impl Exclusive {
|
|
38
|
-
pub(super) fn new(blocking: Arc<dyn Blocking>) -> Self {
|
|
39
|
-
Self {
|
|
40
|
-
holder: Mutex::new(None),
|
|
41
|
-
freed: Condvar::new(),
|
|
42
|
-
blocking,
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
pub(super) fn blocking(&self) -> Arc<dyn Blocking> {
|
|
47
|
-
self.blocking.clone()
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
fn holder(&self) -> MutexGuard<'_, Option<ThreadId>> {
|
|
51
|
-
self.holder
|
|
52
|
-
.lock()
|
|
53
|
-
.unwrap_or_else(|poisoned| poisoned.into_inner())
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
pub(super) fn hold(&self) -> Result<Held<'_>, FilesystemError> {
|
|
57
|
-
let me = thread::current().id();
|
|
58
|
-
let mut holder = self.holder();
|
|
59
|
-
match *holder {
|
|
60
|
-
Some(held) if held == me => return Err(FilesystemError::Deadlock),
|
|
61
|
-
Some(_) => {
|
|
62
|
-
drop(holder);
|
|
63
|
-
self.blocking.wait(&mut || {
|
|
64
|
-
let mut holder = self.holder();
|
|
65
|
-
while holder.is_some() {
|
|
66
|
-
holder = self
|
|
67
|
-
.freed
|
|
68
|
-
.wait(holder)
|
|
69
|
-
.unwrap_or_else(|poisoned| poisoned.into_inner());
|
|
70
|
-
}
|
|
71
|
-
*holder = Some(me);
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
|
-
None => *holder = Some(me),
|
|
75
|
-
}
|
|
76
|
-
Ok(Held(self))
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/// The lock while one call holds it, freed as the call returns.
|
|
81
|
-
pub(super) struct Held<'a>(&'a Exclusive);
|
|
82
|
-
|
|
83
|
-
impl Drop for Held<'_> {
|
|
84
|
-
fn drop(&mut self) {
|
|
85
|
-
*self.0.holder() = None;
|
|
86
|
-
self.0.freed.notify_one();
|
|
87
|
-
}
|
|
88
|
-
}
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
//! Paths as a Filesystem receives them: relative to its root, components
|
|
2
|
-
//! joined by `/`, the empty path being the root.
|
|
3
|
-
|
|
4
|
-
use super::{FileType, FilesystemError};
|
|
5
|
-
|
|
6
|
-
/// The directory holding `path`; the root holds every top-level entry.
|
|
7
|
-
pub(crate) fn parent(path: &str) -> &str {
|
|
8
|
-
path.rsplit_once('/').map_or("", |(parent, _)| parent)
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
/// The last name of `path`, within its parent.
|
|
12
|
-
pub(crate) fn name(path: &str) -> &str {
|
|
13
|
-
path.rsplit_once('/').map_or(path, |(_, name)| name)
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/// The path of `name` within the directory `dir`.
|
|
17
|
-
pub(crate) fn join(dir: &str, name: &str) -> String {
|
|
18
|
-
if dir.is_empty() {
|
|
19
|
-
name.to_string()
|
|
20
|
-
} else {
|
|
21
|
-
format!("{dir}/{name}")
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/// The names of `path`, outermost first; the root has none.
|
|
26
|
-
pub(crate) fn components(path: &str) -> impl Iterator<Item = &str> {
|
|
27
|
-
path.split('/').filter(|name| !name.is_empty())
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/// Every directory above `path`, outermost first, the root left out.
|
|
31
|
-
pub(crate) fn ancestors(path: &str) -> impl Iterator<Item = &str> {
|
|
32
|
-
path.match_indices('/').map(move |(end, _)| &path[..end])
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/// Whether `path` lies beneath `dir`, `dir` itself left out.
|
|
36
|
-
pub(crate) fn is_beneath(path: &str, dir: &str) -> bool {
|
|
37
|
-
dir.is_empty() && !path.is_empty()
|
|
38
|
-
|| path
|
|
39
|
-
.strip_prefix(dir)
|
|
40
|
-
.is_some_and(|rest| rest.starts_with('/'))
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/// Why an entry of `file_type` cannot move beneath itself: a directory
|
|
44
|
-
/// cannot hold itself, and a file stands where the new path needs a
|
|
45
|
-
/// directory, as `rename(2)` answers each.
|
|
46
|
-
pub(crate) fn beneath_itself(file_type: FileType) -> FilesystemError {
|
|
47
|
-
match file_type {
|
|
48
|
-
FileType::Directory => FilesystemError::InvalidInput,
|
|
49
|
-
_ => FilesystemError::NotADirectory,
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/// A path a Host App seeds a Filesystem with, in the form a runtime hands
|
|
54
|
-
/// over: no leading `/`, and no empty or `.` component.
|
|
55
|
-
pub(crate) fn normalize_seed(path: &str) -> String {
|
|
56
|
-
path.split('/')
|
|
57
|
-
.filter(|component| !component.is_empty() && *component != ".")
|
|
58
|
-
.collect::<Vec<_>>()
|
|
59
|
-
.join("/")
|
|
60
|
-
}
|
|
@@ -1,167 +0,0 @@
|
|
|
1
|
-
//! `Temporary`: a Filesystem held in memory that every Invocation starts
|
|
2
|
-
//! empty.
|
|
3
|
-
|
|
4
|
-
use std::sync::Arc;
|
|
5
|
-
|
|
6
|
-
use super::memory::Memory;
|
|
7
|
-
use super::{DirEntry, File, Filesystem, FilesystemError, Hold, Metadata, OpenOptions, Rename};
|
|
8
|
-
|
|
9
|
-
/// Files and directories held in memory for one Invocation. The next one
|
|
10
|
-
/// is handed a new, empty Temporary in its place, so nothing one writes
|
|
11
|
-
/// reaches another. Each one holds as much as a Memory does.
|
|
12
|
-
#[derive(Default)]
|
|
13
|
-
pub struct Temporary {
|
|
14
|
-
memory: Memory,
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
impl Temporary {
|
|
18
|
-
pub fn new() -> Self {
|
|
19
|
-
Self::default()
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/// This Temporary, and every one handed out in its place, with its
|
|
23
|
-
/// files holding at most `limit` bytes together, or without bound for
|
|
24
|
-
/// `None`, as `Memory::with_limit` bounds a Memory.
|
|
25
|
-
pub fn with_limit(self, limit: Option<usize>) -> Self {
|
|
26
|
-
Self {
|
|
27
|
-
memory: self.memory.with_limit(limit),
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/// This Temporary, and every one handed out in its place, holding at
|
|
32
|
-
/// most `inodes` entries, as `Memory::with_inodes` bounds a Memory.
|
|
33
|
-
pub fn with_inodes(self, inodes: Option<usize>) -> Self {
|
|
34
|
-
Self {
|
|
35
|
-
memory: self.memory.with_inodes(inodes),
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
impl Filesystem for Temporary {
|
|
41
|
-
fn open(&self, path: &str, options: &OpenOptions) -> Result<Box<dyn File>, FilesystemError> {
|
|
42
|
-
self.memory.open(path, options)
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
fn metadata(&self, path: &str) -> Result<Metadata, FilesystemError> {
|
|
46
|
-
self.memory.metadata(path)
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
fn symlink_metadata(&self, path: &str) -> Result<Metadata, FilesystemError> {
|
|
50
|
-
self.memory.symlink_metadata(path)
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
fn stored_path(&self, path: &str) -> Result<String, FilesystemError> {
|
|
54
|
-
self.memory.stored_path(path)
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
fn canonicalize_beneath(&self, base: &str, path: &str) -> Result<String, FilesystemError> {
|
|
58
|
-
self.memory.canonicalize_beneath(base, path)
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
fn for_policy(&self) -> Option<Arc<dyn Filesystem>> {
|
|
62
|
-
self.memory.for_policy()
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
fn hold_directory(&self, path: &str) -> Result<Option<Hold>, FilesystemError> {
|
|
66
|
-
self.memory.hold_directory(path)
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
fn read_dir(&self, path: &str) -> Result<Vec<DirEntry>, FilesystemError> {
|
|
70
|
-
self.memory.read_dir(path)
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
fn create_dir(&self, path: &str) -> Result<(), FilesystemError> {
|
|
74
|
-
self.memory.create_dir(path)
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
fn remove_dir(&self, path: &str) -> Result<(), FilesystemError> {
|
|
78
|
-
self.memory.remove_dir(path)
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
fn remove_file(&self, path: &str) -> Result<(), FilesystemError> {
|
|
82
|
-
self.memory.remove_file(path)
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
fn rename(&self, from: &str, to: &str, rename: Rename) -> Result<(), FilesystemError> {
|
|
86
|
-
self.memory.rename(from, to, rename)
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
fn for_invocation(&self) -> Option<Arc<dyn Filesystem>> {
|
|
90
|
-
Some(Arc::new(Self {
|
|
91
|
-
memory: self.memory.to_empty(),
|
|
92
|
-
}))
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
#[cfg(test)]
|
|
97
|
-
mod tests {
|
|
98
|
-
use super::*;
|
|
99
|
-
|
|
100
|
-
// @behavior FS-004
|
|
101
|
-
#[test]
|
|
102
|
-
fn a_temporary_starts_every_invocation_empty() {
|
|
103
|
-
let temporary = Temporary::new();
|
|
104
|
-
temporary
|
|
105
|
-
.open(
|
|
106
|
-
"scratch",
|
|
107
|
-
&OpenOptions {
|
|
108
|
-
write: true,
|
|
109
|
-
create: true,
|
|
110
|
-
..OpenOptions::default()
|
|
111
|
-
},
|
|
112
|
-
)
|
|
113
|
-
.expect("a new file must be created")
|
|
114
|
-
.write_at(b"gone", 0)
|
|
115
|
-
.expect("the write must land");
|
|
116
|
-
|
|
117
|
-
let next = temporary
|
|
118
|
-
.for_invocation()
|
|
119
|
-
.expect("a Temporary must hand the next Invocation a Filesystem of its own");
|
|
120
|
-
assert_eq!(
|
|
121
|
-
next.read_dir("").expect("the root must list"),
|
|
122
|
-
Vec::new(),
|
|
123
|
-
"Temporary::for_invocation must hand the next Invocation a Filesystem holding no file"
|
|
124
|
-
);
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
// @behavior FS-033
|
|
128
|
-
#[test]
|
|
129
|
-
fn a_temporary_keeps_its_inodes_in_every_invocation() {
|
|
130
|
-
let next = Temporary::new()
|
|
131
|
-
.with_inodes(Some(1))
|
|
132
|
-
.for_invocation()
|
|
133
|
-
.expect("a Temporary must hand the next Invocation a Filesystem of its own");
|
|
134
|
-
assert_eq!(
|
|
135
|
-
next.create_dir("dir"),
|
|
136
|
-
Err(FilesystemError::StorageFull),
|
|
137
|
-
"a directory past a Temporary's inodes in the Filesystem Temporary::for_invocation \
|
|
138
|
-
hands the next Invocation must answer StorageFull"
|
|
139
|
-
);
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
// @behavior FS-027
|
|
143
|
-
#[test]
|
|
144
|
-
fn a_temporary_keeps_its_limit_in_every_invocation() {
|
|
145
|
-
let next = Temporary::new()
|
|
146
|
-
.with_limit(Some(4))
|
|
147
|
-
.for_invocation()
|
|
148
|
-
.expect("a Temporary must hand the next Invocation a Filesystem of its own");
|
|
149
|
-
let written = next
|
|
150
|
-
.open(
|
|
151
|
-
"scratch",
|
|
152
|
-
&OpenOptions {
|
|
153
|
-
write: true,
|
|
154
|
-
create: true,
|
|
155
|
-
..OpenOptions::default()
|
|
156
|
-
},
|
|
157
|
-
)
|
|
158
|
-
.expect("a new file must be created")
|
|
159
|
-
.write_at(b"five!", 0);
|
|
160
|
-
assert_eq!(
|
|
161
|
-
written,
|
|
162
|
-
Err(FilesystemError::StorageFull),
|
|
163
|
-
"a write past a Temporary's limit in the Filesystem Temporary::for_invocation hands \
|
|
164
|
-
the next Invocation must answer StorageFull"
|
|
165
|
-
);
|
|
166
|
-
}
|
|
167
|
-
}
|
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
//! Per-channel stdout / stderr capture sizing and clipping.
|
|
2
|
-
//!
|
|
3
|
-
//! Two pure helpers shared by the run path: one
|
|
4
|
-
//! sizes the per-run `MemoryOutputPipe`, the other clips a captured
|
|
5
|
-
//! snapshot back to the configured cap and reports whether the cap was
|
|
6
|
-
//! exceeded. Kept channel-agnostic (a function of `cap`, not of which
|
|
7
|
-
//! channel) so a regression that only breaks one channel cannot sneak
|
|
8
|
-
//! through the test that pins them.
|
|
9
|
-
|
|
10
|
-
/// One byte past the cap, so "wrote exactly cap" and "exceeded cap" stay
|
|
11
|
-
/// distinguishable.
|
|
12
|
-
pub(crate) fn pipe_capacity(cap: Option<usize>) -> usize {
|
|
13
|
-
match cap {
|
|
14
|
-
Some(c) => c.saturating_add(1),
|
|
15
|
-
None => usize::MAX,
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/// Takes the snapshot by value and truncates in place, so the readback
|
|
20
|
-
/// costs one copy out of the pipe rather than two.
|
|
21
|
-
pub(crate) fn clip_capture(mut raw: Vec<u8>, cap: Option<usize>) -> (Vec<u8>, bool) {
|
|
22
|
-
match cap {
|
|
23
|
-
Some(c) if raw.len() > c => {
|
|
24
|
-
raw.truncate(c);
|
|
25
|
-
(raw, true)
|
|
26
|
-
}
|
|
27
|
-
_ => (raw, false),
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
#[cfg(test)]
|
|
32
|
-
mod tests {
|
|
33
|
-
use super::{clip_capture, pipe_capacity};
|
|
34
|
-
|
|
35
|
-
// @behavior S-108
|
|
36
|
-
#[test]
|
|
37
|
-
fn pipe_capacity_adds_one_when_cap_is_set() {
|
|
38
|
-
assert_eq!(pipe_capacity(Some(5)), 6);
|
|
39
|
-
assert_eq!(pipe_capacity(Some(0)), 1);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
// @behavior S-109
|
|
43
|
-
#[test]
|
|
44
|
-
fn pipe_capacity_falls_back_to_usize_max_when_uncapped() {
|
|
45
|
-
assert_eq!(pipe_capacity(None), usize::MAX);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
// @behavior S-110
|
|
49
|
-
#[test]
|
|
50
|
-
fn pipe_capacity_saturates_at_usize_max() {
|
|
51
|
-
assert_eq!(pipe_capacity(Some(usize::MAX)), usize::MAX);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
// @behavior S-111
|
|
55
|
-
#[test]
|
|
56
|
-
fn clip_capture_returns_full_bytes_when_under_cap() {
|
|
57
|
-
let (bytes, truncated) = clip_capture(b"abc".to_vec(), Some(5));
|
|
58
|
-
assert_eq!(bytes, b"abc");
|
|
59
|
-
assert!(!truncated);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
// @behavior S-112
|
|
63
|
-
#[test]
|
|
64
|
-
fn clip_capture_does_not_flag_truncation_at_exactly_cap_bytes() {
|
|
65
|
-
let (bytes, truncated) = clip_capture(b"abcde".to_vec(), Some(5));
|
|
66
|
-
assert_eq!(bytes, b"abcde");
|
|
67
|
-
assert!(!truncated);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
// @behavior S-024
|
|
71
|
-
#[test]
|
|
72
|
-
fn clip_capture_clips_to_cap_and_flags_truncation_on_overflow() {
|
|
73
|
-
// The pipe is sized `cap + 1`, so the snapshot can be at most
|
|
74
|
-
// 6 bytes when `cap == 5`; that surface is what triggers the
|
|
75
|
-
// truncation flag.
|
|
76
|
-
let (bytes, truncated) = clip_capture(b"abcdef".to_vec(), Some(5));
|
|
77
|
-
assert_eq!(bytes, b"abcde");
|
|
78
|
-
assert!(truncated);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
// @behavior S-033
|
|
82
|
-
#[test]
|
|
83
|
-
fn clip_capture_treats_none_as_uncapped() {
|
|
84
|
-
let (bytes, truncated) = clip_capture(b"abcdef".to_vec(), None);
|
|
85
|
-
assert_eq!(bytes, b"abcdef");
|
|
86
|
-
assert!(!truncated);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
// @behavior S-100 S-101
|
|
90
|
-
#[test]
|
|
91
|
-
fn clip_capture_handles_empty_input() {
|
|
92
|
-
let (bytes, truncated) = clip_capture(Vec::new(), Some(5));
|
|
93
|
-
assert_eq!(bytes, b"");
|
|
94
|
-
assert!(!truncated);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
@@ -1,167 +0,0 @@
|
|
|
1
|
-
//! The guest's descriptor table: what each preview1 file descriptor
|
|
2
|
-
//! stands for within one invocation.
|
|
3
|
-
//!
|
|
4
|
-
//! kobako answers every preview1 call that reads this table, so the guest
|
|
5
|
-
//! sees one table and no call falls through to a second one that
|
|
6
|
-
//! disagrees. Numbering follows wasmtime-wasi's: a closed number is reused
|
|
7
|
-
//! before a fresh one is taken.
|
|
8
|
-
|
|
9
|
-
use std::collections::{BTreeMap, BTreeSet};
|
|
10
|
-
use std::sync::Arc;
|
|
11
|
-
|
|
12
|
-
use kobako_runtime::filesystem::{DirEntry, File, Hold, Metadata};
|
|
13
|
-
use kobako_runtime::mount::Mount;
|
|
14
|
-
use wasmtime_wasi::p2::pipe::{MemoryInputPipe, MemoryOutputPipe};
|
|
15
|
-
|
|
16
|
-
/// What one descriptor stands for.
|
|
17
|
-
pub(crate) enum Descriptor {
|
|
18
|
-
/// Descriptor 0, carrying the Preamble.
|
|
19
|
-
Stdin(MemoryInputPipe),
|
|
20
|
-
/// Descriptor 1 or 2, a capture pipe the guest writes a Channel into.
|
|
21
|
-
Channel(MemoryOutputPipe),
|
|
22
|
-
/// A directory beneath a Mount; a preopen is the Mount's own root.
|
|
23
|
-
Directory(OpenDirectory),
|
|
24
|
-
File(OpenFile),
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/// What a directory is, apart from where it stands: the device and inode
|
|
28
|
-
/// numbers its Filesystem names it by.
|
|
29
|
-
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
30
|
-
pub(crate) struct Identity {
|
|
31
|
-
dev: u64,
|
|
32
|
-
ino: u64,
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
impl Identity {
|
|
36
|
-
/// The identity `metadata` names, or `None` where its inode of zero
|
|
37
|
-
/// reports none.
|
|
38
|
-
pub(crate) fn of(metadata: &Metadata) -> Option<Self> {
|
|
39
|
-
(metadata.ino != 0).then_some(Self {
|
|
40
|
-
dev: metadata.dev,
|
|
41
|
-
ino: metadata.ino,
|
|
42
|
-
})
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
pub(crate) struct OpenDirectory {
|
|
47
|
-
pub(crate) mount: Arc<Mount>,
|
|
48
|
-
/// The directory's path relative to the mount's root.
|
|
49
|
-
pub(crate) path: String,
|
|
50
|
-
/// The identity the directory had when it was opened, so one made in
|
|
51
|
-
/// its place is not taken for it; without one it follows its path.
|
|
52
|
-
pub(crate) identity: Option<Identity>,
|
|
53
|
-
/// What keeps that identity from passing to a directory made in this
|
|
54
|
-
/// one's place while the descriptor is open.
|
|
55
|
-
pub(crate) _hold: Option<Hold>,
|
|
56
|
-
pub(crate) preopen: bool,
|
|
57
|
-
/// The entries a listing began with, held until it reaches its end, so
|
|
58
|
-
/// each later call reads on without listing the directory again.
|
|
59
|
-
pub(crate) listing: Option<Vec<DirEntry>>,
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
pub(crate) struct OpenFile {
|
|
63
|
-
pub(crate) file: Box<dyn File>,
|
|
64
|
-
pub(crate) position: u64,
|
|
65
|
-
pub(crate) append: bool,
|
|
66
|
-
pub(crate) readable: bool,
|
|
67
|
-
pub(crate) writable: bool,
|
|
68
|
-
/// The Mount it was opened beneath, whose permission to write answers a
|
|
69
|
-
/// wait to write and a resize, as wasmtime-wasi answers both by its
|
|
70
|
-
/// preopen's permissions.
|
|
71
|
-
pub(crate) mount: Arc<Mount>,
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
pub(crate) struct Descriptors {
|
|
75
|
-
used: BTreeMap<u32, Descriptor>,
|
|
76
|
-
free: BTreeSet<u32>,
|
|
77
|
-
limit: Option<usize>,
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
impl Descriptors {
|
|
81
|
-
/// The table every invocation starts from: stdin, then the two
|
|
82
|
-
/// Channels, then one preopen per Mount from descriptor 3. It holds at
|
|
83
|
-
/// most `limit` descriptors, those included, as a process's
|
|
84
|
-
/// `RLIMIT_NOFILE` counts every one it holds.
|
|
85
|
-
pub(crate) fn new(
|
|
86
|
-
stdin: MemoryInputPipe,
|
|
87
|
-
stdout: MemoryOutputPipe,
|
|
88
|
-
stderr: MemoryOutputPipe,
|
|
89
|
-
mounts: Vec<Mount>,
|
|
90
|
-
limit: Option<usize>,
|
|
91
|
-
) -> Self {
|
|
92
|
-
let mut used = BTreeMap::from([
|
|
93
|
-
(0, Descriptor::Stdin(stdin)),
|
|
94
|
-
(1, Descriptor::Channel(stdout)),
|
|
95
|
-
(2, Descriptor::Channel(stderr)),
|
|
96
|
-
]);
|
|
97
|
-
for (fd, mount) in (3..).zip(mounts) {
|
|
98
|
-
// A root the Filesystem cannot stat or hold reports no
|
|
99
|
-
// identity, and the descriptor follows its path alone.
|
|
100
|
-
let filesystem = mount.filesystem();
|
|
101
|
-
let identity = filesystem
|
|
102
|
-
.metadata("")
|
|
103
|
-
.ok()
|
|
104
|
-
.and_then(|root| Identity::of(&root));
|
|
105
|
-
let hold = filesystem.hold_directory("").ok().flatten();
|
|
106
|
-
used.insert(
|
|
107
|
-
fd,
|
|
108
|
-
Descriptor::Directory(OpenDirectory {
|
|
109
|
-
mount: Arc::new(mount),
|
|
110
|
-
path: String::new(),
|
|
111
|
-
identity,
|
|
112
|
-
_hold: hold,
|
|
113
|
-
preopen: true,
|
|
114
|
-
listing: None,
|
|
115
|
-
}),
|
|
116
|
-
);
|
|
117
|
-
}
|
|
118
|
-
Self {
|
|
119
|
-
used,
|
|
120
|
-
free: BTreeSet::new(),
|
|
121
|
-
limit,
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
pub(crate) fn get(&self, fd: u32) -> Option<&Descriptor> {
|
|
126
|
-
self.used.get(&fd)
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
pub(crate) fn get_mut(&mut self, fd: u32) -> Option<&mut Descriptor> {
|
|
130
|
-
self.used.get_mut(&fd)
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
/// Take `fd` out of the table, freeing its number for reuse.
|
|
134
|
-
pub(crate) fn remove(&mut self, fd: u32) -> Option<Descriptor> {
|
|
135
|
-
let descriptor = self.used.remove(&fd)?;
|
|
136
|
-
self.free.insert(fd);
|
|
137
|
-
Some(descriptor)
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
/// Place `descriptor` at `fd`, a number the caller has just freed.
|
|
141
|
-
pub(crate) fn place(&mut self, fd: u32, descriptor: Descriptor) {
|
|
142
|
-
self.free.remove(&fd);
|
|
143
|
-
self.used.insert(fd, descriptor);
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
/// Whether the table holds as many descriptors as it may.
|
|
147
|
-
pub(crate) fn is_full(&self) -> bool {
|
|
148
|
-
self.limit.is_some_and(|limit| self.used.len() >= limit)
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
/// Add `descriptor` under the highest freed number, or the one past
|
|
152
|
-
/// the highest in use; `None` when the table is full.
|
|
153
|
-
pub(crate) fn push(&mut self, descriptor: Descriptor) -> Option<u32> {
|
|
154
|
-
if self.is_full() {
|
|
155
|
-
return None;
|
|
156
|
-
}
|
|
157
|
-
let fd = match self.free.pop_last() {
|
|
158
|
-
Some(fd) => fd,
|
|
159
|
-
None => match self.used.last_key_value() {
|
|
160
|
-
Some((last, _)) => last.checked_add(1)?,
|
|
161
|
-
None => 0,
|
|
162
|
-
},
|
|
163
|
-
};
|
|
164
|
-
self.used.insert(fd, descriptor);
|
|
165
|
-
Some(fd)
|
|
166
|
-
}
|
|
167
|
-
}
|