-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathdelegate.rs
More file actions
176 lines (169 loc) · 6.1 KB
/
Copy pathdelegate.rs
File metadata and controls
176 lines (169 loc) · 6.1 KB
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
176
#[cfg(any(feature = "net", feature = "virtio-vsock"))]
use alloc::sync::Arc;
use core::mem::MaybeUninit;
use delegate::delegate;
use crate::fd::eventfd::EventFd;
#[cfg(feature = "tcp")]
use crate::fd::socket::tcp;
#[cfg(feature = "udp")]
use crate::fd::socket::udp;
#[cfg(feature = "virtio-vsock")]
use crate::fd::socket::vsock;
use crate::fd::stdio::{ConsoleStderr, ConsoleStdin, ConsoleStdout};
#[cfg(feature = "uhyve")]
use crate::fd::stdio::{UhyveStderr, UhyveStdin, UhyveStdout};
use crate::fd::{AccessPermission, ObjectInterface, PollEvent, StatusFlags};
#[cfg(any(feature = "net", feature = "virtio-vsock"))]
use crate::fd::{Endpoint, ListenEndpoint, SocketOption};
use crate::fs::mem::{MemDirectoryInterface, RamFileInterface, RomFileInterface};
#[cfg(feature = "uhyve")]
use crate::fs::uhyve::UhyveFileHandle;
#[cfg(feature = "virtio-fs")]
use crate::fs::virtio_fs::{VirtioFsDirectoryHandle, VirtioFsFileHandle};
use crate::fs::{DirectoryReader, FileAttr, SeekWhence};
use crate::io;
pub(crate) enum Fd {
ConsoleStdin(ConsoleStdin),
ConsoleStdout(ConsoleStdout),
ConsoleStderr(ConsoleStderr),
#[cfg(feature = "uhyve")]
UhyveStdin(UhyveStdin),
#[cfg(feature = "uhyve")]
UhyveStdout(UhyveStdout),
#[cfg(feature = "uhyve")]
UhyveStderr(UhyveStderr),
EventFd(EventFd),
#[cfg(feature = "tcp")]
TcpSocket(tcp::Socket),
#[cfg(feature = "udp")]
UdpSocket(udp::Socket),
#[cfg(feature = "virtio-vsock")]
VsockNullSocket(vsock::NullSocket),
#[cfg(feature = "virtio-vsock")]
VsockSocket(vsock::Socket),
#[cfg(feature = "virtio-fs")]
VirtioFsFileHandle(VirtioFsFileHandle),
#[cfg(feature = "virtio-fs")]
VirtioFsDirectoryHandle(VirtioFsDirectoryHandle),
RomFileInterface(RomFileInterface),
RamFileInterface(RamFileInterface),
MemDirectoryInterface(MemDirectoryInterface),
DirectoryReader(DirectoryReader),
#[cfg(feature = "uhyve")]
UhyveFileHandle(UhyveFileHandle),
}
macro_rules! fd_from {
() => {};
(
$(#[$meta:meta])*
$ident:ident($ty:ty),
$($rest:tt)*
) => {
$(#[$meta])*
impl From<$ty> for Fd {
fn from(value: $ty) -> Self {
Self::$ident(value)
}
}
fd_from!($($rest)*);
};
}
fd_from! {
ConsoleStdin(ConsoleStdin),
ConsoleStdout(ConsoleStdout),
ConsoleStderr(ConsoleStderr),
#[cfg(feature = "uhyve")]
UhyveStdin(UhyveStdin),
#[cfg(feature = "uhyve")]
UhyveStdout(UhyveStdout),
#[cfg(feature = "uhyve")]
UhyveStderr(UhyveStderr),
EventFd(EventFd),
#[cfg(feature = "tcp")]
TcpSocket(tcp::Socket),
#[cfg(feature = "udp")]
UdpSocket(udp::Socket),
#[cfg(feature = "virtio-vsock")]
VsockNullSocket(vsock::NullSocket),
#[cfg(feature = "virtio-vsock")]
VsockSocket(vsock::Socket),
#[cfg(feature = "virtio-fs")]
VirtioFsFileHandle(VirtioFsFileHandle),
#[cfg(feature = "virtio-fs")]
VirtioFsDirectoryHandle(VirtioFsDirectoryHandle),
RomFileInterface(RomFileInterface),
RamFileInterface(RamFileInterface),
MemDirectoryInterface(MemDirectoryInterface),
DirectoryReader(DirectoryReader),
#[cfg(feature = "uhyve")]
UhyveFileHandle(UhyveFileHandle),
}
impl ObjectInterface for Fd {
delegate! {
to match self {
Self::ConsoleStdin(fd) => fd,
Self::ConsoleStdout(fd) => fd,
Self::ConsoleStderr(fd) => fd,
#[cfg(feature = "uhyve")]
Self::UhyveStdin(fd) => fd,
#[cfg(feature = "uhyve")]
Self::UhyveStdout(fd) => fd,
#[cfg(feature = "uhyve")]
Self::UhyveStderr(fd) => fd,
Self::EventFd(fd) => fd,
#[cfg(feature = "tcp")]
Self::TcpSocket(fd) => fd,
#[cfg(feature = "udp")]
Self::UdpSocket(fd) => fd,
#[cfg(feature = "virtio-vsock")]
Self::VsockNullSocket(fd) => fd,
#[cfg(feature = "virtio-vsock")]
Self::VsockSocket(fd) => fd,
#[cfg(feature = "virtio-fs")]
Self::VirtioFsFileHandle(fd) => fd,
#[cfg(feature = "virtio-fs")]
Self::VirtioFsDirectoryHandle(fd) => fd,
Self::RomFileInterface(fd) => fd,
Self::RamFileInterface(fd) => fd,
Self::MemDirectoryInterface(fd) => fd,
Self::DirectoryReader(fd) => fd,
#[cfg(feature = "uhyve")]
Self::UhyveFileHandle(fd) => fd,
} {
async fn poll(&self, event: PollEvent) -> io::Result<PollEvent>;
async fn read(&self, buf: &mut [u8]) -> io::Result<usize>;
async fn write(&self, buf: &[u8]) -> io::Result<usize>;
async fn lseek(&self, offset: isize, whence: SeekWhence) -> io::Result<isize>;
async fn fstat(&self) -> io::Result<FileAttr>;
async fn getdents(&self, buf: &mut [MaybeUninit<u8>]) -> io::Result<usize>;
#[cfg(any(feature = "net", feature = "virtio-vsock"))]
async fn accept(&mut self) -> io::Result<(Arc<async_lock::RwLock<Fd>>, Endpoint)>;
#[cfg(any(feature = "net", feature = "virtio-vsock"))]
async fn connect(&mut self, endpoint: Endpoint) -> io::Result<()>;
#[cfg(any(feature = "net", feature = "virtio-vsock"))]
async fn bind(&mut self, _name: ListenEndpoint) -> io::Result<()>;
#[cfg(any(feature = "net", feature = "virtio-vsock"))]
async fn listen(&mut self, _backlog: i32) -> io::Result<()>;
#[cfg(any(feature = "net", feature = "virtio-vsock"))]
async fn setsockopt(&self, _opt: SocketOption, _optval: bool) -> io::Result<()>;
#[cfg(any(feature = "net", feature = "virtio-vsock"))]
async fn getsockopt(&self, _opt: SocketOption) -> io::Result<bool>;
#[cfg(any(feature = "net", feature = "virtio-vsock"))]
async fn getsockname(&self) -> io::Result<Option<Endpoint>>;
#[cfg(any(feature = "net", feature = "virtio-vsock"))]
#[allow(dead_code)]
async fn getpeername(&self) -> io::Result<Option<Endpoint>>;
#[cfg(any(feature = "net", feature = "virtio-vsock"))]
async fn recvfrom(&self, _buffer: &mut [MaybeUninit<u8>]) -> io::Result<(usize, Endpoint)>;
#[cfg(any(feature = "net", feature = "virtio-vsock"))]
async fn sendto(&self, _buffer: &[u8], _endpoint: Endpoint) -> io::Result<usize>;
#[cfg(any(feature = "net", feature = "virtio-vsock"))]
async fn shutdown(&self, _how: i32) -> io::Result<()>;
async fn status_flags(&self) -> io::Result<StatusFlags>;
async fn set_status_flags(&mut self, _status_flags: StatusFlags) -> io::Result<()>;
async fn truncate(&self, _size: usize) -> io::Result<()>;
async fn chmod(&self, _access_permission: AccessPermission) -> io::Result<()>;
async fn isatty(&self) -> io::Result<bool>;
}
}
}