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 <code@hornwitser.no>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
authored and Cooldude2606 committed 2026-10-24 00:22:17 +01:00
1 parent c3abf27266
commit a6d2d48e48
4 files changed
+45 -96

No files matched your search

-35
View File
@@ -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);