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 2091708f78 fix(services/compfs): harden copy, rename, listing, and
vectored writes (#8914)
2091708f78 is described below
commit 2091708f78b2cb5976a69ea2f2396a654dc554db
Author: Xuanwo <[email protected]>
AuthorDate: Tue Nov 24 03:11:16 2026 +0800
fix(services/compfs): harden copy, rename, listing, and vectored writes
(#8914)
- Validate copy and rename sources before creating destination parents or
truncating files.
- Preserve backslashes in Unix file names and treat file paths as empty
listings.
- Split vectored writes into batches accepted by IOV_MAX.
---
core/services/compfs/src/backend.rs | 79 +++++++++++++++++++++++++++++++++++++
core/services/compfs/src/lister.rs | 58 +++++++++++++++++++++++----
core/services/compfs/src/reader.rs | 9 ++++-
core/services/compfs/src/writer.rs | 38 +++++++++++++++++-
4 files changed, 175 insertions(+), 9 deletions(-)
diff --git a/core/services/compfs/src/backend.rs
b/core/services/compfs/src/backend.rs
index dede4cef07..63459b51b9 100644
--- a/core/services/compfs/src/backend.rs
+++ b/core/services/compfs/src/backend.rs
@@ -193,6 +193,19 @@ impl Service for CompfsBackend {
let to = self.core.prepare_path(to)?;
Ok(oio::OneShotCopier::new(async move {
+ let from_meta = core
+ .exec({
+ let from = from.clone();
+ move || async move { compio::fs::metadata(from).await }
+ })
+ .await?;
+ if from_meta.is_dir() {
+ return Err(Error::new(
+ ErrorKind::IsADirectory,
+ "copy source is a directory",
+ ));
+ }
+
core.exec(move || async move {
let from = OpenOptions::new().read(true).open(from).await?;
if let Some(parent) = to.parent() {
@@ -239,6 +252,7 @@ impl Service for CompfsBackend {
self.core
.exec_blocking(move || {
+
std::fs::metadata(&from).map_err(parse_conditional_rename_path_error)?;
if let Some(parent) = to.parent() {
std::fs::create_dir_all(parent)
.map_err(parse_conditional_rename_path_error)?;
@@ -253,6 +267,7 @@ impl Service for CompfsBackend {
self.core
.exec(move || async move {
+ compio::fs::metadata(&from).await?;
if let Some(parent) = to.parent() {
compio::fs::create_dir_all(parent).await?;
}
@@ -380,4 +395,68 @@ mod tests {
b"parent contents"
);
}
+
+ #[tokio::test]
+ async fn test_copy_directory_source_preserves_destination() {
+ let temp_dir = tempfile::tempdir().unwrap();
+ let root = temp_dir.path();
+ std::fs::create_dir(root.join("dir")).unwrap();
+ std::fs::write(root.join("dir/child"), b"source contents").unwrap();
+ std::fs::write(root.join("dst"), b"destination contents").unwrap();
+ let op =
Operator::new(CompfsBuilder::default().root(root.to_str().unwrap())).unwrap();
+
+ let err = op.copy("dir", "dst").await.unwrap_err();
+ assert_eq!(err.kind(), ErrorKind::IsADirectory, "{err:?}");
+ assert_eq!(
+ std::fs::read(root.join("dst")).unwrap(),
+ b"destination contents"
+ );
+ }
+
+ #[tokio::test]
+ async fn test_copy_directory_source_does_not_create_destination() {
+ let temp_dir = tempfile::tempdir().unwrap();
+ let root = temp_dir.path();
+ std::fs::create_dir(root.join("dir")).unwrap();
+ let op =
Operator::new(CompfsBuilder::default().root(root.to_str().unwrap())).unwrap();
+
+ let err = op.copy("dir", "new_dir/missing").await.unwrap_err();
+ assert_eq!(err.kind(), ErrorKind::IsADirectory, "{err:?}");
+ assert!(!root.join("new_dir").exists());
+ }
+
+ #[tokio::test]
+ async fn test_rename_missing_source_does_not_create_destination_parent() {
+ let temp_dir = tempfile::tempdir().unwrap();
+ let root = temp_dir.path();
+ let op =
Operator::new(CompfsBuilder::default().root(root.to_str().unwrap())).unwrap();
+
+ let err = op.rename("missing", "ghost/dir/dst").await.unwrap_err();
+ assert_eq!(err.kind(), ErrorKind::NotFound, "{err:?}");
+ assert!(!root.join("ghost").exists());
+ }
+
+ #[tokio::test]
+ async fn
test_conditional_rename_missing_source_does_not_create_destination_parent() {
+ let temp_dir = tempfile::tempdir().unwrap();
+ let root = temp_dir.path();
+ std::fs::write(root.join("file"), b"file contents").unwrap();
+ let op =
Operator::new(CompfsBuilder::default().root(root.to_str().unwrap())).unwrap();
+
+ let err = op
+ .rename_with("missing", "ghost/dir/dst")
+ .if_not_exists(true)
+ .await
+ .unwrap_err();
+ assert_eq!(err.kind(), ErrorKind::NotFound, "{err:?}");
+ assert!(!root.join("ghost").exists());
+
+ let err = op
+ .rename_with("missing", "file/target")
+ .if_not_exists(true)
+ .await
+ .unwrap_err();
+ assert_eq!(err.kind(), ErrorKind::NotFound, "{err:?}");
+ assert_eq!(std::fs::read(root.join("file")).unwrap(), b"file
contents");
+ }
}
diff --git a/core/services/compfs/src/lister.rs
b/core/services/compfs/src/lister.rs
index 9a290b4dbc..4624abe4d8 100644
--- a/core/services/compfs/src/lister.rs
+++ b/core/services/compfs/src/lister.rs
@@ -42,13 +42,13 @@ impl CompfsLister {
}
fn normalize(path: &Path, root: &Path) -> String {
- normalize_path(
- &path
- .strip_prefix(root)
- .expect("cannot fail because the prefix is iterated")
- .to_string_lossy()
- .replace('\\', "/"),
- )
+ let raw = path
+ .strip_prefix(root)
+ .expect("cannot fail because the prefix is iterated")
+ .to_string_lossy();
+ #[cfg(windows)]
+ let raw = raw.replace('\\', "/");
+ normalize_path(&raw)
}
fn next_entry(read_dir: &mut ReadDir, root: &Path) ->
std::io::Result<Option<oio::Entry>> {
@@ -156,4 +156,48 @@ mod tests {
std::fs::remove_dir_all(&root).unwrap();
}
+
+ #[cfg(unix)]
+ #[tokio::test]
+ async fn test_list_preserves_backslashes_in_file_names() {
+ use bytes::Bytes;
+ use opendal_core::EntryMode;
+
+ let temp_dir = tempfile::tempdir().unwrap();
+ let root = temp_dir.path();
+ let names = ["has\\slash", "trailing\\"];
+ for name in names {
+ std::fs::write(root.join(name), name.as_bytes()).unwrap();
+ }
+ let op =
Operator::new(Compfs::default().root(root.to_str().unwrap())).unwrap();
+
+ let entries = op.list("/").await.unwrap();
+ for name in names {
+ let entry = entries
+ .iter()
+ .find(|entry| entry.path() == name)
+ .unwrap_or_else(|| panic!("missing entry {name:?} in
{entries:?}"));
+ assert_eq!(entry.metadata().mode(), EntryMode::FILE);
+ assert_eq!(
+ op.read(entry.path()).await.unwrap().to_bytes(),
+ Bytes::copy_from_slice(name.as_bytes()),
+ );
+ }
+ }
+
+ #[tokio::test]
+ async fn test_list_file_as_directory_returns_empty() {
+ let temp_dir = tempfile::tempdir().unwrap();
+ let root = temp_dir.path();
+ std::fs::write(root.join("f.txt"), b"file contents").unwrap();
+ let op =
Operator::new(Compfs::default().root(root.to_str().unwrap())).unwrap();
+
+ for path in ["f.txt/", "f.txt/sub/"] {
+ let entries = op
+ .list(path)
+ .await
+ .unwrap_or_else(|err| panic!("list {path:?} failed: {err:?}"));
+ assert!(entries.is_empty(), "list {path:?} returned {entries:?}");
+ }
+ }
}
diff --git a/core/services/compfs/src/reader.rs
b/core/services/compfs/src/reader.rs
index d7c90b178f..82ce2f26b5 100644
--- a/core/services/compfs/src/reader.rs
+++ b/core/services/compfs/src/reader.rs
@@ -194,7 +194,14 @@ impl oio::List for CompfsLazyLister {
.await?
{
Ok(read_dir) => Some(CompfsLister::new(self.core.clone(),
&path, read_dir)),
- Err(e) if e.kind() == std::io::ErrorKind::NotFound => None,
+ Err(e)
+ if matches!(
+ e.kind(),
+ std::io::ErrorKind::NotFound |
std::io::ErrorKind::NotADirectory
+ ) =>
+ {
+ None
+ }
Err(e) => return Err(new_std_io_error(e)),
},
);
diff --git a/core/services/compfs/src/writer.rs
b/core/services/compfs/src/writer.rs
index 0405d1e415..87a6274ce1 100644
--- a/core/services/compfs/src/writer.rs
+++ b/core/services/compfs/src/writer.rs
@@ -26,6 +26,9 @@ use super::core::{CompfsBuffer, CompfsCore};
use opendal_core::raw::*;
use opendal_core::*;
+/// Maximum number of slices submitted in one vectored write.
+const MAX_IOV: usize = 1024;
+
#[derive(Debug)]
pub struct CompfsWriter {
core: Arc<CompfsCore>,
@@ -51,7 +54,11 @@ impl oio::Write for CompfsWriter {
let pos = self
.core
.exec(move || async move {
- buf_try!(@try file.write_vectored_all(compfs_buf).await);
+ for batch in compfs_buf.0.chunks(MAX_IOV) {
+ buf_try!(@try file
+ .write_vectored_all(CompfsBuffer(batch.to_vec()))
+ .await);
+ }
Ok(file.position())
})
.await?;
@@ -82,3 +89,32 @@ impl oio::Write for CompfsWriter {
))
}
}
+
+#[cfg(test)]
+mod tests {
+ use bytes::Bytes;
+ use opendal_core::Buffer;
+ use opendal_core::Operator;
+
+ use crate::Compfs;
+
+ #[tokio::test]
+ async fn test_write_buffer_with_more_than_iov_max_chunks() {
+ let temp_dir = tempfile::tempdir().unwrap();
+ let op =
Operator::new(Compfs::default().root(temp_dir.path().to_str().unwrap())).unwrap();
+
+ let chunks = (0..2048)
+ .map(|i| Bytes::from(vec![(i % 251) as u8]))
+ .collect::<Vec<_>>();
+ let mut expected =
chunks.iter().flatten().copied().collect::<Vec<_>>();
+
+ let mut writer = op.writer("many-chunks").await.unwrap();
+ writer.write(Buffer::from(chunks)).await.unwrap();
+ writer.write(Bytes::from_static(b"tail")).await.unwrap();
+ writer.close().await.unwrap();
+ expected.extend_from_slice(b"tail");
+
+ let actual = op.read("many-chunks").await.unwrap().to_bytes();
+ assert_eq!(actual.as_ref(), expected.as_slice());
+ }
+}