From 39e25528eb769c36e2f8fa534861a23822c819fb Mon Sep 17 00:00:00 2001 From: Igor Zinkovsky Date: Tue, 22 May 2012 16:02:10 -0700 Subject: [PATCH] windows: enable creating directory junctions with fs.symlink --- doc/api/fs.markdown | 4 +- lib/fs.js | 18 ++++-- src/node_file.cc | 7 ++- test/simple/test-fs-symlink-dir-junction.js | 68 +++++++++++++++++++++ 4 files changed, 91 insertions(+), 6 deletions(-) create mode 100644 test/simple/test-fs-symlink-dir-junction.js diff --git a/doc/api/fs.markdown b/doc/api/fs.markdown index 0d62956d838..0554eef5170 100644 --- a/doc/api/fs.markdown +++ b/doc/api/fs.markdown @@ -178,8 +178,10 @@ Synchronous link(2). Asynchronous symlink(2). No arguments other than a possible exception are given to the completion callback. -`type` argument can be either `'dir'` or `'file'` (default is `'file'`). It is only +`type` argument can be either `'dir'`, `'file'`, or `'junction'` (default is `'file'`). It is only used on Windows (ignored on other platforms). +Note that Windows junction points require the destination path to be absolute. When using +`'junction'`, the `destination` argument will automatically be normalized to absolute path. ## fs.symlinkSync(destination, path, [type]) diff --git a/lib/fs.js b/lib/fs.js index f4d9a1ef8fe..ec55c19b46a 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -50,6 +50,8 @@ var O_SYNC = constants.O_SYNC || 0; var O_TRUNC = constants.O_TRUNC || 0; var O_WRONLY = constants.O_WRONLY || 0; +var isWindows = process.platform === 'win32' + fs.Stats = binding.Stats; fs.Stats.prototype._checkModeProperty = function(property) { @@ -477,12 +479,21 @@ fs.symlink = function(destination, path, type_, callback) { var type = (typeof(type_) == 'string' ? type_ : null); var callback_ = arguments[arguments.length - 1]; callback = (typeof(callback_) == 'function' ? callback_ : null); - binding.symlink(pathModule._makeLong(destination), + + if (isWindows && type === 'junction') { + destination = pathModule._makeLong(destination); + } + + binding.symlink(destination, pathModule._makeLong(path), type, callback); }; fs.symlinkSync = function(destination, path, type) { - return binding.symlink(pathModule._makeLong(destination), + if (isWindows && type === 'junction') { + destination = pathModule._makeLong(destination); + } + + return binding.symlink(destination, pathModule._makeLong(path), type); }; @@ -867,8 +878,7 @@ fs.unwatchFile = function(filename) { // Not using realpath(2) because it's bad. // See: http://insanecoding.blogspot.com/2007/11/pathmax-simply-isnt.html -var normalize = pathModule.normalize, - isWindows = process.platform === 'win32'; +var normalize = pathModule.normalize; if (isWindows) { // Node doesn't support symlinks / lstat on windows. Hence realpath is just diff --git a/src/node_file.cc b/src/node_file.cc index 73a97a11dc9..65f07a5bd90 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -414,8 +414,13 @@ static Handle Symlink(const Arguments& args) { if (args[2]->IsString()) { String::Utf8Value mode(args[2]); - if (memcmp(*mode, "dir\0", 4) == 0) { + if (strcmp(*mode, "dir") == 0) { flags |= UV_FS_SYMLINK_DIR; + } else if (strcmp(*mode, "junction") == 0) { + flags |= UV_FS_SYMLINK_JUNCTION; + } else if (strcmp(*mode, "file") != 0) { + return ThrowException(Exception::Error( + String::New("Unknown symlink type"))); } } diff --git a/test/simple/test-fs-symlink-dir-junction.js b/test/simple/test-fs-symlink-dir-junction.js new file mode 100644 index 00000000000..c675b920e3e --- /dev/null +++ b/test/simple/test-fs-symlink-dir-junction.js @@ -0,0 +1,68 @@ +// 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'); + +// Delete previously created link +try { + fs.unlinkSync(linkPath); +} catch (e) {} + +console.log('linkData: ' + linkData); +console.log('linkPath: ' + linkPath); + +fs.symlink(linkData, 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); +}); +