test: rust 编译测试

This commit is contained in:
YuleBest committed 2026-04-10 02:55:06 +08:00
1 parent 03ea3a7ac5
commit c6a27159f4
10 files changed
+409 -37

No files matched your search

+50
View File
@@ -0,0 +1,50 @@
name: Build Android Binary
on:
push:
branches: [main]
tags: ['v*']
workflow_dispatch:
jobs:
build-android:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable
with:
targets: aarch64-linux-android
- name: Setup Android NDK
uses: nttld/setup-ndk@v1
id: ndk
with:
ndk-version: r27c
- name: Cache Rust dependencies
uses: Swatinem/rust-cache@v2
with:
workspaces: rust
- name: Install cargo-ndk
run: cargo install cargo-ndk --locked
- name: Update lockfile
working-directory: rust
run: cargo update
- name: Build
working-directory: rust
env:
ANDROID_NDK_HOME: ${{ steps.ndk.outputs.ndk-path }}
run: cargo ndk -t arm64-v8a --platform 26 build --release
- name: Upload binary
uses: actions/upload-artifact@v4
with:
name: lumipro-aarch64-linux-android
path: rust/target/aarch64-linux-android/release/lumipro
if-no-files-found: error
+3
View File
@@ -7,3 +7,6 @@ dist/
# 屏蔽 webroot 下的所有文件,但保留目录本身
webroot/*
!webroot/.gitkeep
# rust
rust/target/
+3
View File
@@ -0,0 +1,3 @@
[target.x86_64-pc-windows-msvc]
linker = "rust-lld"
rustflags = ["-C", "linker-flavor=lld-link"]
+112
View File
@@ -0,0 +1,112 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "futures-core"
version = "0.3.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7e3450815272ef58cec6d564423f6e755e25379b217b0bc688e295ba24df6b1d"
[[package]]
name = "inotify"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fdd168d97690d0b8c412d6b6c10360277f4d7ee495c5d0d5d5fe0854923255cc"
dependencies = [
"bitflags",
"futures-core",
"inotify-sys",
"libc",
"tokio",
]
[[package]]
name = "inotify-sys"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e05c02b5e89bff3b946cedeca278abc628fe811e604f027c45a8aa3cf793d0eb"
dependencies = [
"libc",
]
[[package]]
name = "libc"
version = "0.2.184"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "48f5d2a454e16a5ea0f4ced81bd44e4cfc7bd3a507b61887c99fd3538b28e4af"
[[package]]
name = "lumipro"
version = "0.1.0"
dependencies = [
"inotify",
"libc",
]
[[package]]
name = "mio"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "50b7e5b27aa02a74bac8c3f23f448f8d87ff11f92d3aac1a6ed369ee08cc56c1"
dependencies = [
"libc",
"wasi",
"windows-sys",
]
[[package]]
name = "pin-project-lite"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd"
[[package]]
name = "socket2"
version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3a766e1110788c36f4fa1c2b71b387a7815aa65f88ce0229841826633d93723e"
dependencies = [
"libc",
"windows-sys",
]
[[package]]
name = "tokio"
version = "1.51.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f66bf9585cda4b724d3e78ab34b73fb2bbaba9011b9bfdf69dc836382ea13b8c"
dependencies = [
"libc",
"mio",
"pin-project-lite",
"socket2",
"windows-sys",
]
[[package]]
name = "wasi"
version = "0.11.1+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b"
[[package]]
name = "windows-link"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"
[[package]]
name = "windows-sys"
version = "0.61.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc"
dependencies = [
"windows-link",
]
+20
View File
@@ -0,0 +1,20 @@
[package]
name = "lumipro"
version = "0.1.0"
edition = "2021"
[[bin]]
name = "lumipro"
path = "src/main.rs"
# Linux/Android 专用依赖(本地 Windows 不编译,仅 GitHub Actions 使用)
[target.'cfg(target_os = "linux")'.dependencies]
inotify = "0.10"
libc = "0.2"
[profile.release]
opt-level = "z"
lto = true
codegen-units = 1
strip = true
panic = "abort"
+205
View File
@@ -0,0 +1,205 @@
//! lumipro: 带防抖的 inotify 监听工具
//!
//! 用法: lumipro [--debounce <ms>] <handler> <path:events> [<path:events>...]
//!
//! 事件字母与 busybox inotifyd 完全兼容:
//! a=ACCESS c=CLOSE_WRITE e=CLOSE_NOWRITE w=OPEN
//! r=MOVED_FROM y=MOVED_TO n=CREATE d=DELETE
//! D=DELETE_SELF M=MOVE_SELF u=MODIFY o=ATTRIB *=ALL
//!
//! 防抖机制: 在指定窗口期内多次触发时,只执行最后一次;handler 执行期间
//! 积压的事件(如亮度渐变写入)在 handler 返回后统一丢弃。
#[cfg(target_os = "linux")]
mod linux {
use std::collections::HashMap;
use std::os::unix::io::AsRawFd;
use std::path::PathBuf;
use std::process::Command;
use std::time::{Duration, Instant};
use inotify::{EventMask, Inotify, WatchDescriptor, WatchMask};
fn letters_to_mask(letters: &str) -> WatchMask {
let mut mask = WatchMask::empty();
for c in letters.chars() {
mask |= match c {
'a' => WatchMask::ACCESS,
'c' => WatchMask::CLOSE_WRITE,
'e' => WatchMask::CLOSE_NOWRITE,
'w' => WatchMask::OPEN,
'r' => WatchMask::MOVED_FROM,
'y' => WatchMask::MOVED_TO,
'n' => WatchMask::CREATE,
'd' => WatchMask::DELETE,
'D' => WatchMask::DELETE_SELF,
'M' => WatchMask::MOVE_SELF,
'u' => WatchMask::MODIFY,
'o' => WatchMask::ATTRIB,
'*' => WatchMask::ALL_EVENTS,
_ => WatchMask::empty(),
};
}
mask
}
fn mask_to_letters(mask: EventMask) -> String {
let mut s = String::new();
if mask.contains(EventMask::ACCESS) { s.push('a'); }
if mask.contains(EventMask::CLOSE_WRITE) { s.push('c'); }
if mask.contains(EventMask::CLOSE_NOWRITE) { s.push('e'); }
if mask.contains(EventMask::OPEN) { s.push('w'); }
if mask.contains(EventMask::MOVED_FROM) { s.push('r'); }
if mask.contains(EventMask::MOVED_TO) { s.push('y'); }
if mask.contains(EventMask::CREATE) { s.push('n'); }
if mask.contains(EventMask::DELETE) { s.push('d'); }
if mask.contains(EventMask::DELETE_SELF) { s.push('D'); }
if mask.contains(EventMask::MOVE_SELF) { s.push('M'); }
if mask.contains(EventMask::MODIFY) { s.push('u'); }
if mask.contains(EventMask::ATTRIB) { s.push('o'); }
if s.is_empty() { s.push('?'); }
s
}
/// 对 inotify fd 执行 poll,返回是否有可读事件。
/// timeout_ms = -1 为永久阻塞,0 为立即返回。
fn poll_readable(fd: i32, timeout_ms: i32) -> bool {
let mut pfd = libc::pollfd {
fd,
events: libc::POLLIN,
revents: 0,
};
// SAFETY: pfd 为合法的 pollfd,nfds=1,fd 由 Inotify 保证有效。
unsafe { libc::poll(&mut pfd as *mut libc::pollfd, 1, timeout_ms) > 0 }
}
struct Pending {
triggered_at: Instant,
event_letters: String,
path: PathBuf,
}
pub fn run() {
let args: Vec<String> = std::env::args().skip(1).collect();
let mut idx = 0;
let mut debounce_ms: u64 = 300;
if args.get(idx).map(String::as_str) == Some("--debounce") {
idx += 1;
debounce_ms = args.get(idx).and_then(|s| s.parse().ok()).unwrap_or(300);
idx += 1;
}
let handler = match args.get(idx) {
Some(h) => h.clone(),
None => {
eprintln!("Usage: lumipro [--debounce <ms>] <handler> <path:events>...");
std::process::exit(1);
}
};
idx += 1;
if idx >= args.len() {
eprintln!("Error: no watch targets specified");
std::process::exit(1);
}
let mut inotify = Inotify::init().expect("inotify_init failed");
// 设置 O_NONBLOCK,使 read_events 在无数据时立即返回 WouldBlock,
// 避免 drain 循环阻塞。
let inotify_fd = inotify.as_raw_fd();
unsafe {
let flags = libc::fcntl(inotify_fd, libc::F_GETFL, 0);
libc::fcntl(inotify_fd, libc::F_SETFL, flags | libc::O_NONBLOCK);
}
let mut watches: HashMap<WatchDescriptor, (PathBuf, String)> = HashMap::new();
for spec in &args[idx..] {
let (path_str, letters) = match spec.find(':') {
Some(i) => (&spec[..i], &spec[i + 1..]),
None => (spec.as_str(), "c"),
};
let path = PathBuf::from(path_str);
let mask = letters_to_mask(letters);
match inotify.watches().add(&path, mask) {
Ok(wd) => {
watches.insert(wd, (path, letters.to_string()));
}
Err(e) => {
eprintln!("Failed to watch {path_str}: {e}");
std::process::exit(1);
}
}
}
let debounce = Duration::from_millis(debounce_ms);
let mut pending: Option<Pending> = None;
let mut buffer = [0u8; 4096];
loop {
// ── 1. 计算本次 poll 超时 ──────────────────────────────────────────
let timeout_ms: i32 = match &pending {
// 无待触发事件:永久阻塞直到新事件到来
None => -1,
Some(p) => {
let elapsed = p.triggered_at.elapsed();
if elapsed >= debounce {
0 // 防抖窗口已过期,跳过 poll 直接触发
} else {
(debounce - elapsed).as_millis().min(i32::MAX as u128) as i32
}
}
};
// timeout_ms == 0 时跳过 poll(防抖已到期),直接进入触发分支
let has_data = timeout_ms != 0 && poll_readable(inotify_fd, timeout_ms);
if has_data {
// ── 2. 有新事件:读取并重置防抖计时器 ───────────────────────
match inotify.read_events(&mut buffer) {
Ok(events) => {
for event in events {
if let Some((path, _)) = watches.get(&event.wd) {
pending = Some(Pending {
triggered_at: Instant::now(),
event_letters: mask_to_letters(event.mask),
path: path.clone(),
});
}
}
}
Err(ref e) if e.kind() == std::io::ErrorKind::WouldBlock => {}
Err(e) => eprintln!("read_events: {e}"),
}
} else if let Some(p) = pending.take() {
// ── 3. 防抖窗口结束:同步执行 handler ───────────────────────
let _ = Command::new(&handler)
.arg(&p.event_letters)
.arg(&p.path)
.status();
// ── 4. 丢弃 handler 执行期间(亮度渐变写入)积压的全部事件 ─
loop {
match inotify.read_events(&mut buffer) {
Ok(events) => {
for _ in events {}
}
Err(_) => break, // WouldBlock:积压已清空
}
}
}
}
}
}
fn main() {
#[cfg(target_os = "linux")]
linux::run();
#[cfg(not(target_os = "linux"))]
{
eprintln!("lumipro: only supported on Linux/Android");
std::process::exit(1);
}
}
+5 -5
View File
@@ -74,20 +74,20 @@ while true; do
echo "$current_config" >"$config_cache_file"
fi
_log "正在启动 inotifyd 监听" "INFO"
_log "正在启动 lumipro 监听" "INFO"
now_bri_file="${current_config%|*}"
inotify_events="${current_config#*|}"
# 缓存当前监听路径(供 up.sh 参考)
# 缓存当前监听路径(供参考)
echo -n "$now_bri_file" >"$MODDIR/config/.cached_path"
inotifyd "$MODDIR/script/up.sh" "$now_bri_file:$inotify_events" >>"$log_file" 2>&1 &
"$MODDIR/bin/lumipro" "$MODDIR/script/up.sh" "$now_bri_file:$inotify_events" >>"$log_file" 2>&1 &
inotify_pid=$!
echo "$inotify_pid" >"$pid_file"
_log "inotifyd 已启动 (PID: $inotify_pid), 监听: $now_bri_file, 事件: $inotify_events" "SUCCESS"
_log "lumipro 已启动 (PID: $inotify_pid), 监听: $now_bri_file, 事件: $inotify_events" "SUCCESS"
wait "$inotify_pid"
_log "监听进程 (PID: $inotify_pid) 已退出,即将重启" "WARN"
_log "lumipro 进程 (PID: $inotify_pid) 已退出,即将重启" "WARN"
sleep 0.5
done
+3 -6
View File
@@ -15,13 +15,10 @@ _log "收到手动重启请求"
pause_file="$PID_DIR/daemon.pause"
# 1. 强制通知守护进程并执行清理
_log "通知守护进程挂起,清理残余进程"
# 1. 通知守护进程挂起,终止 lumipro 实例
_log "通知守护进程挂起,清理 lumipro 实例"
touch "$pause_file"
killall -9 inotifyd 2>/dev/null
for p in $(pgrep -f "up.sh"); do
[ "$p" != "$$" ] && kill -9 "$p" 2>/dev/null
done
[ -f "$pid_file" ] && kill -9 "$(cat "$pid_file")" 2>/dev/null
# 2. 清理遗留标记
rm -f "$pid_file"
+7 -26
View File
@@ -1,4 +1,4 @@
#!/system/bin/sh
#!/system/bin/sh
#shellcheck shell=ash
# 亮度提升脚本
@@ -45,23 +45,13 @@ _log() {
printf '[%s] [%s] [%s] %s\n' "$(date '+%m-%d %H:%M:%S')" "up" "$level" "$1" >>"$log_file"
}
_log "收到 inotifyd 事件通知 (PID: $$)" "INFO"
_log "正在接管控制权,清理残余进程..." "INFO"
touch "$pause_file"
killall -9 inotifyd 2>/dev/null
for p in $(pgrep -f "up.sh"); do
[ "$p" != "$$" ] && kill -9 "$p" 2>/dev/null
done
_log "收到亮度变更通知 (PID: $$)" "INFO"
if [ -f "$stop_file" ]; then
_log "服务已暂停,跳过本次处理" "WARN"
exit 0
fi
if [ -f "$flag_file" ]; then
_log "检测到并发执行,当前进程退出" "WARN"
exit 1
fi
touch "$flag_file"
# 读取配置
@@ -133,7 +123,7 @@ CHECK_BRI() {
MAIN() {
if IS_SLEEP_TIME; then
_log "处于休眠时段 ($sleep_start-$sleep_end),跳过提升" "INFO"
rm -f "$flag_file" "$pause_file"
rm -f "$flag_file"
return
fi
@@ -142,7 +132,7 @@ MAIN() {
mode="$(settings get system screen_brightness_mode 2>/dev/null)"
if [ "$mode" = "1" ]; then
_log "自动亮度已启用,跳过提升" "INFO"
rm -f "$flag_file" "$pause_file"
rm -f "$flag_file"
return
fi
fi
@@ -159,7 +149,7 @@ MAIN() {
'.blacklist_apps | map(. == $f or . == $a) | any' \
"$CONFIG_FILE" >/dev/null 2>&1; then
_log "当前前台 ($current_focus) 在黑名单中,跳过提升" "INFO"
rm -f "$flag_file" "$pause_file"
rm -f "$flag_file"
return
fi
fi
@@ -173,7 +163,7 @@ MAIN() {
now="$(date +%s)"
if awk "BEGIN{exit !(($now - $flag_time) < 2)}" 2>/dev/null; then
_log "HDR 冷却期内,跳过提升" "INFO"
rm -f "$flag_file" "$pause_file"
rm -f "$flag_file"
return
else
rm -f "$hdr_flag_file"
@@ -194,7 +184,7 @@ MAIN() {
if awk "BEGIN{exit !($hdr_ratio_rounded > 1.00)}" 2>/dev/null; then
_log "检测到 HDR 内容 (比率: $hdr_ratio_rounded),跳过提升" "INFO"
date +%s >"$hdr_flag_file"
rm -f "$flag_file" "$pause_file"
rm -f "$flag_file"
return
fi
fi
@@ -203,16 +193,7 @@ MAIN() {
_log "正在检测亮度变化..." "INFO"
CHECK_BRI
rm -f "$flag_file"
rm -f "$pause_file"
# 如果监听路径变更,重启 inotifyd
local current_now_bri cached_now_bri
current_now_bri="$(get_cfg now_bri_file "$DEFAULT_NOW_BRI_FILE")"
cached_now_bri="$(cat "$MODDIR/config/.cached_path" 2>/dev/null || echo "$DEFAULT_NOW_BRI_FILE")"
if [ "$current_now_bri" != "$cached_now_bri" ]; then
_log "检测到监听路径变更,重启 inotifyd" "INFO"
killall -9 inotifyd 2>/dev/null
fi
}
MAIN
+1
View File
@@ -43,6 +43,7 @@ _log "最大亮度节点: $max_bri_file" "INFO"
_log "正在启动守护进程" "INFO"
chmod 755 "$MODDIR/script/up.sh"
chmod 755 "$MODDIR/script/daemon.sh"
chmod 755 "$MODDIR/bin/lumipro"
sh "$MODDIR/script/daemon.sh" &
_log "守护进程已启动 (PID: $!)" "SUCCESS"