-
Notifications
You must be signed in to change notification settings - Fork 35
/
md5.rs
89 lines (84 loc) · 3.24 KB
/
md5.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
use crate::{
auth::UserDetail,
server::{
chancomms::ControlChanMsg,
controlchan::{
error::ControlChanError,
handler::{CommandContext, CommandHandler},
Reply, ReplyCode,
},
ftpserver::options::SiteMd5,
},
storage::{StorageBackend, FEATURE_SITEMD5},
};
use async_trait::async_trait;
use std::{path::PathBuf, sync::Arc};
use tokio::sync::mpsc::Sender;
#[derive(Debug)]
pub struct Md5 {
path: PathBuf,
}
impl Md5 {
pub fn new(path: PathBuf) -> Self {
Md5 { path }
}
}
#[async_trait]
impl<Storage, User> CommandHandler<Storage, User> for Md5
where
User: UserDetail,
Storage: StorageBackend<User> + 'static,
{
#[tracing_attributes::instrument]
async fn handle(&self, args: CommandContext<Storage, User>) -> Result<Reply, ControlChanError> {
let session = args.session.lock().await;
let user = session.user.clone();
let storage = Arc::clone(&session.storage);
let path = session.cwd.join(self.path.clone());
let tx_success: Sender<ControlChanMsg> = args.tx_control_chan.clone();
let tx_fail: Sender<ControlChanMsg> = args.tx_control_chan.clone();
let logger = args.logger;
match args.sitemd5 {
SiteMd5::All => {}
SiteMd5::Accounts => match &session.username {
Some(u) => {
if u == "anonymous" || u == "ftp" {
return Ok(Reply::new(ReplyCode::CommandNotImplemented, "Command is not available."));
}
}
None => {
slog::error!(logger, "NoneError for username. This shouldn't happen.");
return Ok(Reply::new(ReplyCode::NotLoggedIn, "Please open a new connection to re-authenticate"));
}
},
SiteMd5::None => {
return Ok(Reply::new(ReplyCode::CommandNotImplemented, "Command is not available."));
}
}
if args.storage_features & FEATURE_SITEMD5 == 0 {
return Ok(Reply::new(ReplyCode::CommandNotImplemented, "Not supported by the selected storage back-end."));
}
tokio::spawn(async move {
match storage.md5((*user).as_ref().unwrap(), &path).await {
Ok(md5) => {
if let Err(err) = tx_success
.send(ControlChanMsg::CommandChannelReply(Reply::new_with_string(
ReplyCode::FileStatus,
format!("{} {}", md5, path.as_path().display()),
)))
.await
{
slog::warn!(logger, "MD5: Could not send internal message to notify of MD5 success: {}", err);
}
}
Err(err) => {
slog::warn!(logger, "MD5: Failed to retrieve MD5 sum for {:?} from backend: {}", path, err);
if let Err(err) = tx_fail.send(ControlChanMsg::StorageError(err)).await {
slog::warn!(logger, "MD5: Could not send internal message to notify of MD5 failure: {}", err);
}
}
}
});
Ok(Reply::none())
}
}