Uploading files
Now, let's do the opposite command: STOR
to upload a file on the server.
As always, we'll add a case in the handle_cmd()
method:
#[async] fn handle_cmd(mut self, cmd: Command) -> Result<Self> { match cmd { Command::Stor(file) => self = await!(self.stor(file))?, // … } }
Here is the start of the corresponding method:
use std::io::Write; #[async] fn stor(mut self, path: PathBuf) -> Result<Self> { if self.data_reader.is_some() { if invalid_path(&path) { let error: io::Error = io::ErrorKind::PermissionDenied.into(); return Err(error.into()); } let path = self.cwd.join(path); self = await!(self.send(Answer::new(ResultCode::DataConnectionAlreadyOpen, "Starting to send file...")))?;
Once again, we check that the data channel is opened. Then, we use a new function to check that the path is valid, by which we mean it does not contain ..
. In the other cases, we used another...