Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions lib/internal/fs/recursive_watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ function isPathMissingForWatch(error, file) {
// elsewhere every file keeps a watcher of its own as well.
const kDirectoryWatchReportsEntries = process.platform === 'linux';

// Inotify reports a change to a directory's own metadata the same way as its
// removal, and a directory created in place of a removed one often gets the
// same inode number, so the birth time is compared as well.
function directoryIdentity(stats) {
return stats === undefined ? undefined : `${stats.ino}:${stats.birthtimeNs}`;
}

class FSWatcher extends EventEmitter {
#options = null;
#closed = false;
Expand All @@ -70,6 +77,9 @@ class FSWatcher extends EventEmitter {
// directory watch does not report its entries).
#watchers = new SafeMap();
#symbolicLinks = new SafeSet();
// The identity of each directory watched below the root (see
// directoryIdentity()), where the directory watch reports its entries.
#directories = new SafeMap();
#rootPath = pathResolve();
#ignoreMatcher = null;

Expand Down Expand Up @@ -126,6 +136,7 @@ class FSWatcher extends EventEmitter {
this.#watchers.clear();
this.#entries.clear();
this.#symbolicLinks.clear();
this.#directories.clear();
this.emit('close');
}

Expand All @@ -141,12 +152,14 @@ class FSWatcher extends EventEmitter {
}
this.#entries.delete(file);
this.#symbolicLinks.delete(file);
this.#directories.delete(file);

const childPrefix = file + pathSep;
for (const entry of this.#entries) {
if (StringPrototypeStartsWith(entry, childPrefix)) {
this.#entries.delete(entry);
this.#symbolicLinks.delete(entry);
this.#directories.delete(entry);
const childWatcher = this.#watchers.get(entry);
if (childWatcher !== undefined) {
childWatcher.close();
Expand Down Expand Up @@ -188,6 +201,12 @@ class FSWatcher extends EventEmitter {
this.#forget(folder);
return;
}
if (kDirectoryWatchReportsEntries && folder !== this.#rootPath &&
!this.#directories.has(folder)) {
const { lstatSync } = lazyLoadFsSync();
this.#directories.set(folder, directoryIdentity(
lstatSync(folder, { bigint: true, throwIfNoEntry: false })));
}

const { readdirSync } = lazyLoadFsSync();
let entries;
Expand Down Expand Up @@ -271,6 +290,23 @@ class FSWatcher extends EventEmitter {
} else if (folder === this.#rootPath && statSync(folder, { throwIfNoEntry: false }) === undefined) {
this.#emit('rename', folder);
this.#forget(folder);
} else if (folder !== this.#rootPath && filename === pathBasename(folder)) {
// An event about `folder` itself: its metadata changed, or it was
// removed or moved away. In the latter case its watcher reports nothing
// more, and something may already exist under the same name (e.g. the
// directory was removed and created again before this event was
// handled), so start over with that.
const current = lstatSync(folder, { bigint: true, throwIfNoEntry: false });
if (current?.isDirectory() &&
directoryIdentity(current) === this.#directories.get(folder)) {
return;
}
this.#forget(folder);
if (current !== undefined) {
this.#addEntry(folder, current, false);
} else {
this.#emit('rename', folder);
}
}
return;
}
Expand Down
51 changes: 51 additions & 0 deletions test/parallel/test-fs-watch-recursive-linux-recreate-directory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
'use strict';
const common = require('../common');

// A directory below a recursive fs.watch() root that is removed and created
// again before its events are handled must still be watched afterwards.
// Refs: https://github.com/nodejs/node/issues/67592

if (!common.isLinux)
common.skip('the recursive watcher is native on this platform');

const assert = require('assert');
const fs = require('fs');
const path = require('path');
const tmpdir = require('../common/tmpdir');

tmpdir.refresh();

const root = tmpdir.resolve('root');
const sub = path.join(root, 'sub');
fs.mkdirSync(sub, { recursive: true });

const watcher = fs.watch(root, { recursive: true });
let step = 0;

function recreate(name) {
fs.rmSync(sub, { recursive: true, force: true });
fs.mkdirSync(sub);
fs.writeFileSync(path.join(sub, name), name);
}

watcher.on('change', common.mustCallAtLeast((eventType, filename) => {
if (step === 0 && filename === path.join('sub', 'a.txt')) {
step = 1;
setTimeout(() => recreate('b.txt'), common.platformTimeout(100));
} else if (step === 1 && filename === path.join('sub', 'b.txt')) {
step = 2;
setTimeout(() => {
fs.writeFileSync(path.join(sub, 'c.txt'), 'c');
}, common.platformTimeout(100));
} else if (step === 2 && filename === path.join('sub', 'c.txt')) {
assert.strictEqual(eventType, 'rename');
step = 3;
watcher.close();
}
}));

setTimeout(() => recreate('a.txt'), common.platformTimeout(100));

process.on('exit', () => {
assert.strictEqual(step, 3);
});
Loading