Skip to content

Commit

Permalink
Don't fail when there's a loop in symlinks
Browse files Browse the repository at this point in the history
  • Loading branch information
Yves Le Maout authored and 3y3 committed Apr 6, 2016
1 parent c769ae9 commit 9de0b02
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 8 deletions.
19 changes: 12 additions & 7 deletions lib/ScriptFileStorage.js
Expand Up @@ -239,6 +239,7 @@ var list = function(root, include, exclude, cb) {
var cache = {};
var list = [];
var node = null;
var realpathCache = {};

iterator();

Expand All @@ -256,14 +257,18 @@ var list = function(root, include, exclude, cb) {
var apath = path.join(node, child);
var rpath = relative(apath);

if (cache[apath] || excluded(rpath)) return cb();
cache[apath] = true;
fs.realpath(apath, realpathCache, function(err, realpath) {
if (err) { return cb(err); }
if (cache[realpath] || excluded(rpath)) return cb();
cache[realpath] = true;

fs.stat(apath, function(error, pstat) {
if (error) return cb(error);
if (pstat.isDirectory()) folders.push(apath);
if (pstat.isFile() && included(rpath)) list.push(apath);
cb();
fs.stat(realpath, function(error, pstat) {
if (error) return cb(error);

if (pstat.isDirectory()) folders.push(realpath);
if (pstat.isFile() && included(rpath)) list.push(realpath);
cb();
});
});
}, cb);
}
Expand Down
46 changes: 45 additions & 1 deletion test/ScriptFileStorage.js
Expand Up @@ -227,6 +227,45 @@ describe('ScriptFileStorage', function() {
);
});

it('does not enter an infinite loop', function(done) {
var originalConsoleWarn = console.warn;
var matchedWarning;
console.warn = function(text) {
matchedWarning = text.match(/ELOOP: too many symbolic links encountered/) && text;
originalConsoleWarn.apply(console, arguments);
};

givenTempFiles('app.js', 'mod.js');

var tempDirA = path.join(TEMP_DIR, 'a');
var tempDirB = path.join(TEMP_DIR, 'a', 'b');
var tempDirC = path.join(TEMP_DIR, 'a', 'b', 'c');
fs.mkdirSync(tempDirA);
fs.mkdirSync(tempDirB);
fs.symlinkSync(tempDirA, tempDirC);

return storage.findAllApplicationScripts(
TEMP_DIR,
path.join(TEMP_DIR, 'app.js'),
function(err, files) {
if (err) { return finish(err); }
expect(files).to.have.length(2);
return finish();
}
);

function finish(err) {
console.warn = originalConsoleWarn;
if (err) { return done(err); }

if (matchedWarning) {
return done(new Error(matchedWarning));
}

return done();
}
});

function relativeToTemp(p) {
return path.relative(TEMP_DIR, p);
}
Expand Down Expand Up @@ -335,7 +374,12 @@ function deleteTemps() {

entries.forEach(function(f) {
if (isDir(f)) {
fs.rmdirSync(f);
var n = f.slice(0, -1);
if (fs.lstatSync(n).isSymbolicLink()) {
fs.unlinkSync(n);
} else {
fs.rmdirSync(f);
}
} else {
fs.unlinkSync(f);
}
Expand Down

0 comments on commit 9de0b02

Please sign in to comment.