kernel, ksud: collect binaries into /data/adb/ksu (#161)

This commit is contained in:
skbeh
2023-02-01 05:58:58 -04:00
committed by GitHub
parent b7ff6b1a51
commit 46913671a8
13 changed files with 121 additions and 65 deletions

View File

@@ -1,7 +1,11 @@
use std::path::Path;
use anyhow::{ensure, Context, Result};
use anyhow::{bail, ensure, Context, Error, Ok, Result};
use retry::delay::NoDelay;
use std::{
fs::{create_dir_all, set_permissions, write, File, Permissions},
io::ErrorKind::AlreadyExists,
os::unix::prelude::PermissionsExt,
path::Path,
};
use sys_mount::{unmount, FilesystemType, Mount, UnmountFlags};
fn do_mount_image(src: &str, target: &str) -> Result<()> {
@@ -33,6 +37,52 @@ pub fn ensure_clean_dir(dir: &str) -> Result<()> {
Ok(std::fs::create_dir_all(path)?)
}
pub fn ensure_file_exists<T: AsRef<Path>>(file: T) -> Result<()> {
match File::options().write(true).create_new(true).open(&file) {
std::result::Result::Ok(_) => Ok(()),
Err(err) => {
if err.kind() == AlreadyExists && file.as_ref().is_file() {
Ok(())
} else {
Err(Error::from(err)).with_context(|| {
format!("{} is not a regular file", file.as_ref().to_str().unwrap())
})
}
}
}
}
pub fn ensure_dir_exists<T: AsRef<Path>>(dir: T) -> Result<()> {
let result = create_dir_all(&dir).map_err(Error::from);
if dir.as_ref().is_dir() {
result
} else if result.is_ok() {
bail!(
"{} is not a regular directory",
dir.as_ref().to_str().unwrap()
)
} else {
result
}
}
pub fn ensure_binary<T: AsRef<Path>>(path: T, contents: &[u8]) -> Result<()> {
if path.as_ref().exists() {
return Ok(());
}
ensure_dir_exists(path.as_ref().parent().ok_or_else(|| {
anyhow::anyhow!(
"{} does not have parent directory",
path.as_ref().to_string_lossy()
)
})?)?;
write(&path, contents)?;
set_permissions(&path, Permissions::from_mode(0o755))?;
Ok(())
}
pub fn getprop(prop: &str) -> Option<String> {
android_properties::getprop(prop).value()
}