Bug in realpath with symlinks to absolute folder paths which have children.
authorisaacs <i@izs.me>
Tue, 14 Sep 2010 16:23:48 +0000 (09:23 -0700)
committerRyan Dahl <ry@tinyclouds.org>
Tue, 14 Sep 2010 17:27:57 +0000 (10:27 -0700)
Found by Cliffano Subagio
http://groups.google.com/group/nodejs/browse_thread/thread/f46f093938265ac0/387e14da08c7dd7b?

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

index abbb11d6734c376bcc7181d5722cf5bef1577f9a..4e1d8b9f66d4e1f67b6410a8c8d79ac2a27c9ca9 100644 (file)
--- 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);
     }
index dc08fd63fd35fb1c23b03bbee24747e9cbbb3bca..50e400a0f963d20dd66f755fa82284b8b1a9c34e 100644 (file)
@@ -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) {