This is an automated email from the ASF dual-hosted git repository. Xuanwo pushed a commit to branch xuanwo/fix-hdfs-native-rename-target-safety in repository https://gitbox.apache.org/repos/asf/opendal.git
commit 2768190760bee417d15a438addb453d5830800ba Author: Xuanwo <[email protected]> AuthorDate: Mon Nov 23 00:25:30 2026 +0100 fix(services/hdfs-native): check source before touching rename target The previous implementation deleted an existing destination or created an empty destination file before calling HDFS rename. A rename from a missing source therefore destroyed the existing target or left a stray zero-byte file at the destination. Stat the source first and return NotFound without touching the target. Use HDFS rename overwrite semantics directly when the target exists, and create missing parent directories only after the source is known to be a file. --- core/services/hdfs-native/src/core.rs | 84 +++++++++++++++++++++-------------- 1 file changed, 50 insertions(+), 34 deletions(-) diff --git a/core/services/hdfs-native/src/core.rs b/core/services/hdfs-native/src/core.rs index 832b69b900..b9e87b47b2 100644 --- a/core/services/hdfs-native/src/core.rs +++ b/core/services/hdfs-native/src/core.rs @@ -170,6 +170,16 @@ impl HdfsNativeCore { let from_path = build_rooted_absolute_path(&self.root, from); let to_path = build_rooted_absolute_path(&self.root, to); + let source = self + .client + .get_file_info(&from_path) + .await + .map_err(parse_hdfs_error)?; + if source.isdir { + return Err(Error::new(ErrorKind::IsADirectory, "path should be a file") + .with_context("input", &from_path)); + } + let target_exists = match self.client.get_file_info(&to_path).await { Ok(status) => { if status.isdir { @@ -184,50 +194,56 @@ impl HdfsNativeCore { }, }; - if args.if_not_exists() { - if let Some((parent, _)) = to_path - .rsplit_once('/') - .filter(|(parent, _)| !target_exists && !parent.is_empty()) - { - self.client - .mkdirs(parent, 0o777, true) - .await - .map_err(parse_hdfs_error)?; + if target_exists { + if args.if_not_exists() { + return Err(Error::new( + ErrorKind::ConditionNotMatch, + "target path already exists while if_not_exists is set", + ) + .with_context("input", &to_path)); } + return self .client - .rename(&from_path, &to_path, false) + .rename(&from_path, &to_path, true) .await - .map_err(|err| { - if matches!(&err, HdfsError::AlreadyExists(_)) { - Error::new( - ErrorKind::ConditionNotMatch, - "target path already exists while if_not_exists is set", - ) - .with_context("input", &to_path) - .set_source(err) - } else { - parse_hdfs_error(err) - } - }); + .map_err(parse_hdfs_error); } - if target_exists { - self.client - .delete(&to_path, true) - .await - .map_err(parse_hdfs_error)?; - } else { - self.client - .create(&to_path, WriteOptions::default().create_parent(true)) - .await - .map_err(parse_hdfs_error)?; + if let Some((parent, _)) = to_path + .rsplit_once('/') + .filter(|(parent, _)| !parent.is_empty()) + { + let parent_exists = match self.client.get_file_info(parent).await { + Ok(_) => true, + Err(err) => match &err { + HdfsError::FileNotFound(_) => false, + _ => return Err(parse_hdfs_error(err)), + }, + }; + if !parent_exists { + self.client + .mkdirs(parent, 0o777, true) + .await + .map_err(parse_hdfs_error)?; + } } self.client - .rename(&from_path, &to_path, true) + .rename(&from_path, &to_path, !args.if_not_exists()) .await - .map_err(parse_hdfs_error)?; + .map_err(|err| { + if args.if_not_exists() && matches!(&err, HdfsError::AlreadyExists(_)) { + Error::new( + ErrorKind::ConditionNotMatch, + "target path already exists while if_not_exists is set", + ) + .with_context("input", &to_path) + .set_source(err) + } else { + parse_hdfs_error(err) + } + })?; Ok(()) }
