From e3f11cf075173c661a199f2bae4ff14458a716b6 Mon Sep 17 00:00:00 2001 From: "qingwen.zhao" Date: Wed, 15 Nov 2023 21:40:03 +0800 Subject: [PATCH] add src check --- core/src/services/hdfs/backend.rs | 57 ++++++++++++++++++++----------- core/src/services/hdfs/docs.md | 4 +-- 2 files changed, 40 insertions(+), 21 deletions(-) diff --git a/core/src/services/hdfs/backend.rs b/core/src/services/hdfs/backend.rs index f242631c2e5..c0866d86672 100644 --- a/core/src/services/hdfs/backend.rs +++ b/core/src/services/hdfs/backend.rs @@ -157,26 +157,41 @@ unsafe impl Sync for HdfsBackend {} impl HdfsBackend { fn create_parent_if_need(&self, path: &str) -> Result<()> { - if let Err(err) = self.client.metadata(path) { - // Early return if other error happened. - if err.kind() != io::ErrorKind::NotFound { - return Err(new_std_io_error(err)); - } + let result = self.client.metadata(path); + match result { + Err(err) => { + // Early return if other error happened. + if err.kind() != io::ErrorKind::NotFound { + return Err(new_std_io_error(err)); + } - let parent = PathBuf::from(path) - .parent() - .ok_or_else(|| { - Error::new( - ErrorKind::Unexpected, - "path should have parent but not, it must be malformed", - ) - .with_context("input", path) - })? - .to_path_buf(); - - self.client - .create_dir(&parent.to_string_lossy()) - .map_err(new_std_io_error)?; + let parent = PathBuf::from(path) + .parent() + .ok_or_else(|| { + Error::new( + ErrorKind::Unexpected, + "path should have parent but not, it must be malformed", + ) + .with_context("input", path) + })? + .to_path_buf(); + + self.client + .create_dir(&parent.to_string_lossy()) + .map_err(new_std_io_error)?; + } + Ok(metadata) => { + if metadata.is_file() { + self.client + .remove_file(&path) + .map_err(new_std_io_error)?; + } else { + return Err(Error::new( + ErrorKind::IsADirectory, + "path should be a file") + .with_context("input", path)) + } + } } Ok(()) @@ -281,6 +296,8 @@ impl Accessor for HdfsBackend { async fn rename(&self, from: &str, to: &str, args: OpRename) -> Result { let from_path = build_rooted_abs_path(&self.root, from); + self.client.metadata(&from_path).map_err(new_std_io_error)?; + let to_path = build_rooted_abs_path(&self.root, to); self.create_parent_if_need(&to_path)?; @@ -410,6 +427,8 @@ impl Accessor for HdfsBackend { fn blocking_rename(&self, from: &str, to: &str, args: OpRename) -> Result { let from_path = build_rooted_abs_path(&self.root, from); + self.client.metadata(&from_path).map_err(new_std_io_error)?; + let to_path = build_rooted_abs_path(&self.root, to); self.create_parent_if_need(&to_path)?; diff --git a/core/src/services/hdfs/docs.md b/core/src/services/hdfs/docs.md index 99eeaeac280..666538df3a4 100644 --- a/core/src/services/hdfs/docs.md +++ b/core/src/services/hdfs/docs.md @@ -10,12 +10,12 @@ This service can be used to: - [x] create_dir - [x] delete - [ ] copy -- [ ] rename +- [x] rename - [x] list - [ ] ~~scan~~ - [ ] ~~presign~~ - [x] blocking -- [x] append +- [ ] append ## Differences with webhdfs