参考所有分组:保持OPFS备份失败非致命

结论:按 LinuxCNC rs274ngc_pre.cc save_parameters() 中 unlink/link 失败不阻止 rename 主参数文件的语义,OPFS 参数 .bak 写入失败时不让解析保存失败,并避免把目录当作可 unlink 的备份文件删除;未扩展 smoke 解析行为。

验证:./test-native.sh;./test-linuxcnc-source-link.sh;./test-web-wasm-browser-smoke.sh。
This commit is contained in:
cnc
2026-06-03 11:22:08 +08:00
parent f98a715ed7
commit 5c2d5cfa05
2 changed files with 47 additions and 24 deletions

View File

@@ -141,6 +141,21 @@ async function removeOpfsEntry(workspacePath, relativePath, recursive) {
}
}
async function removeOpfsFileEntry(workspacePath, relativePath) {
assertRelativePath(relativePath);
const parts = relativePath.split("/");
const entryName = parts.pop();
try {
const directory = await getOpfsDirectoryHandle([workspacePath, ...parts].filter(Boolean).join("/"), false);
await directory.getFileHandle(entryName, { create: false });
await directory.removeEntry(entryName, { recursive: false });
} catch (error) {
if (error?.name !== "NotFoundError" && error?.name !== "TypeMismatchError") {
throw error;
}
}
}
async function opfsEntryExists(workspacePath, relativePath) {
assertRelativePath(relativePath);
const parts = relativePath.split("/");
@@ -498,7 +513,7 @@ function installOpfsWorkspace(module, options) {
if (backupData !== null) {
await writeOpfsFile(workspacePath, `${path}.bak`, backupData);
} else {
await removeOpfsEntry(workspacePath, `${path}.bak`, false);
await removeOpfsFileEntry(workspacePath, `${path}.bak`);
}
return data;
},
@@ -712,9 +727,22 @@ export async function createWasmSimulator(moduleOptions = {}) {
// replacing filename. Browser filesystems may not expose that link
// through the WASM mirror, so preserve the loaded OPFS file here.
if (previousParameterFile === null) {
await opfs.removeFile(`${parameterPath}.bak`);
try {
const backupStat = await opfs.stat(`${parameterPath}.bak`);
if (backupStat.kind === "file") {
await opfs.removeFile(`${parameterPath}.bak`);
}
} catch (error) {
if (error?.name !== "NotFoundError") {
throw error;
}
}
} else {
await opfs.writeFile(`${parameterPath}.bak`, previousParameterFile);
try {
await opfs.writeFile(`${parameterPath}.bak`, previousParameterFile);
} catch (_backupError) {
// LinuxCNC treats link(filename, filename + ".bak") failure as non-fatal.
}
}
return events;
} finally {