#!/system/bin/sh # Driver Manager v2 - Core Utilities # Shared functions for all scripts MODDIR=${MODDIR:-/data/adb/modules/driver-manager} CONFIGDIR="$MODDIR/config" LOGDIR="$MODDIR/logs" RUNDIR="$MODDIR/run" # --- Logging --- _LOGFILE="" log_init() { _LOGFILE="$1" mkdir -p "$(dirname "$_LOGFILE")" 2>/dev/null } log() { local msg="[$(date '+%H:%M:%S')] $1" [ -n "$_LOGFILE" ] && echo "$msg" >> "$_LOGFILE" } log_error() { log "ERROR: $1"; } # --- JSON helpers (minimal, no jq dependency) --- # Read a string value from a JSON file json_get() { local file="$1" key="$2" grep -o "\"$key\"[[:space:]]*:[[:space:]]*\"[^\"]*\"" "$file" 2>/dev/null | \ head -1 | sed 's/.*:[[:space:]]*"\([^"]*\)"/\1/' } # Read a number/bool value from a JSON file json_get_raw() { local file="$1" key="$2" grep -o "\"$key\"[[:space:]]*:[[:space:]]*[^,}]*" "$file" 2>/dev/null | \ head -1 | sed 's/.*:[[:space:]]*//' | tr -d ' ' } # Set a string value in a JSON file json_set() { local file="$1" key="$2" value="$3" if grep -q "\"$key\"" "$file" 2>/dev/null; then sed -i "s|\"$key\"[[:space:]]*:[[:space:]]*\"[^\"]*\"|\"$key\": \"$value\"|" "$file" fi } # --- File helpers --- sha256() { sha256sum "$1" 2>/dev/null | cut -d' ' -f1 } get_selinux_context() { ls -Z "$1" 2>/dev/null | awk '{print $1}' } file_size() { stat -c%s "$1" 2>/dev/null || wc -c < "$1" 2>/dev/null } # --- Process helpers --- pid_of() { pidof "$1" 2>/dev/null || pgrep -f "$1" 2>/dev/null } is_running() { [ -f "$RUNDIR/$1.pid" ] && kill -0 $(cat "$RUNDIR/$1.pid") 2>/dev/null } kill_by_pidfile() { local pidfile="$RUNDIR/$1.pid" if [ -f "$pidfile" ]; then kill $(cat "$pidfile") 2>/dev/null rm -f "$pidfile" fi } # --- Settings helpers --- get_setting() { json_get "$CONFIGDIR/settings.json" "$1" } get_setting_raw() { json_get_raw "$CONFIGDIR/settings.json" "$1" }