ksud: probe for more workdir candidates (#12)

- reuses old ksu functions
- makes sure its an empty dir
- adapted from https://github.com/rsuntk/KernelSU/commit/141f010 71cb86c2e9

Co-authored-by: powellnorma <101364699+powellnorma@users.noreply.github.com>
Co-authored-by: Rissu <90097027+rsuntk@users.noreply.github.com>
This commit is contained in:
backslashxx
2025-03-09 17:00:03 +08:00
committed by GitHub
parent 9ff996b805
commit b28789ac7a
4 changed files with 38 additions and 8 deletions

View File

@@ -1,6 +1,6 @@
use anyhow::{Context, Error, Ok, Result, bail};
use std::{
fs::{File, OpenOptions, create_dir_all, remove_file, write},
fs::{self, File, OpenOptions, create_dir_all, remove_file, write},
io::{
ErrorKind::{AlreadyExists, NotFound},
Write,
@@ -177,6 +177,38 @@ pub fn has_magisk() -> bool {
which::which("magisk").is_ok()
}
fn is_ok_empty(dir: &str) -> bool {
use std::result::Result::Ok;
match fs::read_dir(dir) {
Ok(mut entries) => entries.next().is_none(),
Err(_) => false,
}
}
pub fn get_tmp_path() -> String {
let dirs = [
"/debug_ramdisk",
"/patch_hw",
"/oem",
"/root",
"/sbin",
];
// find empty directory
for dir in dirs {
if is_ok_empty(dir) {
return dir.to_string();
}
}
"".to_string()
}
pub fn get_work_dir() -> String {
let tmp_path = get_tmp_path();
format!("{}/workdir/", tmp_path)
}
#[cfg(target_os = "android")]
fn link_ksud_to_bin() -> Result<()> {
let ksu_bin = PathBuf::from(defs::DAEMON_PATH);