Creating directories
We'll start by handling the command to create a new directory. So, we add a case in handle_cmd()
:
#[async] fn handle_cmd(mut self, cmd: Command) -> Result<Self> { match cmd { Command::Mkd(path) => self = await!(self.mkd(path))?, // … } }
And the function handling this command is:
use std::fs::create_dir; #[async] fn mkd(mut self, path: PathBuf) -> Result<Self> { let path = self.cwd.join(&path); let parent = get_parent(path.clone()); if let Some(parent) = parent { let parent = parent.to_path_buf(); let (new_self, res) = self.complete_path(parent); self = new_self; if let Ok(mut dir) = res {
We first check that the parent
directory is valid and under the server root:
if dir.is_dir() { let filename = get_filename(path); if let Some(filename) = filename { dir.push(filename); if create_dir(dir).is_ok(...