-
Notifications
You must be signed in to change notification settings - Fork 39.6k
Expand file tree
/
Copy pathdiskFileSystemProviderClient.ts
More file actions
267 lines (205 loc) · 9.58 KB
/
diskFileSystemProviderClient.ts
File metadata and controls
267 lines (205 loc) · 9.58 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { VSBuffer } from '../../../base/common/buffer.js';
import { CancellationToken } from '../../../base/common/cancellation.js';
import { toErrorMessage } from '../../../base/common/errorMessage.js';
import { canceled } from '../../../base/common/errors.js';
import { Emitter, Event } from '../../../base/common/event.js';
import { Disposable, DisposableStore, IDisposable, toDisposable } from '../../../base/common/lifecycle.js';
import { newWriteableStream, ReadableStreamEventPayload, ReadableStreamEvents } from '../../../base/common/stream.js';
import { URI } from '../../../base/common/uri.js';
import { generateUuid } from '../../../base/common/uuid.js';
import { IChannel } from '../../../base/parts/ipc/common/ipc.js';
import { createFileSystemProviderError, FileSystemProviderCapabilities, FileSystemProviderErrorCode, FileType, IFileAtomicReadOptions, IFileChange, IFileDeleteOptions, IFileOpenOptions, IFileOverwriteOptions, IFileReadStreamOptions, IFileSystemProviderError, IFileSystemProviderWithFileAppendCapability, IFileSystemProviderWithFileAtomicReadCapability, IFileSystemProviderWithFileCloneCapability, IFileSystemProviderWithFileFolderCopyCapability, IFileSystemProviderWithFileReadStreamCapability, IFileSystemProviderWithFileReadWriteCapability, IFileSystemProviderWithOpenReadWriteCloseCapability, IFileWriteOptions, IStat, IWatchOptions } from './files.js';
import { reviveFileChanges } from './watcher.js';
export const LOCAL_FILE_SYSTEM_CHANNEL_NAME = 'localFilesystem';
/**
* An implementation of a local disk file system provider
* that is backed by a `IChannel` and thus implemented via
* IPC on a different process.
*/
export class DiskFileSystemProviderClient extends Disposable implements
IFileSystemProviderWithFileReadWriteCapability,
IFileSystemProviderWithOpenReadWriteCloseCapability,
IFileSystemProviderWithFileReadStreamCapability,
IFileSystemProviderWithFileFolderCopyCapability,
IFileSystemProviderWithFileAtomicReadCapability,
IFileSystemProviderWithFileAppendCapability,
IFileSystemProviderWithFileCloneCapability {
constructor(
private readonly channel: IChannel,
private readonly extraCapabilities: { trash?: boolean; pathCaseSensitive?: boolean }
) {
super();
this.registerFileChangeListeners();
}
//#region File Capabilities
readonly onDidChangeCapabilities: Event<void> = Event.None;
private _capabilities: FileSystemProviderCapabilities | undefined;
get capabilities(): FileSystemProviderCapabilities {
if (!this._capabilities) {
this._capabilities =
FileSystemProviderCapabilities.FileReadWrite |
FileSystemProviderCapabilities.FileOpenReadWriteClose |
FileSystemProviderCapabilities.FileReadStream |
FileSystemProviderCapabilities.FileFolderCopy |
FileSystemProviderCapabilities.FileWriteUnlock |
FileSystemProviderCapabilities.FileAtomicRead |
FileSystemProviderCapabilities.FileAtomicWrite |
FileSystemProviderCapabilities.FileAtomicDelete |
FileSystemProviderCapabilities.FileAppend |
FileSystemProviderCapabilities.FileClone |
FileSystemProviderCapabilities.FileRealpath;
if (this.extraCapabilities.pathCaseSensitive) {
this._capabilities |= FileSystemProviderCapabilities.PathCaseSensitive;
}
if (this.extraCapabilities.trash) {
this._capabilities |= FileSystemProviderCapabilities.Trash;
}
}
return this._capabilities;
}
//#endregion
//#region File Metadata Resolving
stat(resource: URI): Promise<IStat> {
return this.channel.call('stat', [resource]);
}
realpath(resource: URI): Promise<string> {
return this.channel.call('realpath', [resource]);
}
readdir(resource: URI): Promise<[string, FileType][]> {
return this.channel.call('readdir', [resource]);
}
//#endregion
//#region File Reading/Writing
async readFile(resource: URI, opts?: IFileAtomicReadOptions): Promise<Uint8Array> {
const { buffer } = await this.channel.call('readFile', [resource, opts]) as VSBuffer;
return buffer;
}
readFileStream(resource: URI, opts: IFileReadStreamOptions, token: CancellationToken): ReadableStreamEvents<Uint8Array> {
const stream = newWriteableStream<Uint8Array>(data => VSBuffer.concat(data.map(data => VSBuffer.wrap(data))).buffer);
const disposables = new DisposableStore();
// Reading as file stream goes through an event to the remote side
disposables.add(this.channel.listen<ReadableStreamEventPayload<VSBuffer>>('readFileStream', [resource, opts])(dataOrErrorOrEnd => {
// data
if (dataOrErrorOrEnd instanceof VSBuffer) {
stream.write(dataOrErrorOrEnd.buffer);
}
// end or error
else {
if (dataOrErrorOrEnd === 'end') {
stream.end();
} else {
let error: Error;
// Take Error as is if type matches
if (dataOrErrorOrEnd instanceof Error) {
error = dataOrErrorOrEnd;
}
// Otherwise, try to deserialize into an error.
// Since we communicate via IPC, we cannot be sure
// that Error objects are properly serialized.
else {
const errorCandidate = dataOrErrorOrEnd as IFileSystemProviderError;
error = createFileSystemProviderError(errorCandidate.message ?? toErrorMessage(errorCandidate), errorCandidate.code ?? FileSystemProviderErrorCode.Unknown);
}
stream.error(error);
stream.end();
}
// Signal to the remote side that we no longer listen
disposables.dispose();
}
}));
// Support cancellation
disposables.add(token.onCancellationRequested(() => {
// Ensure to end the stream properly with an error
// to indicate the cancellation.
stream.error(canceled());
stream.end();
// Ensure to dispose the listener upon cancellation. This will
// bubble through the remote side as event and allows to stop
// reading the file.
disposables.dispose();
}));
return stream;
}
writeFile(resource: URI, content: Uint8Array, opts: IFileWriteOptions): Promise<void> {
return this.channel.call('writeFile', [resource, VSBuffer.wrap(content), opts]);
}
appendFile(resource: URI, content: Uint8Array, opts: IFileWriteOptions): Promise<void> {
return this.channel.call('appendFile', [resource, VSBuffer.wrap(content), opts]);
}
open(resource: URI, opts: IFileOpenOptions): Promise<number> {
return this.channel.call('open', [resource, opts]);
}
close(fd: number): Promise<void> {
return this.channel.call('close', [fd]);
}
async read(fd: number, pos: number, data: Uint8Array, offset: number, length: number): Promise<number> {
const [bytes, bytesRead]: [VSBuffer, number] = await this.channel.call('read', [fd, pos, length]);
// copy back the data that was written into the buffer on the remote
// side. we need to do this because buffers are not referenced by
// pointer, but only by value and as such cannot be directly written
// to from the other process.
data.set(bytes.buffer.slice(0, bytesRead), offset);
return bytesRead;
}
write(fd: number, pos: number, data: Uint8Array, offset: number, length: number): Promise<number> {
return this.channel.call('write', [fd, pos, VSBuffer.wrap(data), offset, length]);
}
//#endregion
//#region Move/Copy/Delete/Create Folder
mkdir(resource: URI): Promise<void> {
return this.channel.call('mkdir', [resource]);
}
delete(resource: URI, opts: IFileDeleteOptions): Promise<void> {
return this.channel.call('delete', [resource, opts]);
}
rename(resource: URI, target: URI, opts: IFileOverwriteOptions): Promise<void> {
return this.channel.call('rename', [resource, target, opts]);
}
copy(resource: URI, target: URI, opts: IFileOverwriteOptions): Promise<void> {
return this.channel.call('copy', [resource, target, opts]);
}
//#endregion
//#region Clone File
cloneFile(resource: URI, target: URI): Promise<void> {
return this.channel.call('cloneFile', [resource, target]);
}
//#endregion
//#region File Watching
private readonly _onDidChange = this._register(new Emitter<readonly IFileChange[]>());
readonly onDidChangeFile = this._onDidChange.event;
private readonly _onDidWatchError = this._register(new Emitter<string>());
readonly onDidWatchError = this._onDidWatchError.event;
// The contract for file watching via remote is to identify us
// via a unique but readonly session ID. Since the remote is
// managing potentially many watchers from different clients,
// this helps the server to properly partition events to the right
// clients.
private readonly sessionId = generateUuid();
private registerFileChangeListeners(): void {
// The contract for file changes is that there is one listener
// for both events and errors from the watcher. So we need to
// unwrap the event from the remote and emit through the proper
// emitter.
this._register(this.channel.listen<IFileChange[] | string>('fileChange', [this.sessionId])(eventsOrError => {
if (Array.isArray(eventsOrError)) {
const events = eventsOrError;
this._onDidChange.fire(reviveFileChanges(events));
} else {
const error = eventsOrError;
this._onDidWatchError.fire(error);
}
}));
}
watch(resource: URI, opts: IWatchOptions): IDisposable {
// Generate a request UUID to correlate the watcher
// back to us when we ask to dispose the watcher later.
const req = generateUuid();
this.channel.call('watch', [this.sessionId, req, resource, opts]);
return toDisposable(() => this.channel.call('unwatch', [this.sessionId, req]));
}
//#endregion
}