Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
windows: make fs.realpath(Sync) work with UNC paths
Browse files Browse the repository at this point in the history
Closes #3542
  • Loading branch information
piscisaureus committed Jun 26, 2012
1 parent f00c8bc commit 0cdeb8e
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 42 deletions.
103 changes: 61 additions & 42 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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);
}

Expand Down Expand Up @@ -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();
}
};

Expand Down
54 changes: 54 additions & 0 deletions test/simple/test-regress-GH-3542.js
Original file line number Diff line number Diff line change
@@ -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);
});

0 comments on commit 0cdeb8e

Please sign in to comment.