diff --git a/lib/paths.js b/lib/paths.js index dbde6b8..04ea228 100644 --- a/lib/paths.js +++ b/lib/paths.js @@ -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); @@ -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(); @@ -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)); } }); }); diff --git a/test/fixtures/testdir4/testdir b/test/fixtures/testdir4/testdir new file mode 120000 index 0000000..32709ee --- /dev/null +++ b/test/fixtures/testdir4/testdir @@ -0,0 +1 @@ +../testdir \ No newline at end of file diff --git a/test/test.js b/test/test.js index a4e9f6c..7ed76b9 100644 --- a/test/test.js +++ b/test/test.js @@ -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() { @@ -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(); + }); + }); + });