Skip to content

Commit 7edbf25

Browse files
fix(box): check for invalid file (#5173)
1 parent cb19b29 commit 7edbf25

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

lib/box/index.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,9 @@ class Box extends EventEmitter {
7878
}
7979

8080
_readDir(base, prefix = '') {
81+
const { context: ctx } = this;
8182
const results = [];
82-
return readDirWalker(base, results, this.ignore, prefix)
83+
return readDirWalker(ctx, base, results, this.ignore, prefix)
8384
.return(results)
8485
.map(path => this._checkFileStatus(path))
8586
.map(file => this._processFile(file.type, file.path).return(file.path));
@@ -155,7 +156,7 @@ class Box extends EventEmitter {
155156
path
156157
});
157158
}).catch(err => {
158-
ctx.log.error({err}, 'Process failed: %s', magenta(path));
159+
ctx.log.error({ err }, 'Process failed: %s', magenta(path));
159160
}).finally(() => {
160161
this._processingFiles[path] = false;
161162
}).thenReturn(path);
@@ -245,21 +246,28 @@ function isIgnoreMatch(path, ignore) {
245246
return path && ignore && ignore.length && isMatch(path, ignore);
246247
}
247248

248-
function readDirWalker(base, results, ignore, prefix) {
249+
function readDirWalker(ctx, base, results, ignore, prefix) {
249250
if (isIgnoreMatch(base, ignore)) return Promise.resolve();
250251

251252
return Promise.map(readdir(base).catch(err => {
253+
ctx.log.error({ err }, 'Failed to read directory: %s', base);
252254
if (err && err.code === 'ENOENT') return [];
253255
throw err;
254256
}), async path => {
255257
const fullpath = join(base, path);
256-
const stats = await stat(fullpath);
258+
const stats = await stat(fullpath).catch(err => {
259+
ctx.log.error({ err }, 'Failed to stat file: %s', fullpath);
260+
if (err && err.code === 'ENOENT') return null;
261+
throw err;
262+
});
257263
const prefixdPath = `${prefix}${path}`;
258-
if (stats.isDirectory()) {
259-
return readDirWalker(fullpath, results, ignore, `${prefixdPath}/`);
260-
}
261-
if (!isIgnoreMatch(fullpath, ignore)) {
262-
results.push(prefixdPath);
264+
if (stats) {
265+
if (stats.isDirectory()) {
266+
return readDirWalker(ctx, fullpath, results, ignore, `${prefixdPath}/`);
267+
}
268+
if (!isIgnoreMatch(fullpath, ignore)) {
269+
results.push(prefixdPath);
270+
}
263271
}
264272
});
265273
}

0 commit comments

Comments
 (0)