Skip to content

Commit de72292

Browse files
afonsojanuRyanZim
andauthored
Treat Unicode-normalization-only renames like case-only renames (#1079)
* Treat Unicode-normalization-only renames like case-only renames Moving a file to a name that differs only in Unicode normalization form (NFC vs NFD) throws "Source and destination must not be the same." on filesystems that auto-normalize, such as APFS, even though the source and destination are visually identical and the user is just renaming the file. This mirrors the case-insensitive rename bug fixed in #801, so it gets the same fix: when the basenames differ but resolve to the same inode, and they're equal after either lowercasing or NFC normalization, treat it as a rename instead of raising the same-path error. Fixes #859 * Add tests for capitalization change at the same time * Normalize both case and unicode forms together when comparing --------- Co-authored-by: Ryan Zimmerman <[email protected]>
1 parent 7e0ce7f commit de72292

3 files changed

Lines changed: 177 additions & 2 deletions

File tree

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
'use strict'
2+
3+
const assert = require('assert')
4+
const os = require('os')
5+
const path = require('path')
6+
const fs = require('../../')
7+
8+
/* global beforeEach, afterEach, describe, it */
9+
10+
describe('+ moveSync() - unicode normalization paths', () => {
11+
let TEST_DIR = ''
12+
let src = ''
13+
let dest = ''
14+
15+
// 'é' as one precomposed codepoint (NFC) vs 'e' + combining acute accent (NFD).
16+
// Visually identical, different byte representation.
17+
const nfc = 'café'.normalize('NFC')
18+
const nfd = 'café'.normalize('NFD')
19+
const nfcCapitalized = 'Café'.normalize('NFC')
20+
21+
beforeEach(done => {
22+
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'move-sync-unicode-normalization-paths')
23+
fs.emptyDir(TEST_DIR, done)
24+
})
25+
26+
afterEach(() => fs.removeSync(TEST_DIR))
27+
28+
describe('> when src is a directory', () => {
29+
it('should move successfully', () => {
30+
src = path.join(TEST_DIR, `${nfd}-dir`)
31+
fs.outputFileSync(path.join(src, 'subdir', 'file.txt'), 'some data')
32+
dest = path.join(TEST_DIR, `${nfc}-dir`)
33+
34+
fs.moveSync(src, dest)
35+
assert(fs.existsSync(dest))
36+
assert.strictEqual(fs.readFileSync(path.join(dest, 'subdir', 'file.txt'), 'utf8'), 'some data')
37+
})
38+
})
39+
40+
describe('> when src is a file', () => {
41+
it('should move successfully', () => {
42+
src = path.join(TEST_DIR, `${nfd}-file`)
43+
fs.outputFileSync(src, 'some data')
44+
dest = path.join(TEST_DIR, `${nfc}-file`)
45+
46+
fs.moveSync(src, dest)
47+
assert(fs.existsSync(dest))
48+
assert.strictEqual(fs.readFileSync(dest, 'utf8'), 'some data')
49+
})
50+
})
51+
52+
describe("when there's also a capitalization change", () => {
53+
describe('> when src is a directory', () => {
54+
it('should move successfully', () => {
55+
src = path.join(TEST_DIR, `${nfd}-dir`)
56+
fs.outputFileSync(path.join(src, 'subdir', 'file.txt'), 'some data')
57+
dest = path.join(TEST_DIR, `${nfcCapitalized}-dir`)
58+
59+
fs.moveSync(src, dest)
60+
assert(fs.existsSync(dest))
61+
assert.strictEqual(fs.readFileSync(path.join(dest, 'subdir', 'file.txt'), 'utf8'), 'some data')
62+
})
63+
})
64+
65+
describe('> when src is a file', () => {
66+
it('should move successfully', () => {
67+
src = path.join(TEST_DIR, `${nfd}-file`)
68+
fs.outputFileSync(src, 'some data')
69+
dest = path.join(TEST_DIR, `${nfcCapitalized}-file`)
70+
71+
fs.moveSync(src, dest)
72+
assert(fs.existsSync(dest))
73+
assert.strictEqual(fs.readFileSync(dest, 'utf8'), 'some data')
74+
})
75+
})
76+
})
77+
})
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
'use strict'
2+
3+
const assert = require('assert')
4+
const os = require('os')
5+
const path = require('path')
6+
const fs = require('../../')
7+
8+
/* global beforeEach, afterEach, describe, it */
9+
10+
describe('+ move() - unicode normalization paths', () => {
11+
let TEST_DIR = ''
12+
let src = ''
13+
let dest = ''
14+
15+
// 'é' as one precomposed codepoint (NFC) vs 'e' + combining acute accent (NFD).
16+
// Visually identical, different byte representation.
17+
const nfc = 'café'.normalize('NFC')
18+
const nfd = 'café'.normalize('NFD')
19+
const nfcCapitalized = 'Café'.normalize('NFC')
20+
21+
beforeEach(done => {
22+
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'move-unicode-normalization-paths')
23+
fs.emptyDir(TEST_DIR, done)
24+
})
25+
26+
afterEach(() => fs.removeSync(TEST_DIR))
27+
28+
describe('> when src is a directory', () => {
29+
it('should move successfully', done => {
30+
src = path.join(TEST_DIR, `${nfd}-dir`)
31+
fs.outputFileSync(path.join(src, 'subdir', 'file.txt'), 'some data')
32+
dest = path.join(TEST_DIR, `${nfc}-dir`)
33+
34+
fs.move(src, dest, err => {
35+
assert.ifError(err)
36+
assert(fs.existsSync(dest))
37+
assert.strictEqual(fs.readFileSync(path.join(dest, 'subdir', 'file.txt'), 'utf8'), 'some data')
38+
done()
39+
})
40+
})
41+
})
42+
43+
describe('> when src is a file', () => {
44+
it('should move successfully', done => {
45+
src = path.join(TEST_DIR, `${nfd}-file`)
46+
fs.outputFileSync(src, 'some data')
47+
dest = path.join(TEST_DIR, `${nfc}-file`)
48+
49+
fs.move(src, dest, err => {
50+
assert.ifError(err)
51+
assert(fs.existsSync(dest))
52+
assert.strictEqual(fs.readFileSync(dest, 'utf8'), 'some data')
53+
done()
54+
})
55+
})
56+
})
57+
describe("when there's also a capitalization change", () => {
58+
describe('> when src is a directory', () => {
59+
it('should move successfully', done => {
60+
src = path.join(TEST_DIR, `${nfd}-dir`)
61+
fs.outputFileSync(path.join(src, 'subdir', 'file.txt'), 'some data')
62+
dest = path.join(TEST_DIR, `${nfcCapitalized}-dir`)
63+
64+
fs.move(src, dest, err => {
65+
assert.ifError(err)
66+
assert(fs.existsSync(dest))
67+
assert.strictEqual(fs.readFileSync(path.join(dest, 'subdir', 'file.txt'), 'utf8'), 'some data')
68+
done()
69+
})
70+
})
71+
})
72+
73+
describe('> when src is a file', () => {
74+
it('should move successfully', done => {
75+
src = path.join(TEST_DIR, `${nfd}-file`)
76+
fs.outputFileSync(src, 'some data')
77+
dest = path.join(TEST_DIR, `${nfcCapitalized}-file`)
78+
79+
fs.move(src, dest, err => {
80+
assert.ifError(err)
81+
assert(fs.existsSync(dest))
82+
assert.strictEqual(fs.readFileSync(dest, 'utf8'), 'some data')
83+
done()
84+
})
85+
})
86+
})
87+
})
88+
})

‎lib/util/stat.js‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ async function checkPaths (src, dest, funcName, opts) {
4040
const destBaseName = path.basename(dest)
4141
if (funcName === 'move' &&
4242
srcBaseName !== destBaseName &&
43-
srcBaseName.toLowerCase() === destBaseName.toLowerCase()) {
43+
isCosmeticRename(srcBaseName, destBaseName)) {
4444
return { srcStat, destStat, isChangingCase: true }
4545
}
4646
throw new Error('Source and destination must not be the same.')
@@ -69,7 +69,7 @@ function checkPathsSync (src, dest, funcName, opts) {
6969
const destBaseName = path.basename(dest)
7070
if (funcName === 'move' &&
7171
srcBaseName !== destBaseName &&
72-
srcBaseName.toLowerCase() === destBaseName.toLowerCase()) {
72+
isCosmeticRename(srcBaseName, destBaseName)) {
7373
return { srcStat, destStat, isChangingCase: true }
7474
}
7575
throw new Error('Source and destination must not be the same.')
@@ -88,6 +88,16 @@ function checkPathsSync (src, dest, funcName, opts) {
8888
return { srcStat, destStat }
8989
}
9090

91+
// True when src and dest refer to the same inode and their basenames only
92+
// differ in letter case or Unicode normalization form. Some filesystems
93+
// (APFS, most Windows filesystems) silently normalize case and/or Unicode
94+
// representation, so a rename that looks like a no-op string-wise is
95+
// actually the user renaming a file to a visually-identical but
96+
// differently-encoded name, not a genuine "same path" error.
97+
function isCosmeticRename (srcBaseName, destBaseName) {
98+
return srcBaseName.toLowerCase().normalize('NFC') === destBaseName.toLowerCase().normalize('NFC')
99+
}
100+
91101
// recursively check if dest parent is a subdirectory of src.
92102
// It works for all file types including symlinks since it
93103
// checks the src and dest inodes. It starts from the deepest

0 commit comments

Comments
 (0)