按源保护 OPFS 参数备份失败边界

This commit is contained in:
cnc
2026-06-06 06:06:03 +08:00
parent e320d6d420
commit 8544872198
2 changed files with 33 additions and 1 deletions

View File

@@ -529,7 +529,12 @@ function installOpfsWorkspace(module, options) {
const backupPath = `${LINUXCNC_DEFAULT_PARAMETER_FILE}.bak`;
const backupData = readFirstExistingWasmFile(module, [backupPath, `/${backupPath}`]);
if (backupData !== null) {
await writeOpfsFile(workspacePath, `${path}.bak`, backupData);
try {
await writeOpfsFile(workspacePath, `${path}.bak`, backupData);
} catch (_backupError) {
// LinuxCNC reports link(filename, filename + ".bak") failures
// with perror() and still renames filename + ".new" over filename.
}
} else {
await removeOpfsFileEntry(workspacePath, `${path}.bak`);
}

View File

@@ -1,9 +1,13 @@
export async function runBrowserOpfsMirrorSections(context, simulator) {
const {
expectEvent,
expectIncludes,
expectOpfsDirectoryStat,
expectMissingWasmPath,
expectText,
expectWasmFilesMissing,
near,
readOpfsText,
runSection,
withSmokeSimulator,
} = context;
@@ -123,4 +127,27 @@ export async function runBrowserOpfsMirrorSections(context, simulator) {
const truncatedWasmProgram = new TextDecoder().decode(simulator.fs.module.FS.readFile(truncatedWasmPath));
expectText(truncatedWasmProgram, "G21\n", "OPFS writeFile did not truncate the WASM mirror");
});
await runSection("opfs parameter backup bridge failure is non-fatal", async () => {
// LinuxCNC source basis: rs274ngc_pre.cc save_parameters()
// reports link(filename, filename + ".bak") failure with perror()
// but still renames filename + ".new" over the parameter file.
simulator.fs.module.FS.writeFile("rs274ngc.var", "5161\t12.000000\n");
simulator.fs.module.FS.writeFile("rs274ngc.var.bak", "5161\t11.000000\n");
const backupBridgeDirectory = simulator.fs.opfs.resolvePath("parameters/backup-bridge-directory.var.bak");
simulator.fs.module.FS.mkdir(backupBridgeDirectory);
await simulator.fs.opfs.persistDirectory("parameters/backup-bridge-directory.var.bak");
await simulator.fs.opfs.persistParameterFile("parameters/backup-bridge-directory.var");
expectIncludes(
await readOpfsText(simulator, "parameters/backup-bridge-directory.var"),
"5161\t12.000000",
"LinuxCNC parameter bridge did not persist the main file when backup link failed",
);
await expectOpfsDirectoryStat(
simulator,
"parameters/backup-bridge-directory.var.bak",
"LinuxCNC parameter backup bridge removed an existing OPFS backup directory",
);
expectWasmFilesMissing(simulator.fs.module.FS, ["rs274ngc.var", "rs274ngc.var.new", "rs274ngc.var.bak"], (parameterPath) => `LinuxCNC parameter scratch file remained outside OPFS after non-fatal backup bridge failure: ${parameterPath}`);
});
}