parent
f00c8bcef8
commit
0cdeb8ed96
103
lib/fs.js
103
lib/fs.js
@ -966,10 +966,12 @@ if (isWindows) {
|
|||||||
var nextPartRe = /(.*?)(?:[\/]+|$)/g;
|
var nextPartRe = /(.*?)(?:[\/]+|$)/g;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Regex to split a windows path into three parts: [*, device, slash,
|
// Regex to find the device root, including trailing slash. E.g. 'c:\\'.
|
||||||
// tail] windows-only
|
if (isWindows) {
|
||||||
var splitDeviceRe =
|
var splitRootRe = /^(?:[a-zA-Z]:|[\\\/]{2}[^\\\/]+[\\\/][^\\\/]+)?[\\\/]*/;
|
||||||
/^([a-zA-Z]:|[\\\/]{2}[^\\\/]+[\\\/][^\\\/]+)?([\\\/])?([\s\S]*?)$/;
|
} else {
|
||||||
|
var splitRootRe = /^[\/]*/;
|
||||||
|
}
|
||||||
|
|
||||||
fs.realpathSync = function realpathSync(p, cache) {
|
fs.realpathSync = function realpathSync(p, cache) {
|
||||||
// make p is absolute
|
// make p is absolute
|
||||||
@ -984,13 +986,30 @@ fs.realpathSync = function realpathSync(p, cache) {
|
|||||||
knownHard = {};
|
knownHard = {};
|
||||||
|
|
||||||
// current character position in p
|
// current character position in p
|
||||||
var pos = 0;
|
var pos;
|
||||||
// the partial path so far, including a trailing slash if any
|
// the partial path so far, including a trailing slash if any
|
||||||
var current = '';
|
var current;
|
||||||
// the partial path without a trailing slash
|
// the partial path without a trailing slash (except when pointing at a root)
|
||||||
var base = '';
|
var base;
|
||||||
// the partial path scanned in the previous round, with slash
|
// 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
|
// walk down the path, swapping out linked pathparts for their real
|
||||||
// values
|
// values
|
||||||
@ -1004,16 +1023,8 @@ fs.realpathSync = function realpathSync(p, cache) {
|
|||||||
base = previous + result[1];
|
base = previous + result[1];
|
||||||
pos = nextPartRe.lastIndex;
|
pos = nextPartRe.lastIndex;
|
||||||
|
|
||||||
// continue if not a symlink, or if root
|
// continue if not a symlink
|
||||||
var isRoot = !base;
|
if (knownHard[base] || (cache && cache[base] === 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;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1050,8 +1061,7 @@ fs.realpathSync = function realpathSync(p, cache) {
|
|||||||
|
|
||||||
// resolve the link, then start over
|
// resolve the link, then start over
|
||||||
p = pathModule.resolve(resolvedLink, p.slice(pos));
|
p = pathModule.resolve(resolvedLink, p.slice(pos));
|
||||||
pos = 0;
|
start();
|
||||||
previous = base = current = '';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cache) cache[original] = p;
|
if (cache) cache[original] = p;
|
||||||
@ -1078,17 +1088,38 @@ fs.realpath = function realpath(p, cache, cb) {
|
|||||||
knownHard = {};
|
knownHard = {};
|
||||||
|
|
||||||
// current character position in p
|
// current character position in p
|
||||||
var pos = 0;
|
var pos;
|
||||||
// the partial path so far, including a trailing slash if any
|
// the partial path so far, including a trailing slash if any
|
||||||
var current = '';
|
var current;
|
||||||
// the partial path without a trailing slash
|
// the partial path without a trailing slash (except when pointing at a root)
|
||||||
var base = '';
|
var base;
|
||||||
// the partial path scanned in the previous round, with slash
|
// 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
|
// walk down the path, swapping out linked pathparts for their real
|
||||||
// values
|
// values
|
||||||
return process.nextTick(LOOP);
|
|
||||||
function LOOP() {
|
function LOOP() {
|
||||||
// stop if scanned past end of path
|
// stop if scanned past end of path
|
||||||
if (pos >= p.length) {
|
if (pos >= p.length) {
|
||||||
@ -1104,16 +1135,8 @@ fs.realpath = function realpath(p, cache, cb) {
|
|||||||
base = previous + result[1];
|
base = previous + result[1];
|
||||||
pos = nextPartRe.lastIndex;
|
pos = nextPartRe.lastIndex;
|
||||||
|
|
||||||
// continue if not a symlink, or if root
|
// continue if not a symlink
|
||||||
var isRoot = !base;
|
if (knownHard[base] || (cache && cache[base] === 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)) {
|
|
||||||
return process.nextTick(LOOP);
|
return process.nextTick(LOOP);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1163,13 +1186,9 @@ fs.realpath = function realpath(p, cache, cb) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function gotResolvedLink(resolvedLink) {
|
function gotResolvedLink(resolvedLink) {
|
||||||
|
|
||||||
// resolve the link, then start over
|
// resolve the link, then start over
|
||||||
p = pathModule.resolve(resolvedLink, p.slice(pos));
|
p = pathModule.resolve(resolvedLink, p.slice(pos));
|
||||||
pos = 0;
|
start();
|
||||||
previous = base = current = '';
|
|
||||||
|
|
||||||
return process.nextTick(LOOP);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
54
test/simple/test-regress-GH-3542.js
Normal file
54
test/simple/test-regress-GH-3542.js
Normal file
@ -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);
|
||||||
|
});
|
Loading…
x
Reference in New Issue
Block a user