From 2d578215e365b61c25dcb61ecdd26bc062eb7f17 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Tue, 1 Mar 2016 19:26:40 -0700 Subject: [PATCH] rmdir, mkdir --- scheme/main.rs | 54 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 45 insertions(+), 9 deletions(-) diff --git a/scheme/main.rs b/scheme/main.rs index ac73f35d55..513a260ab0 100644 --- a/scheme/main.rs +++ b/scheme/main.rs @@ -13,7 +13,7 @@ use image::Image; use redoxfs::{FileSystem, Node}; -use system::error::{Error, Result, EISDIR, EPERM, ENOENT, EBADF, EINVAL}; +use system::error::{Error, Result, EEXIST, EISDIR, ENOTDIR, EPERM, ENOENT, EBADF, EINVAL}; use system::scheme::{Packet, Scheme}; use system::syscall::{Stat, O_CREAT, SEEK_SET, SEEK_CUR, SEEK_END}; @@ -189,17 +189,53 @@ impl Scheme for FileScheme { Ok(id) } - fn mkdir(&mut self, path: &str, mode: usize) -> Result { - println!("mkdir {}, {:X}", path, mode); - Err(Error::new(ENOENT)) + fn mkdir(&mut self, url: &str, mode: usize) -> Result { + let path = url.split(':').nth(1).unwrap_or("").trim_matches('/'); + + let mut nodes = Vec::new(); + match self.path_nodes(path, &mut nodes) { + Ok(_node) => Err(Error::new(EEXIST)), + Err(err) => if err.errno == ENOENT { + let mut last_part = String::new(); + for part in path.split('/') { + if ! part.is_empty() { + last_part = part.to_string(); + } + } + if ! last_part.is_empty() { + if let Some(parent) = nodes.last() { + self.fs.create_node(Node::MODE_DIR, &last_part, parent.0).and(Ok(0)) + } else { + Err(Error::new(EPERM)) + } + } else { + Err(Error::new(EPERM)) + } + } else { + Err(err) + } + } } - /* - fn rmdir(&mut self, path: &str) -> Result { - println!("rmdir {}", path); - Err(Error::new(ENOENT)) + fn rmdir(&mut self, url: &str) -> Result { + let path = url.split(':').nth(1).unwrap_or("").trim_matches('/'); + + let mut nodes = Vec::new(); + let child = try!(self.path_nodes(path, &mut nodes)); + if let Some(parent) = nodes.last() { + if child.1.is_dir() { + if let Ok(child_name) = child.1.name() { + self.fs.remove_node(Node::MODE_DIR, child_name, parent.0).and(Ok(0)) + } else { + Err(Error::new(ENOENT)) + } + } else { + Err(Error::new(ENOTDIR)) + } + } else { + Err(Error::new(EPERM)) + } } - */ fn unlink(&mut self, url: &str) -> Result { let path = url.split(':').nth(1).unwrap_or("").trim_matches('/');