Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
windows: enable creating directory junctions with fs.symlink
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Zinkovsky committed May 24, 2012
1 parent a2fcc47 commit 39e2552
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 6 deletions.
4 changes: 3 additions & 1 deletion doc/api/fs.markdown
Expand Up @@ -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])

Expand Down
18 changes: 14 additions & 4 deletions lib/fs.js
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
};

Expand Down Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion src/node_file.cc
Expand Up @@ -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")));
}
}

Expand Down
68 changes: 68 additions & 0 deletions 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);
});

1 comment on commit 39e2552

@hickford
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if you try this on a FAT32 drive, say?

Please sign in to comment.