feat(rust): 批次 A 只读批量 IO(stat_many / read_text_many / list_dir_entries)
Verify (Gitea) / gate (build · unit · facts) (push) Failing after 1m31s
Verify (Gitea) / gate (build · unit · facts) (push) Failing after 1m31s
This commit is contained in:
1 parent
317abf16c0
commit
fff0a643f2
5 files changed
+224
-29
No files matched your search
+110
-1
@@ -313,6 +313,112 @@ fn cancel_fetch(req_id: String) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
|
||||
// 2026-12-05(Rust 化批次 A):只读批量 IO —— 把「逐文件一次 IPC」收敛成一次调用。
|
||||
// 安全:所有路径都相对 base(资料目录),拒绝绝对路径与 ..,解析后必须仍在 base 内。
|
||||
fn resolve_under(base: &std::path::Path, rel: &str) -> Option<std::path::PathBuf> {
|
||||
if rel.is_empty() || rel.contains('\0') {
|
||||
return None;
|
||||
}
|
||||
let p = std::path::Path::new(rel);
|
||||
if p.is_absolute() {
|
||||
return None;
|
||||
}
|
||||
use std::path::Component;
|
||||
for c in p.components() {
|
||||
match c {
|
||||
Component::ParentDir | Component::RootDir | Component::Prefix(_) => return None,
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
let joined = base.join(p);
|
||||
if joined.starts_with(base) {
|
||||
Some(joined)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(serde::Serialize)]
|
||||
struct PathStat {
|
||||
rel: String,
|
||||
exists: bool,
|
||||
size: u64,
|
||||
mtime: u64,
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn stat_many(base: String, rels: Vec<String>) -> Result<Vec<PathStat>, String> {
|
||||
let base_path = std::path::PathBuf::from(&base);
|
||||
let mut out = Vec::with_capacity(rels.len());
|
||||
for rel in rels {
|
||||
let mut item = PathStat { rel: rel.clone(), exists: false, size: 0, mtime: 0 };
|
||||
if let Some(abs) = resolve_under(&base_path, &rel) {
|
||||
if let Ok(meta) = tokio::fs::metadata(&abs).await {
|
||||
item.exists = true;
|
||||
item.size = meta.len();
|
||||
item.mtime = meta
|
||||
.modified()
|
||||
.ok()
|
||||
.and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
|
||||
.map(|d| d.as_millis() as u64)
|
||||
.unwrap_or(0);
|
||||
}
|
||||
}
|
||||
out.push(item);
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
#[derive(serde::Serialize)]
|
||||
struct TextRead {
|
||||
rel: String,
|
||||
text: Option<String>,
|
||||
error: Option<String>,
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn read_text_many(base: String, rels: Vec<String>) -> Result<Vec<TextRead>, String> {
|
||||
let base_path = std::path::PathBuf::from(&base);
|
||||
let mut out = Vec::with_capacity(rels.len());
|
||||
for rel in rels {
|
||||
let mut item = TextRead { rel: rel.clone(), text: None, error: None };
|
||||
match resolve_under(&base_path, &rel) {
|
||||
Some(abs) => match tokio::fs::read_to_string(&abs).await {
|
||||
Ok(t) => item.text = Some(t),
|
||||
Err(e) => item.error = Some(e.to_string()),
|
||||
},
|
||||
None => item.error = Some("bad-path".to_string()),
|
||||
}
|
||||
out.push(item);
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
#[derive(serde::Serialize)]
|
||||
struct DirEntryInfo {
|
||||
name: String,
|
||||
is_dir: bool,
|
||||
size: u64,
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn list_dir_entries(base: String, rel: String) -> Result<Vec<DirEntryInfo>, String> {
|
||||
let base_path = std::path::PathBuf::from(&base);
|
||||
let abs = resolve_under(&base_path, &rel).ok_or_else(|| "bad-path".to_string())?;
|
||||
let mut rd = tokio::fs::read_dir(&abs).await.map_err(|e| e.to_string())?;
|
||||
let mut out = Vec::new();
|
||||
while let Some(entry) = rd.next_entry().await.map_err(|e| e.to_string())? {
|
||||
let name = entry.file_name().to_string_lossy().to_string();
|
||||
let meta = entry.metadata().await.ok();
|
||||
let is_dir = meta.as_ref().map(|m| m.is_dir()).unwrap_or(false);
|
||||
let size = meta.map(|m| m.len()).unwrap_or(0);
|
||||
out.push(DirEntryInfo { name, is_dir, size });
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
|
||||
// 2026-12-04(文件版下载,默认关闭):流式写文件,避免整包 base64 过 IPC。
|
||||
// 目标路径由 JS 提供(资料目录下的 cache/tmp/),这里只负责抓字节 + 落盘;失败会删半成品。
|
||||
#[tauri::command]
|
||||
@@ -2186,7 +2292,10 @@ pub fn run() {
|
||||
img_thumbs,
|
||||
fetch_image_b64,
|
||||
fetch_image_to,
|
||||
cancel_fetch
|
||||
cancel_fetch,
|
||||
stat_many,
|
||||
read_text_many,
|
||||
list_dir_entries,
|
||||
])
|
||||
.setup(|app| {
|
||||
if cfg!(debug_assertions) {
|
||||
|
||||
Reference in new issue
Block a user