diff --git a/src/vs/base/common/paths.ts b/src/vs/base/common/paths.ts index 57757422e749d7fd47f2fc0b9e4007e3b1be8849..ae76ac4562812819c835ccbb879473e0eade002f 100644 --- a/src/vs/base/common/paths.ts +++ b/src/vs/base/common/paths.ts @@ -233,14 +233,48 @@ export const join: (...parts: string[]) => string = function () { }; +/** + * Check if the path follows this pattern: `\\hostname\sharename`. + * + * @see https://msdn.microsoft.com/en-us/library/gg465305.aspx + * @return A boolean indication if the path is a UNC path, on none-windows + * always false. + */ export function isUNC(path: string): boolean { - if (!isWindows || !path) { - return false; // UNC is a windows concept + if (!isWindows) { + // UNC is a windows concept + return false; } - path = this.normalize(path, true); + if (!path || path.length < 5) { + // at least \\a\b + return false; + } - return path[0] === nativeSep && path[1] === nativeSep; + let code = path.charCodeAt(0); + if (code !== _backslash) { + return false; + } + code = path.charCodeAt(1); + if (code !== _backslash) { + return false; + } + let pos = 2; + let start = pos; + for (; pos < path.length; pos++) { + code = path.charCodeAt(pos); + if (code === _backslash) { + break; + } + } + if (start === pos) { + return false; + } + code = path.charCodeAt(pos + 1); + if (isNaN(code) || code === _backslash) { + return false; + } + return true; } function isPosixAbsolute(path: string): boolean { diff --git a/src/vs/base/test/common/paths.test.ts b/src/vs/base/test/common/paths.test.ts index d509bb9c88b16a5b66b451085014ad056ab35c66..f43a6d8dffc38ba901c367a746c6a53097e6961f 100644 --- a/src/vs/base/test/common/paths.test.ts +++ b/src/vs/base/test/common/paths.test.ts @@ -174,14 +174,16 @@ suite('Paths', () => { }); test('isUNC', () => { - assert(!paths.isUNC('foo')); - assert(!paths.isUNC('/foo')); - assert(!paths.isUNC('\\foo')); - if (platform.isWindows) { - assert(paths.isUNC('\\\\foo')); - } else { - assert(!paths.isUNC('\\\\foo')); + assert.ok(!paths.isUNC('foo')); + assert.ok(!paths.isUNC('/foo')); + assert.ok(!paths.isUNC('\\foo')); + assert.ok(!paths.isUNC('\\\\foo')); + assert.ok(paths.isUNC('\\\\a\\b')); + assert.ok(!paths.isUNC('//a/b')); + assert.ok(paths.isUNC('\\\\server\\share')); + assert.ok(paths.isUNC('\\\\server\\share\\')); + assert.ok(paths.isUNC('\\\\server\\share\\path')); } });