From 686d9f9cc6688b4febf8ebc4655c1d1bc3515829 Mon Sep 17 00:00:00 2001 From: isaacs Date: Tue, 14 Sep 2010 09:23:48 -0700 Subject: [PATCH] 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? --- lib/fs.js | 4 +-- test/simple/test-fs-realpath.js | 54 ++++++++++++++++++++++++++++++++- 2 files changed, 55 insertions(+), 3 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index abbb11d673..4e1d8b9f66 100644 --- a/lib/fs.js +++ b/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); } diff --git a/test/simple/test-fs-realpath.js b/test/simple/test-fs-realpath.js index dc08fd63fd..50e400a0f9 100644 --- a/test/simple/test-fs-realpath.js +++ b/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) {