Browse Source

Rewrote realpath implementation solving all known failing tests (also added a bunch of new test cases)

v0.7.4-release
Rasmus Andersson 15 years ago
committed by Ryan Dahl
parent
commit
5c602b750a
  1. 174
      lib/fs.js
  2. 287
      test/simple/test-fs-realpath.js

174
lib/fs.js

@ -292,86 +292,132 @@ exports.unwatchFile = function (filename) {
// Realpath // Realpath
var path = require('path'); var path = require('path');
var dirname = path.dirname, var normalize = path.normalize
basename = path.basename, normalizeArray = path.normalizeArray;
normalize = path.normalize;
exports.realpathSync = function (path) {
function readlinkDeepSync(path, stats) { var seen_links = {}, knownHards = {}, buf, i = 0, part, x, stats;
var seen_links = {}, resolved_link, stats, file_id; if (path.charAt(0) !== '/') {
while (true) { var cwd = process.cwd().split('/');
stats = stats || exports.lstatSync(path); path = cwd.concat(path.split('/'));
file_id = stats.dev.toString(32)+":"+stats.ino.toString(32); path = normalizeArray(path);
if (file_id in seen_links) { i = cwd.length;
throw new Error("cyclic symbolic link at "+path); buf = [].concat(cwd);
} else { } else {
seen_links[file_id] = 1; path = normalizeArray(path.split('/'));
buf = [''];
}
for (; i<path.length; i++) {
part = path.slice(0, i+1).join('/');
if (part.length !== 0) {
if (part in knownHards) {
buf.push(path[i]);
} else {
stats = exports.lstatSync(part);
if (stats.isSymbolicLink()) { if (stats.isSymbolicLink()) {
var newpath = exports.readlinkSync(path); x = stats.dev.toString(32)+":"+stats.ino.toString(32);
if (newpath.charAt(0) === '/') { if (x in seen_links)
path = newpath; throw new Error("cyclic link at "+part);
seen_links[x] = true;
part = exports.readlinkSync(part);
if (part.charAt(0) === '/') {
// absolute
path = normalizeArray(part.split('/'));
buf = [''];
i = 0;
} else {
// relative
Array.prototype.splice.apply(path, [i, 1].concat(part.split('/')));
part = normalizeArray(path);
var y = 0, L = Math.max(path.length, part.length), delta;
for (; y<L && path[y] === part[y]; y++);
if (y !== L) {
path = part;
delta = i-y;
i = y-1;
if (delta > 0) buf.splice(y, delta);
} else { } else {
var dir = dirname(path); i--;
path = (dir !== '') ? dir + '/' + newpath : newpath; }
} }
} else { } else {
return normalize(path); buf.push(path[i]);
knownHards[buf.join('/')] = true;
}
} }
} }
stats = null;
} }
return buf.join('/');
} }
function readlinkDeep(path, stats, callback) {
var seen_links = {}, resolved_link, file_id; exports.realpath = function (path, callback) {
function next(stats) { var seen_links = {}, knownHards = {}, buf = [''], i = 0, part, x;
file_id = stats.dev.toString(32)+":"+stats.ino.toString(32); if (path.charAt(0) !== '/') {
if (file_id in seen_links) { // assumes cwd is canonical
callback(new Error("cyclic symbolic link at "+path)); var cwd = process.cwd().split('/');
path = cwd.concat(path.split('/'));
path = normalizeArray(path);
i = cwd.length-1;
buf = [].concat(cwd);
} else {
path = normalizeArray(path.split('/'));
}
function done(err) {
if (callback) {
if (!err) callback(err, buf.join('/'));
else callback(err);
}
}
function next() {
if (++i === path.length) return done();
part = path.slice(0, i+1).join('/');
if (part.length === 0) return next();
if (part in knownHards) {
buf.push(path[i]);
next();
} else { } else {
seen_links[file_id] = 1; exports.lstat(part, function(err, stats){
if (err) return done(err);
if (stats.isSymbolicLink()) { if (stats.isSymbolicLink()) {
exports.readlink(path, function(err, newpath) { x = stats.dev.toString(32)+":"+stats.ino.toString(32);
if (err) callback(err); if (x in seen_links)
if (newpath.charAt(0) === '/') { return done(new Error("cyclic link at "+part));
path = newpath; seen_links[x] = true;
exports.readlink(part, function(err, npart){
if (err) return done(err);
part = npart;
if (part.charAt(0) === '/') {
// absolute
path = normalizeArray(part.split('/'));
buf = [''];
i = 0;
} else { } else {
var dir = dirname(path); // relative
path = (dir !== '') ? dir + '/' + newpath : newpath; Array.prototype.splice.apply(path, [i, 1].concat(part.split('/')));
part = normalizeArray(path);
var y = 0, L = Math.max(path.length, part.length), delta;
for (; y<L && path[y] === part[y]; y++);
if (y !== L) {
path = part;
delta = i-y;
i = y-1; // resolve new node if needed
if (delta > 0) buf.splice(y, delta);
} }
_next(); else {
}); i--; // resolve new node if needed
} else {
callback(null, normalize(path));
} }
} }
next();
}); // fs.readlink
} }
function _next() { else {
exports.lstat(path, function(err, stats){ buf.push(path[i]);
if (err) callback(err); knownHards[buf.join('/')] = true;
else next(stats); next();
});
} }
if (stats) next(stats); }); // fs.lstat
else _next(); }
} }
next();
exports.realpathSync = function(path) {
var stats = exports.lstatSync(path);
if (stats.isSymbolicLink())
return readlinkDeepSync(path, stats);
else
return normalize(path);
}
exports.realpath = function(path, callback) {
var resolved_path = path;
if (!callback) return;
exports.lstat(path, function(err, stats){
if (err)
callback(err);
else if (stats.isSymbolicLink())
readlinkDeep(path, stats, callback);
else
callback(null, normalize(path));
});
} }

287
test/simple/test-fs-realpath.js

@ -1,56 +1,251 @@
process.mixin(require("../common")); process.mixin(require("../common"));
var async_completed = 0, async_expected = 0; var async_completed = 0, async_expected = 0, unlink = [];
// a. deep relative file symlink
var dstPath = path.join(fixturesDir, 'cycles', 'root.js');
var linkData1 = "../../cycles/root.js";
var linkPath1 = path.join(fixturesDir, "nested-index", 'one', 'symlink1.js');
try {fs.unlinkSync(linkPath1);}catch(e){}
fs.symlinkSync(linkData1, linkPath1);
var linkData2 = "../one/symlink1.js";
var linkPath2 = path.join(fixturesDir, "nested-index", 'two', 'symlink1-b.js');
try {fs.unlinkSync(linkPath2);}catch(e){}
fs.symlinkSync(linkData2, linkPath2);
// b. deep relative directory symlink
var dstPath_b = path.join(fixturesDir, 'cycles', 'folder');
var linkData1b = "../../cycles/folder";
var linkPath1b = path.join(fixturesDir, "nested-index", 'one', 'symlink1-dir');
try {fs.unlinkSync(linkPath1b);}catch(e){}
fs.symlinkSync(linkData1b, linkPath1b);
var linkData2b = "../one/symlink1-dir";
var linkPath2b = path.join(fixturesDir, "nested-index", 'two', 'symlink12-dir');
try {fs.unlinkSync(linkPath2b);}catch(e){}
fs.symlinkSync(linkData2b, linkPath2b);
assert.equal(fs.realpathSync(linkPath2), dstPath);
assert.equal(fs.realpathSync(linkPath2b), dstPath_b);
async_expected++;
fs.realpath(linkPath2, function(err, rpath) {
if (err) throw err;
assert.equal(rpath, dstPath);
async_completed++;
});
async_expected++; function asynctest(testBlock, args, callback, assertBlock) {
fs.realpath(linkPath2b, function(err, rpath) { async_expected++;
if (err) throw err; testBlock.apply(testBlock, args.concat([function(err){
assert.equal(rpath, dstPath_b); var ignoreError = false;
if (assertBlock) {
try {
ignoreError = assertBlock.apply(assertBlock,
Array.prototype.slice.call(arguments));
}
catch (e) {
err = e;
}
}
async_completed++; async_completed++;
}); callback(ignoreError ? null : err);
}]));
}
// todo: test shallow symlinks (file & dir) function bashRealpath(path, callback) {
// todo: test non-symlinks (file & dir) exec("cd '"+path.replace("'","\\'")+"' && pwd -P",function (err, o) {
// todo: test error on cyclic symlinks callback(err, o.trim());
});
}
process.addListener("exit", function () { // sub-tests:
function test_simple_relative_symlink(callback) {
var entry = fixturesDir+'/cycles/symlink',
expected = fixturesDir+'/cycles/root.js';
[
[entry, 'root.js'],
].forEach(function(t) {
try {fs.unlinkSync(t[0]);}catch(e){}
fs.symlinkSync(t[1], t[0]);
unlink.push(t[0]);
});
var result = fs.realpathSync(entry);
assert.equal(result, expected,
'got '+inspect(result)+' expected '+inspect(expected));
asynctest(fs.realpath, [entry], callback, function(err, result){
assert.equal(result, expected,
'got '+inspect(result)+' expected '+inspect(expected));
});
}
function test_simple_absolute_symlink(callback) {
bashRealpath(fixturesDir, function(err, fixturesAbsDir) {
if (err) return callback(err);
var entry = fixturesAbsDir+'/cycles/symlink',
expected = fixturesAbsDir+'/nested-index/one/index.js';
[
[entry, expected],
].forEach(function(t) {
try {fs.unlinkSync(t[0]);}catch(e){}
fs.symlinkSync(t[1], t[0]);
unlink.push(t[0]);
});
var result = fs.realpathSync(entry);
assert.equal(result, expected,
'got '+inspect(result)+' expected '+inspect(expected));
asynctest(fs.realpath, [entry], callback, function(err, result){
assert.equal(result, expected,
'got '+inspect(result)+' expected '+inspect(expected));
});
});
}
function test_deep_relative_file_symlink(callback) {
var expected = path.join(fixturesDir, 'cycles', 'root.js');
var linkData1 = "../../cycles/root.js";
var linkPath1 = path.join(fixturesDir, "nested-index", 'one', 'symlink1.js');
try {fs.unlinkSync(linkPath1);}catch(e){} try {fs.unlinkSync(linkPath1);}catch(e){}
try {fs.unlinkSync(linkPath2);}catch(e){} fs.symlinkSync(linkData1, linkPath1);
var linkData2 = "../one/symlink1.js";
var entry = path.join(fixturesDir, "nested-index", 'two', 'symlink1-b.js');
try {fs.unlinkSync(entry);}catch(e){}
fs.symlinkSync(linkData2, entry);
unlink.push(linkPath1);
unlink.push(entry);
assert.equal(fs.realpathSync(entry), expected);
asynctest(fs.realpath, [entry], callback, function(err, result){
assert.equal(result, expected,
'got '+inspect(result)+' expected '+inspect(expected));
});
}
function test_deep_relative_dir_symlink(callback) {
var expected = path.join(fixturesDir, 'cycles', 'folder');
var linkData1b = "../../cycles/folder";
var linkPath1b = path.join(fixturesDir, "nested-index", 'one', 'symlink1-dir');
try {fs.unlinkSync(linkPath1b);}catch(e){} try {fs.unlinkSync(linkPath1b);}catch(e){}
try {fs.unlinkSync(linkPath2b);}catch(e){} fs.symlinkSync(linkData1b, linkPath1b);
var linkData2b = "../one/symlink1-dir";
var entry = path.join(fixturesDir, "nested-index", 'two', 'symlink12-dir');
try {fs.unlinkSync(entry);}catch(e){}
fs.symlinkSync(linkData2b, entry);
unlink.push(linkPath1b);
unlink.push(entry);
assert.equal(fs.realpathSync(entry), expected);
asynctest(fs.realpath, [entry], callback, function(err, result){
assert.equal(result, expected,
'got '+inspect(result)+' expected '+inspect(expected));
});
}
function test_cyclic_link_protection(callback) {
var entry = fixturesDir+'/cycles/realpath-3a';
[
[entry, '../cycles/realpath-3b'],
[fixturesDir+'/cycles/realpath-3b', '../cycles/realpath-3c'],
[fixturesDir+'/cycles/realpath-3c', '../cycles/realpath-3a'],
].forEach(function(t) {
try {fs.unlinkSync(t[0]);}catch(e){}
fs.symlinkSync(t[1], t[0]);
unlink.push(t[0]);
});
assert.throws(function(){ fs.realpathSync(entry); });
asynctest(fs.realpath, [entry], callback, function(err, result){
assert.ok(err && true);
return true;
});
}
function test_relative_input_cwd(callback) {
var p = fixturesDir.lastIndexOf('/');
var entrydir = fixturesDir.substr(0, p);
var entry = fixturesDir.substr(p+1)+'/cycles/realpath-3a';
var expected = fixturesDir+'/cycles/root.js';
[
[entry, '../cycles/realpath-3b'],
[fixturesDir+'/cycles/realpath-3b', '../cycles/realpath-3c'],
[fixturesDir+'/cycles/realpath-3c', 'root.js'],
].forEach(function(t) {
var fn = t[0];
if (fn.charAt(0) !== '/') fn = entrydir + '/' + fn;
try {fs.unlinkSync(fn);}catch(e){}
fs.symlinkSync(t[1], fn);
unlink.push(fn);
});
var origcwd = process.cwd();
process.chdir(entrydir);
assert.equal(fs.realpathSync(entry), expected);
asynctest(fs.realpath, [entry], callback, function(err, result){
process.chdir(origcwd);
assert.equal(result, expected,
'got '+inspect(result)+' expected '+inspect(expected));
return true;
});
}
function test_deep_symlink_mix(callback) {
// todo: check to see that fixturesDir is not rooted in the
// same directory as our test symlink.
// obtain our current realpath using bash (so we can test ourselves)
bashRealpath(fixturesDir, function(err, fixturesAbsDir) {
if (err) return callback(err);
/*
/tmp/node-test-realpath-f1 -> ../tmp/node-test-realpath-d1/foo
/tmp/node-test-realpath-d1 -> ../node-test-realpath-d2
/tmp/node-test-realpath-d2/foo -> ../node-test-realpath-f2
/tmp/node-test-realpath-f2
-> /node/test/fixtures/nested-index/one/realpath-c
/node/test/fixtures/nested-index/one/realpath-c
-> /node/test/fixtures/nested-index/two/realpath-c
/node/test/fixtures/nested-index/two/realpath-c -> ../../cycles/root.js
/node/test/fixtures/cycles/root.js (hard)
*/
var entry = '/tmp/node-test-realpath-f1';
try {fs.unlinkSync('/tmp/node-test-realpath-d2/foo');}catch(e){}
try {fs.rmdirSync('/tmp/node-test-realpath-d2');}catch(e){}
fs.mkdirSync('/tmp/node-test-realpath-d2', 0700);
try {
[
[entry, '../tmp/node-test-realpath-d1/foo'],
['/tmp/node-test-realpath-d1', '../tmp/node-test-realpath-d2'],
['/tmp/node-test-realpath-d2/foo', '../node-test-realpath-f2'],
['/tmp/node-test-realpath-f2', fixturesAbsDir+'/nested-index/one/realpath-c'],
[fixturesAbsDir+'/nested-index/one/realpath-c', fixturesAbsDir+'/nested-index/two/realpath-c'],
[fixturesAbsDir+'/nested-index/two/realpath-c', '../../cycles/root.js'],
].forEach(function(t) {
//debug('setting up '+t[0]+' -> '+t[1]);
try {fs.unlinkSync(t[0]);}catch(e){}
fs.symlinkSync(t[1], t[0]);
unlink.push(t[0]);
});
} finally {
unlink.push('/tmp/node-test-realpath-d2');
}
var expected = fixturesAbsDir+'/cycles/root.js';
assert.equal(fs.realpathSync(entry), expected);
asynctest(fs.realpath, [entry], callback, function(err, result){
assert.equal(result, expected,
'got '+inspect(result)+' expected '+inspect(expected));
return true;
});
});
}
function test_non_symlinks(callback) {
bashRealpath(fixturesDir, function(err, fixturesAbsDir) {
if (err) return callback(err);
var p = fixturesAbsDir.lastIndexOf('/');
var entrydir = fixturesAbsDir.substr(0, p);
var entry = fixturesAbsDir.substr(p+1)+'/cycles/root.js';
var expected = fixturesAbsDir+'/cycles/root.js';
var origcwd = process.cwd();
process.chdir(entrydir);
assert.equal(fs.realpathSync(entry), expected);
asynctest(fs.realpath, [entry], callback, function(err, result){
process.chdir(origcwd);
assert.equal(result, expected,
'got '+inspect(result)+' expected '+inspect(expected));
return true;
});
});
}
// ----------------------------------------------------------------------------
var tests = [
test_simple_relative_symlink,
test_simple_absolute_symlink,
test_deep_relative_file_symlink,
test_deep_relative_dir_symlink,
test_cyclic_link_protection,
test_relative_input_cwd,
test_deep_symlink_mix,
test_non_symlinks,
];
var numtests = tests.length;
function runNextTest(err) {
if (err) throw err;
var test = tests.shift()
if (!test) puts(numtests+' subtests completed OK for fs.realpath');
else test(runNextTest);
}
runNextTest();
process.addListener("exit", function () {
unlink.forEach(function(path){ try {fs.unlinkSync(path);}catch(e){} });
assert.equal(async_completed, async_expected); assert.equal(async_completed, async_expected);
}); });

Loading…
Cancel
Save