From cd3600bae95613d2dd5353295534721c791d3da9 Mon Sep 17 00:00:00 2001 From: Daniel Salazar Date: Thu, 12 Nov 2026 20:02:39 -0700 Subject: [PATCH] fix(fs): overwrite a file in place when a write spells its name in another case or normalization form (#4620) * fix(fs): overwrite a file in place when a write spells its name in another case or normalization form * docs(fs): say a write needs overwrite off to get a new name beside a look-alike one --- .../services/fs/FSService.foldedNames.test.ts | 70 +++++++++++++++++++ src/backend/services/fs/FSService.ts | 3 +- src/backend/stores/fs/FSEntryStore.ts | 22 ++++-- src/docs/src/FS/write.md | 2 +- 4 files changed, 91 insertions(+), 6 deletions(-) diff --git a/src/backend/services/fs/FSService.foldedNames.test.ts b/src/backend/services/fs/FSService.foldedNames.test.ts index d02023afb..f202fe8f2 100644 --- a/src/backend/services/fs/FSService.foldedNames.test.ts +++ b/src/backend/services/fs/FSService.foldedNames.test.ts @@ -333,6 +333,76 @@ describe.each(engines.filter((engine) => !engine.skip))( expect(await readBack(replaced)).toBe('two'); }); + // The upload a client makes straight to storage, then confirms. + const signedWrite = async (path: string, content: string) => { + const started = await fs.startUrlWrite(userId, { + fileMetadata: { + path, + size: Buffer.byteLength(content), + contentType: 'text/plain', + overwrite: true, + }, + }); + await fetch(started.url!, { + method: 'PUT', + body: content, + headers: { 'content-type': 'text/plain' }, + }); + return fs.completeUrlWrite(userId, { uploadId: started.sessionId }); + }; + + it('overwrites a case-only spelling in place through a signed upload', async () => { + const dir = await freshDir(); + const original = await write(`${dir}/Notes.txt`, 'one'); + + const completed = await signedWrite(`${dir}/notes.txt`, 'two'); + expect(completed.wasOverwrite).toBe(true); + expect(completed.fsEntry.uuid).toBe(original.uuid); + expect(await names(dir)).toEqual(['notes.txt']); + expect(await readBack(completed.fsEntry)).toBe('two'); + }); + + it('overwrites a case-only spelling in place through a signed batch upload', async () => { + const dir = await freshDir(); + const original = await write(`${dir}/Notes.txt`, 'one'); + + const [started] = await fs.batchStartUrlWrites(userId, [ + { + fileMetadata: { + path: `${dir}/notes.txt`, + size: 3, + contentType: 'text/plain', + overwrite: true, + }, + }, + ]); + await fetch(started!.url!, { method: 'PUT', body: 'two' }); + const [completed] = await fs.batchCompleteUrlWrite(userId, [ + { uploadId: started!.sessionId }, + ]); + expect(completed?.wasOverwrite).toBe(true); + expect(completed?.fsEntry.uuid).toBe(original.uuid); + expect(await names(dir)).toEqual(['notes.txt']); + }); + + it('overwrites an NFD spelling of a stored name in place, signed or not', async () => { + const dir = await freshDir(); + const nfc = `${dir}/r\u00e9sum\u00e9.txt`; + const nfd = nfc.normalize('NFD'); + expect(nfd).not.toBe(nfc); + const original = await write(nfc, 'one'); + + const replaced = await write(nfd, 'two', { overwrite: true }); + expect(replaced.uuid).toBe(original.uuid); + expect(await names(dir)).toHaveLength(1); + + const completed = await signedWrite(nfd, 'three'); + expect(completed.wasOverwrite).toBe(true); + expect(completed.fsEntry.uuid).toBe(original.uuid); + expect(await names(dir)).toHaveLength(1); + expect(await readBack(completed.fsEntry)).toBe('three'); + }); + it('reads nothing at a folded name, without caching the sibling under it', async () => { const dir = await freshDir(); const original = await write(`${dir}/😀.txt`, 'grinning'); diff --git a/src/backend/services/fs/FSService.ts b/src/backend/services/fs/FSService.ts index 467b1e884..e70988d19 100644 --- a/src/backend/services/fs/FSService.ts +++ b/src/backend/services/fs/FSService.ts @@ -3027,9 +3027,10 @@ export class FSService extends PuterService { if (!liveKey || liveKey === session.objectKey) return; // The completion was allowed against a path, so that is where the // entry has to still be; a move since would put the bytes elsewhere. + // The upload may spell the entry's name in another case or form. const target = await this.stores.fsEntry.getEntryByUuidFromPrimary(liveKey); - if (!target || target.path !== session.targetPath) { + if (!target || !entryNamesMatch(target.path, session.targetPath)) { throw new HttpError(409, 'Upload target has moved', { legacyCode: 'conflict', }); diff --git a/src/backend/stores/fs/FSEntryStore.ts b/src/backend/stores/fs/FSEntryStore.ts index 2acbbe9d8..5e8df421c 100644 --- a/src/backend/stores/fs/FSEntryStore.ts +++ b/src/backend/stores/fs/FSEntryStore.ts @@ -803,16 +803,30 @@ export class FSEntryStore extends PuterStore { return entries; }, ); - // A row returned under another spelling than any asked for: what it - // stands for may exist only on the primary so far. const requested = new Set(normalizedPaths); - let otherSpelling = false; + const otherSpellings: FSEntry[] = []; for (const chunkEntries of chunkResults) { for (const entry of chunkEntries) { entriesByPath.set(entry.path, entry); - if (!requested.has(entry.path)) otherSpelling = true; + if (!requested.has(entry.path)) otherSpellings.push(entry); } } + // The same name in another case or normalization form is the entry + // asked for, as in `getEntryByPath`. The walk below compares `name`, + // whose collation keeps NFC and NFD apart. + const claimed = new Set(); + for (const path of normalizedPaths) { + if (entriesByPath.has(path)) continue; + const same = otherSpellings + .filter((entry) => entryNamesMatch(entry.path, path)) + .sort((x, y) => x.id - y.id); + if (same.length === 0) continue; + entriesByPath.set(path, same[0]!); + for (const entry of same) claimed.add(entry); + } + // A row left over, returned under a spelling nobody asked for: what it + // stands for may exist only on the primary so far. + const otherSpelling = otherSpellings.length > claimed.size; // Per-path lineage fallback for entries the bulk SELECT missed — // legacy rows with a NULL `path` column (see #resolveEntryByPathLineage). diff --git a/src/docs/src/FS/write.md b/src/docs/src/FS/write.md index e39a6aafb..44dfb5ccb 100755 --- a/src/docs/src/FS/write.md +++ b/src/docs/src/FS/write.md @@ -54,7 +54,7 @@ The promise rejects with `{ message, code }`. These codes mean the name is alrea | -- | -- | | `item_with_same_name_exists` | A file with this name exists, and `overwrite` and `dedupeName` are both `false`. | | `cannot_overwrite_a_directory` | A folder with this name exists. | -| `conflict` | The name differs from an existing file or folder's name only by accents or emoji, such as `😁.txt` next to `😀.txt`. The existing item is never replaced, even with `overwrite: true`. To write the file under a new name instead, set `dedupeName: true`. | +| `conflict` | The name differs from an existing file or folder's name only by accents or emoji, such as `😁.txt` next to `😀.txt`. The existing item is never replaced, even with `overwrite: true`. To write the file under a new name instead, set `overwrite: false` and `dedupeName: true`. | ## Examples