forked from irinazheltisheva/vscode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwatcher.ts
More file actions
192 lines (157 loc) · 6.59 KB
/
Copy pathwatcher.ts
File metadata and controls
192 lines (157 loc) · 6.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { join, basename } from 'vs/base/common/path';
import { watch } from 'fs';
import { isMacintosh } from 'vs/base/common/platform';
import { normalizeNFC } from 'vs/base/common/normalization';
import { toDisposable, IDisposable, dispose } from 'vs/base/common/lifecycle';
import { exists, readdir } from 'vs/base/node/pfs';
export function watchFile(path: string, onChange: (type: 'added' | 'changed' | 'deleted', path: string) => void, onError: (error: string) => void): IDisposable {
return doWatchNonRecursive({ path, isDirectory: false }, onChange, onError);
}
export function watchFolder(path: string, onChange: (type: 'added' | 'changed' | 'deleted', path: string) => void, onError: (error: string) => void): IDisposable {
return doWatchNonRecursive({ path, isDirectory: true }, onChange, onError);
}
export const CHANGE_BUFFER_DELAY = 100;
function doWatchNonRecursive(file: { path: string, isDirectory: boolean }, onChange: (type: 'added' | 'changed' | 'deleted', path: string) => void, onError: (error: string) => void): IDisposable {
const originalFileName = basename(file.path);
const mapPathToStatDisposable = new Map<string, IDisposable>();
let disposed = false;
let watcherDisposables: IDisposable[] = [toDisposable(() => {
mapPathToStatDisposable.forEach(disposable => dispose(disposable));
mapPathToStatDisposable.clear();
})];
try {
// Creating watcher can fail with an exception
const watcher = watch(file.path);
watcherDisposables.push(toDisposable(() => {
watcher.removeAllListeners();
watcher.close();
}));
// Folder: resolve children to emit proper events
const folderChildren: Set<string> = new Set<string>();
if (file.isDirectory) {
readdir(file.path).then(children => children.forEach(child => folderChildren.add(child)));
}
watcher.on('error', (code: number, signal: string) => {
if (!disposed) {
onError(`Failed to watch ${file.path} for changes using fs.watch() (${code}, ${signal})`);
}
});
watcher.on('change', (type, raw) => {
if (disposed) {
return; // ignore if already disposed
}
// Normalize file name
let changedFileName: string = '';
if (raw) { // https://github.com/Microsoft/vscode/issues/38191
changedFileName = raw.toString();
if (isMacintosh) {
// Mac: uses NFD unicode form on disk, but we want NFC
// See also https://github.com/nodejs/node/issues/2165
changedFileName = normalizeNFC(changedFileName);
}
}
if (!changedFileName || (type !== 'change' && type !== 'rename')) {
return; // ignore unexpected events
}
// File path: use path directly for files and join with changed file name otherwise
const changedFilePath = file.isDirectory ? join(file.path, changedFileName) : file.path;
// File
if (!file.isDirectory) {
if (type === 'rename' || changedFileName !== originalFileName) {
// The file was either deleted or renamed. Many tools apply changes to files in an
// atomic way ("Atomic Save") by first renaming the file to a temporary name and then
// renaming it back to the original name. Our watcher will detect this as a rename
// and then stops to work on Mac and Linux because the watcher is applied to the
// inode and not the name. The fix is to detect this case and trying to watch the file
// again after a certain delay.
// In addition, we send out a delete event if after a timeout we detect that the file
// does indeed not exist anymore.
const timeoutHandle = setTimeout(async () => {
const fileExists = await exists(changedFilePath);
if (disposed) {
return; // ignore if disposed by now
}
// File still exists, so emit as change event and reapply the watcher
if (fileExists) {
onChange('changed', changedFilePath);
watcherDisposables = [doWatchNonRecursive(file, onChange, onError)];
}
// File seems to be really gone, so emit a deleted event
else {
onChange('deleted', changedFilePath);
}
}, CHANGE_BUFFER_DELAY);
// Very important to dispose the watcher which now points to a stale inode
// and wire in a new disposable that tracks our timeout that is installed
dispose(watcherDisposables);
watcherDisposables = [toDisposable(() => clearTimeout(timeoutHandle))];
} else {
onChange('changed', changedFilePath);
}
}
// Folder
else {
// Children add/delete
if (type === 'rename') {
// Cancel any previous stats for this file path if existing
const statDisposable = mapPathToStatDisposable.get(changedFilePath);
if (statDisposable) {
dispose(statDisposable);
}
// Wait a bit and try see if the file still exists on disk to decide on the resulting event
const timeoutHandle = setTimeout(async () => {
mapPathToStatDisposable.delete(changedFilePath);
const fileExists = await exists(changedFilePath);
if (disposed) {
return; // ignore if disposed by now
}
// Figure out the correct event type:
// File Exists: either 'added' or 'changed' if known before
// File Does not Exist: always 'deleted'
let type: 'added' | 'deleted' | 'changed';
if (fileExists) {
if (folderChildren.has(changedFileName)) {
type = 'changed';
} else {
type = 'added';
folderChildren.add(changedFileName);
}
} else {
folderChildren.delete(changedFileName);
type = 'deleted';
}
onChange(type, changedFilePath);
}, CHANGE_BUFFER_DELAY);
mapPathToStatDisposable.set(changedFilePath, toDisposable(() => clearTimeout(timeoutHandle)));
}
// Other events
else {
// Figure out the correct event type: if this is the
// first time we see this child, it can only be added
let type: 'added' | 'changed';
if (folderChildren.has(changedFileName)) {
type = 'changed';
} else {
type = 'added';
folderChildren.add(changedFileName);
}
onChange(type, changedFilePath);
}
}
});
} catch (error) {
exists(file.path).then(exists => {
if (exists && !disposed) {
onError(`Failed to watch ${file.path} for changes using fs.watch() (${error.toString()})`);
}
});
}
return toDisposable(() => {
disposed = true;
watcherDisposables = dispose(watcherDisposables);
});
}