Some code for 'mksfs'. Move 'impl Device for std::fs::File' out of test mod.

master
WangRunji 7 years ago
parent 4f58ba5716
commit 075e416a7b

@ -4,13 +4,18 @@ version = "0.0.1"
authors = ["WangRunji <wangrunji0408@163.com>"] authors = ["WangRunji <wangrunji0408@163.com>"]
[lib] [lib]
name = "lib"
#crate-type = ["staticlib"] #crate-type = ["staticlib"]
[[bin]]
name = "mksfs"
path = "src/bin/mksfs.rs"
required-features = ["std"]
[profile.dev] [profile.dev]
panic = 'abort' # prevent `_Unwind_Resume` link error panic = 'abort' # prevent `_Unwind_Resume` link error
[dependencies] [dependencies]
spin = "0.4"
bit-set = { default-features = false, version = "0.5" } # default-features contains 'std' bit-set = { default-features = false, version = "0.5" } # default-features contains 'std'
static_assertions = "0.2.5" static_assertions = "0.2.5"
@ -20,3 +25,4 @@ bitflags = "1.0"
[features] [features]
debug_print = [] debug_print = []
ucore = [] ucore = []
std = []

@ -0,0 +1,61 @@
extern crate lib;
use std::env;
use std::fs;
use std::io::Read;
use std::path::Path;
use lib::*;
fn main () {
println!("USAGE: <zip|unzip> <PATH> <IMG>");
let args: Vec<_> = env::args().collect();
let cmd = &args[1];
let dir_path = Path::new(&args[2]);
let img_path = Path::new(&args[3]);
match cmd.as_str() {
"zip" => zip(dir_path, img_path),
"unzip" => unzip(dir_path, img_path),
_ => panic!("Invalid command: {}", cmd),
}
}
fn zip(path: &Path, img_path: &Path) {
use lib::std_impl;
let mut img = fs::File::create(img_path)
.expect(format!("Failed to create file: {:?}", img_path).as_str());
let sfs = SimpleFileSystem::create(Box::new(img), 0x1000000);
let inode = sfs.root_inode();
zip_dir(path, inode);
}
fn zip_dir(path: &Path, inode: INodePtr) {
println!("{:?}", path);
let dir = fs::read_dir(path).expect("Failed to open dir");
for entry in dir {
let entry = entry.unwrap();
let name_ = entry.file_name();
let name = name_.to_str().unwrap();
let type_ = entry.file_type().unwrap();
if type_.is_file() {
let inode = inode.borrow_mut().create(name, FileType::File)
.expect("Failed to create INode");
let mut file = fs::File::open(entry.path())
.expect("Failed to open file");
let mut buf = Vec::<u8>::new();
file.read_to_end(&mut buf)
.expect("Failed to read file");
inode.borrow_mut().write_at(0, buf.as_ref())
.expect("Failed to write image");
println!("{:?}", entry.path());
} else if type_.is_dir() {
let inode = inode.borrow_mut().create(name, FileType::Dir)
.expect("Failed to create INode");
zip_dir(entry.path().as_path(), inode);
}
}
}
fn unzip(path: &Path, img_path: &Path) {
}

@ -3,10 +3,9 @@
#![cfg_attr(feature = "ucore", feature(allocator_api, global_allocator, lang_items))] #![cfg_attr(feature = "ucore", feature(allocator_api, global_allocator, lang_items))]
#![no_std] #![no_std]
#[cfg(test)] #[cfg(any(test, feature = "std"))]
#[macro_use] #[macro_use]
extern crate std; extern crate std;
extern crate spin;
#[macro_use] #[macro_use]
extern crate alloc; extern crate alloc;
extern crate bit_set; extern crate bit_set;
@ -38,3 +37,28 @@ pub use vfs::*;
#[cfg(feature = "ucore")] #[cfg(feature = "ucore")]
#[global_allocator] #[global_allocator]
pub static UCORE_ALLOCATOR: c_interface::UcoreAllocator = c_interface::UcoreAllocator{}; pub static UCORE_ALLOCATOR: c_interface::UcoreAllocator = c_interface::UcoreAllocator{};
#[cfg(any(test, feature = "std"))]
pub mod std_impl {
use std::fs::{File, OpenOptions};
use std::io::{Read, Write, Seek, SeekFrom};
use super::Device;
impl Device for File {
fn read_at(&mut self, offset: usize, buf: &mut [u8]) -> Option<usize> {
let offset = offset as u64;
match self.seek(SeekFrom::Start(offset)) {
Ok(real_offset) if real_offset == offset => self.read(buf).ok(),
_ => None,
}
}
fn write_at(&mut self, offset: usize, buf: &[u8]) -> Option<usize> {
let offset = offset as u64;
match self.seek(SeekFrom::Start(offset)) {
Ok(real_offset) if real_offset == offset => self.write(buf).ok(),
_ => None,
}
}
}
}

@ -1,4 +1,3 @@
use spin::Mutex;
use bit_set::BitSet; use bit_set::BitSet;
use alloc::{boxed::Box, Vec, BTreeMap, rc::{Rc, Weak}, String}; use alloc::{boxed::Box, Vec, BTreeMap, rc::{Rc, Weak}, String};
use core::cell::{RefCell, RefMut}; use core::cell::{RefCell, RefMut};

@ -8,24 +8,6 @@ use std::rc::Rc;
use std::mem::uninitialized; use std::mem::uninitialized;
use super::structs::{DiskEntry, AsBuf}; use super::structs::{DiskEntry, AsBuf};
impl Device for File {
fn read_at(&mut self, offset: usize, buf: &mut [u8]) -> Option<usize> {
let offset = offset as u64;
match self.seek(SeekFrom::Start(offset)) {
Ok(real_offset) if real_offset == offset => self.read(buf).ok(),
_ => None,
}
}
fn write_at(&mut self, offset: usize, buf: &[u8]) -> Option<usize> {
let offset = offset as u64;
match self.seek(SeekFrom::Start(offset)) {
Ok(real_offset) if real_offset == offset => self.write(buf).ok(),
_ => None,
}
}
}
fn _open_sample_file() -> Rc<SimpleFileSystem> { fn _open_sample_file() -> Rc<SimpleFileSystem> {
let file = File::open("sfs.img") let file = File::open("sfs.img")
.expect("failed to open sfs.img"); .expect("failed to open sfs.img");

Loading…
Cancel
Save