use std::{
borrow::Cow,
collections::HashMap,
path::PathBuf,
sync::{Arc, RwLock, RwLockReadGuard, RwLockWriteGuard, atomic::AtomicU64},
};
use virtual_fs::{Pipe, PipeRx, PipeTx, VirtualFile};
use wasmer_wasix_types::wasi::{Fdflags, Fdflagsext, Filestat, Rights};
use crate::net::socket::InodeSocket;
use crate::os::epoll::EpollState;
use super::{InodeGuard, InodeWeakGuard, NotificationInner};
pub(crate) type VirtualFileLock = Arc<RwLock<Box<dyn VirtualFile + Send + Sync + 'static>>>;
#[derive(Debug, Clone)]
pub struct Fd {
pub inner: FdInner,
pub open_flags: u16,
pub inode: InodeGuard,
pub is_stdio: bool,
}
#[derive(Debug, Clone)]
pub struct FdInner {
pub rights: Rights,
pub rights_inheriting: Rights,
pub flags: Fdflags, pub offset: Arc<AtomicU64>, pub fd_flags: Fdflagsext, }
impl Fd {
pub const READ: u16 = 1;
pub const WRITE: u16 = 2;
pub const APPEND: u16 = 4;
pub const TRUNCATE: u16 = 8;
pub const CREATE: u16 = 16;
}
#[derive(Debug)]
pub struct InodeVal {
pub stat: RwLock<Filestat>,
pub is_preopened: bool,
pub name: RwLock<Cow<'static, str>>,
pub kind: RwLock<Kind>,
}
impl InodeVal {
pub fn read(&self) -> RwLockReadGuard<'_, Kind> {
self.kind.read().unwrap()
}
pub fn write(&self) -> RwLockWriteGuard<'_, Kind> {
self.kind.write().unwrap()
}
}
#[derive(Debug)]
pub enum Kind {
File {
handle: Option<VirtualFileLock>,
path: PathBuf,
fd: Option<u32>,
},
Socket {
socket: InodeSocket,
},
PipeTx {
tx: PipeTx,
},
PipeRx {
rx: PipeRx,
},
DuplexPipe {
pipe: Pipe,
},
Epoll {
state: Arc<EpollState>,
},
Dir {
parent: InodeWeakGuard,
path: PathBuf,
entries: HashMap<String, InodeGuard>,
},
Root {
entries: HashMap<String, InodeGuard>,
},
Symlink {
symlink_kind: SymlinkKind,
path_to_symlink: PathBuf,
relative_path: PathBuf,
},
Buffer {
buffer: Vec<u8>,
},
EventNotifications {
inner: Arc<NotificationInner>,
},
}
#[derive(Clone, Copy, Debug)]
pub enum SymlinkKind {
Backing,
Virtual,
}