-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
node-filesystem.ts
555 lines (506 loc) · 22.4 KB
/
node-filesystem.ts
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
/********************************************************************************
* Copyright (C) 2017 TypeFox and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/
import * as mv from 'mv';
import * as trash from 'trash';
import * as paths from 'path';
import * as fs from 'fs-extra';
import { v4 } from 'uuid';
import * as os from 'os';
import * as touch from 'touch';
import * as drivelist from 'drivelist';
import { injectable, inject, optional } from 'inversify';
import { TextDocument } from 'vscode-languageserver-types';
import { TextDocumentContentChangeEvent } from 'vscode-languageserver-protocol';
import URI from '@theia/core/lib/common/uri';
import { TextDocumentContentChangeDelta } from '@theia/core/lib/common/lsp-types';
import { FileUri } from '@theia/core/lib/node/file-uri';
import { FileStat, FileSystem, FileSystemClient, FileSystemError, FileMoveOptions, FileDeleteOptions, FileAccess } from '../common/filesystem';
import * as iconv from 'iconv-lite';
import { EncodingUtil } from './encoding-util';
@injectable()
export class FileSystemNodeOptions {
encoding: string;
recursive: boolean;
overwrite: boolean;
moveToTrash: boolean;
public static DEFAULT: FileSystemNodeOptions = {
encoding: 'utf8',
overwrite: false,
recursive: true,
moveToTrash: true
};
}
@injectable()
export class FileSystemNode implements FileSystem {
constructor(
@inject(FileSystemNodeOptions) @optional() protected readonly options: FileSystemNodeOptions = FileSystemNodeOptions.DEFAULT
) { }
protected client: FileSystemClient | undefined;
setClient(client: FileSystemClient | undefined): void {
this.client = client;
}
async getFileStat(uri: string): Promise<FileStat | undefined> {
const uri_ = new URI(uri);
const stat = await this.doGetStat(uri_, 1);
return stat;
}
async exists(uri: string): Promise<boolean> {
return fs.pathExists(FileUri.fsPath(new URI(uri)));
}
async resolveContent(uri: string, options?: { encoding?: string }): Promise<{ stat: FileStat, content: string }> {
const _uri = new URI(uri);
const stat = await this.doGetStat(_uri, 0);
if (!stat) {
throw FileSystemError.FileNotFound(uri);
}
if (stat.isDirectory) {
throw FileSystemError.FileIsDirectory(uri, 'Cannot resolve the content.');
}
const encoding = await this.doGetEncoding(options);
const contentBuffer = await fs.readFile(FileUri.fsPath(_uri));
const content = iconv.decode(contentBuffer, encoding);
return { stat, content };
}
async setContent(file: FileStat, content: string, options?: { encoding?: string }): Promise<FileStat> {
const _uri = new URI(file.uri);
const stat = await this.doGetStat(_uri, 0);
if (!stat) {
throw FileSystemError.FileNotFound(file.uri);
}
if (stat.isDirectory) {
throw FileSystemError.FileIsDirectory(file.uri, 'Cannot set the content.');
}
if (!(await this.isInSync(file, stat))) {
throw this.createOutOfSyncError(file, stat);
}
const encoding = await this.doGetEncoding(options);
const encodedContent = iconv.encode(content, encoding);
await fs.writeFile(FileUri.fsPath(_uri), encodedContent);
const newStat = await this.doGetStat(_uri, 1);
if (newStat) {
return newStat;
}
throw FileSystemError.FileNotFound(file.uri, 'Error occurred while writing file content.');
}
async updateContent(file: FileStat, contentChanges: TextDocumentContentChangeEvent[], options?: { encoding?: string, overwriteEncoding?: string }): Promise<FileStat> {
const _uri = new URI(file.uri);
const stat = await this.doGetStat(_uri, 0);
if (!stat) {
throw FileSystemError.FileNotFound(file.uri);
}
if (stat.isDirectory) {
throw FileSystemError.FileIsDirectory(file.uri, 'Cannot set the content.');
}
if (!this.checkInSync(file, stat)) {
throw this.createOutOfSyncError(file, stat);
}
if (contentChanges.length === 0 && !(options && options.overwriteEncoding)) {
return stat;
}
const encoding = await this.doGetEncoding(options);
const contentBuffer = await fs.readFile(FileUri.fsPath(_uri));
const content = iconv.decode(contentBuffer, encoding);
const newContent = this.applyContentChanges(content, contentChanges);
const writeEncoding = options && options.overwriteEncoding ? options.overwriteEncoding : encoding;
const encodedNewContent = iconv.encode(newContent, writeEncoding);
await fs.writeFile(FileUri.fsPath(_uri), encodedNewContent);
const newStat = await this.doGetStat(_uri, 1);
if (newStat) {
return newStat;
}
throw FileSystemError.FileNotFound(file.uri, 'Error occurred while writing file content.');
}
protected applyContentChanges(content: string, contentChanges: TextDocumentContentChangeEvent[]): string {
let document = TextDocument.create('', '', 1, content);
for (const change of contentChanges) {
let newContent: string;
if (TextDocumentContentChangeDelta.is(change)) {
const start = document.offsetAt(change.range.start);
const end = document.offsetAt(change.range.end);
newContent = document.getText().substr(0, start) + change.text + document.getText().substr(end);
} else {
newContent = change.text;
}
document = TextDocument.create(document.uri, document.languageId, document.version, newContent);
}
return document.getText();
}
protected async isInSync(file: FileStat, stat: FileStat): Promise<boolean> {
if (this.checkInSync(file, stat)) {
return true;
}
return this.client ? this.client.shouldOverwrite(file, stat) : false;
}
protected checkInSync(file: FileStat, stat: FileStat): boolean {
return stat.lastModification === file.lastModification && stat.size === file.size;
}
protected createOutOfSyncError(file: FileStat, stat: FileStat): Error {
return FileSystemError.FileIsOutOfSync(file, stat);
}
async move(sourceUri: string, targetUri: string, options?: FileMoveOptions): Promise<FileStat> {
if (this.client) {
this.client.onWillMove(sourceUri, targetUri);
}
const result = await this.doMove(sourceUri, targetUri, options);
if (this.client) {
this.client.onDidMove(sourceUri, targetUri);
}
return result;
}
protected async doMove(sourceUri: string, targetUri: string, options?: FileMoveOptions): Promise<FileStat> {
const _sourceUri = new URI(sourceUri);
const _targetUri = new URI(targetUri);
const [sourceStat, targetStat, overwrite] = await Promise.all([this.doGetStat(_sourceUri, 1), this.doGetStat(_targetUri, 1), this.doGetOverwrite(options)]);
if (!sourceStat) {
throw FileSystemError.FileNotFound(sourceUri);
}
if (targetStat && !overwrite) {
throw FileSystemError.FileExists(targetUri, "Did you set the 'overwrite' flag to true?");
}
// Different types. Files <-> Directory.
if (targetStat && sourceStat.isDirectory !== targetStat.isDirectory) {
if (targetStat.isDirectory) {
throw FileSystemError.FileIsDirectory(targetStat.uri, `Cannot move '${sourceStat.uri}' file to an existing location.`);
}
throw FileSystemError.FileNotDirectory(targetStat.uri, `Cannot move '${sourceStat.uri}' directory to an existing location.`);
}
const [sourceMightHaveChildren, targetMightHaveChildren] = await Promise.all([this.mayHaveChildren(_sourceUri), this.mayHaveChildren(_targetUri)]);
// Handling special Windows case when source and target resources are empty folders.
// Source should be deleted and target should be touched.
if (overwrite && targetStat && targetStat.isDirectory && sourceStat.isDirectory && !sourceMightHaveChildren && !targetMightHaveChildren) {
// The value should be a Unix timestamp in seconds.
// For example, `Date.now()` returns milliseconds, so it should be divided by `1000` before passing it in.
const now = Date.now() / 1000;
await fs.utimes(FileUri.fsPath(_targetUri), now, now);
await fs.rmdir(FileUri.fsPath(_sourceUri));
const newStat = await this.doGetStat(_targetUri, 1);
if (newStat) {
return newStat;
}
throw FileSystemError.FileNotFound(targetUri, `Error occurred when moving resource from '${sourceUri}' to '${targetUri}'.`);
} else if (overwrite && targetStat && targetStat.isDirectory && sourceStat.isDirectory && !targetMightHaveChildren && sourceMightHaveChildren) {
// Copy source to target, since target is empty. Then wipe the source content.
const newStat = await this.copy(sourceUri, targetUri, { overwrite });
await this.delete(sourceUri);
return newStat;
} else {
return new Promise<FileStat>((resolve, reject) => {
mv(FileUri.fsPath(_sourceUri), FileUri.fsPath(_targetUri), { mkdirp: true, clobber: overwrite }, async error => {
if (error) {
reject(error);
return;
}
resolve(await this.doGetStat(_targetUri, 1));
});
});
}
}
async copy(sourceUri: string, targetUri: string, options?: { overwrite?: boolean, recursive?: boolean }): Promise<FileStat> {
const _sourceUri = new URI(sourceUri);
const _targetUri = new URI(targetUri);
const [sourceStat, targetStat, overwrite, recursive] = await Promise.all([
this.doGetStat(_sourceUri, 0),
this.doGetStat(_targetUri, 0),
this.doGetOverwrite(options),
this.doGetRecursive(options)
]);
if (!sourceStat) {
throw FileSystemError.FileNotFound(sourceUri);
}
if (targetStat && !overwrite) {
throw FileSystemError.FileExists(targetUri, "Did you set the 'overwrite' flag to true?");
}
if (targetStat && targetStat.uri === sourceStat.uri) {
throw FileSystemError.FileExists(targetUri, 'Cannot perform copy, source and destination are the same.');
}
await fs.copy(FileUri.fsPath(_sourceUri), FileUri.fsPath(_targetUri), { overwrite, recursive });
const newStat = await this.doGetStat(_targetUri, 1);
if (newStat) {
return newStat;
}
throw FileSystemError.FileNotFound(targetUri, `Error occurred while copying ${sourceUri} to ${targetUri}.`);
}
async createFile(uri: string, options?: { content?: string, encoding?: string }): Promise<FileStat> {
const _uri = new URI(uri);
const parentUri = _uri.parent;
const [stat, parentStat] = await Promise.all([this.doGetStat(_uri, 0), this.doGetStat(parentUri, 0)]);
if (stat) {
throw FileSystemError.FileExists(uri, 'Error occurred while creating the file.');
}
if (!parentStat) {
await fs.mkdirs(FileUri.fsPath(parentUri));
}
const content = await this.doGetContent(options);
const encoding = await this.doGetEncoding(options);
const encodedNewContent = iconv.encode(content, encoding);
await fs.writeFile(FileUri.fsPath(_uri), encodedNewContent);
const newStat = await this.doGetStat(_uri, 1);
if (newStat) {
return newStat;
}
throw FileSystemError.FileNotFound(uri, 'Error occurred while creating the file.');
}
async createFolder(uri: string): Promise<FileStat> {
const _uri = new URI(uri);
const stat = await this.doGetStat(_uri, 0);
if (stat) {
if (stat.isDirectory) {
return stat;
}
throw FileSystemError.FileExists(uri, 'Error occurred while creating the directory: path is a file.');
}
await fs.mkdirs(FileUri.fsPath(_uri));
const newStat = await this.doGetStat(_uri, 1);
if (newStat) {
return newStat;
}
throw FileSystemError.FileNotFound(uri, 'Error occurred while creating the directory.');
}
async touchFile(uri: string): Promise<FileStat> {
const _uri = new URI(uri);
const stat = await this.doGetStat(_uri, 0);
if (!stat) {
return this.createFile(uri);
} else {
return new Promise<FileStat>((resolve, reject) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
touch(FileUri.fsPath(_uri), async (error: any) => {
if (error) {
reject(error);
return;
}
resolve(await this.doGetStat(_uri, 1));
});
});
}
}
async delete(uri: string, options?: FileDeleteOptions): Promise<void> {
const _uri = new URI(uri);
const stat = await this.doGetStat(_uri, 0);
if (!stat) {
throw FileSystemError.FileNotFound(uri);
}
// Windows 10.
// Deleting an empty directory throws `EPERM error` instead of `unlinkDir`.
// https://github.com/paulmillr/chokidar/issues/566
const moveToTrash = await this.doGetMoveToTrash(options);
if (moveToTrash) {
return trash([FileUri.fsPath(_uri)]);
} else {
const filePath = FileUri.fsPath(_uri);
const outputRootPath = paths.join(os.tmpdir(), v4());
try {
await new Promise<void>((resolve, reject) => {
fs.rename(filePath, outputRootPath, async error => {
if (error) {
reject(error);
return;
}
resolve();
});
});
// There is no reason for the promise returned by this function not to resolve
// as soon as the move is complete. Clearing up the temporary files can be
// done in the background.
fs.remove(FileUri.fsPath(outputRootPath));
} catch (error) {
return fs.remove(filePath);
}
}
}
async getEncoding(uri: string): Promise<string> {
const _uri = new URI(uri);
const stat = await this.doGetStat(_uri, 0);
if (!stat) {
throw FileSystemError.FileNotFound(uri);
}
if (stat.isDirectory) {
throw FileSystemError.FileIsDirectory(uri, 'Cannot get the encoding.');
}
return this.options.encoding;
}
async guessEncoding(uri: string): Promise<string | undefined> {
const _uri = new URI(uri);
const stat = await this.doGetStat(_uri, 0);
if (!stat) {
throw FileSystemError.FileNotFound(uri);
}
if (stat.isDirectory) {
throw FileSystemError.FileIsDirectory(uri, 'Cannot guess the encoding.');
}
return EncodingUtil.guessEncodingByBuffer(await fs.readFile(FileUri.fsPath(_uri)));
}
async getRoots(): Promise<FileStat[]> {
const cwdRoot = paths.parse(process.cwd()).root;
const rootUri = FileUri.create(cwdRoot);
const root = await this.doGetStat(rootUri, 1);
if (root) {
return [root];
}
return [];
}
async getCurrentUserHome(): Promise<FileStat | undefined> {
return this.getFileStat(FileUri.create(os.homedir()).toString());
}
getDrives(): Promise<string[]> {
return new Promise<string[]>((resolve, reject) => {
drivelist.list((error: Error, drives: { readonly mountpoints: { readonly path: string; }[] }[]) => {
if (error) {
reject(error);
return;
}
const uris = drives
.map(drive => drive.mountpoints)
.reduce((prev, curr) => prev.concat(curr), [])
.map(mountpoint => mountpoint.path)
.filter(this.filterMountpointPath.bind(this))
.map(path => FileUri.create(path))
.map(uri => uri.toString());
resolve(uris);
});
});
}
/**
* Filters hidden and system partitions.
*/
protected filterMountpointPath(path: string): boolean {
// OS X: This is your sleep-image. When your Mac goes to sleep it writes the contents of its memory to the hard disk. (https://bit.ly/2R6cztl)
if (path === '/private/var/vm') {
return false;
}
// Ubuntu: This system partition is simply the boot partition created when the computers mother board runs UEFI rather than BIOS. (https://bit.ly/2N5duHr)
if (path === '/boot/efi') {
return false;
}
return true;
}
dispose(): void {
// NOOP
}
async access(uri: string, mode: number = FileAccess.Constants.F_OK): Promise<boolean> {
try {
await fs.access(FileUri.fsPath(uri), mode);
return true;
} catch {
return false;
}
}
async getFsPath(uri: string): Promise<string | undefined> {
if (!uri.startsWith('file:/')) {
return undefined;
} else {
return FileUri.fsPath(uri);
}
}
protected async doGetStat(uri: URI, depth: number): Promise<FileStat | undefined> {
try {
const stats = await fs.stat(FileUri.fsPath(uri));
if (stats.isDirectory()) {
return this.doCreateDirectoryStat(uri, stats, depth);
}
return this.doCreateFileStat(uri, stats);
} catch (error) {
if (isErrnoException(error)) {
if (error.code === 'ENOENT' || error.code === 'EACCES' || error.code === 'EBUSY' || error.code === 'EPERM') {
return undefined;
}
}
throw error;
}
}
protected async doCreateFileStat(uri: URI, stat: fs.Stats): Promise<FileStat> {
return {
uri: uri.toString(),
lastModification: stat.mtime.getTime(),
isDirectory: false,
size: stat.size
};
}
protected async doCreateDirectoryStat(uri: URI, stat: fs.Stats, depth: number): Promise<FileStat> {
const children = depth > 0 ? await this.doGetChildren(uri, depth) : [];
return {
uri: uri.toString(),
lastModification: stat.mtime.getTime(),
isDirectory: true,
children
};
}
protected async doGetChildren(uri: URI, depth: number): Promise<FileStat[]> {
const files = await fs.readdir(FileUri.fsPath(uri));
const children = await Promise.all(files.map(fileName => uri.resolve(fileName)).map(childUri => this.doGetStat(childUri, depth - 1)));
return children.filter(notEmpty);
}
/**
* Return `true` if it's possible for this URI to have children.
* It might not be possible to be certain because of permission problems or other filesystem errors.
*/
protected async mayHaveChildren(uri: URI): Promise<boolean> {
/* If there's a problem reading the root directory. Assume it's not empty to avoid overwriting anything. */
try {
const rootStat = await this.doGetStat(uri, 0);
if (rootStat === undefined) {
return true;
}
/* Not a directory. */
if (rootStat !== undefined && rootStat.isDirectory === false) {
return false;
}
} catch {
return true;
}
/* If there's a problem with it's children then the directory must not be empty. */
try {
const stat = await this.doGetStat(uri, 1);
if (stat !== undefined && stat.children !== undefined) {
return stat.children.length > 0;
} else {
return true;
}
} catch {
return true;
}
}
protected async doGetEncoding(option?: { encoding?: string }): Promise<string> {
return option && typeof (option.encoding) !== 'undefined'
? option.encoding
: this.options.encoding;
}
protected async doGetOverwrite(option?: { overwrite?: boolean }): Promise<boolean> {
return option && typeof (option.overwrite) !== 'undefined'
? option.overwrite
: this.options.overwrite;
}
protected async doGetRecursive(option?: { recursive?: boolean }): Promise<boolean> {
return option && typeof (option.recursive) !== 'undefined'
? option.recursive
: this.options.recursive;
}
protected async doGetMoveToTrash(option?: { moveToTrash?: boolean }): Promise<boolean> {
return option && typeof (option.moveToTrash) !== 'undefined'
? option.moveToTrash
: this.options.moveToTrash;
}
protected async doGetContent(option?: { content?: string }): Promise<string> {
return (option && option.content) || '';
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function isErrnoException(error: any | NodeJS.ErrnoException): error is NodeJS.ErrnoException {
return (<NodeJS.ErrnoException>error).code !== undefined && (<NodeJS.ErrnoException>error).errno !== undefined;
}
function notEmpty<T>(value: T | undefined): value is T {
return value !== undefined;
}