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/opendal.git
The following commit(s) were added to refs/heads/main by this push:
new 5893ca211b fix(services/monoiofs,compfs): align delete path and
symlink semantics with fs (#8842)
5893ca211b is described below
commit 5893ca211bcdb1f1d0895f098c6c9899e283a689
Author: Xuanwo <[email protected]>
AuthorDate: Mon Nov 23 18:08:18 2026 +0800
fix(services/monoiofs,compfs): align delete path and symlink semantics with
fs (#8842)
* fix(services/monoiofs,compfs): align delete path and symlink semantics
with fs
* fix(services/compfs): use FILE_ATTRIBUTE_DIRECTORY for Windows symlink
dir deletes
---
core/Cargo.lock | 1 +
core/services/compfs/src/deleter.rs | 265 +++++++++++++++++++++++++++++++++-
core/services/monoiofs/Cargo.toml | 1 +
core/services/monoiofs/src/deleter.rs | 171 +++++++++++++++++++---
4 files changed, 409 insertions(+), 29 deletions(-)
diff --git a/core/Cargo.lock b/core/Cargo.lock
index 7b4e952924..85896ee7f5 100644
--- a/core/Cargo.lock
+++ b/core/Cargo.lock
@@ -8508,6 +8508,7 @@ dependencies = [
"monoio",
"opendal-core",
"serde",
+ "tempfile",
"tokio",
]
diff --git a/core/services/compfs/src/deleter.rs
b/core/services/compfs/src/deleter.rs
index 90c0c716d1..bf8d9283e5 100644
--- a/core/services/compfs/src/deleter.rs
+++ b/core/services/compfs/src/deleter.rs
@@ -33,21 +33,272 @@ impl CompfsDeleter {
impl oio::OneShotDelete for CompfsDeleter {
async fn delete_once(&self, path: String, _: OpDelete) -> Result<()> {
- let res = if path.ends_with('/') {
- let path = self.core.prepare_path(&path)?;
+ let is_dir_path = path.ends_with('/');
+ let path = self.core.prepare_path(&path)?;
+
+ let meta = match self
+ .core
+ .exec({
+ let path = path.clone();
+ move || async move { compio::fs::symlink_metadata(path).await }
+ })
+ .await
+ {
+ Ok(meta) => meta,
+ Err(err) if err.kind() == ErrorKind::NotFound => return Ok(()),
+ Err(err) => return Err(err),
+ };
+ let is_dir = meta.is_dir();
+
+ // prepare_path calls confined_join, which strips the trailing slash.
+ // Check the directory path against the target, then remove the entry
+ // identified by symlink_metadata.
+ if is_dir_path && !is_dir {
+ let target_is_dir = if meta.is_symlink() {
+ match self
+ .core
+ .exec({
+ let path = path.clone();
+ move || async move { compio::fs::metadata(path).await }
+ })
+ .await
+ {
+ Ok(target) => target.is_dir(),
+ Err(err) if err.kind() == ErrorKind::NotFound => return
Ok(()),
+ Err(err) => return Err(err),
+ }
+ } else {
+ false
+ };
+
+ if !target_is_dir {
+ return Err(Error::new(
+ ErrorKind::NotADirectory,
+ "delete path is a directory path, but the target is a
file",
+ ));
+ }
+ }
+
+ // compio_fs::FileType::is_symlink_dir() is always false on Windows
+ // because From<std::fs::FileType> copies std's mutually exclusive
+ // is_dir() and is_symlink() flags. Check FILE_ATTRIBUTE_DIRECTORY on
+ // the raw dwFileAttributes instead so directory symlinks and junctions
+ // are removed with remove_dir.
+ #[cfg(windows)]
+ let remove_as_dir = {
+ const FILE_ATTRIBUTE_DIRECTORY: u32 = 0x10;
+ (meta.file_attributes() & FILE_ATTRIBUTE_DIRECTORY) != 0
+ };
+ #[cfg(not(windows))]
+ let remove_as_dir = is_dir;
+
+ let result = if remove_as_dir {
self.core
.exec(move || async move { compio::fs::remove_dir(path).await
})
.await
} else {
- let path = self.core.prepare_path(&path)?;
self.core
.exec(move || async move { compio::fs::remove_file(path).await
})
.await
};
- match res {
- Ok(()) => Ok(()),
- Err(e) if e.kind() == ErrorKind::NotFound => Ok(()),
- Err(e) => Err(e),
+ match result {
+ Err(err) if err.kind() == ErrorKind::NotFound => Ok(()),
+ result => result,
}
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[tokio::test]
+ async fn test_delete_missing_entry_is_a_no_op() {
+ let temp_dir = tempfile::tempdir().unwrap();
+ let op =
Operator::new(crate::Compfs::default().root(temp_dir.path().to_str().unwrap()))
+ .unwrap();
+
+ op.delete("missing").await.unwrap();
+ op.delete("missing/").await.unwrap();
+ }
+
+ #[tokio::test]
+ async fn test_delete_file_and_dir_paths() {
+ let temp_dir = tempfile::tempdir().unwrap();
+ let op =
Operator::new(crate::Compfs::default().root(temp_dir.path().to_str().unwrap()))
+ .unwrap();
+
+ op.write("file.txt", "hello").await.unwrap();
+ let err = op.delete("file.txt/").await.unwrap_err();
+ assert_eq!(err.kind(), ErrorKind::NotADirectory);
+ // The file must survive: a directory path must never remove a file.
+ assert!(op.stat("file.txt").await.is_ok());
+
+ op.delete("file.txt").await.unwrap();
+ assert!(op.stat("file.txt").await.is_err());
+
+ op.create_dir("empty_dir/").await.unwrap();
+ op.delete("empty_dir/").await.unwrap();
+ assert!(op.stat("empty_dir/").await.is_err());
+
+ op.create_dir("dir_no_slash/").await.unwrap();
+ op.delete("dir_no_slash").await.unwrap();
+ assert!(op.stat("dir_no_slash/").await.is_err());
+
+ op.write("nonempty_dir/child.txt", "data").await.unwrap();
+ let err = op.delete("nonempty_dir/").await.unwrap_err();
+ assert_eq!(err.kind(), ErrorKind::Unexpected);
+ let err = op.delete("nonempty_dir").await.unwrap_err();
+ assert_eq!(err.kind(), ErrorKind::Unexpected);
+ assert!(op.stat("nonempty_dir/child.txt").await.is_ok());
+ }
+
+ #[cfg(unix)]
+ #[tokio::test]
+ async fn test_delete_symlinks() {
+ let temp_dir = tempfile::tempdir().unwrap();
+ let root = temp_dir.path();
+ let op =
Operator::new(crate::Compfs::default().root(root.to_str().unwrap())).unwrap();
+
+ // Symlink to file: directory path is rejected, file path unlinks the
symlink only.
+ std::fs::write(root.join("real.txt"), b"data").unwrap();
+ std::os::unix::fs::symlink("real.txt", root.join("link.txt")).unwrap();
+ let err = op.delete("link.txt/").await.unwrap_err();
+ assert_eq!(err.kind(), ErrorKind::NotADirectory);
+ assert!(
+ std::fs::symlink_metadata(root.join("link.txt"))
+ .unwrap()
+ .is_symlink()
+ );
+ assert_eq!(std::fs::read(root.join("real.txt")).unwrap(), b"data");
+
+ op.delete("link.txt").await.unwrap();
+ assert_eq!(
+ std::fs::symlink_metadata(root.join("link.txt"))
+ .unwrap_err()
+ .kind(),
+ std::io::ErrorKind::NotFound
+ );
+ assert_eq!(std::fs::read(root.join("real.txt")).unwrap(), b"data");
+
+ // Symlink to dir: both file and directory paths unlink the symlink
and keep the target.
+ std::fs::create_dir(root.join("realdir")).unwrap();
+ std::fs::write(root.join("realdir/keep"), b"data").unwrap();
+ for path in ["linkdir", "linkdir/"] {
+ std::os::unix::fs::symlink("realdir",
root.join("linkdir")).unwrap();
+ op.delete(path).await.unwrap();
+ assert_eq!(
+ std::fs::symlink_metadata(root.join("linkdir"))
+ .unwrap_err()
+ .kind(),
+ std::io::ErrorKind::NotFound
+ );
+ assert_eq!(std::fs::read(root.join("realdir/keep")).unwrap(),
b"data");
+ }
+
+ // Dangling symlink: directory path is a no-op leaving the link, file
path removes it.
+ std::os::unix::fs::symlink("nope.txt",
root.join("dangle.txt")).unwrap();
+ op.delete("dangle.txt/").await.unwrap();
+ assert!(
+ std::fs::symlink_metadata(root.join("dangle.txt"))
+ .unwrap()
+ .is_symlink()
+ );
+
+ op.delete("dangle.txt").await.unwrap();
+ assert_eq!(
+ std::fs::symlink_metadata(root.join("dangle.txt"))
+ .unwrap_err()
+ .kind(),
+ std::io::ErrorKind::NotFound
+ );
+ }
+
+ #[cfg(windows)]
+ #[tokio::test]
+ async fn test_delete_symlinks() {
+ const ERROR_PRIVILEGE_NOT_HELD: i32 = 1314;
+
+ let temp_dir = tempfile::tempdir().unwrap();
+ let root = temp_dir.path();
+ let op =
Operator::new(crate::Compfs::default().root(root.to_str().unwrap())).unwrap();
+
+ std::fs::write(root.join("real.txt"), b"data").unwrap();
+ if let Err(err) =
+ std::os::windows::fs::symlink_file(root.join("real.txt"),
root.join("link.txt"))
+ {
+ if err.kind() == std::io::ErrorKind::PermissionDenied
+ || err.raw_os_error() == Some(ERROR_PRIVILEGE_NOT_HELD)
+ {
+ return;
+ }
+ panic!("failed to create file symlink: {err}");
+ }
+
+ let err = op.delete("link.txt/").await.unwrap_err();
+ assert_eq!(err.kind(), ErrorKind::NotADirectory);
+ assert!(
+ std::fs::symlink_metadata(root.join("link.txt"))
+ .unwrap()
+ .is_symlink()
+ );
+ assert_eq!(std::fs::read(root.join("real.txt")).unwrap(), b"data");
+
+ op.delete("link.txt").await.unwrap();
+ assert_eq!(
+ std::fs::symlink_metadata(root.join("link.txt"))
+ .unwrap_err()
+ .kind(),
+ std::io::ErrorKind::NotFound
+ );
+ assert_eq!(std::fs::read(root.join("real.txt")).unwrap(), b"data");
+
+ // Directory symlink: both file and directory paths must use remove_dir
+ // to unlink the symlink itself and leave the target intact.
+ std::fs::create_dir(root.join("realdir")).unwrap();
+ std::fs::write(root.join("realdir/keep"), b"data").unwrap();
+ for path in ["linkdir", "linkdir/"] {
+ std::os::windows::fs::symlink_dir(root.join("realdir"),
root.join("linkdir")).unwrap();
+ op.delete(path).await.unwrap();
+ assert_eq!(
+ std::fs::symlink_metadata(root.join("linkdir"))
+ .unwrap_err()
+ .kind(),
+ std::io::ErrorKind::NotFound
+ );
+ assert_eq!(std::fs::read(root.join("realdir/keep")).unwrap(),
b"data");
+ }
+
+ // Dangling file and directory symlinks: directory path is a no-op
+ // leaving the link; file path removes the link.
+ std::os::windows::fs::symlink_file(root.join("nope.txt"),
root.join("dangle.txt")).unwrap();
+ op.delete("dangle.txt/").await.unwrap();
+ assert!(
+ std::fs::symlink_metadata(root.join("dangle.txt"))
+ .unwrap()
+ .is_symlink()
+ );
+ op.delete("dangle.txt").await.unwrap();
+ assert_eq!(
+ std::fs::symlink_metadata(root.join("dangle.txt"))
+ .unwrap_err()
+ .kind(),
+ std::io::ErrorKind::NotFound
+ );
+
+ std::os::windows::fs::symlink_dir(root.join("nopedir"),
root.join("dangledir")).unwrap();
+ op.delete("dangledir/").await.unwrap();
+ assert!(
+ std::fs::symlink_metadata(root.join("dangledir"))
+ .unwrap()
+ .is_symlink()
+ );
+ op.delete("dangledir").await.unwrap();
+ assert_eq!(
+ std::fs::symlink_metadata(root.join("dangledir"))
+ .unwrap_err()
+ .kind(),
+ std::io::ErrorKind::NotFound
+ );
+ }
+}
diff --git a/core/services/monoiofs/Cargo.toml
b/core/services/monoiofs/Cargo.toml
index 31e1615186..572b7010d2 100644
--- a/core/services/monoiofs/Cargo.toml
+++ b/core/services/monoiofs/Cargo.toml
@@ -45,4 +45,5 @@ opendal-core = { path = "../../core", version = "0.59.5",
default-features = fal
serde = { workspace = true, features = ["derive"] }
[dev-dependencies]
+tempfile = "3"
tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }
diff --git a/core/services/monoiofs/src/deleter.rs
b/core/services/monoiofs/src/deleter.rs
index a040219087..fc5abafcf9 100644
--- a/core/services/monoiofs/src/deleter.rs
+++ b/core/services/monoiofs/src/deleter.rs
@@ -34,32 +34,159 @@ impl MonoiofsDeleter {
impl oio::OneShotDelete for MonoiofsDeleter {
async fn delete_once(&self, path: String, _: OpDelete) -> Result<()> {
+ let is_dir_path = path.ends_with('/');
let path = self.core.prepare_path(&path)?;
- let meta = self
- .core
- .dispatch({
- let path = path.clone();
- move || monoio::fs::metadata(path)
- })
- .await;
- match meta {
- Ok(meta) => {
- if meta.is_dir() {
- self.core
- .dispatch(move || monoio::fs::remove_dir(path))
- .await
- .map_err(new_std_io_error)?;
+
+ self.core
+ .dispatch(move || async move {
+ let meta = match monoio::fs::symlink_metadata(&path).await {
+ Ok(meta) => meta,
+ Err(err) if err.kind() == std::io::ErrorKind::NotFound =>
return Ok(()),
+ Err(err) => return Err(new_std_io_error(err)),
+ };
+ let is_dir = meta.is_dir();
+
+ // prepare_path calls confined_join, which strips the trailing
+ // slash. Check the directory path against the target, then
+ // remove the entry identified by symlink_metadata.
+ if is_dir_path && !is_dir {
+ let target_is_dir = if meta.is_symlink() {
+ match monoio::fs::metadata(&path).await {
+ Ok(target) => target.is_dir(),
+ Err(err) if err.kind() ==
std::io::ErrorKind::NotFound => {
+ return Ok(());
+ }
+ Err(err) => return Err(new_std_io_error(err)),
+ }
+ } else {
+ false
+ };
+
+ if !target_is_dir {
+ return Err(Error::new(
+ ErrorKind::NotADirectory,
+ "delete path is a directory path, but the target
is a file",
+ ));
+ }
+ }
+
+ let result = if is_dir {
+ monoio::fs::remove_dir(&path).await
} else {
- self.core
- .dispatch(move || monoio::fs::remove_file(path))
- .await
- .map_err(new_std_io_error)?;
+ monoio::fs::remove_file(&path).await
+ };
+ match result {
+ Err(err) if err.kind() == std::io::ErrorKind::NotFound =>
Ok(()),
+ result => result.map_err(new_std_io_error),
}
+ })
+ .await
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[tokio::test]
+ async fn test_delete_missing_entry_is_a_no_op() {
+ let temp_dir = tempfile::tempdir().unwrap();
+ let op =
Operator::new(crate::Monoiofs::default().root(temp_dir.path().to_str().unwrap()))
+ .unwrap();
+
+ op.delete("missing").await.unwrap();
+ op.delete("missing/").await.unwrap();
+ }
- Ok(())
- }
- Err(err) if err.kind() == std::io::ErrorKind::NotFound => Ok(()),
- Err(err) => Err(new_std_io_error(err)),
+ #[tokio::test]
+ async fn test_delete_file_and_dir_paths() {
+ let temp_dir = tempfile::tempdir().unwrap();
+ let op =
Operator::new(crate::Monoiofs::default().root(temp_dir.path().to_str().unwrap()))
+ .unwrap();
+
+ op.write("file.txt", "hello").await.unwrap();
+ let err = op.delete("file.txt/").await.unwrap_err();
+ assert_eq!(err.kind(), ErrorKind::NotADirectory);
+ // The file must survive: a directory path must never remove a file.
+ assert!(op.stat("file.txt").await.is_ok());
+
+ op.delete("file.txt").await.unwrap();
+ assert!(op.stat("file.txt").await.is_err());
+
+ op.create_dir("empty_dir/").await.unwrap();
+ op.delete("empty_dir/").await.unwrap();
+ assert!(op.stat("empty_dir/").await.is_err());
+
+ op.create_dir("dir_no_slash/").await.unwrap();
+ op.delete("dir_no_slash").await.unwrap();
+ assert!(op.stat("dir_no_slash/").await.is_err());
+
+ op.write("nonempty_dir/child.txt", "data").await.unwrap();
+ let err = op.delete("nonempty_dir/").await.unwrap_err();
+ assert_eq!(err.kind(), ErrorKind::Unexpected);
+ let err = op.delete("nonempty_dir").await.unwrap_err();
+ assert_eq!(err.kind(), ErrorKind::Unexpected);
+ assert!(op.stat("nonempty_dir/child.txt").await.is_ok());
+ }
+
+ #[cfg(unix)]
+ #[tokio::test]
+ async fn test_delete_symlinks() {
+ let temp_dir = tempfile::tempdir().unwrap();
+ let root = temp_dir.path();
+ let op =
Operator::new(crate::Monoiofs::default().root(root.to_str().unwrap())).unwrap();
+
+ // Symlink to file: directory path is rejected, file path unlinks the
symlink only.
+ std::fs::write(root.join("real.txt"), b"data").unwrap();
+ std::os::unix::fs::symlink("real.txt", root.join("link.txt")).unwrap();
+ let err = op.delete("link.txt/").await.unwrap_err();
+ assert_eq!(err.kind(), ErrorKind::NotADirectory);
+ assert!(
+ std::fs::symlink_metadata(root.join("link.txt"))
+ .unwrap()
+ .is_symlink()
+ );
+ assert_eq!(std::fs::read(root.join("real.txt")).unwrap(), b"data");
+
+ op.delete("link.txt").await.unwrap();
+ assert_eq!(
+ std::fs::symlink_metadata(root.join("link.txt"))
+ .unwrap_err()
+ .kind(),
+ std::io::ErrorKind::NotFound
+ );
+ assert_eq!(std::fs::read(root.join("real.txt")).unwrap(), b"data");
+
+ // Symlink to dir: both file and directory paths unlink the symlink
and keep the target.
+ std::fs::create_dir(root.join("realdir")).unwrap();
+ std::fs::write(root.join("realdir/keep"), b"data").unwrap();
+ for path in ["linkdir", "linkdir/"] {
+ std::os::unix::fs::symlink("realdir",
root.join("linkdir")).unwrap();
+ op.delete(path).await.unwrap();
+ assert_eq!(
+ std::fs::symlink_metadata(root.join("linkdir"))
+ .unwrap_err()
+ .kind(),
+ std::io::ErrorKind::NotFound
+ );
+ assert_eq!(std::fs::read(root.join("realdir/keep")).unwrap(),
b"data");
}
+
+ // Dangling symlink: directory path is a no-op leaving the link, file
path removes it.
+ std::os::unix::fs::symlink("nope.txt",
root.join("dangle.txt")).unwrap();
+ op.delete("dangle.txt/").await.unwrap();
+ assert!(
+ std::fs::symlink_metadata(root.join("dangle.txt"))
+ .unwrap()
+ .is_symlink()
+ );
+
+ op.delete("dangle.txt").await.unwrap();
+ assert_eq!(
+ std::fs::symlink_metadata(root.join("dangle.txt"))
+ .unwrap_err()
+ .kind(),
+ std::io::ErrorKind::NotFound
+ );
}
}