ksud_overlayfs: ability to specify custom sparse image size

This commit is contained in:
rifsxd
2025-03-23 23:15:47 +06:00
parent 30835787c9
commit de189fe426

View File

@@ -13,6 +13,8 @@ use java_properties::PropertiesIter;
use log::{info, warn};
use std::fs::OpenOptions;
use std::fs;
use std::io;
use std::{
collections::HashMap,
env::var as env_var,
@@ -376,7 +378,39 @@ fn _install_module(zip: &str) -> Result<()> {
humansize::format_size(zip_uncompressed_size, humansize::DECIMAL)
);
let sparse_image_size = 6 << 30; // 6GB
fn parse_size(size_str: &str) -> io::Result<u64> {
let size_str = size_str.trim();
if size_str.is_empty() {
return Err(io::Error::new(io::ErrorKind::InvalidData, "Empty size string"));
}
let (num, unit) = size_str.split_at(size_str.len().saturating_sub(1));
let num = u64::from_str(num.trim()).map_err(|_| io::Error::new(io::ErrorKind::InvalidData, "Invalid number"))?;
let multiplier = match unit.to_ascii_uppercase().as_str() {
"G" => 1 << 30,
"M" => 1 << 20,
"K" => 1 << 10,
_ => 1, // Default to bytes if no unit is specified
};
Ok(num * multiplier)
}
fn get_sparse_image_size() -> u64 {
let default_size = 6 << 30; // 6GB
let custom_size_file = "/data/adb/ksu/custom_sparse_size.txt";
match fs::read_to_string(custom_size_file) {
Ok(contents) => match parse_size(&contents) {
Ok(size) => size,
Err(_) => default_size, // Fallback on error
},
Err(_) => default_size, // Fallback if file is missing
}
}
let sparse_image_size = get_sparse_image_size();
if !modules_img_exist && !modules_update_img_exist {
// if no modules and modules_update, it is brand new installation, we should create a new img
// create a tmp module img and mount it to modules_update