You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opendal.apache.org by xu...@apache.org on 2023/04/03 09:27:00 UTC

[incubator-opendal] branch main updated: fix(core): Add checks before doing copy (#1845)

This is an automated email from the ASF dual-hosted git repository.

xuanwo pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-opendal.git


The following commit(s) were added to refs/heads/main by this push:
     new 3dbccbba fix(core): Add checks before doing copy (#1845)
3dbccbba is described below

commit 3dbccbba21c8083e0ef346ea3e2cd69ed679fa5f
Author: Suyan <su...@gmail.com>
AuthorDate: Mon Apr 3 17:26:54 2023 +0800

    fix(core): Add checks before doing copy (#1845)
    
    fix(core): ensure source and target path before copy
    
    Signed-off-by: suyanhanx <su...@gmail.com>
---
 core/src/services/fs/backend.rs | 27 ++++++++++++++++-----------
 1 file changed, 16 insertions(+), 11 deletions(-)

diff --git a/core/src/services/fs/backend.rs b/core/src/services/fs/backend.rs
index fcc459b0..f21e0348 100644
--- a/core/src/services/fs/backend.rs
+++ b/core/src/services/fs/backend.rs
@@ -443,12 +443,14 @@ impl Accessor for FsBackend {
     }
 
     async fn copy(&self, from: &str, to: &str, _args: OpCopy) -> Result<RpCopy> {
-        tokio::fs::copy(
-            self.root.join(from.trim_end_matches('/')),
-            self.root.join(to.trim_end_matches('/')),
-        )
-        .await
-        .map_err(parse_io_error)?;
+        let from = self.root.join(from.trim_end_matches('/'));
+
+        // try to get the metadata of the source file to ensure it exists
+        tokio::fs::metadata(&from).await.map_err(parse_io_error)?;
+
+        let to = Self::ensure_write_abs_path(&self.root, to.trim_end_matches('/')).await?;
+
+        tokio::fs::copy(from, to).await.map_err(parse_io_error)?;
 
         Ok(RpCopy::default())
     }
@@ -640,11 +642,14 @@ impl Accessor for FsBackend {
     }
 
     fn blocking_copy(&self, from: &str, to: &str, _args: OpCopy) -> Result<RpCopy> {
-        std::fs::copy(
-            self.root.join(from.trim_end_matches('/')),
-            self.root.join(to.trim_end_matches('/')),
-        )
-        .map_err(parse_io_error)?;
+        let from = self.root.join(from.trim_end_matches('/'));
+
+        // try to get the metadata of the source file to ensure it exists
+        std::fs::metadata(&from).map_err(parse_io_error)?;
+
+        let to = Self::blocking_ensure_write_abs_path(&self.root, to.trim_end_matches('/'))?;
+
+        std::fs::copy(from, to).map_err(parse_io_error)?;
 
         Ok(RpCopy::default())
     }