-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
mv: preserve the xattr #5835
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
mv: preserve the xattr #5835
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fe3f829
uucore: add a new feature called fsxattr
sylvestre 238fb77
test: add a function to compare the xattr between two files.
sylvestre 2ec4e9f
mv: preserve the xattr
sylvestre 66637a6
move the file_has_acl function into uucore
sylvestre 3872aca
spell: ignore getxattr
sylvestre 69c8753
cp test: use compare_xattrs from tests/utils
sylvestre File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
// This file is part of the uutils coreutils package. | ||
// | ||
// For the full copyright and license information, please view the LICENSE | ||
// file that was distributed with this source code. | ||
|
||
// spell-checker:ignore getxattr | ||
|
||
//! Set of functions to manage xattr on files and dirs | ||
use std::collections::HashMap; | ||
use std::ffi::OsString; | ||
use std::path::Path; | ||
|
||
/// Copies extended attributes (xattrs) from one file or directory to another. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `source` - A reference to the source path. | ||
/// * `dest` - A reference to the destination path. | ||
/// | ||
/// # Returns | ||
/// | ||
/// A result indicating success or failure. | ||
pub fn copy_xattrs<P: AsRef<Path>>(source: P, dest: P) -> std::io::Result<()> { | ||
for attr_name in xattr::list(&source)? { | ||
if let Some(value) = xattr::get(&source, &attr_name)? { | ||
xattr::set(&dest, &attr_name, &value)?; | ||
} | ||
} | ||
Ok(()) | ||
} | ||
|
||
/// Retrieves the extended attributes (xattrs) of a given file or directory. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `source` - A reference to the path of the file or directory. | ||
/// | ||
/// # Returns | ||
/// | ||
/// A result containing a HashMap of attributes names and values, or an error. | ||
pub fn retrieve_xattrs<P: AsRef<Path>>(source: P) -> std::io::Result<HashMap<OsString, Vec<u8>>> { | ||
let mut attrs = HashMap::new(); | ||
for attr_name in xattr::list(&source)? { | ||
if let Some(value) = xattr::get(&source, &attr_name)? { | ||
attrs.insert(attr_name, value); | ||
} | ||
} | ||
Ok(attrs) | ||
} | ||
|
||
/// Applies extended attributes (xattrs) to a given file or directory. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `dest` - A reference to the path of the file or directory. | ||
/// * `xattrs` - A HashMap containing attribute names and their corresponding values. | ||
/// | ||
/// # Returns | ||
/// | ||
/// A result indicating success or failure. | ||
pub fn apply_xattrs<P: AsRef<Path>>( | ||
dest: P, | ||
xattrs: HashMap<OsString, Vec<u8>>, | ||
) -> std::io::Result<()> { | ||
for (attr, value) in xattrs { | ||
xattr::set(&dest, &attr, &value)?; | ||
} | ||
Ok(()) | ||
} | ||
|
||
/// Checks if a file has an Access Control List (ACL) based on its extended attributes. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `file` - A reference to the path of the file. | ||
/// | ||
/// # Returns | ||
/// | ||
/// `true` if the file has extended attributes (indicating an ACL), `false` otherwise. | ||
pub fn has_acl<P: AsRef<Path>>(file: P) -> bool { | ||
// don't use exacl here, it is doing more getxattr call then needed | ||
match xattr::list(file) { | ||
Ok(acl) => { | ||
// if we have extra attributes, we have an acl | ||
acl.count() > 0 | ||
} | ||
Err(_) => false, | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use std::fs::File; | ||
use tempfile::tempdir; | ||
|
||
#[test] | ||
fn test_copy_xattrs() { | ||
let temp_dir = tempdir().unwrap(); | ||
let source_path = temp_dir.path().join("source.txt"); | ||
let dest_path = temp_dir.path().join("dest.txt"); | ||
|
||
File::create(&source_path).unwrap(); | ||
File::create(&dest_path).unwrap(); | ||
|
||
let test_attr = "user.test"; | ||
let test_value = b"test value"; | ||
xattr::set(&source_path, test_attr, test_value).unwrap(); | ||
|
||
copy_xattrs(&source_path, &dest_path).unwrap(); | ||
|
||
let copied_value = xattr::get(&dest_path, test_attr).unwrap().unwrap(); | ||
assert_eq!(copied_value, test_value); | ||
} | ||
|
||
#[test] | ||
fn test_apply_and_retrieve_xattrs() { | ||
let temp_dir = tempdir().unwrap(); | ||
let file_path = temp_dir.path().join("test_file.txt"); | ||
|
||
File::create(&file_path).unwrap(); | ||
|
||
let mut test_xattrs = HashMap::new(); | ||
let test_attr = "user.test_attr"; | ||
let test_value = b"test value"; | ||
test_xattrs.insert(OsString::from(test_attr), test_value.to_vec()); | ||
apply_xattrs(&file_path, test_xattrs).unwrap(); | ||
|
||
let retrieved_xattrs = retrieve_xattrs(&file_path).unwrap(); | ||
assert!(retrieved_xattrs.contains_key(OsString::from(test_attr).as_os_str())); | ||
assert_eq!( | ||
retrieved_xattrs | ||
.get(OsString::from(test_attr).as_os_str()) | ||
.unwrap(), | ||
test_value | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_file_has_acl() { | ||
let temp_dir = tempdir().unwrap(); | ||
let file_path = temp_dir.path().join("test_file.txt"); | ||
|
||
File::create(&file_path).unwrap(); | ||
|
||
assert!(!has_acl(&file_path)); | ||
|
||
let test_attr = "user.test_acl"; | ||
let test_value = b"test value"; | ||
xattr::set(&file_path, test_attr, test_value).unwrap(); | ||
|
||
assert!(has_acl(&file_path)); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there some reason why
xattrs
is set here and not after the followingif
-block? Otherwise you could put this snippet, and the one on line 652, into one block and save onecfg
(and show that they belong together).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, I would have to duplicate the declaration as the xattr detection needs to happen with and without the progress bar
and you can't do at the same time because "from" gets removed/moved :)
so, to rephrase