Skip to content

Commit 7275124

Browse files
committed
fs: add recursive watch to linux
1 parent fdadea8 commit 7275124

File tree

5 files changed

+254
-19
lines changed

5 files changed

+254
-19
lines changed

doc/api/fs.md

-4
Original file line numberDiff line numberDiff line change
@@ -4377,10 +4377,6 @@ the returned {fs.FSWatcher}.
43774377
The `fs.watch` API is not 100% consistent across platforms, and is
43784378
unavailable in some situations.
43794379
4380-
The recursive option is only supported on macOS and Windows.
4381-
An `ERR_FEATURE_UNAVAILABLE_ON_PLATFORM` exception will be thrown
4382-
when the option is used on a platform that does not support it.
4383-
43844380
On Windows, no events will be emitted if the watched directory is moved or
43854381
renamed. An `EPERM` error is reported when the watched directory is deleted.
43864382

lib/fs.js

+18-9
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ const {
5757

5858
const pathModule = require('path');
5959
const { isArrayBufferView } = require('internal/util/types');
60+
const linuxWatcher = require('internal/fs/linux_watcher');
6061

6162
// We need to get the statValues from the binding at the callsite since
6263
// it's re-initialized after deserialization.
@@ -68,7 +69,6 @@ const {
6869
codes: {
6970
ERR_FS_FILE_TOO_LARGE,
7071
ERR_INVALID_ARG_VALUE,
71-
ERR_FEATURE_UNAVAILABLE_ON_PLATFORM,
7272
},
7373
AbortError,
7474
uvErrmapGet,
@@ -161,7 +161,7 @@ let FileReadStream;
161161
let FileWriteStream;
162162

163163
const isWindows = process.platform === 'win32';
164-
const isOSX = process.platform === 'darwin';
164+
const isLinux = process.platform === 'linux';
165165

166166

167167
function showTruncateDeprecation() {
@@ -2297,13 +2297,22 @@ function watch(filename, options, listener) {
22972297

22982298
if (options.persistent === undefined) options.persistent = true;
22992299
if (options.recursive === undefined) options.recursive = false;
2300-
if (options.recursive && !(isOSX || isWindows))
2301-
throw new ERR_FEATURE_UNAVAILABLE_ON_PLATFORM('watch recursively');
2302-
const watcher = new watchers.FSWatcher();
2303-
watcher[watchers.kFSWatchStart](filename,
2304-
options.persistent,
2305-
options.recursive,
2306-
options.encoding);
2300+
2301+
let watcher;
2302+
2303+
// TODO(anonrig): Remove this when/if libuv supports it.
2304+
// libuv does not support recursive file watch on Linux due to
2305+
// the limitations of inotify.
2306+
if (options.recursive && isLinux) {
2307+
watcher = new linuxWatcher.FSWatcher(options);
2308+
watcher[linuxWatcher.kFSWatchStart](filename);
2309+
} else {
2310+
watcher = new watchers.FSWatcher();
2311+
watcher[watchers.kFSWatchStart](filename,
2312+
options.persistent,
2313+
options.recursive,
2314+
options.encoding);
2315+
}
23072316

23082317
if (listener) {
23092318
watcher.addListener('change', listener);

lib/internal/fs/linux_watcher.js

+192
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
'use strict';
2+
3+
const { EventEmitter } = require('events');
4+
const path = require('path');
5+
const { SafeMap, Symbol, StringPrototypeStartsWith } = primordials;
6+
const { validateObject } = require('internal/validators');
7+
const { kEmptyObject } = require('internal/util');
8+
const { ERR_FEATURE_UNAVAILABLE_ON_PLATFORM } = require('internal/errors');
9+
10+
const kFSWatchStart = Symbol('kFSWatchStart');
11+
12+
let internalSync;
13+
let internalPromises;
14+
15+
function lazyLoadFsPromises() {
16+
internalPromises ??= require('fs/promises');
17+
return internalPromises;
18+
}
19+
20+
function lazyLoadFsSync() {
21+
internalSync ??= require('fs');
22+
return internalSync;
23+
}
24+
25+
async function traverse(dir, files = new SafeMap()) {
26+
const { stat, opendir } = lazyLoadFsPromises();
27+
28+
files.set(dir, await stat(dir));
29+
30+
try {
31+
const directory = await opendir(dir);
32+
33+
for await (const file of directory) {
34+
const f = path.join(dir, file.name);
35+
36+
try {
37+
const stats = await stat(f);
38+
39+
files.set(f, stats);
40+
41+
if (stats.isDirectory()) {
42+
await traverse(f, files);
43+
}
44+
} catch (error) {
45+
if (error.code !== 'ENOENT' || error.code !== 'EPERM') {
46+
this.emit('error', error);
47+
}
48+
}
49+
50+
}
51+
} catch (error) {
52+
if (error.code !== 'EACCES') {
53+
this.emit('error', error);
54+
}
55+
}
56+
57+
return files;
58+
}
59+
60+
class FSWatcher extends EventEmitter {
61+
#options = null;
62+
#closed = false;
63+
#files = new SafeMap();
64+
65+
/**
66+
* @param {{
67+
* persistent?: boolean;
68+
* recursive?: boolean;
69+
* encoding?: string;
70+
* signal?: AbortSignal;
71+
* }} [options]
72+
*/
73+
constructor(options = kEmptyObject) {
74+
super();
75+
76+
validateObject(options, 'options');
77+
this.#options = options;
78+
}
79+
80+
close() {
81+
const { unwatchFile } = lazyLoadFsSync();
82+
this.#closed = true;
83+
84+
for (const file of this.#files.keys()) {
85+
unwatchFile(file);
86+
}
87+
88+
this.emit('close');
89+
}
90+
91+
#unwatchFolder(file) {
92+
const { unwatchFile } = lazyLoadFsSync();
93+
94+
for (const filename in this.#files) {
95+
if (StringPrototypeStartsWith(filename, file)) {
96+
unwatchFile(filename);
97+
}
98+
}
99+
}
100+
101+
async #watchFolder(folder) {
102+
const { opendir, stat } = lazyLoadFsPromises();
103+
104+
try {
105+
const files = await opendir(folder);
106+
107+
for await (const file of files) {
108+
const f = path.join(folder, file.name);
109+
110+
if (this.#closed) {
111+
return;
112+
}
113+
114+
if (!this.#files.has(f)) {
115+
const fileStats = await stat(f);
116+
this.#files.set(f, fileStats);
117+
this.emit('change', 'rename', f);
118+
this.#watchFile(f);
119+
}
120+
}
121+
} catch (error) {
122+
this.emit('error', error);
123+
}
124+
}
125+
126+
/**
127+
* @param {string} file
128+
*/
129+
#watchFile(file) {
130+
const { watchFile } = lazyLoadFsSync();
131+
132+
if (this.#closed) {
133+
return;
134+
}
135+
136+
const existingStat = this.#files.get(file);
137+
138+
watchFile(file, {
139+
persistent: this.#options.persistent,
140+
}, (statWatcher, previousStatWatcher) => {
141+
if (existingStat && !existingStat.isDirectory() &&
142+
statWatcher.nlink !== 0 && existingStat.mtime.getTime() === statWatcher.mtime.getTime()) {
143+
return;
144+
}
145+
146+
this.#files.set(file, statWatcher);
147+
148+
if (statWatcher.isDirectory()) {
149+
this.#watchFolder(file);
150+
} else if (statWatcher.birthtimeMs === 0 && previousStatWatcher.birthtimeMs !== 0) {
151+
// The file is now deleted
152+
this.#files.delete(file);
153+
this.emit('change', 'rename', file);
154+
155+
if (statWatcher.isDirectory()) {
156+
this.#unwatchFolder(file);
157+
}
158+
} else {
159+
this.emit('change', 'change', file);
160+
}
161+
});
162+
}
163+
164+
/**
165+
* @param {string | Buffer | URL} filename
166+
*/
167+
async [kFSWatchStart](filename) {
168+
this.#closed = false;
169+
this.#files = await traverse(filename);
170+
171+
this.#watchFile(filename);
172+
173+
for (const f in this.#files) {
174+
this.#watchFile(f);
175+
}
176+
}
177+
178+
ref() {
179+
// This is kept to have the same API with FSWatcher
180+
throw new ERR_FEATURE_UNAVAILABLE_ON_PLATFORM('ref');
181+
}
182+
183+
unref() {
184+
// This is kept to have the same API with FSWatcher
185+
throw new ERR_FEATURE_UNAVAILABLE_ON_PLATFORM('unref');
186+
}
187+
}
188+
189+
module.exports = {
190+
FSWatcher,
191+
kFSWatchStart,
192+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
5+
if (!common.hasCrypto)
6+
common.skip('missing crypto');
7+
8+
// Only run these tests on Linux.
9+
if (!common.isLinux) {
10+
return;
11+
}
12+
13+
const { randomUUID } = require('crypto');
14+
const assert = require('assert');
15+
const path = require('path');
16+
const fs = require('fs');
17+
18+
const tmpdir = require('../common/tmpdir');
19+
const testDir = tmpdir.path;
20+
tmpdir.refresh();
21+
22+
{
23+
const file = `${randomUUID()}.txt`;
24+
const testsubdir = fs.mkdtempSync(testDir + path.sep);
25+
const filePath = path.join(testsubdir, file);
26+
27+
const watcher = fs.watch(testsubdir, { recursive: true });
28+
29+
let watcherClosed = false;
30+
watcher.on('change', function(event, filename) {
31+
assert.ok(event === 'change' || event === 'rename');
32+
33+
watcher.close();
34+
watcherClosed = true;
35+
});
36+
37+
setTimeout(() => {
38+
fs.writeFileSync(filePath, 'world');
39+
}, 100);
40+
41+
process.on('exit', function() {
42+
assert(watcherClosed, 'watcher Object was not closed');
43+
});
44+
}

test/parallel/test-fs-watch-recursive.js

-6
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@ tmpdir.refresh();
1717
const testsubdir = fs.mkdtempSync(testDir + path.sep);
1818
const relativePathOne = path.join(path.basename(testsubdir), filenameOne);
1919
const filepathOne = path.join(testsubdir, filenameOne);
20-
21-
if (!common.isOSX && !common.isWindows) {
22-
assert.throws(() => { fs.watch(testDir, { recursive: true }); },
23-
{ code: 'ERR_FEATURE_UNAVAILABLE_ON_PLATFORM' });
24-
return;
25-
}
2620
const watcher = fs.watch(testDir, { recursive: true });
2721

2822
let watcherClosed = false;

0 commit comments

Comments
 (0)