Browse Source

Bug in realpath with symlinks to absolute folder paths which have children.

Found by Cliffano Subagio
http://groups.google.com/group/nodejs/browse_thread/thread/f46f093938265ac0/387e14da08c7dd7b?
v0.7.4-release
isaacs 14 years ago
committed by Ryan Dahl
parent
commit
686d9f9cc6
  1. 4
      lib/fs.js
  2. 54
      test/simple/test-fs-realpath.js

4
lib/fs.js

@ -508,7 +508,7 @@ function realpathSync (p) {
if (target.charAt(0) === '/') {
// absolute. Start over.
buf = [''];
p = path.normalizeArray(target.split('/'));
p = path.normalizeArray(target.split('/').concat(p.slice(i + 1)));
i = 0;
continue;
}
@ -568,7 +568,7 @@ function realpath (p, cb) {
if (target.charAt(0) === '/') {
// absolute. Start over.
buf = [''];
p = path.normalizeArray(target.split('/'));
p = path.normalizeArray(target.split('/').concat(p.slice(i + 1)));
i = 0;
return process.nextTick(LOOP);
}

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

@ -237,6 +237,57 @@ var uponeActual = fs.realpathSync("..");
assert.equal(upone, uponeActual,
"realpathSync('..') expected: "+upone+" actual:"+uponeActual);
// absolute symlinks with children.
// .
// `-- a/
// |-- b/
// | `-- c/
// | `-- x.txt
// `-- link -> /tmp/node-test-realpath-abs-kids/a/b/
// realpath(root+'/a/link/c/x.txt') ==> root+'/a/b/c/x.txt'
function test_abs_with_kids (cb) {
bashRealpath(common.fixturesDir, function(err, fixturesAbsDir) {
var root = fixturesAbsDir+'/node-test-realpath-abs-kids';
function cleanup () {
;['/a/b/c/x.txt'
, '/a/link'
].forEach(function (file) {
try {fs.unlinkSync(root+file)} catch (ex) {}
});
;['/a/b/c'
, '/a/b'
, '/a'
, ''
].forEach(function (folder) {
try {fs.rmdirSync(root+folder)} catch (ex) {}
});
}
function setup () {
cleanup()
;[''
, '/a'
, '/a/b'
, '/a/b/c'
].forEach(function (folder) {
console.log("mkdir "+root+folder)
fs.mkdirSync(root+folder, 0700);
});
fs.writeFileSync(root+'/a/b/c/x.txt', 'foo');
fs.symlinkSync(root+'/a/b', root+'/a/link');
}
setup();
var linkPath = root+'/a/link/c/x.txt';
var expectPath = root+'/a/b/c/x.txt';
var actual = fs.realpathSync(linkPath);
// console.log({link:linkPath,expect:expectPath,actual:actual},'sync');
assert.equal(actual, expectPath);
asynctest(fs.realpath, [linkPath], cb, function (er, actual) {
// console.log({link:linkPath,expect:expectPath,actual:actual},'async');
assert.equal(actual, expectPath);
cleanup();
});
})
}
// ----------------------------------------------------------------------------
@ -249,7 +300,8 @@ var tests = [
test_relative_input_cwd,
test_deep_symlink_mix,
test_non_symlinks,
test_escape_cwd
test_escape_cwd,
test_abs_with_kids
];
var numtests = tests.length;
function runNextTest(err) {

Loading…
Cancel
Save