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
This commit is contained in:
Daniel Salazar authored and GitHub committed 2026-11-12 20:02:39 -07:00
1 parent 2db55bd636
commit cd3600bae9
4 files changed
+91 -6

No files matched your search

@@ -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');
+2 -1
View File
@@ -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',
});
+18 -4
View File
@@ -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<FSEntry>();
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).
+1 -1
View File
@@ -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