Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions core/services/fs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,7 @@ tokio = { workspace = true, features = ["fs", "rt-multi-thread"] }

[target.'cfg(unix)'.dependencies]
xattr = "1"

[dev-dependencies]
tempfile = "3"
tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }
52 changes: 51 additions & 1 deletion core/services/fs/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,20 @@ impl FsCore {
.ensure_write_abs_path(&self.root, to.trim_end_matches('/'))
.await?;

tokio::fs::copy(from, to).await.map_err(new_std_io_error)?;
tokio::fs::copy(&from, &to)
.await
.map_err(new_std_io_error)?;

// `write_with_user_metadata` is only supported in unix platform.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// `write_with_user_metadata` is only supported in unix platform.
// only *nix supports `write_with_user_metadata`

#[cfg(unix)]
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

write with metadata is only supported on unix for now

#[cfg(unix)]
write_with_user_metadata: true,

{
if let Ok(user_meta) = Self::get_user_metadata(&from) {
if !user_meta.is_empty() {
Self::set_user_metadata(&to, &user_meta)?;
}
}
}

Ok(())
}

Expand Down Expand Up @@ -277,3 +290,40 @@ impl FsCore {
/// Using "user." as the standard namespace for user-defined attributes.
#[cfg(unix)]
const XATTR_USER_PREFIX: &str = "user.";

#[cfg(test)]
mod tests {
use super::*;

#[cfg(unix)]
#[tokio::test]
async fn test_fs_copy_preserves_user_metadata() {
let temp_dir = tempfile::TempDir::new().unwrap();
let root = temp_dir.path().to_path_buf();

let info = Arc::new(AccessorInfo::default());
let core = FsCore {
info,
root: root.clone(),
atomic_write_dir: None,
buf_pool: oio::PooledBuf::new(1),
};

let src = "src_meta.txt";
let dst = "dst_meta.txt";

let src_path = root.join(src);
let dst_path = root.join(dst);

tokio::fs::write(&src_path, b"payload").await.unwrap();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this useful?


let mut meta = HashMap::new();
meta.insert("key".to_string(), "preserved123".to_string());
FsCore::set_user_metadata(&src_path, &meta).unwrap();

core.fs_copy(src, dst).await.unwrap();

let got = FsCore::get_user_metadata(&dst_path).unwrap();
assert_eq!(got.get("key").map(String::as_str), Some("preserved123"));
}
}
Loading