Add comments in ch6

This commit is contained in:
hypocrasy 2022-04-30 16:04:58 +08:00
parent cea2febe35
commit f9346edad1
35 changed files with 374 additions and 116 deletions

View file

@ -1,3 +1,9 @@
//! `Arc<Inode>` -> `OSInodeInner`: In order to open files concurrently
//! we need to wrap `Inode` into `Arc`,but `Mutex` in `Inode` prevents
//! file systems from being accessed simultaneously
//!
//! `UPSafeCell<OSInodeInner>` -> `OSInode`: for static `ROOT_INODE`,we
//! need to wrap `OSInodeInner` into `UPSafeCell`
use super::File;
use crate::drivers::BLOCK_DEVICE;
use crate::mm::UserBuffer;
@ -7,19 +13,21 @@ use alloc::vec::Vec;
use bitflags::*;
use easy_fs::{EasyFileSystem, Inode};
use lazy_static::*;
/// A wrapper around a filesystem inode
/// to implement File trait atop
pub struct OSInode {
readable: bool,
writable: bool,
inner: UPSafeCell<OSInodeInner>,
}
/// The OS inode inner in 'UPSafeCell'
pub struct OSInodeInner {
offset: usize,
inode: Arc<Inode>,
}
impl OSInode {
/// Construct an OS inode from a inode
pub fn new(readable: bool, writable: bool, inode: Arc<Inode>) -> Self {
Self {
readable,
@ -27,6 +35,7 @@ impl OSInode {
inner: unsafe { UPSafeCell::new(OSInodeInner { offset: 0, inode }) },
}
}
/// Read all data inside a inode into vector
pub fn read_all(&self) -> Vec<u8> {
let mut inner = self.inner.exclusive_access();
let mut buffer = [0u8; 512];
@ -49,7 +58,7 @@ lazy_static! {
Arc::new(EasyFileSystem::root_inode(&efs))
};
}
/// List all files in the filesystems
pub fn list_apps() {
println!("/**** APPS ****");
for app in ROOT_INODE.ls() {
@ -58,12 +67,18 @@ pub fn list_apps() {
println!("**************/");
}
bitflags! {
bitflags! {
///Open file flags
pub struct OpenFlags: u32 {
///Read only
const RDONLY = 0;
///Write only
const WRONLY = 1 << 0;
///Read & Write
const RDWR = 1 << 1;
///Allow create
const CREATE = 1 << 9;
///Clear file and return an empty one
const TRUNC = 1 << 10;
}
}
@ -81,7 +96,7 @@ impl OpenFlags {
}
}
}
///Open file with flags
pub fn open_file(name: &str, flags: OpenFlags) -> Option<Arc<OSInode>> {
let (readable, writable) = flags.read_write();
if flags.contains(OpenFlags::CREATE) {