You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opendal.apache.org by "suyanhanx (via GitHub)" <gi...@apache.org> on 2023/05/15 05:11:46 UTC

[GitHub] [incubator-opendal] suyanhanx commented on a diff in pull request #2263: feat(services/sftp): support copy and rename for sftp

suyanhanx commented on code in PR #2263:
URL: https://github.com/apache/incubator-opendal/pull/2263#discussion_r1193331688


##########
core/src/services/sftp/backend.rs:
##########
@@ -375,6 +375,49 @@ impl Accessor for SftpBackend {
         Ok((RpWrite::new(), SftpWriter::new(file)))
     }
 
+    async fn copy(&self, from: &str, to: &str, _args: OpCopy) -> Result<RpCopy> {
+        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?;
+
+        // if remote sftp server supports copy extension, use it
+        if let Ok(()) = src_file.copy_all_to(&mut dst_file).await {
+            return Ok(RpCopy::default());
+        }
+
+        src_file.close().await?;
+        let buffer = fs.read(&src).await?;
+        dst_file.write_all(&buffer).await?;
+
+        Ok(RpCopy::default())

Review Comment:
   We prefer to implement it only when the backend is natively supported.
   
   You could return an error so users can handle it themselves.



##########
core/src/services/sftp/backend.rs:
##########
@@ -375,6 +375,49 @@ impl Accessor for SftpBackend {
         Ok((RpWrite::new(), SftpWriter::new(file)))
     }
 
+    async fn copy(&self, from: &str, to: &str, _args: OpCopy) -> Result<RpCopy> {
+        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?;
+
+        // if remote sftp server supports copy extension, use it
+        if let Ok(()) = src_file.copy_all_to(&mut dst_file).await {
+            return Ok(RpCopy::default());
+        }
+
+        src_file.close().await?;
+        let buffer = fs.read(&src).await?;
+        dst_file.write_all(&buffer).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 is_same_parent(from, to) {
+            fs.rename(from, to).await?;
+        } else {
+            self.copy(from, to, OpCopy::default()).await?;
+            fs.remove_file(from).await?;
+        }

Review Comment:
   ditto. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@opendal.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org