added a macro
This commit is contained in:
parent
649263ff5f
commit
2ce96ae74c
1 changed files with 23 additions and 66 deletions
89
src/sftp.rs
89
src/sftp.rs
|
@ -6,6 +6,24 @@ use russh_sftp::{protocol::{Attrs, Data, File, FileAttributes, Handle as SftpHan
|
||||||
|
|
||||||
use tokio::{fs::{self, OpenOptions, ReadDir}, io::{AsyncReadExt, AsyncSeekExt, AsyncWriteExt}};
|
use tokio::{fs::{self, OpenOptions, ReadDir}, io::{AsyncReadExt, AsyncSeekExt, AsyncWriteExt}};
|
||||||
|
|
||||||
|
macro_rules! match_expr {
|
||||||
|
($match:expr, $err_msg:literal, $id:ident) => {
|
||||||
|
match $match {
|
||||||
|
Ok(()) => Ok(Status { $id, status_code: StatusCode::Ok, error_message: "Ok".to_string(), language_tag: "en-US".to_string() }),
|
||||||
|
Err(e) => {
|
||||||
|
println!($err_msg, e);
|
||||||
|
match e.kind() {
|
||||||
|
ErrorKind::NotFound => Ok(Status { $id, status_code: StatusCode::NoSuchFile, error_message: e.to_string(), language_tag: "en-US".to_string() }),
|
||||||
|
ErrorKind::PermissionDenied => Ok(Status { $id, status_code: StatusCode::PermissionDenied, error_message: e.to_string(), language_tag: "en-US".to_string() }),
|
||||||
|
ErrorKind::ConnectionReset => Ok(Status { $id, status_code: StatusCode::ConnectionLost, error_message: e.to_string(), language_tag: "en-US".to_string() }),
|
||||||
|
ErrorKind::NotConnected => Ok(Status { $id, status_code: StatusCode::NoConnection, error_message: e.to_string(), language_tag: "en-US".to_string() }),
|
||||||
|
_ => Ok(Status { $id, status_code: StatusCode::Failure, error_message: e.to_string(), language_tag: "en-US".to_string() })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
enum Handle {
|
enum Handle {
|
||||||
Dir(ReadDir),
|
Dir(ReadDir),
|
||||||
File(fs::File)
|
File(fs::File)
|
||||||
|
@ -206,10 +224,6 @@ impl SftpHandler for SftpSession {
|
||||||
longname: longname,
|
longname: longname,
|
||||||
attrs: FileAttributes {
|
attrs: FileAttributes {
|
||||||
size: Some(metadata.size()),
|
size: Some(metadata.size()),
|
||||||
// uid: Some(metadata.uid()),
|
|
||||||
// user: None,
|
|
||||||
// gid: Some(metadata.gid()),
|
|
||||||
// group: None,
|
|
||||||
permissions: Some(metadata.mode()),
|
permissions: Some(metadata.mode()),
|
||||||
atime: Some(metadata.atime() as u32),
|
atime: Some(metadata.atime() as u32),
|
||||||
mtime: Some(metadata.mtime() as u32),
|
mtime: Some(metadata.mtime() as u32),
|
||||||
|
@ -256,10 +270,6 @@ impl SftpHandler for SftpSession {
|
||||||
match fs::metadata(path).await {
|
match fs::metadata(path).await {
|
||||||
Ok(metadata) => Ok(Attrs { id, attrs: FileAttributes {
|
Ok(metadata) => Ok(Attrs { id, attrs: FileAttributes {
|
||||||
size: Some(metadata.size()),
|
size: Some(metadata.size()),
|
||||||
// uid: Some(metadata.uid()),
|
|
||||||
// user: None,
|
|
||||||
// gid: Some(metadata.gid()),
|
|
||||||
// group: None,
|
|
||||||
permissions: Some(metadata.mode()),
|
permissions: Some(metadata.mode()),
|
||||||
atime: Some(metadata.atime() as u32),
|
atime: Some(metadata.atime() as u32),
|
||||||
mtime: Some(metadata.mtime() as u32),
|
mtime: Some(metadata.mtime() as u32),
|
||||||
|
@ -279,10 +289,6 @@ impl SftpHandler for SftpSession {
|
||||||
match fs::symlink_metadata(path).await {
|
match fs::symlink_metadata(path).await {
|
||||||
Ok(metadata) => Ok(Attrs { id, attrs: FileAttributes {
|
Ok(metadata) => Ok(Attrs { id, attrs: FileAttributes {
|
||||||
size: Some(metadata.size()),
|
size: Some(metadata.size()),
|
||||||
// uid: Some(metadata.uid()),
|
|
||||||
// user: None,
|
|
||||||
// gid: Some(metadata.gid()),
|
|
||||||
// group: None,
|
|
||||||
permissions: Some(metadata.mode()),
|
permissions: Some(metadata.mode()),
|
||||||
atime: Some(metadata.atime() as u32),
|
atime: Some(metadata.atime() as u32),
|
||||||
mtime: Some(metadata.mtime() as u32),
|
mtime: Some(metadata.mtime() as u32),
|
||||||
|
@ -322,20 +328,8 @@ impl SftpHandler for SftpSession {
|
||||||
filename: String,
|
filename: String,
|
||||||
) -> Result<Status, Self::Error> {
|
) -> Result<Status, Self::Error> {
|
||||||
println!("remove called: {}", filename);
|
println!("remove called: {}", filename);
|
||||||
let filename = format!("{}{}", self.jail_dir, filename);
|
let path = format!("{}{}", self.jail_dir, filename);
|
||||||
match fs::remove_file(filename).await {
|
match_expr!(fs::remove_file(path).await, "error removing file: {}", id)
|
||||||
Ok(()) => Ok(Status { id, status_code: StatusCode::Ok, error_message: "Ok".to_string(), language_tag: "en-US".to_string() }),
|
|
||||||
Err(e) => {
|
|
||||||
println!("error removing file: {}", e);
|
|
||||||
match e.kind() {
|
|
||||||
ErrorKind::NotFound => Ok(Status { id, status_code: StatusCode::NoSuchFile, error_message: e.to_string(), language_tag: "en-US".to_string() }),
|
|
||||||
ErrorKind::PermissionDenied => Ok(Status { id, status_code: StatusCode::PermissionDenied, error_message: e.to_string(), language_tag: "en-US".to_string() }),
|
|
||||||
ErrorKind::ConnectionReset => Ok(Status { id, status_code: StatusCode::ConnectionLost, error_message: e.to_string(), language_tag: "en-US".to_string() }),
|
|
||||||
ErrorKind::NotConnected => Ok(Status { id, status_code: StatusCode::NoConnection, error_message: e.to_string(), language_tag: "en-US".to_string() }),
|
|
||||||
_ => Ok(Status { id, status_code: StatusCode::Failure, error_message: e.to_string(), language_tag: "en-US".to_string() })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn mkdir(
|
async fn mkdir(
|
||||||
|
@ -346,19 +340,7 @@ impl SftpHandler for SftpSession {
|
||||||
) -> Result<Status, Self::Error> {
|
) -> Result<Status, Self::Error> {
|
||||||
println!("mkdir called: {}", path);
|
println!("mkdir called: {}", path);
|
||||||
let path = format!("{}{}", self.jail_dir, path);
|
let path = format!("{}{}", self.jail_dir, path);
|
||||||
match fs::create_dir(&path).await {
|
match_expr!(fs::create_dir(path).await, "error creating dir: {}", id)
|
||||||
Ok(()) => Ok(Status { id, status_code: StatusCode::Ok, error_message: "Ok".to_string(), language_tag: "en-US".to_string() }),
|
|
||||||
Err(e) => {
|
|
||||||
println!("error removing file: {}", e);
|
|
||||||
match e.kind() {
|
|
||||||
ErrorKind::PermissionDenied => Ok(Status { id, status_code: StatusCode::PermissionDenied, error_message: e.to_string(), language_tag: "en-US".to_string() }),
|
|
||||||
ErrorKind::ConnectionReset => Ok(Status { id, status_code: StatusCode::ConnectionLost, error_message: e.to_string(), language_tag: "en-US".to_string() }),
|
|
||||||
ErrorKind::NotConnected => Ok(Status { id, status_code: StatusCode::NoConnection, error_message: e.to_string(), language_tag: "en-US".to_string() }),
|
|
||||||
_ => Ok(Status { id, status_code: StatusCode::Failure, error_message: e.to_string(), language_tag: "en-US".to_string() })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn rmdir(
|
async fn rmdir(
|
||||||
|
@ -368,19 +350,7 @@ impl SftpHandler for SftpSession {
|
||||||
) -> Result<Status, Self::Error> {
|
) -> Result<Status, Self::Error> {
|
||||||
println!("rmdir called: {}", path);
|
println!("rmdir called: {}", path);
|
||||||
let path = format!("{}{}", self.jail_dir, path);
|
let path = format!("{}{}", self.jail_dir, path);
|
||||||
match fs::remove_dir(path).await {
|
match_expr!(fs::remove_dir(path).await, "error removing file: {}", id)
|
||||||
Ok(()) => Ok(Status { id, status_code: StatusCode::Ok, error_message: "Ok".to_string(), language_tag: "en-US".to_string() }),
|
|
||||||
Err(e) => {
|
|
||||||
println!("error removing file: {}", e);
|
|
||||||
match e.kind() {
|
|
||||||
ErrorKind::NotFound => Ok(Status { id, status_code: StatusCode::NoSuchFile, error_message: e.to_string(), language_tag: "en-US".to_string() }),
|
|
||||||
ErrorKind::PermissionDenied => Ok(Status { id, status_code: StatusCode::PermissionDenied, error_message: e.to_string(), language_tag: "en-US".to_string() }),
|
|
||||||
ErrorKind::ConnectionReset => Ok(Status { id, status_code: StatusCode::ConnectionLost, error_message: e.to_string(), language_tag: "en-US".to_string() }),
|
|
||||||
ErrorKind::NotConnected => Ok(Status { id, status_code: StatusCode::NoConnection, error_message: e.to_string(), language_tag: "en-US".to_string() }),
|
|
||||||
_ => Ok(Status { id, status_code: StatusCode::Failure, error_message: e.to_string(), language_tag: "en-US".to_string() })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn rename(
|
async fn rename(
|
||||||
|
@ -392,20 +362,7 @@ impl SftpHandler for SftpSession {
|
||||||
println!("rename called from: {}, to: {}", oldpath, newpath);
|
println!("rename called from: {}, to: {}", oldpath, newpath);
|
||||||
let oldpath = format!("{}{}", self.jail_dir, oldpath);
|
let oldpath = format!("{}{}", self.jail_dir, oldpath);
|
||||||
let newpath = format!("{}{}", self.jail_dir, newpath);
|
let newpath = format!("{}{}", self.jail_dir, newpath);
|
||||||
|
match_expr!(fs::rename(oldpath, newpath).await, "error renaming file: {}", id)
|
||||||
match fs::rename(oldpath, newpath).await {
|
|
||||||
Ok(()) => Ok(Status { id, status_code: StatusCode::Ok, error_message: "Ok".to_string(), language_tag: "en-US".to_string() }),
|
|
||||||
Err(e) => {
|
|
||||||
println!("error removing file: {}", e);
|
|
||||||
match e.kind() {
|
|
||||||
ErrorKind::NotFound => Ok(Status { id, status_code: StatusCode::NoSuchFile, error_message: e.to_string(), language_tag: "en-US".to_string() }),
|
|
||||||
ErrorKind::PermissionDenied => Ok(Status { id, status_code: StatusCode::PermissionDenied, error_message: e.to_string(), language_tag: "en-US".to_string() }),
|
|
||||||
ErrorKind::ConnectionReset => Ok(Status { id, status_code: StatusCode::ConnectionLost, error_message: e.to_string(), language_tag: "en-US".to_string() }),
|
|
||||||
ErrorKind::NotConnected => Ok(Status { id, status_code: StatusCode::NoConnection, error_message: e.to_string(), language_tag: "en-US".to_string() }),
|
|
||||||
_ => Ok(Status { id, status_code: StatusCode::Failure, error_message: e.to_string(), language_tag: "en-US".to_string() })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue