Skip to content

Commit

Permalink
Don’t stat() in readdirp if alwaysStat is false.
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmillr committed May 14, 2019
1 parent f373fee commit 2a3706d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
10 changes: 7 additions & 3 deletions index.js
Expand Up @@ -164,8 +164,9 @@ class WatchHelper {
// only need to resolve once
// first entry should always have entry.parentDir === ''
if (this.globSymlink == null) {
this.globSymlink = entry.fullParentDir === this.fullWatchPath ?
false : {realPath: entry.fullParentDir, linkPath: this.fullWatchPath};
const realPath = sysPath.dirname(entry.fullPath);
this.globSymlink = realPath === this.fullWatchPath ?
false : {realPath, linkPath: this.fullWatchPath};
}

if (this.globSymlink) {
Expand Down Expand Up @@ -258,6 +259,7 @@ constructor(_opts) {
if (undef(opts, 'interval')) opts.interval = 100;
if (undef(opts, 'binaryInterval')) opts.binaryInterval = 300;
if (undef(opts, 'disableGlobbing')) opts.disableGlobbing = false;
if (undef(opts, 'alwaysStat')) opts.alwaysStat = false;
opts.enableBinaryInterval = opts.binaryInterval !== opts.interval;

// Enable fsevents on OS X when polling isn't explicitly enabled.
Expand Down Expand Up @@ -769,6 +771,7 @@ _getWatchedDir(directory) {
*/
_hasReadPermissions(stats) {
if (this.options.ignorePermissionErrors) return true;
if (!stats) return true;

const st = (stats && stats.mode) & 0o777;
const it = parseInt(st.toString(8)[0], 10);
Expand Down Expand Up @@ -864,7 +867,8 @@ _addPathCloser(path, closer) {
}

_readdirp(root, opts) {
const options = Object.assign({type: 'all', alwaysStat: true, lstat: true}, opts);
const options = Object.assign({type: 'all', alwaysStat: this.options.alwaysStat, lstat: this.options.followSymlinks}, opts);
// const options = Object.assign({type: 'all', alwaysStat: true, lstat: true}, opts);
let stream = readdirp(root, options);
this._streams.add(stream);
stream.once('close', () => {
Expand Down
8 changes: 5 additions & 3 deletions lib/fsevents-handler.js
Expand Up @@ -427,21 +427,23 @@ async _addToFsEvents(path, transform, forceAdd, priorDepth) {
directoryFilter: entry => wh.filterDir(entry),
...Option("depth", opts.depth - (priorDepth || 0))
}).on('data', (entry) => {
const estats = 'stats' in entry ? entry.stats : entry.dirent;

// need to check filterPath on dirs b/c filterDir is less restrictive
if (entry.stats.isDirectory() && !wh.filterPath(entry)) return;
if (estats.isDirectory() && !wh.filterPath(entry)) return;

const joinedPath = sysPath.join(wh.watchPath, entry.path);
const fullPath = entry.fullPath;

if (wh.followSymlinks && entry.stats.isSymbolicLink()) {
if (wh.followSymlinks && estats.isSymbolicLink()) {
// preserve the current depth here since it can't be derived from
// real paths past the symlink
const curDepth = opts.depth === undefined ?
undefined : depth(joinedPath, sysPath.resolve(wh.watchPath)) + 1;

this._handleFsEventsSymlink(joinedPath, fullPath, processPath, curDepth);
} else {
this.emitAdd(joinedPath, entry.stats, processPath, opts, forceAdd);
this.emitAdd(joinedPath, estats, processPath, opts, forceAdd);
}
}).on('error', EMPTY_FN).on('end', () => {
this.fsw._emitReady();
Expand Down
12 changes: 7 additions & 5 deletions lib/nodefs-handler.js
Expand Up @@ -320,7 +320,7 @@ _handleFile(file, stats, initialAdd) {
} catch (error) {
// Fix issues where mtime is null but file is still present
this.fsw._remove(dirname, basename);
}
}
// add is about to be emitted if file not already tracked in parent
} else if (parent.has(basename)) {
// Check that change event was not fired because of changed only accessTime.
Expand Down Expand Up @@ -401,8 +401,10 @@ _handleRead(directory, initialAdd, wh, target, dir, depth, throttler) {
let path = sysPath.join(directory, item);
current.add(item);

if (entry.stats.isSymbolicLink() && await this._handleSymlink(entry, directory, path, item))
if (entry.stats && entry.stats.isSymbolicLink() &&
await this._handleSymlink(entry, directory, path, item)) {
return;
}

// Files that present in current directory snapshot
// but absent in previous are added to watch list and
Expand All @@ -416,7 +418,7 @@ _handleRead(directory, initialAdd, wh, target, dir, depth, throttler) {
this._addToNodeFs(path, initialAdd, wh, depth + 1);
}
}).on('error', this._boundHandleError);
return new Promise(res =>
return new Promise(res =>
stream
.on('end', () => {
const wasThrottled = throttler ? throttler.clear() : false;
Expand All @@ -426,7 +428,7 @@ _handleRead(directory, initialAdd, wh, target, dir, depth, throttler) {
// Files that absent in current directory snapshot
// but present in previous emit `remove` event
// and are removed from @watched[directory].
previous.getChildren().filter((item) =>
previous.getChildren().filter((item) =>
item !== directory &&
!current.has(item) &&
// in case of intersecting globs;
Expand Down Expand Up @@ -476,7 +478,7 @@ async _handleDir(dir, stats, initialAdd, depth, target, wh, realpath) {
if (!target) {
await this._handleRead(dir, initialAdd, wh, target, dir, depth, throttler);
}

closer = this._watchWithNodeFs(dir, (dirPath, stats) => {
// if current directory is removed, do nothing
if (stats && stats.mtimeMs === 0) return;
Expand Down

0 comments on commit 2a3706d

Please sign in to comment.