Skip to content
Snippets Groups Projects
Commit 81d2aa7b authored by the ssd's avatar the ssd
Browse files

added other function

parent b0f6cf9c
No related branches found
No related tags found
No related merge requests found
......@@ -18,7 +18,7 @@ enum FilesystemAction {
path: String,
contents: String,
},
/*WriteFile {
WriteFile {
index: u32,
contents: String,
},
......@@ -27,7 +27,7 @@ enum FilesystemAction {
},
DeleteFile {
index: u32
},*/
},
//read/delete/rename file that doesn't exist
......@@ -43,7 +43,19 @@ impl FilesystemAction {
match self {
FilesystemAction::CreateFile { path, contents } => {
if !path.is_ascii() || path.contains(| ch: char | !ch.is_alphanumeric() ) { // || path.contains(&['\0', '\n', '\t'])
return Ok(None);
}
if contents.contains('\0') {
return Ok(None);
}
let path = Path::new(path);
if path.file_name().is_none() {
return Ok(None);
}
paths.push(path);
match helper::create_inner(path.as_ref(), Node::MODE_FILE, &metadata, tx) {
......@@ -66,18 +78,30 @@ impl FilesystemAction {
};
},
/*FilesystemAction::WriteFile { index, contents } => {
FilesystemAction::WriteFile { index, contents } => {
if paths.len() == 0 {
return Ok(None);
}
if contents.contains('\0') {
return Ok(None);
}
let path = paths[*index as usize % paths.len()];
let name = path.file_name().ok_or(syscall::Error::new(-1))?.to_str().unwrap();
let node = tx.find_node(TreePtr::root(), name)?;
let data = contents.as_bytes();
let name = match path.file_name() {
Some(s) => s,
None => panic!("Path is wrong"),
}.to_str().unwrap();
let mut children = vec![];
tx.child_nodes(TreePtr::root(), &mut children)?;
let child = children.into_iter().find(| x | x.name().unwrap() == name).unwrap();
let data = contents.as_bytes();
let count = tx
.write_node(
node.ptr(),
child.node_ptr(),
0,
&data,
metadata.mtime,
......@@ -87,32 +111,50 @@ impl FilesystemAction {
if count != data.len() {
panic!("file write count {} != {}", count, data.len());
}
},
FilesystemAction::DeleteFile { index } => {
/*FilesystemAction::DeleteFile { index } => {
if paths.len() == 0 {
return Ok(None);
}
let path = paths[*index as usize % paths.len()];
let name = path.file_name().ok_or(syscall::Error::new(-1))?.to_str().unwrap();
let node = tx.find_node(TreePtr::root(), name)?;
tx.remove_node(node.ptr(), name, Node::MODE_FILE)?;
}
let name = match path.file_name() {
Some(s) => s,
None => panic!("Path is wrong"),
}.to_str().unwrap();
let mut children = vec![];
tx.child_nodes(TreePtr::root(), &mut children)?;
let child = children.into_iter().find(| x | x.name().unwrap() == name).unwrap();
tx.remove_node(child.node_ptr(), child.name().unwrap(), Node::MODE_FILE)?;
paths.remove(*index as usize % paths.len());
},*/
FilesystemAction::ReadFile { index } => {
if paths.len() == 0 {
return Ok(None);
}
let path = paths[*index as usize % paths.len()];
let name = path.file_name().ok_or(syscall::Error::new(-1))?.to_str().unwrap();
let node = tx.find_node(TreePtr::root(), name)?;
let name = match path.file_name() {
Some(s) => s,
None => panic!("Path is wrong"),
}.to_str().unwrap();
let mut children = vec![];
tx.child_nodes(TreePtr::root(), &mut children)?;
let child = children.into_iter().find(| x | x.name().unwrap() == name).unwrap();
let mut buffer: [u8; 1024] = [0; 1024];
tx.read_node(node.ptr(), 0, &mut buffer, 0, 0)?;
tx.read_node(child.node_ptr(), 0, &mut buffer, 0, 0)?;
let string = match String::from_utf8(buffer.to_vec()) {
/*let string = match String::from_utf8(buffer.to_vec()) {
Ok(s) => s,
Err(_) => { return Ok(Some(io::Error::from(ErrorKind::Other))) },
};
Err(e) => panic!("Contents are wrong {e:?}"),
};*/
// TODO: Validate
},*/
},
_ => ()
}
......@@ -121,7 +163,7 @@ impl FilesystemAction {
let end_block = tx.header.size() / BLOCK_SIZE;
tx.header.size = (end_block * BLOCK_SIZE).into();
tx.header_changed = true;*/
tx.sync(false)?;
tx.sync(true)?;
Ok(None)
}).map_err(|err| io::Error::from_raw_os_error(err.errno))?;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment