Add debugging to scheme, add padding to header

This commit is contained in:
Jeremy Soller
2016-04-12 14:07:49 -06:00
parent 9cf1d60253
commit bef665af8d
9 changed files with 91 additions and 49 deletions
+2
View File
@@ -30,12 +30,14 @@ impl Image {
impl Disk for Image {
fn read_at(&mut self, block: u64, buffer: &mut [u8]) -> Result<usize> {
//println!("Image read at {}", block);
try_disk!(self.file.seek(SeekFrom::Start(block * 512)));
let count = try_disk!(self.file.read(buffer));
Ok(count)
}
fn write_at(&mut self, block: u64, buffer: &[u8]) -> Result<usize> {
//println!("Image write at {}", block);
try_disk!(self.file.seek(SeekFrom::Start(block * 512)));
let count = try_disk!(self.file.write(buffer));
Ok(count)
+47 -22
View File
@@ -6,6 +6,8 @@ use std::env;
use std::fs::File;
use std::io::{Read, Write};
use std::mem::size_of;
use std::sync::{Arc, Mutex};
use std::thread;
use image::Image;
use scheme::FileScheme;
@@ -18,32 +20,55 @@ pub mod image;
pub mod resource;
pub mod scheme;
fn scheme(fs: FileSystem) {
//In order to handle example:, we create :example
let mut scheme = FileScheme::new(fs);
let mut socket = File::create(":redoxfs").unwrap();
loop {
let mut packet = Packet::default();
while socket.read(&mut packet).unwrap() == size_of::<Packet>() {
scheme.handle(&mut packet);
socket.write(&packet).unwrap();
}
}
enum Status {
Starting,
Running,
Stopping
}
fn main() {
let mut args = env::args();
if let Some(path) = args.nth(1) {
//Open an existing image
match Image::open(&path) {
Ok(disk) => match FileSystem::open(Box::new(disk)) {
Ok(filesystem) => {
println!("redoxfs: opened filesystem {}", path);
scheme(filesystem);
if let Some(path) = env::args().nth(1) {
let status_mutex = Arc::new(Mutex::new(Status::Starting));
let status_daemon = status_mutex.clone();
thread::spawn(move || {
match Image::open(&path) {
Ok(disk) => match FileSystem::open(Box::new(disk)) {
Ok(fs) => match File::create(":file") {
Ok(mut socket) => {
println!("redoxfs: mounted filesystem {} on file:", path);
*status_daemon.lock().unwrap() = Status::Running;
let mut scheme = FileScheme::new(fs);
loop {
let mut packet = Packet::default();
while socket.read(&mut packet).unwrap() == size_of::<Packet>() {
println!("Read {:?}", packet);
scheme.handle(&mut packet);
println!("Write {:?}", packet);
socket.write(&packet).unwrap();
}
}
},
Err(err) => println!("redoxfs: failed to create file scheme: {}", err)
},
Err(err) => println!("redoxfs: failed to open filesystem {}: {}", path, err)
},
Err(err) => println!("redoxfs: failed to open filesystem {}: {}", path, err)
},
Err(err) => println!("redoxfs: failed to open image {}: {}", path, err)
Err(err) => println!("redoxfs: failed to open image {}: {}", path, err)
}
*status_daemon.lock().unwrap() = Status::Stopping;
});
'waiting: loop {
match *status_mutex.lock().unwrap() {
Status::Starting => (),
Status::Running => break 'waiting,
Status::Stopping => break 'waiting,
}
thread::sleep_ms(30);
}
} else {
println!("redoxfs: no disk image provided");
+17 -1
View File
@@ -28,6 +28,8 @@ impl Scheme for FileScheme {
fn open(&mut self, url: &str, flags: usize, _mode: usize) -> Result<usize> {
let path = url.split(':').nth(1).unwrap_or("").trim_matches('/');
println!("Open '{}' {:X}", path, flags);
let mut nodes = Vec::new();
let node_result = self.fs.path_nodes(path, &mut nodes);
@@ -97,6 +99,8 @@ impl Scheme for FileScheme {
fn mkdir(&mut self, url: &str, _mode: usize) -> Result<usize> {
let path = url.split(':').nth(1).unwrap_or("").trim_matches('/');
println!("Mkdir '{}'", path);
let mut nodes = Vec::new();
match self.fs.path_nodes(path, &mut nodes) {
Ok(_node) => Err(Error::new(EEXIST)),
@@ -125,6 +129,8 @@ impl Scheme for FileScheme {
fn rmdir(&mut self, url: &str) -> Result<usize> {
let path = url.split(':').nth(1).unwrap_or("").trim_matches('/');
println!("Rmdir '{}'", path);
let mut nodes = Vec::new();
let child = try!(self.fs.path_nodes(path, &mut nodes));
if let Some(parent) = nodes.last() {
@@ -145,6 +151,8 @@ impl Scheme for FileScheme {
fn unlink(&mut self, url: &str) -> Result<usize> {
let path = url.split(':').nth(1).unwrap_or("").trim_matches('/');
println!("Unlink '{}'", path);
let mut nodes = Vec::new();
let child = try!(self.fs.path_nodes(path, &mut nodes));
if let Some(parent) = nodes.last() {
@@ -165,6 +173,8 @@ impl Scheme for FileScheme {
/* Resource operations */
#[allow(unused_variables)]
fn read(&mut self, id: usize, buf: &mut [u8]) -> Result<usize> {
println!("Read {}, {:X} {}", id, buf.as_ptr() as usize, buf.len());
if let Some(mut file) = self.files.get_mut(&id) {
file.read(buf)
} else {
@@ -173,6 +183,7 @@ impl Scheme for FileScheme {
}
fn write(&mut self, id: usize, buf: &[u8]) -> Result<usize> {
println!("Write {}, {:X} {}", id, buf.as_ptr() as usize, buf.len());
if let Some(mut file) = self.files.get_mut(&id) {
file.write(buf)
} else {
@@ -181,6 +192,7 @@ impl Scheme for FileScheme {
}
fn seek(&mut self, id: usize, pos: usize, whence: usize) -> Result<usize> {
println!("Seek {}, {} {}", id, pos, whence);
if let Some(mut file) = self.files.get_mut(&id) {
file.seek(pos, whence)
} else {
@@ -189,6 +201,7 @@ impl Scheme for FileScheme {
}
fn fpath(&self, id: usize, buf: &mut [u8]) -> Result<usize> {
println!("Fpath {}, {:X} {}", id, buf.as_ptr() as usize, buf.len());
if let Some(file) = self.files.get(&id) {
file.path(buf)
} else {
@@ -197,7 +210,7 @@ impl Scheme for FileScheme {
}
fn fstat(&self, id: usize, stat: &mut Stat) -> Result<usize> {
println!("fstat {}, {:X}", id, stat as *mut Stat as usize);
println!("Fstat {}, {:X}", id, stat as *mut Stat as usize);
if let Some(file) = self.files.get(&id) {
file.stat(stat)
} else {
@@ -206,6 +219,7 @@ impl Scheme for FileScheme {
}
fn fsync(&mut self, id: usize) -> Result<usize> {
println!("Fsync {}", id);
if let Some(mut file) = self.files.get_mut(&id) {
file.sync()
} else {
@@ -214,6 +228,7 @@ impl Scheme for FileScheme {
}
fn ftruncate(&mut self, id: usize, len: usize) -> Result<usize> {
println!("Ftruncate {}, {}", id, len);
if let Some(mut file) = self.files.get_mut(&id) {
file.truncate(len)
} else {
@@ -222,6 +237,7 @@ impl Scheme for FileScheme {
}
fn close(&mut self, id: usize) -> Result<usize> {
println!("Close {}", id);
if self.files.remove(&id).is_some() {
Ok(0)
} else {
+1 -3
View File
@@ -1,6 +1,4 @@
use collections::Vec;
use core::{fmt, mem, ops, slice};
use std::{fmt, mem, ops, slice};
use super::Extent;
+1 -1
View File
@@ -1,4 +1,4 @@
use core::cmp::min;
use std::cmp::min;
pub struct BlockIter {
block: u64,
+1 -5
View File
@@ -1,7 +1,3 @@
use alloc::boxed::Box;
use collections::Vec;
use system::error::{Result, Error, EEXIST, EISDIR, ENOENT, ENOSPC, ENOTDIR, ENOTEMPTY};
use super::{Disk, ExNode, Extent, Header, Node};
@@ -145,7 +141,7 @@ impl FileSystem {
let mut block = self.header.1.root;
nodes.push(try!(self.node(block)));
for part in path.split('/') {
for part in path.split(':') {
if ! part.is_empty() {
let node = try!(self.find_node(part, block));
block = node.0;
+21 -5
View File
@@ -1,11 +1,10 @@
use core::{mem, slice};
use core::ops::{Deref, DerefMut};
use std::{fmt, mem, slice};
use std::ops::{Deref, DerefMut};
/// The header of the filesystem
#[derive(Debug)]
#[repr(packed)]
pub struct Header {
/// Signature, should be b"REDOXFS\0"
/// Signature, should be b"RedoxFS\0"
pub signature: [u8; 8],
/// Version, should be 1
pub version: u64,
@@ -17,6 +16,8 @@ pub struct Header {
pub root: u64,
/// Block of free space node
pub free: u64,
/// Padding
pub padding: [u8; 456]
}
impl Header {
@@ -31,6 +32,7 @@ impl Header {
size: 0,
root: 0,
free: 0,
padding: [0; 456]
}
}
@@ -42,6 +44,7 @@ impl Header {
size: size,
root: root,
free: free,
padding: [0; 456]
}
}
@@ -50,6 +53,19 @@ impl Header {
}
}
impl fmt::Debug for Header {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Header")
.field("signature", &self.signature)
.field("version", &self.version)
.field("uuid", &self.uuid)
.field("size", &self.size)
.field("root", &self.root)
.field("free", &self.free)
.finish()
}
}
impl Deref for Header {
type Target = [u8];
fn deref(&self) -> &[u8] {
@@ -69,5 +85,5 @@ impl DerefMut for Header {
#[test]
fn header_size_test(){
assert!(mem::size_of::<Header>() <= 512);
assert_eq!(mem::size_of::<Header>(), 512);
}
-9
View File
@@ -1,18 +1,9 @@
#![crate_name="redoxfs"]
#![crate_type="lib"]
#![feature(alloc)]
#![feature(associated_consts)]
#![feature(collections)]
#![no_std]
#![deny(warnings)]
#[macro_use]
extern crate alloc;
#[macro_use]
extern crate collections;
extern crate system;
pub use self::disk::Disk;
+1 -3
View File
@@ -1,6 +1,4 @@
use collections::Vec;
use core::{fmt, mem, ops, slice, str};
use std::{fmt, mem, ops, slice, str};
use super::Extent;