1
0
mirror of https://github.com/rcore-os/rCore.git synced 2024-11-27 10:13:28 +04:00
rCore/kernel/src/fs/file.rs

41 lines
972 B
Rust
Raw Normal View History

2019-02-22 08:54:42 +04:00
//! File structure for process
use alloc::{string::String, sync::Arc};
2019-02-22 13:10:07 +04:00
use rcore_fs::vfs::{Metadata, INode, Result};
2019-02-22 08:54:42 +04:00
#[derive(Clone)]
pub struct File {
inode: Arc<INode>,
offset: usize,
readable: bool,
writable: bool,
}
impl File {
pub fn new(inode: Arc<INode>, readable: bool, writable: bool) -> Self {
File { inode, offset: 0, readable, writable }
}
pub fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
assert!(self.readable);
let len = self.inode.read_at(self.offset, buf)?;
self.offset += len;
Ok(len)
}
pub fn write(&mut self, buf: &[u8]) -> Result<usize> {
assert!(self.writable);
let len = self.inode.write_at(self.offset, buf)?;
self.offset += len;
Ok(len)
}
2019-02-22 13:10:07 +04:00
pub fn info(&self) -> Result<Metadata> {
self.inode.metadata()
2019-02-22 08:54:42 +04:00
}
pub fn get_entry(&self, id: usize) -> Result<String> {
self.inode.get_entry(id)
}
}