Browse Source

windows: enable creating directory junctions with fs.symlink

v0.9.1-release
Igor Zinkovsky 13 years ago
parent
commit
39e25528eb
  1. 4
      doc/api/fs.markdown
  2. 18
      lib/fs.js
  3. 7
      src/node_file.cc
  4. 68
      test/simple/test-fs-symlink-dir-junction.js

4
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])

18
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

7
src/node_file.cc

@ -414,8 +414,13 @@ static Handle<Value> 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")));
}
}

68
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);
});
Loading…
Cancel
Save