-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rsjudge-runner): ✨ make the program capability-aware
- Loading branch information
1 parent
392382f
commit c4ee29c
Showing
2 changed files
with
24 additions
and
19 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 |
---|---|---|
@@ -1,27 +1,32 @@ | ||
use caps::{errors::CapsError, has_cap, Capability}; | ||
use caps::{has_cap, raise, Capability}; | ||
|
||
use crate::error::{Error, Result}; | ||
|
||
fn raise_cap(cap: Capability) -> Result<bool> { | ||
if has_cap(None, caps::CapSet::Permitted, cap)? { | ||
raise(None, caps::CapSet::Effective, cap)?; | ||
Ok(true) | ||
} else { | ||
Ok(false) | ||
} | ||
} | ||
|
||
pub fn require_caps<I>(caps: I) -> Result<()> | ||
where | ||
I: IntoIterator<Item = Capability>, | ||
{ | ||
let missing_caps = caps | ||
.into_iter() | ||
.map(|cap| match has_cap(None, caps::CapSet::Effective, cap) { | ||
Ok(has_cap) => Ok((!has_cap).then_some(cap)), | ||
Err(e) => Err(e), | ||
}) | ||
.collect::<Result<Vec<_>, CapsError>>()? | ||
.into_iter() | ||
.flatten() | ||
.collect::<Vec<_>>(); | ||
let missing_caps = caps.into_iter().try_fold(vec![], |s, cap| { | ||
let mut v: Vec<_> = s; | ||
if !raise_cap(cap)? { | ||
v.push(cap); | ||
} | ||
Ok::<_, Error>(v) | ||
})?; | ||
|
||
if missing_caps.is_empty() { | ||
Ok(()) | ||
} else { | ||
if !missing_caps.is_empty() { | ||
Err(Error::CapsRequired { | ||
caps: missing_caps.into_boxed_slice(), | ||
}) | ||
})? | ||
} | ||
Ok(()) | ||
} |