Skip to content

Commit

Permalink
Fixed for polling servers
Browse files Browse the repository at this point in the history
It should now be able to watch symlinks that lead to outside of the watched folder when polling
  • Loading branch information
ArjhanToteck committed Jul 27, 2024
1 parent c54bf67 commit d8e305f
Showing 1 changed file with 45 additions and 29 deletions.
74 changes: 45 additions & 29 deletions lib/DirectoryWatcher.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
Expand Down Expand Up @@ -61,6 +61,7 @@ class DirectoryWatcher extends EventEmitter {
this.watcherManager = watcherManager;
this.options = options;
this.path = directoryPath;
this.watchingSymlink = false;
// safeTime is the point in time after which reading is safe to be unchanged
// timestamp is a value that should be compared with another timestamp (mtime)
/** @type {Map<string, { safeTime: number, timestamp: number }} */
Expand Down Expand Up @@ -111,8 +112,8 @@ class DirectoryWatcher extends EventEmitter {
this.watchInParentDirectory();
}
this.watcher = watchEventSource.watch(this.path);
this.watcher.on("change", this.onWatchEvent.bind(this));
this.watcher.on("error", this.onWatcherError.bind(this));
this.watcher.on("change", this.onWatchEvent.bind(this));
}
} catch (err) {
this.onWatcherError(err);
Expand Down Expand Up @@ -550,11 +551,6 @@ class DirectoryWatcher extends EventEmitter {
fs.readdir(this.path, (err, items) => {
if (this.closed) return;
if (err) {
if (err.code === "ENOENT" || err.code === "EPERM") {
this.onDirectoryRemoved("scan readdir failed");
} else {
this.onScanError(err);
}
this.initialScan = false;
this.initialScanFinished = Date.now();
if (initial) {
Expand Down Expand Up @@ -627,22 +623,7 @@ class DirectoryWatcher extends EventEmitter {
}
});
for (const itemPath of itemPaths) {
const handleStats = (err2, stats) => {
if (this.closed) return;
if (err2) {
if (
err2.code === "ENOENT" ||
err2.code === "EPERM" ||
err2.code === "EACCES" ||
err2.code === "EBUSY"
) {
this.setMissing(itemPath, initial, "scan (" + err2.code + ")");
} else {
this.onScanError(err2);
}
itemFinished();
return;
}
const handleStats = (stats, symlinkStats) => {
if (stats.isFile() || stats.isSymbolicLink()) {
if (stats.mtime) {
ensureFsAccuracy(stats.mtime);
Expand All @@ -654,7 +635,11 @@ class DirectoryWatcher extends EventEmitter {
true,
"scan (file)"
);
} else if (stats.isDirectory()) {
}
if (
stats.isDirectory() ||
(symlinkStats && symlinkStats.isDirectory())
) {
if (!initial || !this.directories.has(itemPath))
this.setDirectory(
itemPath,
Expand All @@ -665,11 +650,42 @@ class DirectoryWatcher extends EventEmitter {
}
itemFinished();
};
if (this.watcherManager.options.followSymlinks) {
fs.stat(itemPath, handleStats);
} else {
fs.lstat(itemPath, handleStats);
}
fs.lstat(itemPath, (err2, stats) => {
if (this.closed) return;
if (err2) {
if (
err2.code === "ENOENT" ||
err2.code === "EPERM" ||
err2.code === "EACCES" ||
err2.code === "EBUSY"

Check warning on line 660 in lib/DirectoryWatcher.js

View check run for this annotation

Codecov / codecov/patch

lib/DirectoryWatcher.js#L656-L660

Added lines #L656 - L660 were not covered by tests
) {
this.setMissing(itemPath, initial, "scan (" + err2.code + ")");

Check warning on line 662 in lib/DirectoryWatcher.js

View check run for this annotation

Codecov / codecov/patch

lib/DirectoryWatcher.js#L662

Added line #L662 was not covered by tests
} else {
this.onScanError(err2);

Check warning on line 664 in lib/DirectoryWatcher.js

View check run for this annotation

Codecov / codecov/patch

lib/DirectoryWatcher.js#L664

Added line #L664 was not covered by tests
}
itemFinished();
return;

Check warning on line 667 in lib/DirectoryWatcher.js

View check run for this annotation

Codecov / codecov/patch

lib/DirectoryWatcher.js#L666-L667

Added lines #L666 - L667 were not covered by tests
}
if (
stats.isSymbolicLink() &&
this.watcherManager.options.followSymlinks
) {
fs.stat(itemPath, (err3, symlinkStats) => {
if (this.closed) return;
// something is wrong with the symlink, but not with the file itself
if (err3) {
handleStats(stats);
this.watchingSymlink = false;
return;

Check warning on line 679 in lib/DirectoryWatcher.js

View check run for this annotation

Codecov / codecov/patch

lib/DirectoryWatcher.js#L677-L679

Added lines #L677 - L679 were not covered by tests
}
this.watchingSymlink = true;
handleStats(stats, symlinkStats);
});
} else {
this.watchingSymlink = false;
handleStats(stats);
}
});
}
itemFinished();
});
Expand Down

0 comments on commit d8e305f

Please sign in to comment.