-
Notifications
You must be signed in to change notification settings - Fork 35
/
rnto.rs
70 lines (65 loc) · 2.28 KB
/
rnto.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//! The RFC 959 Rename To (`RNTO`) command
use crate::server::ControlChanMsg;
use crate::storage::{Metadata, StorageBackend};
use crate::{
auth::UserDetail,
server::controlchan::{
error::ControlChanError,
handler::{CommandContext, CommandHandler},
Reply, ReplyCode,
},
};
use async_trait::async_trait;
use std::{path::PathBuf, sync::Arc};
#[derive(Debug)]
pub struct Rnto {
path: PathBuf,
}
impl Rnto {
pub fn new(path: PathBuf) -> Self {
Rnto { path }
}
}
#[async_trait]
impl<Storage, User> CommandHandler<Storage, User> for Rnto
where
User: UserDetail + 'static,
Storage: StorageBackend<User> + 'static,
Storage::Metadata: Metadata,
{
#[tracing_attributes::instrument]
async fn handle(&self, args: CommandContext<Storage, User>) -> Result<Reply, ControlChanError> {
let CommandContext {
logger,
session,
tx_control_chan,
..
} = args;
let mut session = session.lock().await;
let storage = Arc::clone(&session.storage);
let (from, to) = match session.rename_from.take() {
Some(from) => {
let to = session.cwd.join(self.path.clone());
(from, to)
}
None => return Ok(Reply::new(ReplyCode::TransientFileError, "Please tell me what file you want to rename first")),
};
let user = (*session.user).as_ref().unwrap();
let old_path = from.to_string_lossy().to_string();
let new_path = to.to_string_lossy().to_string();
match storage.rename(user, &from, &to).await {
Ok(_) => {
slog::info!(logger, "RNTO: Successfully renamed {:?} to {:?}", from, to);
if let Err(err) = tx_control_chan.send(ControlChanMsg::RenameSuccess { old_path, new_path }).await {
slog::warn!(logger, "RNTO: Could not send internal message to notify of RNTO success: {}", err);
}
}
Err(err) => {
if let Err(err) = tx_control_chan.send(ControlChanMsg::StorageError(err)).await {
slog::warn!(logger, "RNTO: Could not send internal message to notify of RNTO failure: {}", err);
}
}
}
Ok(Reply::none())
}
}