Skip to content

Commit

Permalink
Update: Avoid realpath calls & use stat instead (#236)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikkemperman authored and phated committed Nov 30, 2017
1 parent 6043981 commit e060d2e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 19 deletions.
20 changes: 2 additions & 18 deletions lib/src/resolve-symlinks.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,13 @@ function resolveSymlinks(opt) {
return callback(statErr);
}

globFile.stat = stat;

if (!stat.isSymbolicLink() || !resolveSymlinks) {
globFile.stat = stat;
return callback(null, globFile);
}

fs.realpath(globFile.path, onRealpath);
}

function onRealpath(realpathErr, filePath) {
if (realpathErr) {
return callback(realpathErr);
}

if (!globFile.originalSymlinkPath) {
// Store the original symlink path before the recursive call
// to later rewrite it back.
globFile.originalSymlinkPath = globFile.path;
}
globFile.path = filePath;

// Recurse to get real file stat
resolveFile(globFile, enc, callback);
fs.stat(globFile.path, onStat);
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"to-through": "^2.0.0",
"value-or-function": "^2.0.0",
"vinyl": "^2.0.0",
"vinyl-prepare": "^0.2.0",
"vinyl-prepare": "^0.3.0",
"vinyl-sourcemap": "^0.4.0"
},
"devDependencies": {
Expand Down
22 changes: 22 additions & 0 deletions test/src-symlinks.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ var outputDirpath = testConstants.outputDirpath;
var symlinkNestedTarget = testConstants.symlinkNestedTarget;
var symlinkPath = testConstants.symlinkPath;
var symlinkDirpath = testConstants.symlinkDirpath;
var symlinkMultiDirpath = testConstants.symlinkMultiDirpath;
var symlinkMultiDirpathSecond = testConstants.symlinkMultiDirpathSecond;
var symlinkNestedFirst = testConstants.symlinkNestedFirst;
var symlinkNestedSecond = testConstants.symlinkNestedSecond;

Expand All @@ -33,6 +35,8 @@ describe('.src() with symlinks', function() {
fs.mkdirSync(outputBase);
fs.mkdirSync(outputDirpath);
fs.symlinkSync(inputDirpath, symlinkDirpath);
fs.symlinkSync(symlinkDirpath, symlinkMultiDirpath);
fs.symlinkSync(symlinkMultiDirpath, symlinkMultiDirpathSecond);
fs.symlinkSync(inputPath, symlinkPath);
fs.symlinkSync(symlinkNestedTarget, symlinkNestedSecond);
fs.symlinkSync(symlinkNestedSecond, symlinkNestedFirst);
Expand Down Expand Up @@ -75,6 +79,24 @@ describe('.src() with symlinks', function() {
], done);
});

it('resolves nested symlinks to directories correctly', function(done) {
function assert(files) {
expect(files.length).toEqual(1);
// The path should be the symlink itself
expect(files[0].path).toEqual(symlinkMultiDirpathSecond);
// But the contents should be null
expect(files[0].contents).toEqual(null);
// And the stats should have been updated
expect(files[0].stat.isSymbolicLink()).toEqual(false);
expect(files[0].stat.isDirectory()).toEqual(true);
}

pipe([
vfs.src(symlinkMultiDirpathSecond),
concat(assert),
], done);
});

it('preserves file symlinks with resolveSymlinks option set to false', function(done) {
var expectedRelativeSymlinkPath = fs.readlinkSync(symlinkPath);

Expand Down
4 changes: 4 additions & 0 deletions test/utils/test-constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ var leEncodedInputPath = path.join(inputBase, './bom-utf16le.txt');
var symlinkNestedTarget = path.join(inputBase, './foo/bar/baz.txt');
var symlinkPath = path.join(outputBase, './test-symlink');
var symlinkDirpath = path.join(outputBase, './test-symlink-dir');
var symlinkMultiDirpath = path.join(outputBase, './test-multi-layer-symlink-dir');
var symlinkMultiDirpathSecond = path.join(outputBase, './test-multi-layer-symlink-dir2');
var symlinkNestedFirst = path.join(outputBase, './test-multi-layer-symlink');
var symlinkNestedSecond = path.join(outputBase, './foo/baz-link.txt');
// Used for contents of files
Expand Down Expand Up @@ -61,6 +63,8 @@ module.exports = {
symlinkNestedTarget: symlinkNestedTarget,
symlinkPath: symlinkPath,
symlinkDirpath: symlinkDirpath,
symlinkMultiDirpath: symlinkMultiDirpath,
symlinkMultiDirpathSecond: symlinkMultiDirpathSecond,
symlinkNestedFirst: symlinkNestedFirst,
symlinkNestedSecond: symlinkNestedSecond,
contents: contents,
Expand Down

0 comments on commit e060d2e

Please sign in to comment.