diff options
author | Matthias Beyer <matthias.beyer@ifm.com> | 2022-09-11 11:19:09 +0200 |
---|---|---|
committer | Matthias Beyer <matthias.beyer@ifm.com> | 2022-09-11 11:19:23 +0200 |
commit | ff51394068b06a9834b0dda0874c56da981e94e7 (patch) | |
tree | 4825b4c52da0339043b0f9bd2a1c850a1750ee1e | |
parent | 88f5884318972d19226b273786c347cb0c33ca32 (diff) |
Add impl Handle<Rename> for FdManPlugin
Signed-off-by: Matthias Beyer <matthias.beyer@ifm.com>
-rw-r--r-- | plugins/plugin_fdman/src/plugin.rs | 40 |
1 files changed, 35 insertions, 5 deletions
diff --git a/plugins/plugin_fdman/src/plugin.rs b/plugins/plugin_fdman/src/plugin.rs index 4f97174f..2c10d173 100644 --- a/plugins/plugin_fdman/src/plugin.rs +++ b/plugins/plugin_fdman/src/plugin.rs @@ -10,7 +10,10 @@ use tedge_api::{ use crate::{ error::Error, guard::Guard, - message::{OpenOptions, OpenOptionsError, OpenOptionsResult, Copy, CopyError, CopyResult}, + message::{ + Copy, CopyError, CopyResult, OpenOptions, OpenOptionsError, OpenOptionsResult, Rename, + RenameError, RenameResult, + }, }; #[derive(Debug)] @@ -108,10 +111,37 @@ impl Handle<Copy> for FdManPlugin { .map_err(PluginError::from) } Err(err @ Error::InsufficientHandles { .. }) => sender - .reply(CopyResult::new( - message, - Err(CopyError::from(err)), - )) + .reply(CopyResult::new(message, Err(CopyError::from(err)))) + .map_err(|_| Error::SendingReply) + .map_err(PluginError::from), + Err(other) => Err(PluginError::from(other)), + } + } +} + +#[async_trait::async_trait] +impl Handle<Rename> for FdManPlugin { + async fn handle_message( + &self, + message: Rename, + sender: tedge_api::address::ReplySenderFor<Rename>, + ) -> Result<(), PluginError> { + let handles_to_aquire = 2; + match self.aquire_handles(handles_to_aquire) { + Ok(guard) => { + let rename_res = tokio::fs::rename(message.src(), message.dst()) + .await + .map_err(Error::from) + .map_err(RenameError::from); + drop(guard); // We can now drop the guard, as the copy process is done + let res = RenameResult::new(message, rename_res); + sender + .reply(res) + .map_err(|_| Error::SendingReply) + .map_err(PluginError::from) + } + Err(err @ Error::InsufficientHandles { .. }) => sender + .reply(RenameResult::new(message, Err(RenameError::from(err)))) .map_err(|_| Error::SendingReply) .map_err(PluginError::from), Err(other) => Err(PluginError::from(other)), |