Removing directories
Finally, let's see the code to remove directories:
#[async] fn handle_cmd(mut self, cmd: Command) -> Result<Self> { match cmd { Command::Rmd(path) => self = await!(self.rmd(path))?, // … } }
Like for the previous commands, we add a new case that calls the method that will handle it:
use std::fs::remove_dir_all; #[async] fn rmd(mut self, directory: PathBuf) -> Result<Self> { let path = self.cwd.join(&directory); let (new_self, res) = self.complete_path(path); self = new_self; if let Ok(dir) = res { if remove_dir_all(dir).is_ok() { self = await!(self.send(Answer::new(ResultCode::RequestedFileActionOkay, "Folder successfully removed")))?; return Ok(self); } } self = await!(self.send(Answer::new(ResultCode::FileNotFound, "Couldn't remove folder")))?; Ok(self) }
Here again...