Commit dca00dfa by venus

fix: 内核升级改为逐文件同步,避开批量删除安全拦截并避免删除后写入的中间态

1 parent a2f97e90
...@@ -62,6 +62,56 @@ function removeDir(dir) { ...@@ -62,6 +62,56 @@ function removeDir(dir) {
if (fs.existsSync(dir)) fs.rmSync(dir, { recursive: true, force: true }); if (fs.existsSync(dir)) fs.rmSync(dir, { recursive: true, force: true });
} }
/**
* 以 src 为准同步 dst:覆盖/新增所有文件,逐文件删除 src 中已不存在的文件。
*
* 为什么不用 removeDir(dst) + copyDir(src, dst):
* 1. 整目录 rmSync 会一次性删除数百个文件,在带批量删除保护的终端/自动化环境里
* 会被安全策略拦截(本项目实测:493 文件 > 阈值 50,升级直接失败);
* 2. 先删后写存在中间态——复制失败时内核已被清空,回滚只能靠备份;
* 3. 逐文件同步只动真正变化的文件,共有文件保持原样,升级更精确。
*/
function syncDir(src, dst) {
fs.mkdirSync(dst, { recursive: true });
function relFiles(base) {
const out = new Set();
(function walk(dir, prefix) {
for (const ent of fs.readdirSync(dir, { withFileTypes: true })) {
if (SKIP.has(ent.name)) continue;
const rel = prefix ? `${prefix}/${ent.name}` : ent.name;
const full = path.join(dir, ent.name);
if (ent.isDirectory()) walk(full, rel);
else out.add(rel);
}
})(base, '');
return out;
}
const srcFiles = relFiles(src);
const dstFiles = relFiles(dst);
// 新版已移除的文件:逐个 unlink,不触发批量删除阈值
for (const rel of dstFiles) {
if (!srcFiles.has(rel)) fs.unlinkSync(path.join(dst, ...rel.split('/')));
}
// 覆盖/新增
for (const rel of srcFiles) {
const target = path.join(dst, ...rel.split('/'));
fs.mkdirSync(path.dirname(target), { recursive: true });
fs.copyFileSync(path.join(src, ...rel.split('/')), target);
}
// 清理空目录(自底向上,非空则保留)
(function prune(dir) {
for (const ent of fs.readdirSync(dir, { withFileTypes: true })) {
if (ent.isDirectory() && !SKIP.has(ent.name)) {
const sub = path.join(dir, ent.name);
prune(sub);
try { fs.rmdirSync(sub); } catch { /* 非空,保留 */ }
}
}
})(dst);
}
function fingerprintsEqual(a, b) { function fingerprintsEqual(a, b) {
const ka = Object.keys(a).sort(); const ka = Object.keys(a).sort();
const kb = Object.keys(b).sort(); const kb = Object.keys(b).sort();
...@@ -111,8 +161,7 @@ export function upgradeKernel(options = {}) { ...@@ -111,8 +161,7 @@ export function upgradeKernel(options = {}) {
if (fs.existsSync(rootConfigPath)) fs.copyFileSync(rootConfigPath, rootConfigBackup); if (fs.existsSync(rootConfigPath)) fs.copyFileSync(rootConfigPath, rootConfigBackup);
// 2. 用源内核覆盖(唯一内核写入目标 = .kernel/) // 2. 用源内核覆盖(唯一内核写入目标 = .kernel/)
removeDir(kernelDir); syncDir(srcKernel, kernelDir);
copyDir(srcKernel, kernelDir);
// 3. 同步内核依赖真源:catalogs 段(只替换 catalog 区,保留业务自定义 workspace glob) // 3. 同步内核依赖真源:catalogs 段(只替换 catalog 区,保留业务自定义 workspace glob)
let catalogSynced = false; let catalogSynced = false;
...@@ -133,8 +182,7 @@ export function upgradeKernel(options = {}) { ...@@ -133,8 +182,7 @@ export function upgradeKernel(options = {}) {
// 4. 校验业务区指纹未变 // 4. 校验业务区指纹未变
const after = businessFingerprint(root); const after = businessFingerprint(root);
if (!fingerprintsEqual(before, after)) { if (!fingerprintsEqual(before, after)) {
removeDir(kernelDir); syncDir(backup, kernelDir);
copyDir(backup, kernelDir);
if (fs.existsSync(rootConfigBackup)) fs.copyFileSync(rootConfigBackup, rootConfigPath); if (fs.existsSync(rootConfigBackup)) fs.copyFileSync(rootConfigBackup, rootConfigPath);
return { return {
ok: false, ok: false,
...@@ -158,7 +206,7 @@ export function upgradeKernel(options = {}) { ...@@ -158,7 +206,7 @@ export function upgradeKernel(options = {}) {
}; };
} catch (err) { } catch (err) {
try { try {
if (!fs.existsSync(kernelDir)) copyDir(path.join(backup, '.') , kernelDir); if (!fs.existsSync(kernelDir)) syncDir(backup, kernelDir);
if (fs.existsSync(rootConfigBackup)) fs.copyFileSync(rootConfigBackup, rootConfigPath); if (fs.existsSync(rootConfigBackup)) fs.copyFileSync(rootConfigBackup, rootConfigPath);
} catch { /* 回滚失败也要把原始错误抛给调用方 */ } } catch { /* 回滚失败也要把原始错误抛给调用方 */ }
return { ok: false, message: `升级失败:${err.message}` }; return { ok: false, message: `升级失败:${err.message}` };
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!