forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileSystem.ts
More file actions
324 lines (293 loc) · 11.1 KB
/
fileSystem.ts
File metadata and controls
324 lines (293 loc) · 11.1 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
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { createHash } from 'crypto';
import * as fileSystem from 'fs';
import * as fs from 'fs-extra';
import * as glob from 'glob';
import { inject, injectable } from 'inversify';
import { promisify } from 'util';
import { createDeferred } from '../utils/async';
import { noop } from '../utils/misc';
import { FileSystemPaths, FileSystemPathUtils } from './fs-paths';
import { TemporaryFileSystem } from './fs-temp';
// prettier-ignore
import {
FileStat, FileType,
IFileSystem, IFileSystemPaths, IPlatformService,
TemporaryFile
} from './types';
const globAsync = promisify(glob);
// This helper function determines the file type of the given stats
// object. The type follows the convention of node's fs module, where
// a file has exactly one type. Symlinks are not resolved.
function convertFileType(stat: fs.Stats): FileType {
if (stat.isFile()) {
return FileType.File;
} else if (stat.isDirectory()) {
return FileType.Directory;
} else if (stat.isSymbolicLink()) {
// The caller is responsible for combining this ("logical or")
// with File or Directory as necessary.
return FileType.SymbolicLink;
} else {
return FileType.Unknown;
}
}
async function getFileType(filename: string): Promise<FileType> {
let stat: fs.Stats;
try {
// Note that we used to use stat() here instead of lstat().
// This shouldn't matter because the only consumers were
// internal methods that have been updated appropriately.
stat = await fs.lstat(filename);
} catch {
return FileType.Unknown;
}
if (!stat.isSymbolicLink()) {
return convertFileType(stat);
}
// For symlinks we emulate the behavior of the vscode.workspace.fs API.
// See: https://code.visualstudio.com/api/references/vscode-api#FileType
try {
stat = await fs.stat(filename);
} catch {
return FileType.SymbolicLink;
}
if (stat.isFile()) {
return FileType.SymbolicLink | FileType.File;
} else if (stat.isDirectory()) {
return FileType.SymbolicLink | FileType.Directory;
} else {
return FileType.SymbolicLink;
}
}
export function convertStat(old: fs.Stats, filetype: FileType): FileStat {
return {
type: filetype,
size: old.size,
// FileStat.ctime and FileStat.mtime only have 1-millisecond
// resolution, while node provides nanosecond resolution. So
// for now we round to the nearest integer.
// See: https://github.com/microsoft/vscode/issues/84526
ctime: Math.round(old.ctimeMs),
mtime: Math.round(old.mtimeMs)
};
}
@injectable()
export class FileSystem implements IFileSystem {
private readonly paths: IFileSystemPaths;
private readonly pathUtils: FileSystemPathUtils;
private readonly tmp: TemporaryFileSystem;
// prettier-ignore
constructor(
@inject(IPlatformService) platformService: IPlatformService
) {
// prettier-ignore
this.paths = FileSystemPaths.withDefaults(
platformService.isWindows
);
this.pathUtils = FileSystemPathUtils.withDefaults(this.paths);
this.tmp = TemporaryFileSystem.withDefaults();
}
//=================================
// path-related
public get directorySeparatorChar(): string {
return this.paths.sep;
}
public arePathsSame(path1: string, path2: string): boolean {
return this.pathUtils.arePathsSame(path1, path2);
}
//=================================
// "raw" operations
public async stat(filePath: string): Promise<FileStat> {
// Do not import vscode directly, as this isn't available in the Debugger Context.
// If stat is used in debugger context, it will fail, however theres a separate PR that will resolve this.
// tslint:disable-next-line: no-require-imports
const vscode = require('vscode');
// Note that, prior to the November release of VS Code,
// stat.ctime was always 0.
// See: https://github.com/microsoft/vscode/issues/84525
return vscode.workspace.fs.stat(vscode.Uri.file(filePath));
}
public async lstat(filename: string): Promise<FileStat> {
const stat = await fs.lstat(filename);
// Note that, unlike stat(), lstat() does not include the type
// of the symlink's target.
const fileType = convertFileType(stat);
return convertStat(stat, fileType);
}
// Return the UTF8-decoded text of the file.
public readFile(filePath: string): Promise<string> {
return fs.readFile(filePath, 'utf8');
}
public readFileSync(filePath: string): string {
return fs.readFileSync(filePath, 'utf8');
}
public readData(filePath: string): Promise<Buffer> {
return fs.readFile(filePath);
}
public async writeFile(filePath: string, data: {}, options: string | fs.WriteFileOptions = { encoding: 'utf8' }): Promise<void> {
await fs.writeFile(filePath, data, options);
}
public createDirectory(directoryPath: string): Promise<void> {
return fs.mkdirp(directoryPath);
}
public deleteDirectory(directoryPath: string): Promise<void> {
const deferred = createDeferred<void>();
fs.rmdir(directoryPath, err => (err ? deferred.reject(err) : deferred.resolve()));
return deferred.promise;
}
public async listdir(dirname: string): Promise<[string, FileType][]> {
const files = await fs.readdir(dirname);
const promises = files.map(async basename => {
const filename = this.paths.join(dirname, basename);
const fileType = await getFileType(filename);
return [filename, fileType] as [string, FileType];
});
return Promise.all(promises);
}
public appendFile(filename: string, data: {}): Promise<void> {
return fs.appendFile(filename, data);
}
public copyFile(src: string, dest: string): Promise<void> {
const deferred = createDeferred<void>();
const rs = fs.createReadStream(src).on('error', err => {
deferred.reject(err);
});
const ws = fs
.createWriteStream(dest)
.on('error', err => {
deferred.reject(err);
})
.on('close', () => {
deferred.resolve();
});
rs.pipe(ws);
return deferred.promise;
}
public deleteFile(filename: string): Promise<void> {
const deferred = createDeferred<void>();
fs.unlink(filename, err => (err ? deferred.reject(err) : deferred.resolve()));
return deferred.promise;
}
public chmod(filePath: string, mode: string | number): Promise<void> {
return new Promise<void>((resolve, reject) => {
fileSystem.chmod(filePath, mode, (err: NodeJS.ErrnoException | null) => {
if (err) {
return reject(err);
}
resolve();
});
});
}
public async move(src: string, tgt: string) {
await fs.rename(src, tgt);
}
public createReadStream(filePath: string): fileSystem.ReadStream {
return fileSystem.createReadStream(filePath);
}
public createWriteStream(filePath: string): fileSystem.WriteStream {
return fileSystem.createWriteStream(filePath);
}
//=================================
// utils
public objectExists(filePath: string, statCheck: (s: fs.Stats) => boolean): Promise<boolean> {
return new Promise<boolean>(resolve => {
// Note that we are using stat() rather than lstat(). This
// means that any symlinks are getting resolved.
fs.stat(filePath, (error, stats) => {
if (error) {
return resolve(false);
}
return resolve(statCheck(stats));
});
});
}
public fileExists(filePath: string): Promise<boolean> {
return this.objectExists(filePath, stats => stats.isFile());
}
public fileExistsSync(filePath: string): boolean {
return fs.existsSync(filePath);
}
public directoryExists(filePath: string): Promise<boolean> {
return this.objectExists(filePath, stats => stats.isDirectory());
}
public async getSubDirectories(dirname: string): Promise<string[]> {
let files: [string, FileType][];
try {
files = await this.listdir(dirname);
} catch {
// We're only preserving pre-existng behavior here...
return [];
}
return files
.filter(([_file, fileType]) => {
// We preserve the pre-existing behavior of following
// symlinks.
return (fileType & FileType.Directory) > 0;
})
.map(([filename, _ft]) => filename);
}
public async getFiles(dirname: string): Promise<string[]> {
let files: [string, FileType][];
try {
files = await this.listdir(dirname);
} catch (err) {
// This matches what getSubDirectories() does.
if (!(await fs.pathExists(dirname))) {
return [];
}
throw err; // re-throw
}
return files
.filter(([_file, fileType]) => {
// We preserve the pre-existing behavior of following
// symlinks.
return (fileType & FileType.File) > 0;
})
.map(([filename, _ft]) => filename);
}
public getFileHash(filePath: string): Promise<string> {
return new Promise<string>((resolve, reject) => {
fs.lstat(filePath, (err, stats) => {
if (err) {
reject(err);
} else {
const actual = createHash('sha512')
.update(`${stats.ctimeMs}-${stats.mtimeMs}`)
.digest('hex');
resolve(actual);
}
});
});
}
public async search(globPattern: string, cwd?: string): Promise<string[]> {
let found: string[];
if (cwd) {
const options = {
cwd: cwd
};
found = await globAsync(globPattern, options);
} else {
found = await globAsync(globPattern);
}
return Array.isArray(found) ? found : [];
}
public createTemporaryFile(extension: string): Promise<TemporaryFile> {
return this.tmp.createFile(extension);
}
public async isDirReadonly(dirname: string): Promise<boolean> {
const filePath = `${dirname}${this.paths.sep}___vscpTest___`;
return new Promise<boolean>(resolve => {
fs.open(filePath, fs.constants.O_CREAT | fs.constants.O_RDWR, (error, fd) => {
if (!error) {
fs.close(fd, () => {
fs.unlink(filePath, noop);
});
}
return resolve(!error);
});
});
}
}