Skip to content

Commit

Permalink
Add close to FileIO
Browse files Browse the repository at this point in the history
  • Loading branch information
vinc committed Jul 9, 2023
1 parent 553124e commit 99c640c
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/api/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub use crate::sys::fs::{FileInfo, DeviceType};
pub trait FileIO {
fn read(&mut self, buf: &mut [u8]) -> Result<usize, ()>;
fn write(&mut self, buf: &[u8]) -> Result<usize, ()>;
fn close(&mut self);
}

pub fn dirname(pathname: &str) -> &str {
Expand Down
8 changes: 8 additions & 0 deletions src/sys/clock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,13 @@ impl FileIO for Uptime {
Err(())
}
}

fn write(&mut self, _buf: &[u8]) -> Result<usize, ()> {
unimplemented!();
}

fn close(&mut self) {
}
}

// NOTE: This clock is monotonic
Expand Down Expand Up @@ -65,9 +69,13 @@ impl FileIO for Realtime {
Err(())
}
}

fn write(&mut self, _buf: &[u8]) -> Result<usize, ()> {
unimplemented!();
}

fn close(&mut self) {
}
}

// NOTE: This clock is not monotonic
Expand Down
3 changes: 3 additions & 0 deletions src/sys/cmos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ impl FileIO for RTC {
CMOS::new().update_rtc(self);
Ok(buf.len())
}

fn close(&mut self) {
}
}

pub struct CMOS {
Expand Down
3 changes: 3 additions & 0 deletions src/sys/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ impl FileIO for Console {
print_fmt(format_args!("{}", s));
Ok(n)
}

fn close(&mut self) {
}
}

pub fn cols() -> usize {
Expand Down
13 changes: 13 additions & 0 deletions src/sys/fs/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,17 @@ impl FileIO for Device {
Device::TcpSocket(io) => io.write(buf),
}
}

fn close(&mut self) {
match self {
Device::Null => {},
Device::File(io) => io.close(),
Device::Console(io) => io.close(),
Device::Random(io) => io.close(),
Device::Uptime(io) => io.close(),
Device::Realtime(io) => io.close(),
Device::RTC(io) => io.close(),
Device::TcpSocket(io) => io.close(),
}
}
}
4 changes: 4 additions & 0 deletions src/sys/fs/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,13 @@ impl FileIO for Dir {
}
Ok(i)
}

fn write(&mut self, _buf: &[u8]) -> Result<usize, ()> {
Err(())
}

fn close(&mut self) {
}
}

// Truncate to the given number of bytes at most while respecting char boundaries
Expand Down
3 changes: 3 additions & 0 deletions src/sys/fs/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@ impl FileIO for File {
}
Ok(bytes)
}

fn close(&mut self) {
}
}

#[test_case]
Expand Down
8 changes: 8 additions & 0 deletions src/sys/fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,14 @@ impl FileIO for Resource {
Resource::Device(io) => io.write(buf),
}
}

fn close(&mut self) {
match self {
Resource::Dir(io) => io.close(),
Resource::File(io) => io.close(),
Resource::Device(io) => io.close(),
}
}
}

pub fn canonicalize(path: &str) -> Result<String, ()> {
Expand Down
22 changes: 22 additions & 0 deletions src/sys/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,28 @@ impl FileIO for TcpSocket {
Err(())
}
}

fn close(&mut self) {
let mut closed = false;
if let Some((ref mut iface, ref mut device)) = *sys::net::NET.lock() {
let mut sockets = SOCKETS.lock();
loop {
iface.poll(time(), device, &mut sockets);
let socket = sockets.get_mut::<tcp::Socket>(self.handle);

if closed {
break;
}
socket.close();
closed = true;

if let Some(duration) = iface.poll_delay(time(), &sockets) {
wait(duration);
}
sys::time::halt();
}
}
}
}

fn find_pci_io_base(vendor_id: u16, device_id: u16) -> Option<u16> {
Expand Down
4 changes: 4 additions & 0 deletions src/sys/random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@ impl FileIO for Random {
}
Ok(n)
}

fn write(&mut self, _buf: &[u8]) -> Result<usize, ()> {
unimplemented!();
}

fn close(&mut self) {
}
}

pub fn get_u64() -> u64 {
Expand Down
5 changes: 4 additions & 1 deletion src/sys/syscall/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ pub fn write(handle: usize, buf: &mut [u8]) -> isize {
}

pub fn close(handle: usize) {
sys::process::delete_file_handle(handle);
if let Some(mut file) = sys::process::file_handle(handle) {
file.close();
sys::process::delete_file_handle(handle);
}
}

pub fn spawn(path: &str, args_ptr: usize, args_len: usize) -> ExitCode {
Expand Down

0 comments on commit 99c640c

Please sign in to comment.