This is an automated email from the ASF dual-hosted git repository. Xuanwo pushed a commit to branch xuanwo/fix-fs-staged-cleanup-xattr-direct-io in repository https://gitbox.apache.org/repos/asf/opendal.git
commit 0ae30a46a83a029fec474419ddacf9d62e37db9c Author: Xuanwo <[email protected]> AuthorDate: Mon Nov 23 20:29:31 2026 +0100 fix(services/fs): fix staged write cleanup, xattr ordering, and direct_io create cleanup --- core/services/fs/src/core.rs | 83 ++++++- core/services/fs/src/direct_io.rs | 89 +++++++- core/services/fs/src/reader.rs | 8 +- core/services/fs/src/writer.rs | 416 +++++++++++++++++++++++++++++++----- core/services/fs/tests/direct_io.rs | 21 ++ 5 files changed, 535 insertions(+), 82 deletions(-) diff --git a/core/services/fs/src/core.rs b/core/services/fs/src/core.rs index b3c15f961a..869fe55a29 100644 --- a/core/services/fs/src/core.rs +++ b/core/services/fs/src/core.rs @@ -241,7 +241,21 @@ impl FsCore { pub fn set_user_metadata(path: &Path, user_metadata: &HashMap<String, String>) -> Result<()> { for (key, value) in user_metadata { let attr_name = format!("{XATTR_USER_PREFIX}{key}"); - xattr::set(path, &attr_name, value.as_bytes()).map_err(new_std_io_error)?; + xattr::set(path, &attr_name, value.as_bytes()).map_err(|err| { + if is_xattr_unsupported(&err) { + Error::new( + ErrorKind::Unsupported, + "user metadata is not supported by the filesystem", + ) + .set_source(err) + } else if err.kind() == std::io::ErrorKind::InvalidInput + || matches!(err.raw_os_error(), Some(libc::E2BIG | libc::ERANGE)) + { + Error::new(ErrorKind::Unexpected, err.kind().to_string()).set_source(err) + } else { + new_std_io_error(err) + } + })?; } Ok(()) } @@ -381,10 +395,12 @@ pub(super) fn publish_staged_file(from: &Path, to: &Path) -> Result<()> { // the staging entry already; the destination is still published. Err(err) if err.kind() == std::io::ErrorKind::NotFound => Ok(()), // The destination was published, but the staging file is orphaned - // now. Report it instead of leaking it silently. - Err(err) => { - Err(new_std_io_error(err).with_context("service_operation", "RemoveStagedFile")) - } + // now. Report it instead of leaking it silently, and mark it + // permanent so retries do not re-run hard_link against the + // already-published destination. + Err(err) => Err(new_std_io_error(err) + .set_permanent() + .with_context("service_operation", "RemoveStagedFile")), Ok(()) => Ok(()), } } @@ -437,18 +453,37 @@ impl ErrorContext { } } +/// Parse an error from an unconditional rename. +pub(crate) fn parse_rename_error(err: std::io::Error) -> Error { + parse_io_error(err, false).with_context("service_operation", "Rename") +} + /// Parse an error using its service operation context. pub(crate) fn parse_error(ctx: ErrorContext, err: std::io::Error) -> Error { - let err = match err.kind() { - std::io::ErrorKind::AlreadyExists => Error::new( + parse_io_error(err, true).with_context("service_operation", ctx.service_operation.0) +} + +fn parse_io_error(err: std::io::Error, already_exists_as_condition: bool) -> Error { + match err.kind() { + std::io::ErrorKind::AlreadyExists if already_exists_as_condition => Error::new( ErrorKind::ConditionNotMatch, "The file already exists in the filesystem", ) .set_source(err), - _ => new_std_io_error(err), - }; - - err.with_context("service_operation", ctx.service_operation.0) + std::io::ErrorKind::IsADirectory => { + Error::new(ErrorKind::IsADirectory, err.kind().to_string()).set_source(err) + } + std::io::ErrorKind::NotADirectory => { + Error::new(ErrorKind::NotADirectory, err.kind().to_string()).set_source(err) + } + _ => { + #[cfg(unix)] + if err.raw_os_error() == Some(libc::EXDEV) { + return Error::new(ErrorKind::Unexpected, err.kind().to_string()).set_source(err); + } + new_std_io_error(err) + } + } } #[cfg(test)] @@ -516,6 +551,32 @@ mod tests { assert_eq!(err.kind(), ErrorKind::NotFound, "{err:?}"); } + #[test] + fn test_parse_rename_error_classifies_permanent_and_temporary_errors() { + let err = parse_rename_error(std::io::Error::from(std::io::ErrorKind::IsADirectory)); + assert_eq!(err.kind(), ErrorKind::IsADirectory); + assert!(!err.is_temporary()); + + let err = parse_rename_error(std::io::Error::from(std::io::ErrorKind::NotADirectory)); + assert_eq!(err.kind(), ErrorKind::NotADirectory); + assert!(!err.is_temporary()); + + let err = parse_rename_error(std::io::Error::from(std::io::ErrorKind::PermissionDenied)); + assert_eq!(err.kind(), ErrorKind::PermissionDenied); + assert!(!err.is_temporary()); + + let err = parse_rename_error(std::io::Error::from(std::io::ErrorKind::Interrupted)); + assert_eq!(err.kind(), ErrorKind::Unexpected); + assert!(err.is_temporary()); + + #[cfg(unix)] + { + let err = parse_rename_error(std::io::Error::from_raw_os_error(libc::EXDEV)); + assert_eq!(err.kind(), ErrorKind::Unexpected); + assert!(!err.is_temporary()); + } + } + #[cfg(target_os = "linux")] #[test] fn test_conditional_rename_directory_errno_mapping() { diff --git a/core/services/fs/src/direct_io.rs b/core/services/fs/src/direct_io.rs index 53bb30cc14..d81230b871 100644 --- a/core/services/fs/src/direct_io.rs +++ b/core/services/fs/src/direct_io.rs @@ -41,11 +41,74 @@ pub(super) struct DirectIoAlignment { impl DirectIoAlignment { pub(super) fn open( path: &Path, - mut options: OpenOptions, + options: OpenOptions, operation: ServiceOperation, - cleanup_on_error: bool, ) -> Result<(File, Self)> { - let file = options + let file = Self::open_raw(path, options, operation)?; + Self::discover_or_cleanup(path, file, operation, false) + } + + pub(super) fn open_for_write( + path: &Path, + operation: ServiceOperation, + create_new: bool, + ) -> Result<(File, Self)> { + if create_new { + let mut options = OpenOptions::new(); + options.write(true).create_new(true); + let file = Self::open_raw(path, options, operation)?; + return Self::discover_or_cleanup(path, file, operation, true); + } + + for _ in 0..8 { + // Try opening an existing file first without O_CREAT so a failed + // O_DIRECT check never removes or truncates a pre-existing file. + let mut existing = OpenOptions::new(); + existing.write(true); + match Self::open_raw(path, existing, operation) { + Ok(file) => { + let (file, alignment) = + Self::discover_or_cleanup(path, file, operation, false)?; + file.set_len(0).map_err(new_std_io_error)?; + return Ok((file, alignment)); + } + Err(err) if err.kind() == OpendalErrorKind::NotFound => {} + Err(err) => return Err(err), + } + + // The target did not exist; create it exclusively so we know we + // created the file and can remove it if direct I/O discovery fails. + let mut create = OpenOptions::new(); + create.write(true).create_new(true); + match Self::open_raw(path, create, operation) { + Ok(file) => return Self::discover_or_cleanup(path, file, operation, true), + Err(err) if err.kind() == OpendalErrorKind::ConditionNotMatch => { + // O_CREAT|O_EXCL fails with EEXIST on a dangling symlink while + // opening without O_CREAT fails with ENOENT. + if std::fs::symlink_metadata(path) + .is_ok_and(|meta| meta.file_type().is_symlink()) + { + break; + } + } + Err(err) => return Err(err), + } + } + + let mut fallback = OpenOptions::new(); + fallback.write(true).create(true); + let file = Self::open_raw(path, fallback, operation)?; + let (file, alignment) = Self::discover_or_cleanup(path, file, operation, false)?; + file.set_len(0).map_err(new_std_io_error)?; + Ok((file, alignment)) + } + + fn open_raw( + path: &Path, + mut options: OpenOptions, + operation: ServiceOperation, + ) -> Result<File> { + options .custom_flags(OFlags::DIRECT.bits() as i32) .open(path) .map_err(|err| match err.raw_os_error() { @@ -58,18 +121,30 @@ impl DirectIoAlignment { .set_source(err) } _ => parse_error(ErrorContext::new(operation), err), - })?; + }) + } + fn discover_or_cleanup( + path: &Path, + file: File, + operation: ServiceOperation, + cleanup_on_error: bool, + ) -> Result<(File, Self)> { match Self::discover(&file) { Ok(alignment) => Ok((file, alignment)), Err(err) => { + let err = err.with_context("service_operation", operation.0); if cleanup_on_error { drop(file); - if let Err(cleanup) = std::fs::remove_file(path) { - return Err(err.with_context("file_cleanup", cleanup.to_string())); + match std::fs::remove_file(path) { + Ok(()) => {} + Err(cleanup) if cleanup.kind() == ErrorKind::NotFound => {} + Err(cleanup) => { + return Err(err.with_context("file_cleanup", cleanup.to_string())); + } } } - Err(err.with_context("service_operation", operation.0)) + Err(err) } } } diff --git a/core/services/fs/src/reader.rs b/core/services/fs/src/reader.rs index 6d9c95aed9..c050695888 100644 --- a/core/services/fs/src/reader.rs +++ b/core/services/fs/src/reader.rs @@ -63,12 +63,8 @@ impl oio::PositionRead for FsReader { let (file, direct_io) = if core.direct_io { let mut options = std::fs::OpenOptions::new(); options.read(true); - let (file, alignment) = DirectIoAlignment::open( - &path, - options, - ServiceOperation("OpenForRead"), - false, - )?; + let (file, alignment) = + DirectIoAlignment::open(&path, options, ServiceOperation("OpenForRead"))?; (file, Some(alignment)) } else { (File::open(path).map_err(new_std_io_error)?, None) diff --git a/core/services/fs/src/writer.rs b/core/services/fs/src/writer.rs index f844ac4f11..5b59603776 100644 --- a/core/services/fs/src/writer.rs +++ b/core/services/fs/src/writer.rs @@ -27,7 +27,7 @@ use bytes::Buf; use bytes::Bytes; use tokio::io::AsyncWriteExt; -use super::core::{ErrorContext, FsCore, parse_error, publish_staged_file}; +use super::core::{ErrorContext, FsCore, parse_error, parse_rename_error, publish_staged_file}; #[cfg(target_os = "linux")] use super::direct_io::DirectIoAlignment; use opendal_core::raw::*; @@ -203,18 +203,6 @@ impl FsWriter { } } - let mut options = OpenOptions::new(); - options.write(true); - let operation = if temp_path.is_some() { - options.create_new(true); - ServiceOperation("OpenTemporaryFile") - } else { - options - .create_new(op.if_not_exists()) - .create(!op.if_not_exists()); - options.append(op.append()); - ServiceOperation("OpenForWrite") - }; let open_path = temp_path.as_ref().unwrap_or(&target_path); // Opening an existing file without appending truncates it in place, @@ -225,16 +213,14 @@ impl FsWriter { #[cfg(target_os = "linux")] if core.direct_io { let open_path = open_path.clone(); - let temporary = temp_path.is_some(); - let cleanup_on_error = temporary || op.if_not_exists(); + let create_new = temp_path.is_some() || op.if_not_exists(); + let operation = if temp_path.is_some() { + ServiceOperation("OpenTemporaryFile") + } else { + ServiceOperation("OpenForWrite") + }; let (f, alignment) = tokio::task::spawn_blocking(move || { - let (file, alignment) = - DirectIoAlignment::open(&open_path, options, operation, cleanup_on_error)?; - // Validate O_DIRECT on the opened fd before truncating an overwrite. - if !temporary { - file.set_len(0).map_err(new_std_io_error)?; - } - Ok::<_, Error>((file, alignment)) + DirectIoAlignment::open_for_write(&open_path, operation, create_new) }) .await .map_err(new_task_join_error)??; @@ -251,6 +237,19 @@ impl FsWriter { }); } + let mut options = OpenOptions::new(); + options.write(true); + let operation = if temp_path.is_some() { + options.create_new(true); + ServiceOperation("OpenTemporaryFile") + } else { + options + .create_new(op.if_not_exists()) + .create(!op.if_not_exists()); + options.append(op.append()); + ServiceOperation("OpenForWrite") + }; + options.truncate(!op.append() || temp_path.is_some()); let f = tokio::fs::OpenOptions::from(options) .open(open_path) @@ -274,47 +273,58 @@ impl FsWriter { /// /// A non-append write without staging truncates the existing file in place, /// so user metadata from the previous content must be replaced. Staged - /// writes publish a new inode, while appends must keep the metadata already - /// attached to the file. + /// writes attach metadata to the staging file before publishing the new + /// inode, while appends keep the metadata already attached to the file. #[cfg(unix)] fn update_user_metadata(&self) -> Result<()> { if self.overwrite_in_place { FsCore::replace_user_metadata(&self.target_path, self.user_metadata.as_ref()) } else if let Some(user_metadata) = &self.user_metadata { - FsCore::set_user_metadata(&self.target_path, user_metadata) + let path = self.temp_path.as_deref().unwrap_or(&self.target_path); + FsCore::set_user_metadata(path, user_metadata) } else { Ok(()) } } + /// Discard the staged file on a permanent failure while keeping it intact + /// for temporary errors so retry layers can retry `close()`. + async fn cleanup_staged_on_permanent_error(&self, err: Error) -> Error { + if err.is_temporary() { + return err; + } + let Some(temp_path) = &self.temp_path else { + return err; + }; + + match tokio::fs::remove_file(temp_path).await { + Ok(()) => err, + Err(cleanup_err) if cleanup_err.kind() == std::io::ErrorKind::NotFound => err, + Err(cleanup_err) => err.with_context("staging_cleanup", cleanup_err), + } + } + async fn commit_staged_file(&self) -> Result<()> { let Some(temp_path) = &self.temp_path else { return Ok(()); }; - if self.if_not_exists { + let result = if self.if_not_exists { let temp_path = temp_path.clone(); let target_path = self.target_path.clone(); - return tokio::task::spawn_blocking(move || { - publish_staged_file(&temp_path, &target_path).map_err(|err| { - // The staged file is discarded, so this close cannot be retried. - let err = err.set_permanent(); - match std::fs::remove_file(&temp_path) { - Ok(()) => err, - Err(cleanup_err) if cleanup_err.kind() == std::io::ErrorKind::NotFound => { - err - } - Err(cleanup_err) => err.with_context("staging_cleanup", cleanup_err), - } - }) - }) - .await - .map_err(new_task_join_error)?; - } + tokio::task::spawn_blocking(move || publish_staged_file(&temp_path, &target_path)) + .await + .map_err(new_task_join_error)? + } else { + tokio::fs::rename(temp_path, &self.target_path) + .await + .map_err(parse_rename_error) + }; - tokio::fs::rename(temp_path, &self.target_path) - .await - .map_err(new_std_io_error) + match result { + Ok(()) => Ok(()), + Err(err) => Err(self.cleanup_staged_on_permanent_error(err).await), + } } } @@ -334,14 +344,19 @@ impl oio::Write for FsWriter { } async fn close(&mut self) -> Result<Metadata> { - self.f.flush().await.map_err(new_std_io_error)?; - self.f.sync_all().await.map_err(new_std_io_error)?; + if let Err(err) = self.f.flush().await.map_err(new_std_io_error) { + return Err(self.cleanup_staged_on_permanent_error(err).await); + } + #[cfg(unix)] + if let Err(err) = self.update_user_metadata() { + return Err(self.cleanup_staged_on_permanent_error(err).await); + } + if let Err(err) = self.f.sync_all().await.map_err(new_std_io_error) { + return Err(self.cleanup_staged_on_permanent_error(err).await); + } self.commit_staged_file().await?; - #[cfg(unix)] - self.update_user_metadata()?; - let file_meta = self.f.metadata().await.map_err(new_std_io_error)?; let mut meta = MetadataBuilder::file(file_meta.len()); meta.last_modified(Timestamp::try_from( @@ -400,8 +415,10 @@ impl oio::PositionWrite for FsWriter { async fn close(&self, _size: u64) -> Result<Metadata> { #[cfg(target_os = "linux")] - if self.direct_io.is_some() { - self.f.set_len(_size).await.map_err(new_std_io_error)?; + if self.direct_io.is_some() + && let Err(err) = self.f.set_len(_size).await.map_err(new_std_io_error) + { + return Err(self.cleanup_staged_on_permanent_error(err).await); } let mut f = self @@ -412,14 +429,19 @@ impl oio::PositionWrite for FsWriter { .into_std() .await; - f.flush().map_err(new_std_io_error)?; - f.sync_all().map_err(new_std_io_error)?; + if let Err(err) = f.flush().map_err(new_std_io_error) { + return Err(self.cleanup_staged_on_permanent_error(err).await); + } + #[cfg(unix)] + if let Err(err) = self.update_user_metadata() { + return Err(self.cleanup_staged_on_permanent_error(err).await); + } + if let Err(err) = f.sync_all().map_err(new_std_io_error) { + return Err(self.cleanup_staged_on_permanent_error(err).await); + } self.commit_staged_file().await?; - #[cfg(unix)] - self.update_user_metadata()?; - let file_meta = f.metadata().map_err(new_std_io_error)?; let mode = if file_meta.is_file() { EntryMode::FILE @@ -465,8 +487,9 @@ fn write_at(f: &File, buf: &[u8], offset: u64) -> Result<usize> { f.write_at(buf, offset).map_err(new_std_io_error) } -#[cfg(all(test, unix))] +#[cfg(test)] mod tests { + #[cfg(unix)] use std::collections::HashMap; use std::path::Path; @@ -474,6 +497,7 @@ mod tests { use super::*; + #[cfg(unix)] fn xattrs_supported(dir: &Path) -> bool { let probe = dir.join(".xattr-probe"); if std::fs::write(&probe, b"probe").is_err() { @@ -484,6 +508,10 @@ mod tests { supported } + fn staging_is_empty(staging: &Path) -> bool { + std::fs::read_dir(staging).unwrap().next().is_none() + } + fn operator(root: &Path, atomic_write_dir: Option<&Path>) -> Operator { let builder = crate::Fs::default().root(root.to_str().unwrap()); let builder = match atomic_write_dir { @@ -493,6 +521,7 @@ mod tests { Operator::new(builder).unwrap() } + #[cfg(unix)] async fn overwrite_replaces_user_metadata(atomic_write: bool) { let temp_dir = tempfile::TempDir::new().unwrap(); let root = temp_dir.path().join("root"); @@ -533,12 +562,14 @@ mod tests { assert!(FsCore::get_user_metadata(&file).unwrap().is_empty()); } + #[cfg(unix)] #[tokio::test] async fn test_overwrite_replaces_user_metadata() { overwrite_replaces_user_metadata(false).await; overwrite_replaces_user_metadata(true).await; } + #[cfg(unix)] async fn append_preserves_user_metadata(atomic_write: bool) { let temp_dir = tempfile::TempDir::new().unwrap(); let root = temp_dir.path().join("root"); @@ -571,9 +602,278 @@ mod tests { assert_eq!(op.read("file").await.unwrap().to_vec(), b"firstmore"); } + #[cfg(unix)] #[tokio::test] async fn test_append_preserves_user_metadata() { append_preserves_user_metadata(false).await; append_preserves_user_metadata(true).await; } + + #[tokio::test] + async fn test_staged_write_cleans_up_on_is_a_directory() { + let temp_dir = tempfile::TempDir::new().unwrap(); + let root = temp_dir.path().join("root"); + let staging = temp_dir.path().join("staging"); + std::fs::create_dir(&root).unwrap(); + std::fs::create_dir(&staging).unwrap(); + std::fs::create_dir(root.join("target")).unwrap(); + + let op = operator(&root, Some(&staging)); + let err = op.write("target", "data").await.unwrap_err(); + assert!(!err.is_temporary(), "{err:?}"); + #[cfg(unix)] + assert_eq!(err.kind(), ErrorKind::IsADirectory, "{err:?}"); + assert!(staging_is_empty(&staging)); + } + + #[tokio::test] + async fn test_staged_write_cleans_up_on_not_a_directory() { + let temp_dir = tempfile::TempDir::new().unwrap(); + let root = temp_dir.path().join("root"); + let staging = temp_dir.path().join("staging"); + std::fs::create_dir(&root).unwrap(); + std::fs::create_dir(&staging).unwrap(); + + let op = operator(&root, Some(&staging)); + let mut writer = op.writer("sub/file").await.unwrap(); + writer.write("data").await.unwrap(); + assert!(!staging_is_empty(&staging)); + + // Replace the parent directory with a regular file before commit so + // rename fails permanently with ENOTDIR. + std::fs::remove_dir(root.join("sub")).unwrap(); + std::fs::write(root.join("sub"), b"blocker").unwrap(); + + let err = writer.close().await.unwrap_err(); + assert!(!err.is_temporary(), "{err:?}"); + #[cfg(unix)] + assert_eq!(err.kind(), ErrorKind::NotADirectory, "{err:?}"); + assert!(staging_is_empty(&staging)); + } + + #[cfg(unix)] + #[tokio::test] + async fn test_staged_write_cleans_up_on_permission_denied() { + use std::os::unix::fs::PermissionsExt; + + // Root bypasses directory write permission checks. + if unsafe { libc::geteuid() } == 0 { + return; + } + + let temp_dir = tempfile::TempDir::new().unwrap(); + let root = temp_dir.path().join("root"); + let staging = temp_dir.path().join("staging"); + let ro_dir = root.join("ro"); + std::fs::create_dir_all(&ro_dir).unwrap(); + std::fs::create_dir(&staging).unwrap(); + std::fs::set_permissions(&ro_dir, std::fs::Permissions::from_mode(0o555)).unwrap(); + + let op = operator(&root, Some(&staging)); + let result = op.write("ro/file", "data").await; + + std::fs::set_permissions(&ro_dir, std::fs::Permissions::from_mode(0o755)).unwrap(); + + let err = result.unwrap_err(); + assert_eq!(err.kind(), ErrorKind::PermissionDenied, "{err:?}"); + assert!(!err.is_temporary(), "{err:?}"); + assert!(staging_is_empty(&staging)); + } + + #[cfg(target_os = "linux")] + #[tokio::test] + async fn test_staged_write_cleans_up_on_cross_device_rename() { + use std::os::unix::fs::MetadataExt; + + let shm = Path::new("/dev/shm"); + let Ok(staging_dir) = tempfile::tempdir_in(shm) else { + return; + }; + let root_dir = tempfile::tempdir().unwrap(); + if std::fs::metadata(root_dir.path()).unwrap().dev() + == std::fs::metadata(staging_dir.path()).unwrap().dev() + { + return; + } + + let op = operator(root_dir.path(), Some(staging_dir.path())); + let err = op.write("file", "data").await.unwrap_err(); + assert!(!err.is_temporary(), "{err:?}"); + assert!(staging_is_empty(staging_dir.path())); + } + + #[tokio::test] + async fn test_cleanup_staged_preserves_temp_file_on_temporary_error() { + let temp_dir = tempfile::TempDir::new().unwrap(); + let root = temp_dir.path().join("root"); + let staging = temp_dir.path().join("staging"); + std::fs::create_dir(&root).unwrap(); + std::fs::create_dir(&staging).unwrap(); + + let core = Arc::new(FsCore { + info: ServiceInfo::new(crate::FS_SCHEME, root.to_string_lossy(), ""), + capability: Capability::default(), + root: root.clone(), + atomic_write_dir: Some(staging.clone()), + direct_io: false, + buf_pool: oio::PooledBuf::new(1), + }); + + let writer = FsWriter::create(&core, "file", &OpWrite::default()) + .await + .unwrap(); + oio::PositionWrite::write_all_at(&writer, 0, Buffer::from("data")) + .await + .unwrap(); + let temp_path = writer.temp_path.clone().unwrap(); + assert!(temp_path.exists()); + + // Temporary errors must leave the staging file intact for retry. + let transient = Error::new(ErrorKind::Unexpected, "transient").set_temporary(); + let returned = writer.cleanup_staged_on_permanent_error(transient).await; + assert!(returned.is_temporary()); + assert!(temp_path.exists()); + + // A retried close must publish the preserved staging file cleanly. + oio::PositionWrite::close(&writer, 4).await.unwrap(); + assert_eq!(std::fs::read(root.join("file")).unwrap(), b"data"); + assert!(staging_is_empty(&staging)); + } + + #[cfg(unix)] + #[tokio::test] + async fn test_staged_write_applies_user_metadata_before_commit() { + let temp_dir = tempfile::TempDir::new().unwrap(); + let root = temp_dir.path().join("root"); + let staging = temp_dir.path().join("staging"); + std::fs::create_dir(&root).unwrap(); + std::fs::create_dir(&staging).unwrap(); + if !xattrs_supported(&root) { + return; + } + + let core = Arc::new(FsCore { + info: ServiceInfo::new(crate::FS_SCHEME, root.to_string_lossy(), ""), + capability: Capability::default(), + root: root.clone(), + atomic_write_dir: Some(staging.clone()), + direct_io: false, + buf_pool: oio::PooledBuf::new(1), + }); + + let expected = HashMap::from([("key".to_string(), "value".to_string())]); + let capability = Capability { + write_with_user_metadata: true, + ..Default::default() + }; + let (op_write, _) = OpWrite::from_options( + &capability, + options::WriteOptions { + user_metadata: Some(expected.clone()), + ..Default::default() + }, + ) + .unwrap(); + let writer = FsWriter::create(&core, "file", &op_write).await.unwrap(); + oio::PositionWrite::write_all_at(&writer, 0, Buffer::from("data")) + .await + .unwrap(); + + let temp_path = writer.temp_path.clone().unwrap(); + writer.update_user_metadata().unwrap(); + assert!(!root.join("file").exists()); + assert_eq!(FsCore::get_user_metadata(&temp_path).unwrap(), expected); + + writer.commit_staged_file().await.unwrap(); + assert_eq!( + FsCore::get_user_metadata(&root.join("file")).unwrap(), + expected + ); + assert!(staging_is_empty(&staging)); + } + + #[cfg(unix)] + #[tokio::test] + async fn test_staged_conditional_write_loss_preserves_winner_user_metadata() { + let temp_dir = tempfile::TempDir::new().unwrap(); + let root = temp_dir.path().join("root"); + let staging = temp_dir.path().join("staging"); + std::fs::create_dir(&root).unwrap(); + std::fs::create_dir(&staging).unwrap(); + if !xattrs_supported(&root) { + return; + } + + let op = operator(&root, Some(&staging)); + for append in [false, true] { + let path = if append { "race_append" } else { "race" }; + let mut loser = op + .writer_with(path) + .if_not_exists(true) + .append(append) + .user_metadata([("owner".to_string(), "loser".to_string())]) + .await + .unwrap(); + loser.write("loser").await.unwrap(); + + op.write_with(path, "winner") + .user_metadata([("owner".to_string(), "winner".to_string())]) + .await + .unwrap(); + + let err = loser.close().await.unwrap_err(); + assert_eq!(err.kind(), ErrorKind::ConditionNotMatch, "{err:?}"); + assert_eq!(op.read(path).await.unwrap().to_bytes(), "winner"); + assert_eq!( + FsCore::get_user_metadata(&root.join(path)).unwrap(), + HashMap::from([("owner".to_string(), "winner".to_string())]) + ); + assert!(staging_is_empty(&staging)); + } + } + + #[cfg(unix)] + #[tokio::test] + async fn test_staged_write_user_metadata_failure_does_not_publish_or_leak() { + let temp_dir = tempfile::TempDir::new().unwrap(); + let root = temp_dir.path().join("root"); + let staging = temp_dir.path().join("staging"); + std::fs::create_dir(&root).unwrap(); + std::fs::create_dir(&staging).unwrap(); + + let op = operator(&root, Some(&staging)); + // A NUL byte in the xattr key fails deterministically on all Unix + // platforms before or at setxattr without depending on filesystem + // xattr configuration. + let invalid_metadata = [("bad\0key".to_string(), "v".to_string())]; + + // New file must not be published and staged file must be removed for + // both PositionWrite::close and oio::Write::close (append + if_not_exists). + for (name, append, if_not_exists) in + [("new_file", false, false), ("new_append", true, true)] + { + let err = op + .write_with(name, "payload") + .append(append) + .if_not_exists(if_not_exists) + .user_metadata(invalid_metadata.clone()) + .await + .unwrap_err(); + assert!(!err.is_temporary(), "{err:?}"); + assert!(!root.join(name).exists()); + assert!(staging_is_empty(&staging)); + } + + // Overwriting an existing file must leave it untouched if setting + // user metadata on the staged file fails. + std::fs::write(root.join("existing"), b"original").unwrap(); + let err = op + .write_with("existing", "updated") + .user_metadata(invalid_metadata) + .await + .unwrap_err(); + assert!(!err.is_temporary(), "{err:?}"); + assert_eq!(std::fs::read(root.join("existing")).unwrap(), b"original"); + assert!(staging_is_empty(&staging)); + } } diff --git a/core/services/fs/tests/direct_io.rs b/core/services/fs/tests/direct_io.rs index 1abc1a330f..6db07f49e0 100644 --- a/core/services/fs/tests/direct_io.rs +++ b/core/services/fs/tests/direct_io.rs @@ -161,6 +161,27 @@ mod linux { assert_eq!(fs::read(path).unwrap(), b"original append"); } + #[tokio::test] + async fn tmpfs_failed_direct_io_create_leaves_no_file() { + let shm = Path::new("/dev/shm"); + let Ok(dir) = File::open(shm) else { return }; + if fstatfs(dir).unwrap().f_type as u32 != 0x0102_1994 { + return; + } + let dir = tempfile::tempdir_in(shm).unwrap(); + let full_path = dir.path().join("data"); + let op = Operator::new( + Fs::default() + .root(dir.path().to_str().unwrap()) + .direct_io(true), + ) + .unwrap(); + + let err = op.write("data", "content").await.unwrap_err(); + assert_eq!(err.kind(), ErrorKind::Unsupported); + assert!(!full_path.exists(), "failed write left a file behind"); + } + #[tokio::test] async fn tmpfs_failed_direct_io_create_new_leaves_no_file() { let shm = Path::new("/dev/shm");
