Skip to content

Commit

Permalink
Merge pull request #37 from miloss/master
Browse files Browse the repository at this point in the history
Fix traversing symlinked directories
  • Loading branch information
fshost authored Aug 13, 2016
2 parents fd46600 + e0983e6 commit 772ccc6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
13 changes: 9 additions & 4 deletions lib/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,14 @@ exports.files = function files(dir, type, callback, /* used internally */ ignore
}
};

var getStatHandler = function(statPath) {
var getStatHandler = function(statPath, lstatCalled) {
return function(err, stat) {
if (err) return callback(err);
if (err) {
if (!lstatCalled) {
return fs.lstat(statPath, getStatHandler(statPath, true));
}
return callback(err);
}
if (stat && stat.isDirectory() && stat.mode !== 17115) {
if (type !== 'file') {
results.dirs.push(statPath);
Expand Down Expand Up @@ -63,7 +68,7 @@ exports.files = function files(dir, type, callback, /* used internally */ ignore
type = 'file';
}

fs.lstat(dir, function(err, stat) {
fs.stat(dir, function(err, stat) {
if (err) return callback(err);
if(stat && stat.mode === 17115) return done();

Expand All @@ -73,7 +78,7 @@ exports.files = function files(dir, type, callback, /* used internally */ ignore
if (!pending) return done();
for (var file, i = 0, l = list.length; i < l; i++) {
file = path.join(dir, list[i]);
fs.lstat(file, getStatHandler(file));
fs.stat(file, getStatHandler(file));
}
});
});
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/testdir4/testdir
33 changes: 32 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
fixturesDir = path.join(__dirname, 'fixtures'),
tdir = path.join(fixturesDir, 'testdir'),
tdir2 = path.join(fixturesDir, 'testdir2'),
tdir3 = path.join(fixturesDir, 'testdir3');
tdir3 = path.join(fixturesDir, 'testdir3'),
tdir4 = path.join(fixturesDir, 'testdir4');

describe('readfiles method', function() {

Expand Down Expand Up @@ -1139,6 +1140,36 @@ describe("files method", function() {
});
});

it("should return broken symlinks as files", function(done) {
dir.files(tdir3, function(err, files) {
should.not.exist(err);
var relFiles = files.map(function(curPath) {
return path.relative(fixturesDir, curPath);
});
relFiles.sort().should.eql([
'testdir3/broken_link.txt',
'testdir3/file1.txt'
]);
done();
});
});

it("should iterate files of symlinked directories (recursively)", function(done) {
dir.files(tdir4, function(err, files) {
should.not.exist(err);
var relFiles = files.map(function(curPath) {
return path.relative(fixturesDir, curPath);
});
relFiles.sort().should.eql([
'testdir4/testdir/file1.txt',
'testdir4/testdir/file2.text',
'testdir4/testdir/subdir/file3.txt',
'testdir4/testdir/subdir/file4.text'
]);
done();
});
});

});


Expand Down

0 comments on commit 772ccc6

Please sign in to comment.