From a6d2d48e48b9a8d49275c23fa745f079c7ca851c Mon Sep 17 00:00:00 2001 From: bbassie <17990055+bbassie@users.noreply.github.com> Date: Tue, 20 Oct 2026 18:52:38 +0000 Subject: [PATCH 1/3] Remove lib.findUnusedName The name it returns may already be taken by a concurrently running process by the time the caller uses it. Callers now try the name and retry with a -2, -3, ... suffix on EEXIST instead. Save transfers previously renamed with fs.rename, which silently overwrites a save that appears at the target, and copied without COPYFILE_EXCL. Moves now use link + unlink like save rename does, and copies pass COPYFILE_EXCL, so both fail with EEXIST rather than overwrite. Supersedes #880. Co-authored-by: Hornwitser Co-Authored-By: Claude Opus 5.5 --- packages/host/src/Host.ts | 50 +++++++++++++++++++++++------------ packages/lib/src/file_ops.ts | 46 ++------------------------------ test/integration/clusterio.js | 10 +++++++ test/lib/file_ops.js | 35 ------------------------ 4 files changed, 45 insertions(+), 96 deletions(-) diff --git a/packages/host/src/Host.ts b/packages/host/src/Host.ts index f5ce4ad3..507bae82 100644 --- a/packages/host/src/Host.ts +++ b/packages/host/src/Host.ts @@ -499,7 +499,7 @@ export default class Host extends lib.Link { let instancesDir = this.config.get("host.instances_directory"); for (let i = 0; i < 10; i++) { // Limit attempts in case this is somehow an infinite loop - let candidateDir = path.join(instancesDir, await lib.findUnusedName(instancesDir, name)); + let candidateDir = path.join(instancesDir, i === 0 ? name : `${name}-${i + 1}`); try { await fs.mkdir(candidateDir); } catch (err: any) { @@ -1064,6 +1064,32 @@ export default class Host extends lib.Link { await this.sendSaveListUpdate(instanceId, path.join(instance.path, "saves")); } + async _moveOrCopySave(sourcePath: string, targetPath: string, copy: boolean) { + const { dir, name, ext } = path.parse(targetPath); + for (let i = 0; i < 10; i++) { + targetPath = path.format({ dir, name: i === 0 ? name : `${name}-${i + 1}`, ext }); + try { + if (copy) { + await fs.copyFile( + sourcePath, + targetPath, + fs.constants.COPYFILE_EXCL | fs.constants.COPYFILE_FICLONE + ); + } else { + // Unlike rename, link fails if the target exists. + await fs.link(sourcePath, targetPath); + await fs.unlink(sourcePath); + } + return path.basename(targetPath); + } catch (err: any) { + if (err.code !== "EEXIST") { + throw err; + } + } + } + throw new lib.RequestError("Unable to transfer save, retry threshold reached"); + } + async handleInstanceTransferSaveRequest(request: lib.InstanceTransferSaveRequest) { let { sourceName, targetName, copy, sourceInstanceId, targetInstanceId } = request; checkRequestSaveName(sourceName); @@ -1073,29 +1099,19 @@ export default class Host extends lib.Link { // For consistency with remote transfer initiated through pullSave the // target is renamed if it already exists. - targetName = await lib.findUnusedName( - path.join(targetInstance.path, "saves"), targetName, ".zip" - ); - try { - if (copy) { - await fs.copyFile( - path.join(sourceInstance.path, "saves", sourceName), - path.join(targetInstance.path, "saves", targetName), - fs.constants.COPYFILE_FICLONE - ); - } else { - await fs.rename( - path.join(sourceInstance.path, "saves", sourceName), - path.join(targetInstance.path, "saves", targetName), - ); - } + targetName = await this._moveOrCopySave( + path.join(sourceInstance.path, "saves", sourceName), + path.join(targetInstance.path, "saves", targetName), + copy, + ); } catch (err: any) { if (err.code === "ENOENT") { throw new lib.RequestError(`${sourceName} does not exist`); } throw err; } + await this.sendSaveListUpdate(sourceInstanceId, path.join(sourceInstance.path, "saves")); await this.sendSaveListUpdate(targetInstanceId, path.join(targetInstance.path, "saves")); diff --git a/packages/lib/src/file_ops.ts b/packages/lib/src/file_ops.ts index 4b3028ca..de8b30c4 100644 --- a/packages/lib/src/file_ops.ts +++ b/packages/lib/src/file_ops.ts @@ -70,48 +70,7 @@ export async function directorySize(directory: string) { } /** - * Modifies name in case it already exisist at the given directory - * - * Checks the directory passed if it already contains a file or folder with - * the given name, if it does not returns name unmodified, otherwise it - * returns a modified name that does not exist in the directory. - * - * Warning: this function should not be relied upon to be accurate for - * security sensitive applications. The selection process is inherently - * racy and a file or folder may have been created in the folder by the time - * this function returns. - * - * @param directory - directory to check in. - * @param name - file name to check for, may have extension. - * @param extension - dot extension used for the file name. - * @returns modified name with extension that likely does - * not exist in the folder - */ -export async function findUnusedName(directory: string, name: string, extension = "") { - if (extension && name.endsWith(extension)) { - name = name.slice(0, -extension.length); - } - - while (true) { - try { - await fs.access(path.join(directory, `${name}${extension}`)); - } catch (err: any) { - if (err.code === "ENOENT") { - return `${name}${extension}`; - } - } - - let match = /^(.*?)(-(\d+))?$/.exec(name)!; - if (!match[2]) { - name = `${match[1]}-2`; - } else { - name = `${match[1]}-${Number.parseInt(match[3], 10) + 1}`; - } - } -} - -/** - * Generate collision resistante temporary file name + * Generate collision resistant temporary file name * * Warning: this function should not be relied upon to be accurate for * security sensitive applications. The selection process is inherently @@ -125,8 +84,7 @@ export async function findUnusedName(directory: string, name: string, extension */ export async function getTempFile(prefix = "tmp.", suffix = "", tmpdir = "./") { let fileName = path.join(prefix + crypto.randomBytes(16).toString("hex") + suffix); - let freeFile = await findUnusedName(tmpdir, fileName); - let fullPath = path.join(tmpdir, freeFile); + let fullPath = path.join(tmpdir, fileName); return fullPath; } diff --git a/test/integration/clusterio.js b/test/integration/clusterio.js index 08ee24b7..b6ed3627 100644 --- a/test/integration/clusterio.js +++ b/test/integration/clusterio.js @@ -1469,6 +1469,16 @@ describe("Integration of Clusterio", function() { await deleteSave(pri, "transfer.zip"); await deleteSave(sec, "transfer.zip"); }); + it("should auto-rename copy if target exists", async function() { + await uploadSave(pri, "transfer.zip", "transfer.zip"); + await uploadSave(sec, "transfer.zip", "transfer.zip"); + await execCtlProcess(`instance save transfer ${pri} transfer.zip ${sec} --copy`); + await assert.doesNotReject(fs.access(path.join(priSaves, "transfer.zip")), "save not at pri"); + await assert.doesNotReject(fs.access(path.join(secSaves, "transfer-2.zip")), "save not at sec"); + await deleteSave(pri, "transfer.zip"); + await deleteSave(sec, "transfer.zip"); + await deleteSave(sec, "transfer-2.zip"); + }); it("should fail if save does not exist", async function() { await assert.rejects(execCtl(`instance save transfer ${pri} not-here.zip ${sec}`)); }); diff --git a/test/lib/file_ops.js b/test/lib/file_ops.js index dfaf905f..3e8dd21f 100644 --- a/test/lib/file_ops.js +++ b/test/lib/file_ops.js @@ -17,13 +17,6 @@ describe("lib/file_ops", function() { await fs.writeFile(path.join(baseDir, "test", "file.txt"), "contents"); await fs.writeFile(path.join(baseDir, "test", "another file.txt"), "more contents"); - await fs.mkdir(path.join(baseDir, "find"), { recursive: true }); - await fs.writeFile(path.join(baseDir, "find", "file"), "contents"); - await fs.writeFile(path.join(baseDir, "find", "file.txt"), "contents"); - await fs.writeFile(path.join(baseDir, "find", "foo-1"), "contents"); - await fs.writeFile(path.join(baseDir, "find", "foo-2"), "contents"); - await fs.writeFile(path.join(baseDir, "find", "bar-1.txt"), "contents"); - await fs.writeFile(path.join(baseDir, "find", "bar-2.txt"), "contents"); } before(setupTestingEnv); @@ -58,34 +51,6 @@ describe("lib/file_ops", function() { }); }); - describe("findUnusedName()", function() { - it("should return named unchanged if it does not exist", async function() { - let cases = [ - [["file"], "file"], - [["file", ".txt"], "file.txt"], - [["file.txt", ".txt"], "file.txt"], - ]; - for (let [args, expected] of cases) { - let actual = await lib.findUnusedName(path.join(baseDir, "test", "folder"), ...args); - assert.equal(actual, expected); - } - }); - it("should return changed name if it does exist", async function() { - let cases = [ - [["file"], "file-2"], - [["file", ".txt"], "file-2.txt"], - [["file.txt", ".txt"], "file-2.txt"], - [["foo-1"], "foo-3"], - [["bar-1", ".txt"], "bar-3.txt"], - [["bar-1.txt", ".txt"], "bar-3.txt"], - ]; - for (let [args, expected] of cases) { - let actual = await lib.findUnusedName(path.join(baseDir, "find"), ...args); - assert.equal(actual, expected); - } - }); - }); - describe("safeOutputFile()", function() { async function assertNoTemporary(target) { let { dir, name } = path.parse(target); From e16d0bc4107030631376e70d789dfa62de44266f Mon Sep 17 00:00:00 2001 From: bbassie <17990055+bbassie@users.noreply.github.com> Date: Tue, 20 Oct 2026 19:06:49 +0000 Subject: [PATCH 2/3] Address review of save transfer and instance dir naming Instance dir and save names are tried without a retry cap, as on master and in downloadFile, so an 11th instance named "New Instance" still gets a folder. Moves fall back to an exclusive copy when hard links are unavailable (EXDEV, EPERM, ENOTSUP), and remove the new target if the source can't be unlinked, so a failed move leaves no duplicate. Local transfers keep appending .zip to the target name, as findUnusedName did. The copy auto-rename test now checks that the existing target save is left untouched. Co-Authored-By: Claude Opus 5.5 --- packages/host/src/Host.ts | 54 ++++++++++++++++++++++++++--------- test/integration/clusterio.js | 5 ++-- 2 files changed, 43 insertions(+), 16 deletions(-) diff --git a/packages/host/src/Host.ts b/packages/host/src/Host.ts index 507bae82..06261c43 100644 --- a/packages/host/src/Host.ts +++ b/packages/host/src/Host.ts @@ -26,6 +26,36 @@ function checkRequestSaveName(name: string) { } } +async function copySaveExclusive(sourcePath: string, targetPath: string) { + await fs.copyFile( + sourcePath, + targetPath, + fs.constants.COPYFILE_EXCL | fs.constants.COPYFILE_FICLONE + ); +} + +/** + * Move a file without overwriting an existing target, fails with EEXIST + * if the target exists. + */ +async function moveSaveExclusive(sourcePath: string, targetPath: string) { + try { + await fs.link(sourcePath, targetPath); + } catch (err: any) { + // Hard links are not possible across filesystems and on some filesystems. + if (!["EXDEV", "EPERM", "ENOTSUP"].includes(err.code)) { + throw err; + } + await copySaveExclusive(sourcePath, targetPath); + } + try { + await fs.unlink(sourcePath); + } catch (err: any) { + await fs.unlink(targetPath).catch(() => {}); + throw err; + } +} + /** * Checks that no socket is bound to the given UDP port, on IPv4 or IPv6 * @@ -498,8 +528,8 @@ export default class Host extends lib.Link { } let instancesDir = this.config.get("host.instances_directory"); - for (let i = 0; i < 10; i++) { // Limit attempts in case this is somehow an infinite loop - let candidateDir = path.join(instancesDir, i === 0 ? name : `${name}-${i + 1}`); + for (let i = 1; ; i++) { + let candidateDir = path.join(instancesDir, i === 1 ? name : `${name}-${i}`); try { await fs.mkdir(candidateDir); } catch (err: any) { @@ -510,7 +540,6 @@ export default class Host extends lib.Link { } return candidateDir; } - throw Error("Unable to create instance dir, retry threshold reached"); } async broadcastEventToInstance(event: lib.Event) { @@ -1066,19 +1095,13 @@ export default class Host extends lib.Link { async _moveOrCopySave(sourcePath: string, targetPath: string, copy: boolean) { const { dir, name, ext } = path.parse(targetPath); - for (let i = 0; i < 10; i++) { - targetPath = path.format({ dir, name: i === 0 ? name : `${name}-${i + 1}`, ext }); + for (let i = 1; ; i++) { + targetPath = path.format({ dir, name: i === 1 ? name : `${name}-${i}`, ext }); try { if (copy) { - await fs.copyFile( - sourcePath, - targetPath, - fs.constants.COPYFILE_EXCL | fs.constants.COPYFILE_FICLONE - ); + await copySaveExclusive(sourcePath, targetPath); } else { - // Unlike rename, link fails if the target exists. - await fs.link(sourcePath, targetPath); - await fs.unlink(sourcePath); + await moveSaveExclusive(sourcePath, targetPath); } return path.basename(targetPath); } catch (err: any) { @@ -1087,7 +1110,6 @@ export default class Host extends lib.Link { } } } - throw new lib.RequestError("Unable to transfer save, retry threshold reached"); } async handleInstanceTransferSaveRequest(request: lib.InstanceTransferSaveRequest) { @@ -1097,6 +1119,10 @@ export default class Host extends lib.Link { let sourceInstance = this.getRequestInstance(sourceInstanceId); let targetInstance = this.getRequestInstance(targetInstanceId); + if (!targetName.endsWith(".zip")) { + targetName += ".zip"; + } + // For consistency with remote transfer initiated through pullSave the // target is renamed if it already exists. try { diff --git a/test/integration/clusterio.js b/test/integration/clusterio.js index b6ed3627..65076237 100644 --- a/test/integration/clusterio.js +++ b/test/integration/clusterio.js @@ -1471,10 +1471,11 @@ describe("Integration of Clusterio", function() { }); it("should auto-rename copy if target exists", async function() { await uploadSave(pri, "transfer.zip", "transfer.zip"); - await uploadSave(sec, "transfer.zip", "transfer.zip"); + await uploadSave(sec, "transfer.zip", "existing"); await execCtlProcess(`instance save transfer ${pri} transfer.zip ${sec} --copy`); await assert.doesNotReject(fs.access(path.join(priSaves, "transfer.zip")), "save not at pri"); - await assert.doesNotReject(fs.access(path.join(secSaves, "transfer-2.zip")), "save not at sec"); + assert.equal(await fs.readFile(path.join(secSaves, "transfer.zip"), "utf8"), "existing"); + assert.equal(await fs.readFile(path.join(secSaves, "transfer-2.zip"), "utf8"), "transfer.zip"); await deleteSave(pri, "transfer.zip"); await deleteSave(sec, "transfer.zip"); await deleteSave(sec, "transfer-2.zip"); From 5355a0170a942ab30f4285fa5365016f9d7448a0 Mon Sep 17 00:00:00 2001 From: bbassie <17990055+bbassie@users.noreply.github.com> Date: Thu, 22 Oct 2026 11:19:16 +0000 Subject: [PATCH 3/3] Use the exclusive move for save rename and widen its copy fallback Fall back to copying on any link error except EEXIST, since filesystems without hard links report it with several codes (EISDIR on exFAT under Windows, ENOSYS on some FUSE mounts). Rename now goes through the same move so it also gets the fallback and the rollback. Only report a missing source save on transfer when the source is actually missing. Co-Authored-By: Claude Opus 5.5 --- packages/host/src/Host.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/host/src/Host.ts b/packages/host/src/Host.ts index 06261c43..ca3a872e 100644 --- a/packages/host/src/Host.ts +++ b/packages/host/src/Host.ts @@ -42,8 +42,9 @@ async function moveSaveExclusive(sourcePath: string, targetPath: string) { try { await fs.link(sourcePath, targetPath); } catch (err: any) { - // Hard links are not possible across filesystems and on some filesystems. - if (!["EXDEV", "EPERM", "ENOTSUP"].includes(err.code)) { + // Hard links fail across filesystems and on some filesystems with + // varying codes, the copy is still exclusive. + if (err.code === "EEXIST") { throw err; } await copySaveExclusive(sourcePath, targetPath); @@ -1054,13 +1055,10 @@ export default class Host extends lib.Link { checkRequestSaveName(newName); let instance = this.getRequestInstance(instanceId); try { - // Link save in the new location. - await fs.link( + await moveSaveExclusive( path.join(instance.path, "saves", oldName), path.join(instance.path, "saves", newName), ); - // Unlink save in old location. - await fs.unlink(path.join(instance.path, "saves", oldName)); } catch (err: any) { if (err.code === "EEXIST") { throw new lib.RequestError(`${newName} already exists`); @@ -1125,14 +1123,16 @@ export default class Host extends lib.Link { // For consistency with remote transfer initiated through pullSave the // target is renamed if it already exists. + const sourcePath = path.join(sourceInstance.path, "saves", sourceName); try { targetName = await this._moveOrCopySave( - path.join(sourceInstance.path, "saves", sourceName), + sourcePath, path.join(targetInstance.path, "saves", targetName), copy, ); } catch (err: any) { - if (err.code === "ENOENT") { + // ENOENT may also come from a missing target saves folder. + if (err.code === "ENOENT" && !await fs.access(sourcePath).then(() => true, () => false)) { throw new lib.RequestError(`${sourceName} does not exist`); } throw err;