Skip to content

Commit 772ccc6

Browse files
authored
Merge pull request fshost#37 from miloss/master
Fix traversing symlinked directories
2 parents fd46600 + e0983e6 commit 772ccc6

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

lib/paths.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,14 @@ exports.files = function files(dir, type, callback, /* used internally */ ignore
2828
}
2929
};
3030

31-
var getStatHandler = function(statPath) {
31+
var getStatHandler = function(statPath, lstatCalled) {
3232
return function(err, stat) {
33-
if (err) return callback(err);
33+
if (err) {
34+
if (!lstatCalled) {
35+
return fs.lstat(statPath, getStatHandler(statPath, true));
36+
}
37+
return callback(err);
38+
}
3439
if (stat && stat.isDirectory() && stat.mode !== 17115) {
3540
if (type !== 'file') {
3641
results.dirs.push(statPath);
@@ -63,7 +68,7 @@ exports.files = function files(dir, type, callback, /* used internally */ ignore
6368
type = 'file';
6469
}
6570

66-
fs.lstat(dir, function(err, stat) {
71+
fs.stat(dir, function(err, stat) {
6772
if (err) return callback(err);
6873
if(stat && stat.mode === 17115) return done();
6974

@@ -73,7 +78,7 @@ exports.files = function files(dir, type, callback, /* used internally */ ignore
7378
if (!pending) return done();
7479
for (var file, i = 0, l = list.length; i < l; i++) {
7580
file = path.join(dir, list[i]);
76-
fs.lstat(file, getStatHandler(file));
81+
fs.stat(file, getStatHandler(file));
7782
}
7883
});
7984
});

test/fixtures/testdir4/testdir

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../testdir

test/test.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
fixturesDir = path.join(__dirname, 'fixtures'),
55
tdir = path.join(fixturesDir, 'testdir'),
66
tdir2 = path.join(fixturesDir, 'testdir2'),
7-
tdir3 = path.join(fixturesDir, 'testdir3');
7+
tdir3 = path.join(fixturesDir, 'testdir3'),
8+
tdir4 = path.join(fixturesDir, 'testdir4');
89

910
describe('readfiles method', function() {
1011

@@ -1139,6 +1140,36 @@ describe("files method", function() {
11391140
});
11401141
});
11411142

1143+
it("should return broken symlinks as files", function(done) {
1144+
dir.files(tdir3, function(err, files) {
1145+
should.not.exist(err);
1146+
var relFiles = files.map(function(curPath) {
1147+
return path.relative(fixturesDir, curPath);
1148+
});
1149+
relFiles.sort().should.eql([
1150+
'testdir3/broken_link.txt',
1151+
'testdir3/file1.txt'
1152+
]);
1153+
done();
1154+
});
1155+
});
1156+
1157+
it("should iterate files of symlinked directories (recursively)", function(done) {
1158+
dir.files(tdir4, function(err, files) {
1159+
should.not.exist(err);
1160+
var relFiles = files.map(function(curPath) {
1161+
return path.relative(fixturesDir, curPath);
1162+
});
1163+
relFiles.sort().should.eql([
1164+
'testdir4/testdir/file1.txt',
1165+
'testdir4/testdir/file2.text',
1166+
'testdir4/testdir/subdir/file3.txt',
1167+
'testdir4/testdir/subdir/file4.text'
1168+
]);
1169+
done();
1170+
});
1171+
});
1172+
11421173
});
11431174

11441175

0 commit comments

Comments
 (0)