Skip to content

Commit

Permalink
fix_xattr
Browse files Browse the repository at this point in the history
Signed-off-by: Hiroyuki Moriya <[email protected]>
  • Loading branch information
Gekko0114 committed Sep 28, 2024
1 parent e0d53a8 commit 3d2d34a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 18 deletions.
4 changes: 2 additions & 2 deletions experiment/selinux/src/selinux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ mod tests {

fn create_temp_file(content: &[u8], file_name: &str) {
let path = Path::new(file_name);
let mut file = File::create(&path).expect("Failed to create file");
let mut file = File::create(path).expect("Failed to create file");
file.write_all(content).expect("Failed to write to file");
file.sync_all().expect("Failed to sync file");
}
Expand Down Expand Up @@ -570,7 +570,7 @@ mod tests {
let expected = PathBuf::from(expected_array[i]);
match SELinux::check_line_include_selinux_fs_mount_point(input) {
Some(output) => assert_eq!(expected, output),
None => assert_eq!(succeeded_array[i], false),
None => assert!(!succeeded_array[i]),
}
}
}
Expand Down
65 changes: 49 additions & 16 deletions experiment/selinux/src/tools/xattr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,55 @@ where
// set_xattr sets extended attributes on a file specified by its path.
fn set_xattr(&self, attr: &str, data: &[u8]) -> Result<(), XattrError> {
let path = self.as_ref();
match rfs::setxattr(path, attr, data, rfs::XattrFlags::REPLACE) {
Ok(_) => Ok(()),
Err(e) => {
let errno = e.raw_os_error();
if errno == libc::EINTR {
return Err(XattrError::EINTR(errno));
match path.get_xattr(attr) {
Ok(_) => match rfs::setxattr(path, attr, data, rfs::XattrFlags::REPLACE) {
Ok(_) => Ok(()),
Err(e) => {
let errno = e.raw_os_error();
if errno == libc::EINTR {
return Err(XattrError::EINTR(errno));
}
Err(XattrError::SetXattr(e.to_string()))
}
Err(XattrError::SetXattr(e.to_string()))
}
},
Err(_) => match rfs::setxattr(path, attr, data, rfs::XattrFlags::CREATE) {
Ok(_) => Ok(()),
Err(e) => {
let errno = e.raw_os_error();
if errno == libc::EINTR {
return Err(XattrError::EINTR(errno));
}
Err(XattrError::SetXattr(e.to_string()))
}
},
}
}

// function similar with lsetxattr in golang.org/x/sys/unix repo.
// lset_xattr sets extended attributes on a symbolic link.
fn lset_xattr(&self, attr: &str, data: &[u8]) -> Result<(), XattrError> {
let path = self.as_ref();
match rfs::lsetxattr(path, attr, data, rfs::XattrFlags::REPLACE) {
Ok(_) => Ok(()),
Err(e) => {
let errno = e.raw_os_error();
if errno == libc::EINTR {
return Err(XattrError::EINTR(errno));
match path.lget_xattr(attr) {
Ok(_) => match rfs::lsetxattr(path, attr, data, rfs::XattrFlags::REPLACE) {
Ok(_) => Ok(()),
Err(e) => {
let errno = e.raw_os_error();
if errno == libc::EINTR {
return Err(XattrError::EINTR(errno));
}
Err(XattrError::LSetXattr(e.to_string()))
}
Err(XattrError::LSetXattr(e.to_string()))
}
},
Err(_) => match rfs::lsetxattr(path, attr, data, rfs::XattrFlags::CREATE) {
Ok(_) => Ok(()),
Err(e) => {
let errno = e.raw_os_error();
if errno == libc::EINTR {
return Err(XattrError::EINTR(errno));
}
Err(XattrError::LSetXattr(e.to_string()))
}
},
}
}

Expand Down Expand Up @@ -128,6 +152,15 @@ mod tests {
let temp_file = NamedTempFile::new().expect("Failed to create temp file");
let file_path = temp_file.path();

// Verify that the first "set_xattr" operation succeeds, which means it doesn't have xattr yet.
file_path
.set_xattr(attr_name, attr_value.as_bytes())
.expect("Failed to set xattr");
let actual = file_path.get_xattr(attr_name).expect("Failed to get xattr");
assert_eq!(actual, attr_value);

// Verify that the second "set_xattr" operation succeds, which means it already has xattr.

Check warning on line 162 in experiment/selinux/src/tools/xattr.rs

View workflow job for this annotation

GitHub Actions / check (x86_64, musl)

"succeds" should be "succeeds".

Check failure on line 162 in experiment/selinux/src/tools/xattr.rs

View workflow job for this annotation

GitHub Actions / check (x86_64, musl)

`succeds` should be `succeeds`
let attr_value = "system_u:object_r:another_label_t";
file_path
.set_xattr(attr_name, attr_value.as_bytes())
.expect("Failed to set xattr");
Expand Down

0 comments on commit 3d2d34a

Please sign in to comment.