Skip to content

Commit

Permalink
fix: compilation (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
llenotre committed Aug 17, 2024
1 parent 3c0600d commit 27a4200
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 107 deletions.
20 changes: 1 addition & 19 deletions kernel/src/file/buffer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ use crate::{
};
use core::{alloc::AllocError, any::Any, ffi::c_void};
use utils::{
boxed::Box,
collections::hashmap::HashMap,
errno::{AllocResult, EResult},
lock::Mutex,
Expand All @@ -54,7 +53,7 @@ pub trait BufferOps: Any + NodeOps {

/// A buffer.
#[derive(Clone, Debug)]
pub struct Buffer(Arc<dyn BufferOps>);
pub struct Buffer(pub Arc<dyn BufferOps>);

impl Buffer {
/// Creates a new instance with the given buffer type.
Expand Down Expand Up @@ -91,20 +90,3 @@ impl NodeOps for Buffer {
///
/// The key is the location of the file associated with the entry.
pub static BUFFERS: Mutex<HashMap<FileLocation, Buffer>> = Mutex::new(HashMap::new());

/// Returns the buffer associated with the file at location `loc`.
///
/// If the buffer does not exist, the function registers a new default buffer.
pub fn get_or_default<B: BufferOps + TryDefault<Error = AllocError> + 'static>(
loc: &FileLocation,
) -> AllocResult<Box<Buffer>> {
let mut buffers = BUFFERS.lock();
match buffers.get(loc).cloned() {
Some(buf) => Box::new(buf.clone()),
None => {
let buf = Buffer::new(B::try_default()?)?;
buffers.insert(loc.clone(), buf.clone())?;
Ok(buf)
}
}
}
4 changes: 2 additions & 2 deletions kernel/src/file/fs/tmp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ impl Node {
inode: Option<INode>,
parent_inode: Option<INode>,
) -> AllocResult<Self> {
let file_type = stat.get_type().ok_or_else(|| errno!(EINVAL))?;
let file_type = stat.get_type().unwrap();
let content = match file_type {
FileType::Regular => NodeContent::Regular(Vec::new()),
FileType::Directory => {
Expand Down Expand Up @@ -371,7 +371,7 @@ impl NodeOps for Node {
// Get node
let node = fs.nodes.lock().get_node(inode)?.clone();
let mut inner = node.0.lock();
let entry_type = FileType::from_mode(inner.as_stat().mode).unwrap();
let entry_type = inner.as_stat().get_type().unwrap();
let mut parent_inner = self.0.lock();
// Get parent entries
let NodeContent::Directory(parent_entries) = &mut parent_inner.content else {
Expand Down
11 changes: 10 additions & 1 deletion kernel/src/file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ use crate::{
device,
device::{DeviceID, DeviceType},
file::{
buffer::{Buffer, BufferOps},
fs::{Filesystem, NodeOps},
path::{Path, PathBuf},
perm::{Gid, Uid},
Expand All @@ -50,7 +51,7 @@ use crate::{
unit::{Timestamp, TimestampScale},
},
};
use core::{ffi::c_void, ops::Deref};
use core::{any::Any, ffi::c_void, ops::Deref};
use mountpoint::{MountPoint, MountSource};
use perm::AccessProfile;
use utils::{
Expand Down Expand Up @@ -436,6 +437,14 @@ impl File {
FileType::from_mode(stat.mode).ok_or_else(|| errno!(EUCLEAN))
}

/// Returns the file's associated buffer.
///
/// If the file does not have a buffer of type `B`, the function returns `None`.
pub fn get_buffer<B: BufferOps>(&self) -> Option<&B> {
let buf = (&self.ops as &dyn Any).downcast_ref::<Buffer>()?;
(buf.0.deref() as &dyn Any).downcast_ref()
}

/// Reads the whole content of a file into a buffer.
///
/// This function does not change the file's offset.
Expand Down
3 changes: 1 addition & 2 deletions kernel/src/process/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,8 +432,7 @@ impl Process {
let mut fds_table = FileDescriptorTable::default();
let tty_path = PathBuf::try_from(TTY_DEVICE_PATH.as_bytes())?;
let tty_file = vfs::get_file_from_path(&tty_path, &rs)?;
let file = File::open(tty_file, Some(tty_path), file::O_RDWR)?;
let (stdin_fd_id, _) = fds_table.create_fd(0, file)?;
let (stdin_fd_id, _) = fds_table.create_fd(0, tty_file)?;
assert_eq!(stdin_fd_id, STDIN_FILENO);
fds_table.duplicate_fd(
STDIN_FILENO as _,
Expand Down
14 changes: 3 additions & 11 deletions kernel/src/syscall/bind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,9 @@ pub fn bind(
return Err(errno!(EINVAL));
}
// Get socket
let loc = fds
.lock()
.get_fd(sockfd)?
.get_file()
.lock()
.get_location()
.clone();
let sock = buffer::get(&loc).ok_or_else(|| errno!(ENOENT))?;
let sock = (&*sock as &dyn Any)
.downcast_ref::<Socket>()
.ok_or_else(|| errno!(ENOTSOCK))?;
let file_mutex = fds.lock().get_fd(sockfd)?.get_file().clone();
let file = file_mutex.lock();
let sock: &Socket = file.get_buffer().ok_or_else(|| errno!(ENOTSOCK))?;
let addr = addr
.copy_from_user(..(addrlen as usize))?
.ok_or_else(|| errno!(EFAULT))?;
Expand Down
14 changes: 3 additions & 11 deletions kernel/src/syscall/connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,9 @@ pub fn connect(
return Err(errno!(EINVAL));
}
// Get socket
let loc = fds
.lock()
.get_fd(sockfd)?
.get_file()
.lock()
.get_location()
.clone();
let sock = buffer::get(&loc).ok_or_else(|| errno!(ENOENT))?;
let _sock = (&*sock as &dyn Any)
.downcast_ref::<Socket>()
.ok_or_else(|| errno!(ENOTSOCK))?;
let file_mutex = fds.lock().get_fd(sockfd)?.get_file().clone();
let file = file_mutex.lock();
let _sock: &Socket = file.get_buffer().ok_or_else(|| errno!(ENOTSOCK))?;
let _addr = addr
.copy_from_user(..(addrlen as usize))?
.ok_or_else(|| errno!(EFAULT))?;
Expand Down
8 changes: 2 additions & 6 deletions kernel/src/syscall/fcntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,12 +253,8 @@ pub fn do_fcntl(
F_GETPIPE_SZ => {
let file_mutex = fds.get_fd(fd)?.get_file();
let file = file_mutex.lock();
match file.get_type()? {
FileType::Fifo => {
let buf = buffer::get_or_default::<PipeBuffer>(&file.get_location())?;
let buf: &PipeBuffer = (&*buf as &dyn Any).downcast_ref().unwrap();
Ok(buf.get_capacity() as _)
}
match file.get_buffer::<PipeBuffer>() {
Some(fifo) => Ok(fifo.get_capacity() as _),
_ => Ok(0),
}
}
Expand Down
14 changes: 3 additions & 11 deletions kernel/src/syscall/getsockname.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,9 @@ pub fn getsockname(
fds: Arc<Mutex<FileDescriptorTable>>,
) -> EResult<usize> {
// Get socket
let loc = fds
.lock()
.get_fd(sockfd)?
.get_file()
.lock()
.get_location()
.clone();
let sock = buffer::get(&loc).ok_or_else(|| errno!(ENOENT))?;
let sock = (&*sock as &dyn Any)
.downcast_ref::<Socket>()
.ok_or_else(|| errno!(ENOTSOCK))?;
let file_mutex = fds.lock().get_fd(sockfd)?.get_file().clone();
let file = file_mutex.lock();
let sock: &Socket = file.get_buffer().ok_or_else(|| errno!(ENOTSOCK))?;
// Read and check buffer length
let addrlen_val = addrlen.copy_from_user()?.ok_or_else(|| errno!(EFAULT))?;
if addrlen_val < 0 {
Expand Down
14 changes: 3 additions & 11 deletions kernel/src/syscall/getsockopt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,9 @@ pub fn getsockopt(
fds: Arc<Mutex<FileDescriptorTable>>,
) -> EResult<usize> {
// Get socket
let loc = fds
.lock()
.get_fd(sockfd)?
.get_file()
.lock()
.get_location()
.clone();
let sock = buffer::get(&loc).ok_or_else(|| errno!(ENOENT))?;
let sock = (&*sock as &dyn Any)
.downcast_ref::<Socket>()
.ok_or_else(|| errno!(ENOTSOCK))?;
let file_mutex = fds.lock().get_fd(sockfd)?.get_file().clone();
let file = file_mutex.lock();
let sock: &Socket = file.get_buffer().ok_or_else(|| errno!(ENOTSOCK))?;
let val = sock.get_opt(level, optname)?;
// Write back
let len = min(val.len(), optlen);
Expand Down
14 changes: 3 additions & 11 deletions kernel/src/syscall/sendto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,9 @@ pub fn sendto(
return Err(errno!(EINVAL));
}
// Get socket
let loc = fds
.lock()
.get_fd(sockfd)?
.get_file()
.lock()
.get_location()
.clone();
let sock = buffer::get(&loc).ok_or_else(|| errno!(ENOENT))?;
let _sock = (&*sock as &dyn Any)
.downcast_ref::<Socket>()
.ok_or_else(|| errno!(ENOTSOCK))?;
let file_mutex = fds.lock().get_fd(sockfd)?.get_file().clone();
let file = file_mutex.lock();
let _sock: &Socket = file.get_buffer().ok_or_else(|| errno!(ENOTSOCK))?;
// Get slices
let _buf_slice = buf.copy_from_user(..len)?.ok_or(errno!(EFAULT))?;
let _dest_addr_slice = dest_addr
Expand Down
14 changes: 3 additions & 11 deletions kernel/src/syscall/setsockopt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,9 @@ pub fn setsockopt(
fds: Arc<Mutex<FileDescriptorTable>>,
) -> EResult<usize> {
// Get socket
let loc = fds
.lock()
.get_fd(sockfd)?
.get_file()
.lock()
.get_location()
.clone();
let sock = buffer::get(&loc).ok_or_else(|| errno!(ENOENT))?;
let sock = (&*sock as &dyn Any)
.downcast_ref::<Socket>()
.ok_or_else(|| errno!(ENOTSOCK))?;
let file_mutex = fds.lock().get_fd(sockfd)?.get_file().clone();
let file = file_mutex.lock();
let sock: &Socket = file.get_buffer().ok_or_else(|| errno!(ENOTSOCK))?;
// Set opt
let optval_slice = optval.copy_from_user(..optlen)?.ok_or(errno!(EFAULT))?;
sock.set_opt(level, optname, &optval_slice)
Expand Down
14 changes: 3 additions & 11 deletions kernel/src/syscall/shutdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,9 @@ pub fn shutdown(
fds: Arc<Mutex<FileDescriptorTable>>,
) -> EResult<usize> {
// Get socket
let loc = fds
.lock()
.get_fd(sockfd)?
.get_file()
.lock()
.get_location()
.clone();
let sock = buffer::get(&loc).ok_or_else(|| errno!(ENOENT))?;
let sock = (&*sock as &dyn Any)
.downcast_ref::<Socket>()
.ok_or_else(|| errno!(ENOTSOCK))?;
let file_mutex = fds.lock().get_fd(sockfd)?.get_file().clone();
let file = file_mutex.lock();
let sock: &Socket = file.get_buffer().ok_or_else(|| errno!(ENOTSOCK))?;
// Do shutdown
match how {
SHUT_RD => sock.shutdown_reception(),
Expand Down

0 comments on commit 27a4200

Please sign in to comment.