Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
Signed-off-by: Hiroyuki Moriya <[email protected]>
  • Loading branch information
Gekko0114 committed Sep 29, 2024
1 parent df77c11 commit 1ee2515
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 43 deletions.
2 changes: 1 addition & 1 deletion experiment/selinux/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn main() -> Result<(), SELinuxError> {
}

let file_path = Path::new("./test_file.txt");
let _file = File::create(file_path).map_err(|e| SELinuxError::Run(e.to_string()))?;
let _file = File::create(file_path).unwrap();
let selinux_label =
SELinuxLabel::try_from("system_u:object_r:public_content_t:s0".to_string())?;
SELinux::set_file_label(file_path, selinux_label)?;
Expand Down
2 changes: 0 additions & 2 deletions experiment/selinux/src/selinux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,6 @@ pub enum SELinuxError {
GetConfigKey(String),
#[error("Invalid format for SELinux label: {0}")]
InvalidSELinuxLabel(String),
#[error("Failed to run: {0}")]
Run(String),
}

pub struct SELinux {
Expand Down
64 changes: 24 additions & 40 deletions experiment/selinux/src/tools/xattr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,55 +34,39 @@ 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 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(_) => 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()))
let op = match path.get_xattr(attr) {
Ok(_) => rfs::XattrFlags::REPLACE,
Err(_) => rfs::XattrFlags::CREATE,
};
match rfs::setxattr(path, attr, data, op) {
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 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(_) => 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()))
let op = match path.lget_xattr(attr) {
Ok(_) => rfs::XattrFlags::REPLACE,
Err(_) => rfs::XattrFlags::CREATE,
};
match rfs::lsetxattr(path, attr, data, op) {
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

0 comments on commit 1ee2515

Please sign in to comment.