Migrate prune_su_access to Rust

This commit is contained in:
topjohnwu
2025-01-10 09:04:56 +08:00
committed by John Wu
parent 5637a258fc
commit 4d4195c02d
12 changed files with 104 additions and 88 deletions

View File

@@ -1,4 +1,4 @@
use crate::daemon::MagiskD;
use crate::daemon::{to_app_id, MagiskD, AID_APP_END, AID_APP_START};
use crate::db::DbArg::Integer;
use crate::db::{SqlTable, SqliteResult, SqliteReturn};
use crate::ffi::{DbValues, RootSettings, SuPolicy};
@@ -35,6 +35,14 @@ impl SqlTable for RootSettings {
}
}
struct UidList(Vec<i32>);
impl SqlTable for UidList {
fn on_row(&mut self, _: &[String], values: &DbValues) {
self.0.push(values.get_int(0));
}
}
impl MagiskD {
fn get_root_settings(&self, uid: i32, settings: &mut RootSettings) -> SqliteResult {
self.db_exec_with_rows(
@@ -49,6 +57,36 @@ impl MagiskD {
pub fn get_root_settings_for_cxx(&self, uid: i32, settings: &mut RootSettings) -> bool {
self.get_root_settings(uid, settings).log().is_ok()
}
pub fn prune_su_access(&self) {
let mut list = UidList(Vec::new());
if self
.db_exec_with_rows("SELECT uid FROM policies", &[], &mut list)
.sql_result()
.log()
.is_err()
{
return;
}
let app_list = self.get_app_no_list();
let mut rm_uids = Vec::new();
for uid in list.0 {
let app_id = to_app_id(uid);
if app_id >= AID_APP_START && app_id <= AID_APP_END {
let app_no = app_id - AID_APP_START;
if !app_list.contains(app_no as usize) {
// The app_id is no longer installed
rm_uids.push(uid);
}
}
}
for uid in rm_uids {
self.db_exec("DELETE FROM policies WHERE uid=?", &[Integer(uid as i64)]);
}
}
}
pub fn get_default_root_settings() -> RootSettings {