@theia/filesystem 1.45.0 → 1.46.0-next.106

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (140) hide show
  1. package/lib/browser/breadcrumbs/filepath-breadcrumbs-container.js +8 -19
  2. package/lib/browser/breadcrumbs/filepath-breadcrumbs-container.js.map +1 -1
  3. package/lib/browser/breadcrumbs/filepath-breadcrumbs-contribution.js +8 -16
  4. package/lib/browser/breadcrumbs/filepath-breadcrumbs-contribution.js.map +1 -1
  5. package/lib/browser/download/file-download-command-contribution.js +6 -14
  6. package/lib/browser/download/file-download-command-contribution.js.map +1 -1
  7. package/lib/browser/download/file-download-service.js +6 -14
  8. package/lib/browser/download/file-download-service.js.map +1 -1
  9. package/lib/browser/file-dialog/file-dialog-hidden-files-renderer.js +7 -15
  10. package/lib/browser/file-dialog/file-dialog-hidden-files-renderer.js.map +1 -1
  11. package/lib/browser/file-dialog/file-dialog-model.js +8 -16
  12. package/lib/browser/file-dialog/file-dialog-model.js.map +1 -1
  13. package/lib/browser/file-dialog/file-dialog-service.js +14 -22
  14. package/lib/browser/file-dialog/file-dialog-service.js.map +1 -1
  15. package/lib/browser/file-dialog/file-dialog-tree-filters-renderer.js +4 -15
  16. package/lib/browser/file-dialog/file-dialog-tree-filters-renderer.js.map +1 -1
  17. package/lib/browser/file-dialog/file-dialog-tree.js +2 -7
  18. package/lib/browser/file-dialog/file-dialog-tree.js.map +1 -1
  19. package/lib/browser/file-dialog/file-dialog-widget.js +6 -17
  20. package/lib/browser/file-dialog/file-dialog-widget.js.map +1 -1
  21. package/lib/browser/file-dialog/file-dialog.js +34 -45
  22. package/lib/browser/file-dialog/file-dialog.js.map +1 -1
  23. package/lib/browser/file-dialog/index.js +5 -14
  24. package/lib/browser/file-dialog/index.js.map +1 -1
  25. package/lib/browser/file-resource.d.ts +6 -2
  26. package/lib/browser/file-resource.d.ts.map +1 -1
  27. package/lib/browser/file-resource.js +39 -22
  28. package/lib/browser/file-resource.js.map +1 -1
  29. package/lib/browser/file-service.d.ts +13 -0
  30. package/lib/browser/file-service.d.ts.map +1 -1
  31. package/lib/browser/file-service.js +46 -40
  32. package/lib/browser/file-service.js.map +1 -1
  33. package/lib/browser/file-tree/file-tree-decorator-adapter.js +10 -18
  34. package/lib/browser/file-tree/file-tree-decorator-adapter.js.map +1 -1
  35. package/lib/browser/file-tree/file-tree-label-provider.js +6 -14
  36. package/lib/browser/file-tree/file-tree-label-provider.js.map +1 -1
  37. package/lib/browser/file-tree/file-tree-model.js +14 -22
  38. package/lib/browser/file-tree/file-tree-model.js.map +1 -1
  39. package/lib/browser/file-tree/file-tree-widget.js +10 -21
  40. package/lib/browser/file-tree/file-tree-widget.js.map +1 -1
  41. package/lib/browser/file-tree/file-tree.js +6 -14
  42. package/lib/browser/file-tree/file-tree.js.map +1 -1
  43. package/lib/browser/file-tree/index.js +7 -16
  44. package/lib/browser/file-tree/index.js.map +1 -1
  45. package/lib/browser/file-upload-service.d.ts +2 -4
  46. package/lib/browser/file-upload-service.d.ts.map +1 -1
  47. package/lib/browser/file-upload-service.js +14 -22
  48. package/lib/browser/file-upload-service.js.map +1 -1
  49. package/lib/browser/filesystem-frontend-contribution.js +18 -26
  50. package/lib/browser/filesystem-frontend-contribution.js.map +1 -1
  51. package/lib/browser/filesystem-save-resource-service.d.ts +1 -1
  52. package/lib/browser/filesystem-save-resource-service.d.ts.map +1 -1
  53. package/lib/browser/filesystem-save-resource-service.js +10 -17
  54. package/lib/browser/filesystem-save-resource-service.js.map +1 -1
  55. package/lib/browser/filesystem-watcher-error-handler.js +6 -14
  56. package/lib/browser/filesystem-watcher-error-handler.js.map +1 -1
  57. package/lib/browser/index.js +6 -15
  58. package/lib/browser/index.js.map +1 -1
  59. package/lib/browser/location/index.js +3 -12
  60. package/lib/browser/location/index.js.map +1 -1
  61. package/lib/browser/location/location-renderer.js +12 -23
  62. package/lib/browser/location/location-renderer.js.map +1 -1
  63. package/lib/browser/remote-file-service-contribution.js +4 -12
  64. package/lib/browser/remote-file-service-contribution.js.map +1 -1
  65. package/lib/browser-only/browser-only-filesystem-frontend-module.d.ts +4 -0
  66. package/lib/browser-only/browser-only-filesystem-frontend-module.d.ts.map +1 -0
  67. package/lib/browser-only/browser-only-filesystem-frontend-module.js +41 -0
  68. package/lib/browser-only/browser-only-filesystem-frontend-module.js.map +1 -0
  69. package/lib/browser-only/browser-only-filesystem-provider-server.d.ts +12 -0
  70. package/lib/browser-only/browser-only-filesystem-provider-server.d.ts.map +1 -0
  71. package/lib/browser-only/browser-only-filesystem-provider-server.js +40 -0
  72. package/lib/browser-only/browser-only-filesystem-provider-server.js.map +1 -0
  73. package/lib/browser-only/browserfs-filesystem-initialization.d.ts +13 -0
  74. package/lib/browser-only/browserfs-filesystem-initialization.d.ts.map +1 -0
  75. package/lib/browser-only/browserfs-filesystem-initialization.js +55 -0
  76. package/lib/browser-only/browserfs-filesystem-initialization.js.map +1 -0
  77. package/lib/browser-only/browserfs-filesystem-provider.d.ts +46 -0
  78. package/lib/browser-only/browserfs-filesystem-provider.d.ts.map +1 -0
  79. package/lib/browser-only/browserfs-filesystem-provider.js +440 -0
  80. package/lib/browser-only/browserfs-filesystem-provider.js.map +1 -0
  81. package/lib/common/files.d.ts +8 -0
  82. package/lib/common/files.d.ts.map +1 -1
  83. package/lib/common/files.js +9 -1
  84. package/lib/common/files.js.map +1 -1
  85. package/lib/common/index.js +3 -12
  86. package/lib/common/index.js.map +1 -1
  87. package/lib/common/remote-file-system-provider.d.ts +11 -2
  88. package/lib/common/remote-file-system-provider.d.ts.map +1 -1
  89. package/lib/common/remote-file-system-provider.js +46 -24
  90. package/lib/common/remote-file-system-provider.js.map +1 -1
  91. package/lib/electron-browser/file-dialog/electron-file-dialog-service.js +5 -13
  92. package/lib/electron-browser/file-dialog/electron-file-dialog-service.js.map +1 -1
  93. package/lib/electron-main/electron-api-main.js +2 -7
  94. package/lib/electron-main/electron-api-main.js.map +1 -1
  95. package/lib/node/disk-file-system-provider.d.ts.map +1 -1
  96. package/lib/node/disk-file-system-provider.js +20 -22
  97. package/lib/node/disk-file-system-provider.js.map +1 -1
  98. package/lib/node/disk-file-system-provider.spec.js +35 -4
  99. package/lib/node/disk-file-system-provider.spec.js.map +1 -1
  100. package/lib/node/download/directory-archiver.js +3 -8
  101. package/lib/node/download/directory-archiver.js.map +1 -1
  102. package/lib/node/download/directory-archiver.spec.js +1 -1
  103. package/lib/node/download/directory-archiver.spec.js.map +1 -1
  104. package/lib/node/download/file-download-cache.js +4 -12
  105. package/lib/node/download/file-download-cache.js.map +1 -1
  106. package/lib/node/download/file-download-endpoint.js +9 -17
  107. package/lib/node/download/file-download-endpoint.js.map +1 -1
  108. package/lib/node/download/file-download-handler.d.ts.map +1 -1
  109. package/lib/node/download/file-download-handler.js +17 -25
  110. package/lib/node/download/file-download-handler.js.map +1 -1
  111. package/lib/node/file-change-collection.spec.js +1 -1
  112. package/lib/node/file-change-collection.spec.js.map +1 -1
  113. package/lib/node/filesystem-watcher-client.js +6 -14
  114. package/lib/node/filesystem-watcher-client.js.map +1 -1
  115. package/lib/node/filesystem-watcher-dispatcher.js +2 -7
  116. package/lib/node/filesystem-watcher-dispatcher.js.map +1 -1
  117. package/lib/node/node-file-upload-service.js +2 -7
  118. package/lib/node/node-file-upload-service.js.map +1 -1
  119. package/lib/node/nsfw-watcher/nsfw-filesystem-service.js +1 -1
  120. package/lib/node/nsfw-watcher/nsfw-filesystem-service.js.map +1 -1
  121. package/package.json +9 -6
  122. package/src/browser/file-resource.ts +36 -7
  123. package/src/browser/file-service.ts +40 -13
  124. package/src/browser/file-upload-service.ts +4 -6
  125. package/src/browser/filesystem-save-resource-service.ts +3 -2
  126. package/src/browser-only/browser-only-filesystem-frontend-module.ts +38 -0
  127. package/src/browser-only/browser-only-filesystem-provider-server.ts +32 -0
  128. package/src/browser-only/browserfs-filesystem-initialization.ts +61 -0
  129. package/src/browser-only/browserfs-filesystem-provider.ts +462 -0
  130. package/src/common/files.ts +13 -0
  131. package/src/common/remote-file-system-provider.ts +40 -2
  132. package/src/electron-browser/file-dialog/electron-file-dialog-service.ts +1 -1
  133. package/src/node/disk-file-system-provider.spec.ts +38 -5
  134. package/src/node/disk-file-system-provider.ts +9 -4
  135. package/src/node/download/directory-archiver.spec.ts +1 -1
  136. package/src/node/download/directory-archiver.ts +1 -1
  137. package/src/node/download/file-download-endpoint.ts +1 -1
  138. package/src/node/download/file-download-handler.ts +6 -6
  139. package/src/node/file-change-collection.spec.ts +1 -1
  140. package/src/node/nsfw-watcher/nsfw-filesystem-service.ts +1 -1
@@ -0,0 +1,61 @@
1
+ // *****************************************************************************
2
+ // Copyright (C) 2023 EclipseSource and others.
3
+ //
4
+ // This program and the accompanying materials are made available under the
5
+ // terms of the Eclipse Public License v. 2.0 which is available at
6
+ // http://www.eclipse.org/legal/epl-2.0.
7
+ //
8
+ // This Source Code may also be made available under the following Secondary
9
+ // Licenses when the conditions for such availability set forth in the Eclipse
10
+ // Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
+ // with the GNU Classpath Exception which is available at
12
+ // https://www.gnu.org/software/classpath/license.html.
13
+ //
14
+ // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
15
+ // *****************************************************************************
16
+
17
+ import type { FSModule } from 'browserfs/dist/node/core/FS';
18
+ import type { BrowserFSFileSystemProvider } from './browserfs-filesystem-provider';
19
+ import { injectable } from '@theia/core/shared/inversify';
20
+ import { FileSystem, initialize } from 'browserfs';
21
+ import MountableFileSystem from 'browserfs/dist/node/backend/MountableFileSystem';
22
+
23
+ export const BrowserFSInitialization = Symbol('BrowserFSInitialization');
24
+ export interface BrowserFSInitialization {
25
+ createMountableFileSystem(): Promise<MountableFileSystem>
26
+ initializeFS: (fs: FSModule, provider: BrowserFSFileSystemProvider) => Promise<void>;
27
+ }
28
+
29
+ @injectable()
30
+ export class DefaultBrowserFSInitialization implements BrowserFSInitialization {
31
+
32
+ createMountableFileSystem(): Promise<MountableFileSystem> {
33
+ return new Promise(resolve => {
34
+ FileSystem.IndexedDB.Create({}, (e, persistedFS) => {
35
+ if (e) {
36
+ throw e;
37
+ }
38
+ if (!persistedFS) {
39
+ throw Error('Could not create filesystem');
40
+ }
41
+ FileSystem.MountableFileSystem.Create({
42
+ '/home': persistedFS
43
+
44
+ }, (error, mountableFS) => {
45
+ if (error) {
46
+ throw error;
47
+ }
48
+ if (!mountableFS) {
49
+ throw Error('Could not create filesystem');
50
+ }
51
+ initialize(mountableFS);
52
+ resolve(mountableFS);
53
+ });
54
+ });
55
+ });
56
+ }
57
+
58
+ async initializeFS(fs: FSModule, provider: BrowserFSFileSystemProvider): Promise<void> {
59
+
60
+ }
61
+ }
@@ -0,0 +1,462 @@
1
+ // *****************************************************************************
2
+ // Copyright (C) 2023 EclipseSource and others.
3
+ //
4
+ // This program and the accompanying materials are made available under the
5
+ // terms of the Eclipse Public License v. 2.0 which is available at
6
+ // http://www.eclipse.org/legal/epl-2.0.
7
+ //
8
+ // This Source Code may also be made available under the following Secondary
9
+ // Licenses when the conditions for such availability set forth in the Eclipse
10
+ // Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
+ // with the GNU Classpath Exception which is available at
12
+ // https://www.gnu.org/software/classpath/license.html.
13
+ //
14
+ // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
15
+ // *****************************************************************************
16
+ /*---------------------------------------------------------------------------------------------
17
+ * Copyright (c) Microsoft Corporation. All rights reserved.
18
+ * Licensed under the MIT License. See License.txt in the project root for license information.
19
+ *--------------------------------------------------------------------------------------------*/
20
+ // based on https://github.com/microsoft/vscode/blob/04c36be045a94fee58e5f8992d3e3fd980294a84/src/vs/platform/files/node/diskFileSystemProvider.ts
21
+
22
+ /* eslint-disable no-null/no-null */
23
+
24
+ import { inject, injectable } from '@theia/core/shared/inversify';
25
+ import {
26
+ FileChange, FileDeleteOptions, FileOpenOptions,
27
+ FileOverwriteOptions, FileReadStreamOptions, FileSystemProviderCapabilities,
28
+ FileSystemProviderError,
29
+ FileSystemProviderErrorCode,
30
+ FileSystemProviderWithFileReadWriteCapability,
31
+ FileType, FileUpdateOptions, FileUpdateResult, FileWriteOptions, Stat, WatchOptions, createFileSystemProviderError
32
+ } from '../common/files';
33
+ import { Event, URI, Disposable, CancellationToken } from '@theia/core';
34
+ import { TextDocumentContentChangeEvent } from '@theia/core/shared/vscode-languageserver-protocol';
35
+ import { ReadableStreamEvents } from '@theia/core/lib/common/stream';
36
+ import { BFSRequire } from 'browserfs';
37
+ import type { FSModule } from 'browserfs/dist/node/core/FS';
38
+ import type { FileSystem } from 'browserfs/dist/node/core/file_system';
39
+ import MountableFileSystem from 'browserfs/dist/node/backend/MountableFileSystem';
40
+ import { basename, dirname, normalize } from 'path';
41
+ import Stats from 'browserfs/dist/node/core/node_fs_stats';
42
+ import { retry } from '@theia/core/lib/common/promise-util';
43
+ import { BrowserFSInitialization } from './browserfs-filesystem-initialization';
44
+
45
+ // adapted from DiskFileSystemProvider
46
+ @injectable()
47
+ export class BrowserFSFileSystemProvider implements FileSystemProviderWithFileReadWriteCapability {
48
+ capabilities: FileSystemProviderCapabilities = FileSystemProviderCapabilities.FileReadWrite;
49
+ onDidChangeCapabilities: Event<void> = Event.None;
50
+ onDidChangeFile: Event<readonly FileChange[]> = Event.None;
51
+ onFileWatchError: Event<void> = Event.None;
52
+ private mapHandleToPos: Map<number, number> = new Map();
53
+ private writeHandles: Set<number> = new Set();
54
+ private canFlush: boolean = true;
55
+
56
+ private fs: FSModule;
57
+ private mountableFS: MountableFileSystem;
58
+ private initialized: Promise<true>;
59
+
60
+ constructor(@inject(BrowserFSInitialization) readonly initialization: BrowserFSInitialization) {
61
+ const init = async (): Promise<true> => {
62
+ this.mountableFS = await initialization.createMountableFileSystem();
63
+ this.fs = BFSRequire('fs');
64
+ await initialization.initializeFS(this.fs, new Proxy(this, {
65
+ get(target, prop, receiver): unknown {
66
+ if (prop === 'initialized') {
67
+ return Promise.resolve(true);
68
+ }
69
+ return Reflect.get(target, prop, receiver);
70
+ }
71
+ }));
72
+ return true;
73
+ };
74
+ this.initialized = init();
75
+ }
76
+
77
+ async mount(mountPoint: string, fs: FileSystem): Promise<void> {
78
+ await this.initialized;
79
+ this.mountableFS.mount(mountPoint, fs);
80
+ };
81
+
82
+ watch(_resource: URI, _opts: WatchOptions): Disposable {
83
+ return Disposable.NULL;
84
+ }
85
+ async stat(resource: URI): Promise<Stat> {
86
+ await this.initialized;
87
+ const path = this.toFilePath(resource);
88
+
89
+ let stats: Stats;
90
+ try {
91
+ stats = await this.promisify(this.fs.stat)(path) as Stats;
92
+ } catch (error) {
93
+ throw this.toFileSystemProviderError(error);
94
+ }
95
+ if (stats === undefined) {
96
+ throw new Error(`Could not read file stat for resource '${path}'`);
97
+ }
98
+ return {
99
+ type: this.toType(stats, /* symbolicLink */undefined), // FIXME: missing symbolicLink
100
+ ctime: stats.birthtime.getTime(), // intentionally not using ctime here, we want the creation time
101
+ mtime: stats.mtime.getTime(),
102
+ size: stats.size,
103
+ // FIXME: missing mode, permissions
104
+ };
105
+
106
+ }
107
+ async mkdir(resource: URI): Promise<void> {
108
+ await this.initialized;
109
+ try {
110
+ await this.promisify(this.fs.mkdir)(this.toFilePath(resource));
111
+ } catch (error) {
112
+ throw this.toFileSystemProviderError(error);
113
+ }
114
+ }
115
+ async readdir(resource: URI): Promise<[string, FileType][]> {
116
+ await this.initialized;
117
+ try {
118
+
119
+ const children = await this.promisify(this.fs.readdir)(this.toFilePath(resource)) as string[];
120
+ const result: [string, FileType][] = [];
121
+ await Promise.all(children.map(async child => {
122
+ try {
123
+ const stat = await this.stat(resource.resolve(child));
124
+ result.push([child, stat.type]);
125
+ } catch (error) {
126
+ console.trace(error); // ignore errors for individual entries that can arise from permission denied
127
+ }
128
+ }));
129
+
130
+ return result;
131
+ } catch (error) {
132
+ throw this.toFileSystemProviderError(error);
133
+ }
134
+ }
135
+ async delete(resource: URI, _opts: FileDeleteOptions): Promise<void> {
136
+ await this.initialized;
137
+ // FIXME use options
138
+ try {
139
+ await this.promisify(this.fs.unlink)(this.toFilePath(resource));
140
+ } catch (error) {
141
+ throw this.toFileSystemProviderError(error);
142
+ }
143
+ }
144
+ async rename(from: URI, to: URI, opts: FileOverwriteOptions): Promise<void> {
145
+ await this.initialized;
146
+ const fromFilePath = this.toFilePath(from);
147
+ const toFilePath = this.toFilePath(to);
148
+ if (fromFilePath === toFilePath) {
149
+ return; // simulate node.js behaviour here and do a no-op if paths match
150
+ }
151
+ try {
152
+ // assume FS is path case sensitive - correct?
153
+ const targetExists = await this.promisify(this.fs.exists)(toFilePath);
154
+ if (targetExists) {
155
+ throw Error(`File '${toFilePath}' already exists.`);
156
+ }
157
+ if (fromFilePath === toFilePath) {
158
+ return Promise.resolve();
159
+ }
160
+
161
+ await this.promisify(this.fs.rename)(fromFilePath, toFilePath);
162
+
163
+ const stat = await this.promisify(this.fs.lstat)(toFilePath) as Stats;
164
+ if (stat.isDirectory() || stat.isSymbolicLink()) {
165
+ return Promise.resolve(); // only for files
166
+ }
167
+ const fd = await this.promisify(open)(toFilePath, 'a');
168
+ try {
169
+ await this.promisify(this.fs.futimes)(fd, stat.atime, new Date());
170
+ } catch (error) {
171
+ // ignore
172
+ }
173
+
174
+ this.promisify(this.fs.close)(fd);
175
+ } catch (error) {
176
+ // rewrite some typical errors that can happen especially around symlinks
177
+ // to something the user can better understand
178
+ if (error.code === 'EINVAL' || error.code === 'EBUSY' || error.code === 'ENAMETOOLONG') {
179
+ error = new Error(`Unable to move '${basename(fromFilePath)}' into '${basename(dirname(toFilePath))}' (${error.toString()}).`);
180
+ }
181
+
182
+ throw this.toFileSystemProviderError(error);
183
+ }
184
+ }
185
+ async copy?(from: URI, to: URI, opts: FileOverwriteOptions): Promise<void> {
186
+ await this.initialized;
187
+ throw new Error('Method not implemented.');
188
+ }
189
+ async readFile(resource: URI): Promise<Uint8Array> {
190
+ await this.initialized;
191
+ try {
192
+ const filePath = this.toFilePath(resource);
193
+ return await this.promisify(this.fs.readFile)(filePath) as Uint8Array;
194
+ } catch (error) {
195
+ throw this.toFileSystemProviderError(error);
196
+ }
197
+ }
198
+ async writeFile(resource: URI, content: Uint8Array, opts: FileWriteOptions): Promise<void> {
199
+ await this.initialized;
200
+ let handle: number | undefined = undefined;
201
+ try {
202
+ const filePath = this.toFilePath(resource);
203
+
204
+ // Validate target unless { create: true, overwrite: true }
205
+ if (!opts.create || !opts.overwrite) {
206
+ const fileExists = await this.promisify(this.fs.exists)(filePath);
207
+ if (fileExists) {
208
+ if (!opts.overwrite) {
209
+ throw createFileSystemProviderError('File already exists', FileSystemProviderErrorCode.FileExists);
210
+ }
211
+ } else {
212
+ if (!opts.create) {
213
+ throw createFileSystemProviderError('File does not exist', FileSystemProviderErrorCode.FileNotFound);
214
+ }
215
+ }
216
+ }
217
+
218
+ // Open
219
+ handle = await this.open(resource, { create: true });
220
+
221
+ // Write content at once
222
+ await this.write(handle, 0, content, 0, content.byteLength);
223
+ } catch (error) {
224
+ throw this.toFileSystemProviderError(error);
225
+ } finally {
226
+ if (typeof handle === 'number') {
227
+ await this.close(handle);
228
+ }
229
+ }
230
+ }
231
+ readFileStream?(resource: URI, opts: FileReadStreamOptions, token: CancellationToken): ReadableStreamEvents<Uint8Array> {
232
+ throw new Error('Method not implemented.');
233
+ }
234
+ async open(resource: URI, opts: FileOpenOptions): Promise<number> {
235
+ await this.initialized;
236
+ try {
237
+ const filePath = this.toFilePath(resource);
238
+
239
+ let flags: string | undefined = undefined;
240
+ if (opts.create) {
241
+ // we take opts.create as a hint that the file is opened for writing
242
+ // as such we use 'w' to truncate an existing or create the
243
+ // file otherwise. we do not allow reading.
244
+ if (!flags) {
245
+ flags = 'w';
246
+ }
247
+ } else {
248
+ // otherwise we assume the file is opened for reading
249
+ // as such we use 'r' to neither truncate, nor create
250
+ // the file.
251
+ flags = 'r';
252
+ }
253
+
254
+ const handle = await this.promisify(this.fs.open)(filePath, flags) as number;
255
+
256
+ // remember this handle to track file position of the handle
257
+ // we init the position to 0 since the file descriptor was
258
+ // just created and the position was not moved so far (see
259
+ // also http://man7.org/linux/man-pages/man2/open.2.html -
260
+ // "The file offset is set to the beginning of the file.")
261
+ this.mapHandleToPos.set(handle, 0);
262
+
263
+ // remember that this handle was used for writing
264
+ if (opts.create) {
265
+ this.writeHandles.add(handle);
266
+ }
267
+
268
+ return handle;
269
+ } catch (error) {
270
+ throw this.toFileSystemProviderError(error);
271
+ }
272
+ }
273
+ async close(fd: number): Promise<void> {
274
+ await this.initialized;
275
+ // remove this handle from map of positions
276
+ this.mapHandleToPos.delete(fd);
277
+
278
+ // if a handle is closed that was used for writing, ensure
279
+ // to flush the contents to disk if possible.
280
+ if (this.writeHandles.delete(fd) && this.canFlush) {
281
+ try {
282
+ await this.promisify(this.fs.fdatasync)(fd);
283
+ } catch (error) {
284
+ // In some exotic setups it is well possible that node fails to sync
285
+ // In that case we disable flushing and log the error to our logger
286
+ this.canFlush = false;
287
+ console.error(error);
288
+ }
289
+ }
290
+
291
+ await this.promisify(this.fs.close)(fd);
292
+ }
293
+ async read(fd: number, pos: number, data: Uint8Array, offset: number, length: number): Promise<number> {
294
+ await this.initialized;
295
+ const normalizedPos = this.normalizePos(fd, pos);
296
+
297
+ let bytesRead: number | null = null;
298
+ try {
299
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
300
+ const result: { bytesRead: number, buffer: Uint8Array } | number = (await this.promisify(this.fs.read)(fd, data, offset, length, normalizedPos)) as any;
301
+
302
+ if (typeof result === 'number') {
303
+ bytesRead = result; // node.d.ts fail
304
+ } else {
305
+ bytesRead = result.bytesRead;
306
+ }
307
+
308
+ return bytesRead;
309
+ } catch (error) {
310
+ throw this.toFileSystemProviderError(error);
311
+ } finally {
312
+ this.updatePos(fd, normalizedPos, bytesRead);
313
+ }
314
+ }
315
+ async write(fd: number, pos: number, data: Uint8Array, offset: number, length: number): Promise<number> {
316
+ await this.initialized;
317
+ // we know at this point that the file to write to is truncated and thus empty
318
+ // if the write now fails, the file remains empty. as such we really try hard
319
+ // to ensure the write succeeds by retrying up to three times.
320
+ return retry(() => this.doWrite(fd, pos, data, offset, length), 100 /* ms delay */, 3 /* retries */);
321
+
322
+ }
323
+ private async doWrite(fd: number, pos: number, data: Uint8Array, offset: number, length: number): Promise<number> {
324
+ await this.initialized;
325
+ const normalizedPos = this.normalizePos(fd, pos);
326
+
327
+ let bytesWritten: number | null = null;
328
+ try {
329
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
330
+ const result: { bytesWritten: number, buffer: Uint8Array } | number = (await this.promisify(this.fs.write)(fd, data, offset, length, normalizedPos)) as any;
331
+
332
+ if (typeof result === 'number') {
333
+ bytesWritten = result; // node.d.ts fail
334
+ } else {
335
+ bytesWritten = result.bytesWritten;
336
+ }
337
+
338
+ return bytesWritten;
339
+ } catch (error) {
340
+ throw this.toFileSystemProviderError(error);
341
+ } finally {
342
+ this.updatePos(fd, normalizedPos, bytesWritten);
343
+ }
344
+ }
345
+ private normalizePos(fd: number, pos: number): number | null {
346
+
347
+ // when calling fs.read/write we try to avoid passing in the "pos" argument and
348
+ // rather prefer to pass in "null" because this avoids an extra seek(pos)
349
+ // call that in some cases can even fail (e.g. when opening a file over FTP -
350
+ // see https://github.com/microsoft/vscode/issues/73884).
351
+ //
352
+ // as such, we compare the passed in position argument with our last known
353
+ // position for the file descriptor and use "null" if they match.
354
+ if (pos === this.mapHandleToPos.get(fd)) {
355
+ return null;
356
+ }
357
+
358
+ return pos;
359
+ }
360
+ private updatePos(fd: number, pos: number | null, bytesLength: number | null): void {
361
+ const lastKnownPos = this.mapHandleToPos.get(fd);
362
+ if (typeof lastKnownPos === 'number') {
363
+
364
+ // pos !== null signals that previously a position was used that is
365
+ // not null. node.js documentation explains, that in this case
366
+ // the internal file pointer is not moving and as such we do not move
367
+ // our position pointer.
368
+ //
369
+ // Docs: "If position is null, data will be read from the current file position,
370
+ // and the file position will be updated. If position is an integer, the file position
371
+ // will remain unchanged."
372
+ if (typeof pos === 'number') {
373
+ // do not modify the position
374
+ } else if (typeof bytesLength === 'number') {
375
+ this.mapHandleToPos.set(fd, lastKnownPos + bytesLength);
376
+ } else {
377
+ this.mapHandleToPos.delete(fd);
378
+ }
379
+ }
380
+ }
381
+ async access?(resource: URI, mode?: number | undefined): Promise<void> {
382
+ await this.initialized;
383
+ throw new Error('Method not implemented.');
384
+ }
385
+ async fsPath?(resource: URI): Promise<string> {
386
+ await this.initialized;
387
+ throw new Error('Method not implemented.');
388
+ }
389
+ async updateFile?(resource: URI, changes: TextDocumentContentChangeEvent[], opts: FileUpdateOptions): Promise<FileUpdateResult> {
390
+ await this.initialized;
391
+ throw new Error('Method not implemented.');
392
+ }
393
+
394
+ private toFilePath(resource: URI): string {
395
+ return normalize(resource.path.toString());
396
+ }
397
+
398
+ private toType(entry: Stats, symbolicLink?: { dangling: boolean }): FileType {
399
+ // Signal file type by checking for file / directory, except:
400
+ // - symbolic links pointing to non-existing files are FileType.Unknown
401
+ // - files that are neither file nor directory are FileType.Unknown
402
+ let type: FileType;
403
+ if (symbolicLink?.dangling) {
404
+ type = FileType.Unknown;
405
+ } else if (entry.isFile()) {
406
+ type = FileType.File;
407
+ } else if (entry.isDirectory()) {
408
+ type = FileType.Directory;
409
+ } else {
410
+ type = FileType.Unknown;
411
+ }
412
+
413
+ // Always signal symbolic link as file type additionally
414
+ if (symbolicLink) {
415
+ type |= FileType.SymbolicLink;
416
+ }
417
+
418
+ return type;
419
+ }
420
+
421
+ // FIXME typing
422
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
423
+ private promisify<T>(f: Function): (...args: any[]) => Promise<T> {
424
+ // eslint-disable-next-line @typescript-eslint/tslint/config, @typescript-eslint/no-explicit-any
425
+ return function (...args: any[]) {
426
+ return new Promise((resolve, reject) => {
427
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
428
+ f(...args, (err: Error, result: T) => err ? reject(err) : resolve(result));
429
+ });
430
+ };
431
+ }
432
+
433
+ private toFileSystemProviderError(error: NodeJS.ErrnoException): FileSystemProviderError {
434
+ if (error instanceof FileSystemProviderError) {
435
+ return error; // avoid double conversion
436
+ }
437
+
438
+ let code: FileSystemProviderErrorCode;
439
+ switch (error.code) {
440
+ case 'ENOENT':
441
+ code = FileSystemProviderErrorCode.FileNotFound;
442
+ break;
443
+ case 'EISDIR':
444
+ code = FileSystemProviderErrorCode.FileIsADirectory;
445
+ break;
446
+ case 'ENOTDIR':
447
+ code = FileSystemProviderErrorCode.FileNotADirectory;
448
+ break;
449
+ case 'EEXIST':
450
+ code = FileSystemProviderErrorCode.FileExists;
451
+ break;
452
+ case 'EPERM':
453
+ case 'EACCES':
454
+ code = FileSystemProviderErrorCode.NoPermissions;
455
+ break;
456
+ default:
457
+ code = FileSystemProviderErrorCode.Unknown;
458
+ }
459
+
460
+ return createFileSystemProviderError(error, code);
461
+ }
462
+ }
@@ -27,6 +27,7 @@ import type { TextDocumentContentChangeEvent } from '@theia/core/shared/vscode-l
27
27
  import { ReadableStreamEvents } from '@theia/core/lib/common/stream';
28
28
  import { CancellationToken } from '@theia/core/lib/common/cancellation';
29
29
  import { isObject } from '@theia/core/lib/common';
30
+ import { MarkdownString } from '@theia/core/lib/common/markdown-rendering';
30
31
 
31
32
  export const enum FileOperation {
32
33
  CREATE,
@@ -765,6 +766,18 @@ export function hasUpdateCapability(provider: FileSystemProvider): provider is F
765
766
  return !!(provider.capabilities & FileSystemProviderCapabilities.Update);
766
767
  }
767
768
 
769
+ export interface ReadOnlyMessageFileSystemProvider {
770
+ readOnlyMessage: MarkdownString | undefined;
771
+ readonly onDidChangeReadOnlyMessage: Event<MarkdownString | undefined>;
772
+ }
773
+
774
+ export namespace ReadOnlyMessageFileSystemProvider {
775
+ export function is(arg: unknown): arg is ReadOnlyMessageFileSystemProvider {
776
+ return isObject<ReadOnlyMessageFileSystemProvider>(arg)
777
+ && 'readOnlyMessage' in arg;
778
+ }
779
+ }
780
+
768
781
  /**
769
782
  * Subtype of {@link FileSystemProvider} that ensures that the optional functions, needed for providers
770
783
  * that should be able to read & write files, are implemented.
@@ -23,7 +23,8 @@ import {
23
23
  FileWriteOptions, FileOpenOptions, FileChangeType,
24
24
  FileSystemProviderCapabilities, FileChange, Stat, FileOverwriteOptions, WatchOptions, FileType, FileSystemProvider, FileDeleteOptions,
25
25
  hasOpenReadWriteCloseCapability, hasFileFolderCopyCapability, hasReadWriteCapability, hasAccessCapability,
26
- FileSystemProviderError, FileSystemProviderErrorCode, FileUpdateOptions, hasUpdateCapability, FileUpdateResult, FileReadStreamOptions, hasFileReadStreamCapability
26
+ FileSystemProviderError, FileSystemProviderErrorCode, FileUpdateOptions, hasUpdateCapability, FileUpdateResult, FileReadStreamOptions, hasFileReadStreamCapability,
27
+ ReadOnlyMessageFileSystemProvider
27
28
  } from './files';
28
29
  import { RpcServer, RpcProxy, RpcProxyFactory } from '@theia/core/lib/common/messaging/proxy-factory';
29
30
  import { ApplicationError } from '@theia/core/lib/common/application-error';
@@ -31,6 +32,7 @@ import { Deferred } from '@theia/core/lib/common/promise-util';
31
32
  import type { TextDocumentContentChangeEvent } from '@theia/core/shared/vscode-languageserver-protocol';
32
33
  import { newWriteableStream, ReadableStreamEvents } from '@theia/core/lib/common/stream';
33
34
  import { CancellationToken, cancelled } from '@theia/core/lib/common/cancellation';
35
+ import { MarkdownString } from '@theia/core/lib/common/markdown-rendering';
34
36
 
35
37
  export const remoteFileSystemPath = '/services/remote-filesystem';
36
38
 
@@ -38,6 +40,7 @@ export const RemoteFileSystemServer = Symbol('RemoteFileSystemServer');
38
40
  export interface RemoteFileSystemServer extends RpcServer<RemoteFileSystemClient> {
39
41
  getCapabilities(): Promise<FileSystemProviderCapabilities>
40
42
  stat(resource: string): Promise<Stat>;
43
+ getReadOnlyMessage(): Promise<MarkdownString | undefined>;
41
44
  access(resource: string, mode?: number): Promise<void>;
42
45
  fsPath(resource: string): Promise<string>;
43
46
  open(resource: string, opts: FileOpenOptions): Promise<number>;
@@ -70,6 +73,7 @@ export interface RemoteFileSystemClient {
70
73
  notifyDidChangeFile(event: { changes: RemoteFileChange[] }): void;
71
74
  notifyFileWatchError(): void;
72
75
  notifyDidChangeCapabilities(capabilities: FileSystemProviderCapabilities): void;
76
+ notifyDidChangeReadOnlyMessage(readOnlyMessage: MarkdownString | undefined): void;
73
77
  onFileStreamData(handle: number, data: Uint8Array): void;
74
78
  onFileStreamEnd(handle: number, error: RemoteFileStreamError | undefined): void;
75
79
  }
@@ -109,7 +113,7 @@ export class RemoteFileSystemProxyFactory<T extends object> extends RpcProxyFact
109
113
  * Wraps the remote filesystem provider living on the backend.
110
114
  */
111
115
  @injectable()
112
- export class RemoteFileSystemProvider implements Required<FileSystemProvider>, Disposable {
116
+ export class RemoteFileSystemProvider implements Required<FileSystemProvider>, Disposable, ReadOnlyMessageFileSystemProvider {
113
117
 
114
118
  private readonly onDidChangeFileEmitter = new Emitter<readonly FileChange[]>();
115
119
  readonly onDidChangeFile = this.onDidChangeFileEmitter.event;
@@ -120,6 +124,9 @@ export class RemoteFileSystemProvider implements Required<FileSystemProvider>, D
120
124
  private readonly onDidChangeCapabilitiesEmitter = new Emitter<void>();
121
125
  readonly onDidChangeCapabilities = this.onDidChangeCapabilitiesEmitter.event;
122
126
 
127
+ private readonly onDidChangeReadOnlyMessageEmitter = new Emitter<MarkdownString | undefined>();
128
+ readonly onDidChangeReadOnlyMessage = this.onDidChangeReadOnlyMessageEmitter.event;
129
+
123
130
  private readonly onFileStreamDataEmitter = new Emitter<[number, Uint8Array]>();
124
131
  private readonly onFileStreamData = this.onFileStreamDataEmitter.event;
125
132
 
@@ -129,6 +136,7 @@ export class RemoteFileSystemProvider implements Required<FileSystemProvider>, D
129
136
  protected readonly toDispose = new DisposableCollection(
130
137
  this.onDidChangeFileEmitter,
131
138
  this.onDidChangeCapabilitiesEmitter,
139
+ this.onDidChangeReadOnlyMessageEmitter,
132
140
  this.onFileStreamDataEmitter,
133
141
  this.onFileStreamEndEmitter
134
142
  );
@@ -146,6 +154,11 @@ export class RemoteFileSystemProvider implements Required<FileSystemProvider>, D
146
154
  private _capabilities: FileSystemProviderCapabilities = 0;
147
155
  get capabilities(): FileSystemProviderCapabilities { return this._capabilities; }
148
156
 
157
+ private _readOnlyMessage: MarkdownString | undefined = undefined;
158
+ get readOnlyMessage(): MarkdownString | undefined {
159
+ return this._readOnlyMessage;
160
+ }
161
+
149
162
  protected readonly readyDeferred = new Deferred<void>();
150
163
  readonly ready = this.readyDeferred.promise;
151
164
 
@@ -161,6 +174,9 @@ export class RemoteFileSystemProvider implements Required<FileSystemProvider>, D
161
174
  this._capabilities = capabilities;
162
175
  this.readyDeferred.resolve();
163
176
  }, this.readyDeferred.reject);
177
+ this.server.getReadOnlyMessage().then(readOnlyMessage => {
178
+ this._readOnlyMessage = readOnlyMessage;
179
+ });
164
180
  this.server.setClient({
165
181
  notifyDidChangeFile: ({ changes }) => {
166
182
  this.onDidChangeFileEmitter.fire(changes.map(event => ({ resource: new URI(event.resource), type: event.type })));
@@ -169,6 +185,7 @@ export class RemoteFileSystemProvider implements Required<FileSystemProvider>, D
169
185
  this.onFileWatchErrorEmitter.fire();
170
186
  },
171
187
  notifyDidChangeCapabilities: capabilities => this.setCapabilities(capabilities),
188
+ notifyDidChangeReadOnlyMessage: readOnlyMessage => this.setReadOnlyMessage(readOnlyMessage),
172
189
  onFileStreamData: (handle, data) => this.onFileStreamDataEmitter.fire([handle, data]),
173
190
  onFileStreamEnd: (handle, error) => this.onFileStreamEndEmitter.fire([handle, error])
174
191
  });
@@ -188,6 +205,11 @@ export class RemoteFileSystemProvider implements Required<FileSystemProvider>, D
188
205
  this.onDidChangeCapabilitiesEmitter.fire(undefined);
189
206
  }
190
207
 
208
+ protected setReadOnlyMessage(readOnlyMessage: MarkdownString | undefined): void {
209
+ this._readOnlyMessage = readOnlyMessage;
210
+ this.onDidChangeReadOnlyMessageEmitter.fire(readOnlyMessage);
211
+ }
212
+
191
213
  // --- forwarding calls
192
214
 
193
215
  stat(resource: URI): Promise<Stat> {
@@ -362,6 +384,14 @@ export class FileSystemProviderServer implements RemoteFileSystemServer {
362
384
  this.client.notifyDidChangeCapabilities(this.provider.capabilities);
363
385
  }
364
386
  }));
387
+ if (ReadOnlyMessageFileSystemProvider.is(this.provider)) {
388
+ const providerWithReadOnlyMessage: ReadOnlyMessageFileSystemProvider = this.provider;
389
+ this.toDispose.push(this.provider.onDidChangeReadOnlyMessage(() => {
390
+ if (this.client) {
391
+ this.client.notifyDidChangeReadOnlyMessage(providerWithReadOnlyMessage.readOnlyMessage);
392
+ }
393
+ }));
394
+ }
365
395
  this.toDispose.push(this.provider.onDidChangeFile(changes => {
366
396
  if (this.client) {
367
397
  this.client.notifyDidChangeFile({
@@ -380,6 +410,14 @@ export class FileSystemProviderServer implements RemoteFileSystemServer {
380
410
  return this.provider.capabilities;
381
411
  }
382
412
 
413
+ async getReadOnlyMessage(): Promise<MarkdownString | undefined> {
414
+ if (ReadOnlyMessageFileSystemProvider.is(this.provider)) {
415
+ return this.provider.readOnlyMessage;
416
+ } else {
417
+ return undefined;
418
+ }
419
+ }
420
+
383
421
  stat(resource: string): Promise<Stat> {
384
422
  return this.provider.stat(new URI(resource));
385
423
  }