Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(services/sftp): support copy and rename for sftp #2263

Merged
merged 5 commits into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,7 @@ services-s3 = [
"reqsign?/services-aws",
"reqsign?/reqwest_request",
]
services-sftp = [
"dep:openssh",
"dep:openssh-sftp-client",
"dep:dirs",
"futures/executor",
]
services-sftp = ["dep:openssh", "dep:openssh-sftp-client", "dep:dirs"]
services-sled = ["dep:sled"]
services-supabase = []
services-vercel-artifacts = []
Expand Down
52 changes: 42 additions & 10 deletions core/src/services/sftp/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ use crate::*;
/// - [x] write
/// - [x] create_dir
/// - [x] delete
/// - [ ] copy
/// - [ ] rename
/// - [ ] copy (partially, only supoort when remote server has copy-file extension)
silver-ymz marked this conversation as resolved.
Show resolved Hide resolved
/// - [x] rename (partially, only support to rename files on the same mount point)
silver-ymz marked this conversation as resolved.
Show resolved Hide resolved
/// - [x] list
/// - [ ] ~~scan~~
/// - [ ] ~~presign~~
Expand Down Expand Up @@ -276,15 +276,20 @@ impl Accessor for SftpBackend {
stat: true,

read: true,
read_can_seek: true,
read_with_range: true,

write: true,
write_without_content_length: true,
create_dir: true,
delete: true,

list: true,
list_with_limit: true,
list_with_delimiter_slash: true,

rename: true,

..Default::default()
});

Expand Down Expand Up @@ -352,14 +357,7 @@ impl Accessor for SftpBackend {
Ok((RpRead::new(end - start), r))
}

async fn write(&self, path: &str, args: OpWrite) -> Result<(RpWrite, Self::Writer)> {
if args.content_length().is_none() {
return Err(Error::new(
ErrorKind::Unsupported,
"write without content length is not supported",
));
}

async fn write(&self, path: &str, _args: OpWrite) -> Result<(RpWrite, Self::Writer)> {
if let Some((dir, _)) = path.rsplit_once('/') {
self.create_dir(dir, OpCreateDir::default()).await?;
}
Expand All @@ -375,6 +373,40 @@ impl Accessor for SftpBackend {
Ok((RpWrite::new(), SftpWriter::new(file)))
}

async fn copy(&self, from: &str, to: &str, _args: OpCopy) -> Result<RpCopy> {
silver-ymz marked this conversation as resolved.
Show resolved Hide resolved
let client = self.connect().await?;

let mut fs = client.fs();
fs.set_cwd(&self.root);

if let Some((dir, _)) = to.rsplit_once('/') {
self.create_dir(dir, OpCreateDir::default()).await?;
}

let src = fs.canonicalize(from).await?;
let dst = fs.canonicalize(to).await?;
let mut src_file = client.open(&src).await?;
let mut dst_file = client.create(dst).await?;

src_file.copy_all_to(&mut dst_file).await?;

Ok(RpCopy::default())
}

async fn rename(&self, from: &str, to: &str, _args: OpRename) -> Result<RpRename> {
let client = self.connect().await?;

let mut fs = client.fs();
fs.set_cwd(&self.root);

if let Some((dir, _)) = to.rsplit_once('/') {
self.create_dir(dir, OpCreateDir::default()).await?;
}
fs.rename(from, to).await?;

Ok(RpRename::default())
}

async fn stat(&self, path: &str, _: OpStat) -> Result<RpStat> {
let client = self.connect().await?;
let mut fs = client.fs();
Expand Down