处理0字节临时文件

This commit is contained in:
zqm
2026-04-10 09:27:54 +08:00
parent e79ee55d1e
commit 9c10e08f90

View File

@@ -1243,7 +1243,10 @@ fn scan_local_tmp_files(app_name: &str) -> Vec<(String, u64)> {
.unwrap_or(&rel_str);
// 获取文件大小
if let Ok(metadata) = fs::metadata(&path) {
tmp_files.push((official_name.to_string(), metadata.len()));
// 跳过 0 字节的临时文件MD5 无意义,视为从头续传)
if metadata.len() > 0 {
tmp_files.push((official_name.to_string(), metadata.len()));
}
}
}
}
@@ -1760,22 +1763,28 @@ async fn run_updater(debug_mode: bool) -> bool {
let tmp_path = upgrade_base.join(&tmp_filename);
let has_tmp = tmp_path.exists();
let has_official = file_path.exists();
let tmp_is_zero = has_tmp && tmp_path.metadata().map(|m| m.len() == 0).unwrap_or(false);
if has_tmp {
// 情况A本地有临时文件 → 比较临时文件
let local_md5 = compute_file_md5(&tmp_path);
if let Some(lm) = local_md5 {
if &lm != server_md5 {
// MD5 不对 → 清空临时文件内容
log_print!("{} [AllFile] tmp {} MD5不一致 (本地={}, 服务端={}),清空", ts, tmp_filename, lm, server_md5);
if let Ok(file) = fs::File::create(&tmp_path) {
let _ = file.set_len(0);
if tmp_is_zero {
// 情况0本地有 0 字节临时文件0 字节未上报服务端,无法比对 MD5
// 直接从 offset 0 断点续传
request_download_for_app(&sender, &app_name, filename, 0);
update_performed_clone2.store(true, std::sync::atomic::Ordering::SeqCst);
} else {
// 情况A本地有临时文件>0字节→ 比较临时文件
let local_md5 = compute_file_md5(&tmp_path);
if let Some(lm) = local_md5 {
if &lm != server_md5 {
log_print!("{} [AllFile] tmp {} MD5不一致 (本地={}, 服务端={}),清空", ts, tmp_filename, lm, server_md5);
if let Ok(file) = fs::File::create(&tmp_path) {
let _ = file.set_len(0);
}
request_download_for_app(&sender, &app_name, filename, 0);
update_performed_clone2.store(true, std::sync::atomic::Ordering::SeqCst);
} else {
log_print!("{} [AllFile] tmp {} MD5一致续传", ts, tmp_filename);
}
// 发送下载请求
request_download_for_app(&sender, &app_name, filename, 0);
update_performed_clone2.store(true, std::sync::atomic::Ordering::SeqCst);
} else {
log_print!("{} [AllFile] tmp {} MD5一致续传", ts, tmp_filename);
}
}
} else if has_official {
@@ -1783,7 +1792,6 @@ async fn run_updater(debug_mode: bool) -> bool {
let local_md5 = compute_file_md5(&file_path);
if let Some(lm) = local_md5 {
if &lm != server_md5 {
// MD5 不对 → 创建空的临时文件
log_print!("{} [AllFile] {} MD5不一致 (本地={}, 服务端={}),创建 tmp", ts, filename, lm, server_md5);
if let Some(parent) = tmp_path.parent() {
let _ = fs::create_dir_all(parent);
@@ -1811,13 +1819,16 @@ async fn run_updater(debug_mode: bool) -> bool {
}
}
// 4. 为剩余的有效临时文件发送续传请求
// 4. 为有效临时文件>0字节发送续传请求
for (filename, _, _) in &server_files {
let tmp_filename = format!("{}.tmp", filename);
let tmp_path = upgrade_base.join(&tmp_filename);
if tmp_path.exists() {
// tmp 存在,发送续传请求
let file_size = fs::metadata(&tmp_path).map(|m| m.len()).unwrap_or(0);
// 0 字节 tmp 已在步骤3中处理过跳过
if file_size == 0 {
continue;
}
request_download_for_app(&sender, &app_name, filename, file_size);
}
}