Merge remote-tracking branch 'wangrunji0408/multi-thread' into ucore-fs-enhance

master
Ben Pig Chu 6 years ago
commit b216da64ce

@ -2,4 +2,5 @@ language: rust
rust:
- nightly
script:
- cargo build --bin mksfs --features "std"
- cargo test --verbose -- --test-threads=1

@ -2,6 +2,7 @@
name = "simple-filesystem"
version = "0.0.1"
authors = ["WangRunji <wangrunji0408@163.com>"]
edition = "2018"
[[bin]]
name = "mksfs"
@ -10,7 +11,7 @@ required-features = ["std"]
[dependencies]
bit-vec = { default-features = false, git = "https://github.com/AltSysrq/bit-vec.git" } # default-features contains 'std'
static_assertions = "0.2.5"
static_assertions = "0.3"
spin = "0.4"
[features]

@ -1,10 +1,9 @@
extern crate simple_filesystem;
use std::env;
use std::fs;
use std::io::{Read, Write, Result};
use std::path::Path;
use std::mem::uninitialized;
use std::sync::Arc;
use simple_filesystem::*;
fn main() -> Result<()> {
@ -31,7 +30,7 @@ fn zip(path: &Path, img_path: &Path) -> Result<()> {
Ok(())
}
fn zip_dir(path: &Path, inode: INodePtr) -> Result<()> {
fn zip_dir(path: &Path, inode: Arc<INode>) -> Result<()> {
let dir = fs::read_dir(path).expect("Failed to open dir");
for entry in dir {
let entry = entry?;
@ -39,19 +38,19 @@ fn zip_dir(path: &Path, inode: INodePtr) -> Result<()> {
let name = name_.to_str().unwrap();
let type_ = entry.file_type()?;
if type_.is_file() {
let inode = inode.borrow_mut().create(name, FileType::File).expect("Failed to create INode");
let inode = inode.create(name, FileType::File).expect("Failed to create INode");
let mut file = fs::File::open(entry.path())?;
inode.borrow_mut().resize(file.metadata().unwrap().len() as usize).expect("Failed to resize INode");
inode.resize(file.metadata().unwrap().len() as usize).expect("Failed to resize INode");
let mut buf: [u8; 4096] = unsafe { uninitialized() };
let mut offset = 0usize;
let mut len = 4096;
while len == 4096 {
len = file.read(&mut buf)?;
inode.borrow().write_at(offset, &buf).expect("Failed to write image");
inode.write_at(offset, &buf).expect("Failed to write image");
offset += len;
}
} else if type_.is_dir() {
let inode = inode.borrow_mut().create(name, FileType::Dir).expect("Failed to create INode");
let inode = inode.create(name, FileType::Dir).expect("Failed to create INode");
zip_dir(entry.path().as_path(), inode)?;
}
}
@ -66,13 +65,13 @@ fn unzip(path: &Path, img_path: &Path) -> Result<()> {
unzip_dir(path, inode)
}
fn unzip_dir(path: &Path, inode: INodePtr) -> Result<()> {
let files = inode.borrow().list().expect("Failed to list files from INode");
fn unzip_dir(path: &Path, inode: Arc<INode>) -> Result<()> {
let files = inode.list().expect("Failed to list files from INode");
for name in files.iter().skip(2) {
let inode = inode.borrow().lookup(name.as_str()).expect("Failed to lookup");
let inode = inode.lookup(name.as_str()).expect("Failed to lookup");
let mut path = path.to_path_buf();
path.push(name);
let info = inode.borrow().info().expect("Failed to get file info");
let info = inode.info().expect("Failed to get file info");
match info.type_ {
FileType::File => {
let mut file = fs::File::create(&path)?;
@ -80,7 +79,7 @@ fn unzip_dir(path: &Path, inode: INodePtr) -> Result<()> {
let mut offset = 0usize;
let mut len = 4096;
while len == 4096 {
len = inode.borrow().read_at(offset, buf.as_mut()).expect("Failed to read from INode");
len = inode.read_at(offset, buf.as_mut()).expect("Failed to read from INode");
file.write(&buf[..len])?;
offset += len;
}

@ -1,5 +1,5 @@
use util::*;
use vfs::Device;
use crate::util::*;
use crate::vfs::Device;
/// Device which can only R/W in blocks
pub trait BlockedDevice: Send {

@ -1,4 +1,4 @@
use vfs::{INode, Result, FileInfo};
use crate::vfs::{INode, Result, FileInfo};
use alloc::{sync::Arc, string::String};
pub struct File {

@ -1,18 +1,10 @@
#![cfg_attr(not(any(test, feature = "std")), no_std)]
#![feature(alloc)]
#![feature(const_fn)]
#![feature(const_str_len)]
#![feature(nll)]
#![cfg_attr(target_arch = "riscv", feature(match_default_bindings))]
#![no_std]
#[cfg(any(test, feature = "std"))]
#[macro_use]
extern crate std;
extern crate alloc;
extern crate bit_vec;
#[macro_use]
extern crate static_assertions;
extern crate spin;
#[cfg(not(test))]
#[allow(unused_macros)]
@ -32,9 +24,9 @@ mod structs;
#[cfg(test)]
mod tests;
pub use sfs::*;
pub use vfs::*;
pub use blocked_device::BlockedDevice;
pub use crate::sfs::*;
pub use crate::vfs::*;
pub use crate::blocked_device::BlockedDevice;
#[cfg(any(test, feature = "std"))]
pub mod std_impl {

@ -4,11 +4,11 @@ use core::mem::uninitialized;
use core::slice;
use core::fmt::{Debug, Formatter, Error};
use core::any::Any;
use dirty::Dirty;
use structs::*;
use vfs::{self, Device, INode, FsError};
use util::*;
use spin::{Mutex, RwLock};
use crate::dirty::Dirty;
use crate::structs::*;
use crate::vfs::{self, Device, INode, FileSystem, FsError};
use crate::util::*;
impl Device {
fn read_block(&mut self, id: BlockId, offset: usize, buf: &mut [u8]) -> vfs::Result<()> {
@ -703,7 +703,6 @@ impl vfs::FileSystem for SimpleFileSystem {
impl Drop for SimpleFileSystem {
/// Auto sync when drop
fn drop(&mut self) {
use vfs::FileSystem;
self.sync().expect("Failed to sync when dropping the SimpleFileSystem");
}
}

@ -4,6 +4,7 @@ use core::slice;
use core::mem::{size_of_val, size_of};
use core::fmt::{Debug, Formatter, Error};
use alloc::str;
use static_assertions::const_assert;
/// On-disk superblock
#[repr(C)]

@ -2,9 +2,9 @@ use std::fs::{self, OpenOptions};
use std::boxed::Box;
use std::sync::Arc;
use std::mem::uninitialized;
use super::sfs::*;
use super::vfs::*;
use super::structs::AsBuf;
use crate::sfs::*;
use crate::vfs::*;
use crate::structs::AsBuf;
fn _open_sample_file() -> Arc<SimpleFileSystem> {
fs::copy("sfs.img", "test.img").expect("failed to open sfs.img");
@ -34,21 +34,6 @@ fn create_new_sfs() {
let _root = sfs.root_inode();
}
#[test]
#[ignore]
fn print_root() -> Result<()> {
let sfs = _open_sample_file();
let root = sfs.root_inode();
println!("{:?}", root);
let files = root.list()?;
println!("{:?}", files);
assert_eq!(files[3], root.get_entry(3)?);
sfs.sync()?;
Ok(())
}
#[test]
fn create_file() -> Result<()> {
let sfs = _create_new_sfs();

@ -1,5 +1,4 @@
use alloc::{vec::Vec, string::String, sync::Arc};
use core::fmt::Debug;
use core::any::Any;
use core::result;
@ -11,7 +10,7 @@ pub trait Device: Send {
}
/// Abstract operations on a inode.
pub trait INode: Debug + Any + Sync + Send {
pub trait INode: Any + Sync + Send {
fn read_at(&self, offset: usize, buf: &mut [u8]) -> Result<usize>;
fn write_at(&self, offset: usize, buf: &[u8]) -> Result<usize>;
fn info(&self) -> Result<FileInfo>;

Loading…
Cancel
Save