mirror of
https://github.com/asterinas/asterinas.git
synced 2025-06-09 13:26:48 +00:00
Add support for close syscall
This commit is contained in:
parent
551406b9f4
commit
15d2e07a8c
@ -28,4 +28,8 @@ pub trait File: Send + Sync + Any {
|
|||||||
fn poll(&self) -> IoEvents {
|
fn poll(&self) -> IoEvents {
|
||||||
IoEvents::empty()
|
IoEvents::empty()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn flush(&self) -> Result<()> {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,7 @@ mod inode_handle;
|
|||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use crate::rights::{ReadOp, WriteOp};
|
use crate::rights::{ReadOp, WriteOp};
|
||||||
use alloc::sync::Arc;
|
use alloc::sync::Arc;
|
||||||
|
use core::ops::Range;
|
||||||
|
|
||||||
pub use self::file::File;
|
pub use self::file::File;
|
||||||
pub use self::inode_handle::InodeHandle;
|
pub use self::inode_handle::InodeHandle;
|
||||||
@ -65,4 +66,20 @@ impl FileHandle {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn clean_for_close(&self) -> Result<()> {
|
||||||
|
match &self.inner {
|
||||||
|
Inner::Inode(inode_handle) => {
|
||||||
|
let dentry = inode_handle.dentry();
|
||||||
|
let ref_count = Arc::strong_count(dentry);
|
||||||
|
// The dentry is held by dentry cache and self
|
||||||
|
if ref_count == 2 {
|
||||||
|
let page_cache_size = dentry.vnode().pages().size();
|
||||||
|
dentry.vnode().pages().decommit(0..page_cache_size)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Inner::File(file) => file.flush()?,
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -58,8 +58,8 @@ impl FileTable {
|
|||||||
fd
|
fd
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn close_file(&mut self, fd: FileDescripter) {
|
pub fn close_file(&mut self, fd: FileDescripter) -> Option<FileHandle> {
|
||||||
self.table.remove(&fd);
|
self.table.remove(&fd)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_file(&self, fd: FileDescripter) -> Result<&FileHandle> {
|
pub fn get_file(&self, fd: FileDescripter) -> Result<&FileHandle> {
|
||||||
|
@ -9,6 +9,7 @@ pub fn sys_close(fd: FileDescripter) -> Result<SyscallReturn> {
|
|||||||
let current = current!();
|
let current = current!();
|
||||||
let mut file_table = current.file_table().lock();
|
let mut file_table = current.file_table().lock();
|
||||||
let _ = file_table.get_file(fd)?;
|
let _ = file_table.get_file(fd)?;
|
||||||
file_table.close_file(fd);
|
let file = file_table.close_file(fd).unwrap();
|
||||||
|
file.clean_for_close()?;
|
||||||
Ok(SyscallReturn::Return(0))
|
Ok(SyscallReturn::Return(0))
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user