diff --git a/lib/fs.js b/lib/fs.js index 076ddb50c37..4aac1923a5f 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -966,10 +966,12 @@ if (isWindows) { var nextPartRe = /(.*?)(?:[\/]+|$)/g; } -// Regex to split a windows path into three parts: [*, device, slash, -// tail] windows-only -var splitDeviceRe = - /^([a-zA-Z]:|[\\\/]{2}[^\\\/]+[\\\/][^\\\/]+)?([\\\/])?([\s\S]*?)$/; +// Regex to find the device root, including trailing slash. E.g. 'c:\\'. +if (isWindows) { + var splitRootRe = /^(?:[a-zA-Z]:|[\\\/]{2}[^\\\/]+[\\\/][^\\\/]+)?[\\\/]*/; +} else { + var splitRootRe = /^[\/]*/; +} fs.realpathSync = function realpathSync(p, cache) { // make p is absolute @@ -984,13 +986,30 @@ fs.realpathSync = function realpathSync(p, cache) { knownHard = {}; // current character position in p - var pos = 0; + var pos; // the partial path so far, including a trailing slash if any - var current = ''; - // the partial path without a trailing slash - var base = ''; + var current; + // the partial path without a trailing slash (except when pointing at a root) + var base; // the partial path scanned in the previous round, with slash - var previous = ''; + var previous; + + start(); + + function start() { + // Skip over roots + var m = splitRootRe.exec(p); + pos = m[0].length; + current = m[0]; + base = m[0]; + previous = ''; + + // On windows, check that the root exists. On unix there is no need. + if (isWindows && !knownHard[base]) { + fs.lstatSync(base); + knownHard[base] = true; + } + } // walk down the path, swapping out linked pathparts for their real // values @@ -1004,16 +1023,8 @@ fs.realpathSync = function realpathSync(p, cache) { base = previous + result[1]; pos = nextPartRe.lastIndex; - // continue if not a symlink, or if root - var isRoot = !base; - if (isWindows) { - // if it doens't have a tail, then it's the root. - var split = base.match(splitDeviceRe); - if (split) { - isRoot = !split[2]; - } - } - if (isRoot || knownHard[base] || (cache && cache[base] === base)) { + // continue if not a symlink + if (knownHard[base] || (cache && cache[base] === base)) { continue; } @@ -1050,8 +1061,7 @@ fs.realpathSync = function realpathSync(p, cache) { // resolve the link, then start over p = pathModule.resolve(resolvedLink, p.slice(pos)); - pos = 0; - previous = base = current = ''; + start(); } if (cache) cache[original] = p; @@ -1078,17 +1088,38 @@ fs.realpath = function realpath(p, cache, cb) { knownHard = {}; // current character position in p - var pos = 0; + var pos; // the partial path so far, including a trailing slash if any - var current = ''; - // the partial path without a trailing slash - var base = ''; + var current; + // the partial path without a trailing slash (except when pointing at a root) + var base; // the partial path scanned in the previous round, with slash - var previous = ''; + var previous; + + start(); + + function start() { + // Skip over roots + var m = splitRootRe.exec(p); + pos = m[0].length; + current = m[0]; + base = m[0]; + previous = ''; + + // On windows, check that the root exists. On unix there is no need. + if (isWindows && !knownHard[base]) { + fs.lstat(base, function (err) { + if (err) return cb(err); + knownHard[base] = true; + LOOP(); + }); + } else { + process.nextTick(LOOP); + } + } // walk down the path, swapping out linked pathparts for their real // values - return process.nextTick(LOOP); function LOOP() { // stop if scanned past end of path if (pos >= p.length) { @@ -1104,16 +1135,8 @@ fs.realpath = function realpath(p, cache, cb) { base = previous + result[1]; pos = nextPartRe.lastIndex; - // continue if not a symlink, or if root - var isRoot = !base; - if (isWindows) { - // if it doens't have a tail, then it's the root. - var split = base.match(splitDeviceRe); - if (split) { - isRoot = !split[2]; - } - } - if (isRoot || knownHard[base] || (cache && cache[base] === base)) { + // continue if not a symlink + if (knownHard[base] || (cache && cache[base] === base)) { return process.nextTick(LOOP); } @@ -1163,13 +1186,9 @@ fs.realpath = function realpath(p, cache, cb) { } function gotResolvedLink(resolvedLink) { - // resolve the link, then start over p = pathModule.resolve(resolvedLink, p.slice(pos)); - pos = 0; - previous = base = current = ''; - - return process.nextTick(LOOP); + start(); } }; diff --git a/test/simple/test-regress-GH-3542.js b/test/simple/test-regress-GH-3542.js new file mode 100644 index 00000000000..90e0b78501a --- /dev/null +++ b/test/simple/test-regress-GH-3542.js @@ -0,0 +1,54 @@ +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + +// This test is only relevant on Windows. +if (process.platform !== 'win32') { + return process.exit(0); +} + +var common = require('../common.js'), + assert = require('assert'), + fs = require('fs'), + path = require('path'), + succeeded = 0; + +function test(p) { + var result = fs.realpathSync(p); + assert.strictEqual(result, path.resolve(p)); + + fs.realpath(p, function(err, result) { + assert.ok(!err); + assert.strictEqual(result, path.resolve(p)); + succeeded++; + }); +} + +test('//localhost/c$/windows/system32'); +test('//localhost/c$/windows'); +test('//localhost/c$/') +test('\\\\localhost\\c$') +test('c:\\'); +test('c:'); +test(process.env.windir); + +process.on('exit', function() { + assert.strictEqual(succeeded, 7); +}); \ No newline at end of file