1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

test: add a function to compare the xattr between two files.

used by cp & mv (at least)
This commit is contained in:
Sylvestre Ledru 2024-01-14 00:38:17 +01:00
parent fe3f8293ef
commit 238fb776ad

View file

@ -756,6 +756,26 @@ pub fn get_root_path() -> &'static str {
}
}
/// Compares the extended attributes (xattrs) of two files or directories.
///
/// # Returns
///
/// `true` if both paths have the same set of extended attributes, `false` otherwise.
#[cfg(all(unix, not(target_os = "macos")))]
pub fn compare_xattrs<P: AsRef<std::path::Path>>(path1: P, path2: P) -> bool {
let get_sorted_xattrs = |path: P| {
xattr::list(path)
.map(|attrs| {
let mut attrs = attrs.collect::<Vec<_>>();
attrs.sort();
attrs
})
.unwrap_or_else(|_| Vec::new())
};
get_sorted_xattrs(path1) == get_sorted_xattrs(path2)
}
/// Object-oriented path struct that represents and operates on
/// paths relative to the directory it was constructed for.
#[derive(Clone)]
@ -3375,4 +3395,26 @@ mod tests {
);
assert!(command.tmpd.is_some());
}
#[cfg(all(unix, not(target_os = "macos")))]
#[test]
fn test_compare_xattrs() {
use tempfile::tempdir;
let temp_dir = tempdir().unwrap();
let file_path1 = temp_dir.path().join("test_file1.txt");
let file_path2 = temp_dir.path().join("test_file2.txt");
File::create(&file_path1).unwrap();
File::create(&file_path2).unwrap();
let test_attr = "user.test_attr";
let test_value = b"test value";
xattr::set(&file_path1, test_attr, test_value).unwrap();
assert!(!compare_xattrs(&file_path1, &file_path2));
xattr::set(&file_path2, test_attr, test_value).unwrap();
assert!(compare_xattrs(&file_path1, &file_path2));
}
}