fs: resolve junction targets relative to their parent

PR-URL: https://github.com/joyent/node/pull/8813
Reviewed-By: Bert Belder <bertbelder@gmail.com>
This commit is contained in:
Vincent Weevers 2014-12-03 15:38:53 +01:00 committed by Bert Belder
parent b4d6203b38
commit 764c5c7fd1
2 changed files with 75 additions and 3 deletions

View File

@ -734,12 +734,14 @@ fs.readlinkSync = function(path) {
return binding.readlink(pathModule._makeLong(path));
};
function preprocessSymlinkDestination(path, type) {
function preprocessSymlinkDestination(path, type, linkPath) {
if (!isWindows) {
// No preprocessing is needed on Unix.
return path;
} else if (type === 'junction') {
// Junctions paths need to be absolute and \\?\-prefixed.
// A relative target is relative to the link's parent directory.
path = pathModule.resolve(linkPath, '..', path);
return pathModule._makeLong(path);
} else {
// Windows symlinks don't tolerate forward slashes.
@ -754,7 +756,7 @@ fs.symlink = function(destination, path, type_, callback) {
if (!nullCheck(destination, callback)) return;
if (!nullCheck(path, callback)) return;
binding.symlink(preprocessSymlinkDestination(destination, type),
binding.symlink(preprocessSymlinkDestination(destination, type, path),
pathModule._makeLong(path),
type,
callback);
@ -766,7 +768,7 @@ fs.symlinkSync = function(destination, path, type) {
nullCheck(destination);
nullCheck(path);
return binding.symlink(preprocessSymlinkDestination(destination, type),
return binding.symlink(preprocessSymlinkDestination(destination, type, path),
pathModule._makeLong(path),
type);
};

View File

@ -0,0 +1,70 @@
// 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.
var common = require('../common');
var assert = require('assert');
var path = require('path');
var fs = require('fs');
var completed = 0;
var expected_tests = 4;
// test creating and reading symbolic link
var linkData = path.join(common.fixturesDir, 'cycles/');
var linkPath = path.join(common.tmpDir, 'cycles_link');
var relative = '../fixtures/cycles'
// Delete previously created link
try {
fs.unlinkSync(linkPath);
} catch (e) {}
console.log('linkData: ' + linkData);
console.log('linkPath: ' + linkPath);
console.log('relative target: ' + relative)
fs.symlink(relative, linkPath, 'junction', function(err) {
if (err) throw err;
completed++;
fs.lstat(linkPath, function(err, stats) {
if (err) throw err;
assert.ok(stats.isSymbolicLink());
completed++;
fs.readlink(linkPath, function(err, destination) {
if (err) throw err;
assert.equal(destination, linkData);
completed++;
fs.unlink(linkPath, function(err) {
if (err) throw err;
assert(!fs.existsSync(linkPath));
assert(fs.existsSync(linkData));
completed++;
});
});
});
});
process.on('exit', function() {
assert.equal(completed, expected_tests);
});