Skip to content

Commit 47b5d1a

Browse files
committed
handle //?/UNC/host/share long UNC forms
1 parent 05bce27 commit 47b5d1a

5 files changed

Lines changed: 112 additions & 30 deletions

File tree

‎README.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ On Windows, UNC paths like `//?/c:/...` or
118118
`//<host>/<share>/...` are compared case-insensitively with
119119
those of the file path, if neither has magic. For example,
120120
`//host/share/*` matches `//HOST/SHARE/x`.
121+
- Patterns starting with `//?/UNC/<host>/<share>/...` will match
122+
file paths starting with `//<host>/<share>/...`, and vice
123+
versa, the same as `//?/<drive letter>:`. The `?` is not a
124+
wildcard character.
121125

122126
Note that specifying a UNC path using `\` characters as path
123127
separators is always allowed in the file path argument, but only

‎changelog.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
- On Windows, compare the host and share of UNC paths like
3434
`//host/share/...` case-insensitively, the same as drive
3535
letters.
36+
- On Windows, long UNC paths like `//?/UNC/host/share/...` match
37+
`//host/share/...` and vice versa, the same as `//?/c:/...` and
38+
`c:/...`.
3639

3740
## 10.2
3841

‎src/index.ts‎

Lines changed: 57 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -397,16 +397,33 @@ const isDots = (p: string) => p === '.' || p === '..'
397397
const isDrive = (p: ParseReturn | undefined) =>
398398
typeof p === 'string' && /^[a-z]:$/i.test(p)
399399

400-
// a row or split path that starts with a Windows UNC root, like
401-
// `//host/share`, with a string host and share. `//?/...` is not one.
402-
const isUNC = (p: ParseReturn[]) =>
403-
p[0] === '' &&
404-
p[1] === '' &&
405-
typeof p[2] === 'string' &&
406-
p[2] !== '' &&
407-
p[2] !== '?' &&
408-
typeof p[3] === 'string' &&
409-
p[3] !== ''
400+
// true for the `UNC` portion of a long UNC root, //?/UNC/host/share
401+
const isLongUNC = (p: ParseReturn | undefined) =>
402+
typeof p === 'string' && /^unc$/i.test(p)
403+
404+
// The index of the host in a row or split path that starts with a
405+
// Windows UNC root: 2 for `//host/share`, and 4 for
406+
// `//?/UNC/host/share`. Otherwise -1. `//?/c:` is not one.
407+
const uncHost = (p: ParseReturn[]) => {
408+
if (p[0] !== '' || p[1] !== '') return -1
409+
const h =
410+
p[2] !== '?' ? 2
411+
: isLongUNC(p[3]) ? 4
412+
: -1
413+
const host = p[h]
414+
const share = p[h + 1]
415+
return (
416+
h !== -1 &&
417+
host !== undefined &&
418+
host !== '' &&
419+
host !== GLOBSTAR &&
420+
share !== undefined &&
421+
share !== '' &&
422+
share !== GLOBSTAR
423+
) ?
424+
h
425+
: -1
426+
}
410427

411428
// true if ** can walk the path portion: not `.` or `..`, and not a
412429
// dotfile unless `dot` is set
@@ -626,13 +643,16 @@ export class Minimatch {
626643

627644
// On Windows, the index of the last root portion of a row, as
628645
// #matchRow finds it: 0 for `c:/...`, 3 for `//?/c:/...` and
629-
// `//host/share/...`. Otherwise -1.
646+
// `//host/share/...`, and 5 for `//?/UNC/host/share/...`. Otherwise
647+
// -1.
630648
#rootIndex(row: ParseReturn[]) {
631649
if (!this.isWindows) return -1
632650
if (isDrive(row[0])) return 0
633651
const uncDrive =
634652
row[0] === '' && row[1] === '' && row[2] === '?' && isDrive(row[3])
635-
return uncDrive || isUNC(row) ? 3 : -1
653+
if (uncDrive) return 3
654+
const h = uncHost(row)
655+
return h === -1 ? -1 : h + 1
636656
}
637657

638658
hasMagic(): boolean {
@@ -683,17 +703,19 @@ export class Minimatch {
683703
// glob --> Matchers
684704
let set = this.globParts.map((s, _, __) => {
685705
if (this.isWindows && this.windowsNoMagicRoot) {
686-
// check if it's a drive or unc path.
706+
// check if it's a drive or unc path. The root of a long UNC
707+
// path, //?/UNC/host/share, is 6 portions.
708+
const n = s[2] === '?' && isLongUNC(s[3]) ? 6 : 4
687709
const isUNC =
688710
s[0] === '' &&
689711
s[1] === '' &&
690712
(s[2] === '?' || !globMagic.test(s[2])) &&
691-
!globMagic.test(s[3])
713+
s.slice(3, n).every(p => !globMagic.test(p))
692714
const isDrive = /^[a-z]:/i.test(s[0])
693715
if (isUNC) {
694716
return [
695-
...s.slice(0, 4).map(p => this.#norm(p)),
696-
...s.slice(4).map(ss => this.parse(ss)),
717+
...s.slice(0, n).map(p => this.#norm(p)),
718+
...s.slice(n).map(ss => this.parse(ss)),
697719
]
698720
} else if (isDrive) {
699721
return [
@@ -718,9 +740,9 @@ export class Minimatch {
718740
p[0] === '' &&
719741
p[1] === '' &&
720742
this.globParts[i][2] === '?' &&
721-
typeof p[3] === 'string' &&
722-
/^[a-z]:$/i.test(p[3])
743+
(isDrive(p[3]) || isLongUNC(this.globParts[i][3]))
723744
) {
745+
if (isLongUNC(this.globParts[i][3])) p[3] = 'UNC'
724746
p[2] = '?'
725747
}
726748
}
@@ -1319,20 +1341,25 @@ export class Minimatch {
13191341
let fileStartIndex = 0
13201342
let patternStartIndex = 0
13211343

1322-
// UNC paths like //?/X:/... can match X:/... and vice versa
1344+
// UNC paths like //?/X:/... can match X:/... and vice versa, and
1345+
// //?/UNC/host/share/... can match //host/share/... and vice versa.
13231346
// Drive letters in absolute drive or unc paths, and the host and
1324-
// share of UNC paths like //host/share/..., are always compared
1325-
// case-insensitively.
1326-
// Level 2 can resolve the share away (//h/s/../x), so the UNC root
1327-
// is found in the resolved parts.
1328-
if (this.isWindows && isUNC(resolved) && isUNC(pattern)) {
1329-
const [, , fh, fs] = resolved
1330-
const [, , ph, ps] = pattern as string[]
1331-
if (
1332-
fh!.toLowerCase() === ph!.toLowerCase() &&
1333-
fs!.toLowerCase() === ps!.toLowerCase()
1347+
// share of UNC paths, are always compared case-insensitively.
1348+
const fh = this.isWindows ? uncHost(resolved) : -1
1349+
const ph = fh === -1 ? -1 : uncHost(pattern)
1350+
if (ph !== -1) {
1351+
const host = pattern[ph]
1352+
const share = pattern[ph + 1]
1353+
if (typeof host !== 'string' || typeof share !== 'string') {
1354+
// a host or share with magic is matched as usual
1355+
fileStartIndex = fh
1356+
patternStartIndex = ph
1357+
} else if (
1358+
resolved[fh]!.toLowerCase() === host.toLowerCase() &&
1359+
resolved[fh + 1]!.toLowerCase() === share.toLowerCase()
13341360
) {
1335-
patternStartIndex = fileStartIndex = 4
1361+
fileStartIndex = fh + 2
1362+
patternStartIndex = ph + 2
13361363
}
13371364
} else if (this.isWindows) {
13381365
const fileDrive =

‎test/unc.ts‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,26 @@ import type { MinimatchOptions } from '../dist/esm/index.js'
33
import { minimatch, Minimatch } from '../dist/esm/index.js'
44
import { Matcher } from '@isaacs/nonrecursive-glob-match'
55

6+
t.test('long UNC roots are not magic', async t => {
7+
for (const o of [{ nocase: true }, { windowsNoMagicRoot: true }]) {
8+
const mm = new Minimatch('//?/unc/host/share/*', {
9+
platform: 'win32',
10+
...o,
11+
})
12+
t.match(mm.set, [['', '', '?', 'UNC', 'host', 'share', Matcher]])
13+
t.equal(mm.match('//?/UNC/HOST/SHARE/x'), true)
14+
t.equal(mm.match('//HOST/share/x'), true)
15+
t.equal(mm.match('//x/UNC/host/share/x'), false)
16+
}
17+
// with magic, the host and share are Matchers
18+
const mm = new Minimatch('//?/UNC/h*/share/*', {
19+
platform: 'win32',
20+
nocase: true,
21+
})
22+
t.match(mm.set, [['', '', '?', 'UNC', Matcher, Matcher, Matcher]])
23+
t.equal(mm.match('//HOST/share/x'), true)
24+
})
25+
626
t.test('UNC patterns do not lose their //', async t => {
727
const share = new Minimatch('//host/share/*', { platform: 'win32' })
828
t.match(share.set, [['', '', 'host', 'share', Matcher]])
@@ -52,6 +72,29 @@ const cases: Case[] = [
5272
['//h/s/../H', '//h/S', false, { optimizationLevel: 2 }],
5373
['//h/x/../S', '//H/s', false, { optimizationLevel: 2 }],
5474
['//h/s/x/../y', '//H/S/y', true, { optimizationLevel: 2 }],
75+
76+
// long UNC paths, //?/UNC/host/share, are the same root
77+
['//?/UNC/host/share/x', '//host/share/*', true],
78+
['//host/share/x', '//?/UNC/host/share/*', true],
79+
['//?/unc/Host/Share/x', '//?/UNC/HOST/share/x', true],
80+
['//?/UNC/host/share/a/b', '//HOST/SHARE/**', true],
81+
['//?/UNC/host/other/x', '//host/share/*', false],
82+
['//?/UNC/other/share/x', '//?/UNC/host/share/*', false],
83+
// the ? is not a wild card here either
84+
['//x/UNC/host/share/a', '//?/UNC/host/share/a', false],
85+
// magic in the host or share is matched as usual
86+
['//?/UNC/host/s1/x', '//h*/s?/x', true],
87+
['//host/s1/x', '//?/UNC/h*/s?/x', true],
88+
['//?/UNC/HOST/s1/x', '//h*/s?/x', false],
89+
// .. can not remove the share
90+
['//?/UNC/host/x', '//?/UNC/host/share/../x', false],
91+
['//?/UNC/host/share/y', '//?/UNC/host/share/x/../y', true],
92+
[
93+
'//?/UNC/host/share/a/../../x',
94+
'//?/UNC/host/x',
95+
false,
96+
{ optimizationLevel: 2 },
97+
],
5598
]
5699

57100
t.test('UNC drive letter paths match normal paths', async t => {

‎test/windows-root-dotdot.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ t.test('patterns keep their root', t => {
1717
['//srv/share/a/../../x', ['', '', 'srv', 'share', '..', 'x']],
1818
['//srv/..', ['', '', 'srv', '..']],
1919
['//?/C:/a/../../x', ['', '', '?', 'C:', '..', 'x']],
20+
[
21+
'//?/UNC/srv/share/a/../../x',
22+
['', '', '?', 'UNC', 'srv', 'share', '..', 'x'],
23+
],
24+
['//srv/*/a/../../x', ['', '', 'srv', '*', '..', 'x']],
2025
]
2126
for (const optimizationLevel of [1, 2]) {
2227
for (const [p, parts] of cases) {

0 commit comments

Comments
 (0)