处理0字节临时文件
This commit is contained in:
@@ -1243,6 +1243,8 @@ fn scan_local_tmp_files(app_name: &str) -> Vec<(String, u64)> {
|
||||
.unwrap_or(&rel_str);
|
||||
// 获取文件大小
|
||||
if let Ok(metadata) = fs::metadata(&path) {
|
||||
// 跳过 0 字节的临时文件(MD5 无意义,视为从头续传)
|
||||
if metadata.len() > 0 {
|
||||
tmp_files.push((official_name.to_string(), metadata.len()));
|
||||
}
|
||||
}
|
||||
@@ -1252,6 +1254,7 @@ fn scan_local_tmp_files(app_name: &str) -> Vec<(String, u64)> {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tmp_files
|
||||
}
|
||||
@@ -1760,30 +1763,35 @@ 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:本地有临时文件 → 比较临时文件
|
||||
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 {
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if has_official {
|
||||
// 情况B:本地没有临时文件但有正式文件 → 比较正式文件
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user