@theia/filesystem 1.45.1 → 1.46.0-next.72

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 (272) hide show
  1. package/README.md +30 -30
  2. package/lib/browser/breadcrumbs/filepath-breadcrumb.d.ts +15 -15
  3. package/lib/browser/breadcrumbs/filepath-breadcrumb.js +41 -41
  4. package/lib/browser/breadcrumbs/filepath-breadcrumbs-container.d.ts +13 -13
  5. package/lib/browser/breadcrumbs/filepath-breadcrumbs-container.js +81 -81
  6. package/lib/browser/breadcrumbs/filepath-breadcrumbs-contribution.d.ts +27 -27
  7. package/lib/browser/breadcrumbs/filepath-breadcrumbs-contribution.js +126 -126
  8. package/lib/browser/download/file-download-command-contribution.d.ts +18 -18
  9. package/lib/browser/download/file-download-command-contribution.js +83 -83
  10. package/lib/browser/download/file-download-frontend-module.d.ts +3 -3
  11. package/lib/browser/download/file-download-frontend-module.js +25 -25
  12. package/lib/browser/download/file-download-service.d.ts +28 -28
  13. package/lib/browser/download/file-download-service.js +175 -175
  14. package/lib/browser/file-dialog/file-dialog-container.d.ts +5 -5
  15. package/lib/browser/file-dialog/file-dialog-container.js +60 -60
  16. package/lib/browser/file-dialog/file-dialog-hidden-files-renderer.d.ts +15 -15
  17. package/lib/browser/file-dialog/file-dialog-hidden-files-renderer.js +67 -67
  18. package/lib/browser/file-dialog/file-dialog-model.d.ts +25 -25
  19. package/lib/browser/file-dialog/file-dialog-model.js +108 -108
  20. package/lib/browser/file-dialog/file-dialog-module.d.ts +3 -3
  21. package/lib/browser/file-dialog/file-dialog-module.js +45 -45
  22. package/lib/browser/file-dialog/file-dialog-service.d.ts +32 -32
  23. package/lib/browser/file-dialog/file-dialog-service.js +109 -109
  24. package/lib/browser/file-dialog/file-dialog-tree-filters-renderer.d.ts +39 -39
  25. package/lib/browser/file-dialog/file-dialog-tree-filters-renderer.js +92 -92
  26. package/lib/browser/file-dialog/file-dialog-tree.d.ts +26 -26
  27. package/lib/browser/file-dialog/file-dialog-tree.js +88 -88
  28. package/lib/browser/file-dialog/file-dialog-widget.d.ts +15 -15
  29. package/lib/browser/file-dialog/file-dialog-widget.js +86 -86
  30. package/lib/browser/file-dialog/file-dialog.d.ts +129 -129
  31. package/lib/browser/file-dialog/file-dialog.js +362 -362
  32. package/lib/browser/file-dialog/index.d.ts +4 -4
  33. package/lib/browser/file-dialog/index.js +31 -31
  34. package/lib/browser/file-resource.d.ts +66 -63
  35. package/lib/browser/file-resource.d.ts.map +1 -1
  36. package/lib/browser/file-resource.js +362 -352
  37. package/lib/browser/file-resource.js.map +1 -1
  38. package/lib/browser/file-selection.d.ts +14 -14
  39. package/lib/browser/file-selection.js +36 -36
  40. package/lib/browser/file-service.d.ts +413 -412
  41. package/lib/browser/file-service.d.ts.map +1 -1
  42. package/lib/browser/file-service.js +1362 -1360
  43. package/lib/browser/file-service.js.map +1 -1
  44. package/lib/browser/file-tree/file-tree-container.d.ts +3 -3
  45. package/lib/browser/file-tree/file-tree-container.js +35 -35
  46. package/lib/browser/file-tree/file-tree-decorator-adapter.d.ts +30 -30
  47. package/lib/browser/file-tree/file-tree-decorator-adapter.js +177 -177
  48. package/lib/browser/file-tree/file-tree-label-provider.d.ts +12 -12
  49. package/lib/browser/file-tree/file-tree-label-provider.js +62 -62
  50. package/lib/browser/file-tree/file-tree-model.d.ts +37 -37
  51. package/lib/browser/file-tree/file-tree-model.js +225 -225
  52. package/lib/browser/file-tree/file-tree-widget.d.ts +44 -44
  53. package/lib/browser/file-tree/file-tree-widget.js +328 -328
  54. package/lib/browser/file-tree/file-tree.d.ts +46 -46
  55. package/lib/browser/file-tree/file-tree.js +184 -184
  56. package/lib/browser/file-tree/index.d.ts +6 -6
  57. package/lib/browser/file-tree/index.js +33 -33
  58. package/lib/browser/file-upload-service.d.ts +109 -111
  59. package/lib/browser/file-upload-service.d.ts.map +1 -1
  60. package/lib/browser/file-upload-service.js +442 -442
  61. package/lib/browser/file-upload-service.js.map +1 -1
  62. package/lib/browser/filesystem-frontend-contribution.d.ts +63 -63
  63. package/lib/browser/filesystem-frontend-contribution.js +327 -327
  64. package/lib/browser/filesystem-frontend-module.d.ts +5 -5
  65. package/lib/browser/filesystem-frontend-module.js +64 -64
  66. package/lib/browser/filesystem-preferences.d.ts +29 -29
  67. package/lib/browser/filesystem-preferences.js +110 -110
  68. package/lib/browser/filesystem-save-resource-service.d.ts +29 -29
  69. package/lib/browser/filesystem-save-resource-service.d.ts.map +1 -1
  70. package/lib/browser/filesystem-save-resource-service.js +143 -142
  71. package/lib/browser/filesystem-save-resource-service.js.map +1 -1
  72. package/lib/browser/filesystem-watcher-error-handler.d.ts +10 -10
  73. package/lib/browser/filesystem-watcher-error-handler.js +69 -69
  74. package/lib/browser/index.d.ts +5 -5
  75. package/lib/browser/index.js +32 -32
  76. package/lib/browser/location/index.d.ts +2 -2
  77. package/lib/browser/location/index.js +29 -29
  78. package/lib/browser/location/location-renderer.d.ts +100 -100
  79. package/lib/browser/location/location-renderer.js +354 -354
  80. package/lib/browser/location/location-service.d.ts +5 -5
  81. package/lib/browser/location/location-service.js +17 -17
  82. package/lib/browser/remote-file-service-contribution.d.ts +6 -6
  83. package/lib/browser/remote-file-service-contribution.js +47 -47
  84. package/lib/browser-only/browser-only-filesystem-frontend-module.d.ts +4 -0
  85. package/lib/browser-only/browser-only-filesystem-frontend-module.d.ts.map +1 -0
  86. package/lib/browser-only/browser-only-filesystem-frontend-module.js +41 -0
  87. package/lib/browser-only/browser-only-filesystem-frontend-module.js.map +1 -0
  88. package/lib/browser-only/browser-only-filesystem-provider-server.d.ts +12 -0
  89. package/lib/browser-only/browser-only-filesystem-provider-server.d.ts.map +1 -0
  90. package/lib/browser-only/browser-only-filesystem-provider-server.js +45 -0
  91. package/lib/browser-only/browser-only-filesystem-provider-server.js.map +1 -0
  92. package/lib/browser-only/browserfs-filesystem-initialization.d.ts +13 -0
  93. package/lib/browser-only/browserfs-filesystem-initialization.d.ts.map +1 -0
  94. package/lib/browser-only/browserfs-filesystem-initialization.js +60 -0
  95. package/lib/browser-only/browserfs-filesystem-initialization.js.map +1 -0
  96. package/lib/browser-only/browserfs-filesystem-provider.d.ts +46 -0
  97. package/lib/browser-only/browserfs-filesystem-provider.d.ts.map +1 -0
  98. package/lib/browser-only/browserfs-filesystem-provider.js +451 -0
  99. package/lib/browser-only/browserfs-filesystem-provider.js.map +1 -0
  100. package/lib/common/delegating-file-system-provider.d.ts +76 -76
  101. package/lib/common/delegating-file-system-provider.js +168 -168
  102. package/lib/common/download/file-download-data.d.ts +6 -6
  103. package/lib/common/download/file-download-data.js +26 -26
  104. package/lib/common/file-upload.d.ts +1 -1
  105. package/lib/common/file-upload.js +19 -19
  106. package/lib/common/files.d.ts +651 -651
  107. package/lib/common/files.js +347 -347
  108. package/lib/common/files.spec.d.ts +1 -1
  109. package/lib/common/files.spec.js +51 -51
  110. package/lib/common/filesystem-utils.d.ts +14 -14
  111. package/lib/common/filesystem-utils.js +63 -63
  112. package/lib/common/filesystem-utils.spec.d.ts +1 -1
  113. package/lib/common/filesystem-utils.spec.js +378 -378
  114. package/lib/common/filesystem-watcher-protocol.d.ts +71 -71
  115. package/lib/common/filesystem-watcher-protocol.js +20 -20
  116. package/lib/common/filesystem.d.ts +22 -22
  117. package/lib/common/filesystem.js +42 -42
  118. package/lib/common/index.d.ts +2 -2
  119. package/lib/common/index.js +29 -29
  120. package/lib/common/io.d.ts +19 -19
  121. package/lib/common/io.js +110 -110
  122. package/lib/common/remote-file-system-provider.d.ts +164 -164
  123. package/lib/common/remote-file-system-provider.js +413 -413
  124. package/lib/electron-browser/file-dialog/electron-file-dialog-module.d.ts +3 -3
  125. package/lib/electron-browser/file-dialog/electron-file-dialog-module.js +24 -24
  126. package/lib/electron-browser/file-dialog/electron-file-dialog-service.d.ts +19 -19
  127. package/lib/electron-browser/file-dialog/electron-file-dialog-service.js +156 -156
  128. package/lib/electron-browser/file-dialog/electron-file-dialog-service.js.map +1 -1
  129. package/lib/electron-browser/preload.d.ts +1 -1
  130. package/lib/electron-browser/preload.js +30 -30
  131. package/lib/electron-common/electron-api.d.ts +34 -34
  132. package/lib/electron-common/electron-api.js +20 -20
  133. package/lib/electron-main/electron-api-main.d.ts +5 -5
  134. package/lib/electron-main/electron-api-main.js +78 -78
  135. package/lib/electron-main/electron-main-module.d.ts +3 -3
  136. package/lib/electron-main/electron-main-module.js +24 -24
  137. package/lib/node/disk-file-system-provider.d.ts +72 -72
  138. package/lib/node/disk-file-system-provider.d.ts.map +1 -1
  139. package/lib/node/disk-file-system-provider.js +795 -789
  140. package/lib/node/disk-file-system-provider.js.map +1 -1
  141. package/lib/node/disk-file-system-provider.spec.d.ts +1 -1
  142. package/lib/node/disk-file-system-provider.spec.js +122 -91
  143. package/lib/node/disk-file-system-provider.spec.js.map +1 -1
  144. package/lib/node/download/directory-archiver.d.ts +9 -9
  145. package/lib/node/download/directory-archiver.js +132 -132
  146. package/lib/node/download/directory-archiver.js.map +1 -1
  147. package/lib/node/download/directory-archiver.spec.d.ts +1 -1
  148. package/lib/node/download/directory-archiver.spec.js +97 -97
  149. package/lib/node/download/directory-archiver.spec.js.map +1 -1
  150. package/lib/node/download/file-download-backend-module.d.ts +3 -3
  151. package/lib/node/download/file-download-backend-module.js +32 -32
  152. package/lib/node/download/file-download-cache.d.ts +21 -21
  153. package/lib/node/download/file-download-cache.js +90 -90
  154. package/lib/node/download/file-download-endpoint.d.ts +11 -11
  155. package/lib/node/download/file-download-endpoint.js +75 -75
  156. package/lib/node/download/file-download-endpoint.js.map +1 -1
  157. package/lib/node/download/file-download-handler.d.ts +50 -50
  158. package/lib/node/download/file-download-handler.d.ts.map +1 -1
  159. package/lib/node/download/file-download-handler.js +315 -315
  160. package/lib/node/download/file-download-handler.js.map +1 -1
  161. package/lib/node/download/test/mock-directory-archiver.d.ts +7 -7
  162. package/lib/node/download/test/mock-directory-archiver.js +29 -29
  163. package/lib/node/file-change-collection.d.ts +22 -22
  164. package/lib/node/file-change-collection.js +77 -77
  165. package/lib/node/file-change-collection.spec.d.ts +1 -1
  166. package/lib/node/file-change-collection.spec.js +90 -90
  167. package/lib/node/file-change-collection.spec.js.map +1 -1
  168. package/lib/node/filesystem-backend-module.d.ts +26 -26
  169. package/lib/node/filesystem-backend-module.js +120 -120
  170. package/lib/node/filesystem-watcher-client.d.ts +23 -23
  171. package/lib/node/filesystem-watcher-client.js +83 -83
  172. package/lib/node/filesystem-watcher-dispatcher.d.ts +23 -23
  173. package/lib/node/filesystem-watcher-dispatcher.js +85 -85
  174. package/lib/node/node-file-upload-service.d.ts +16 -16
  175. package/lib/node/node-file-upload-service.js +84 -84
  176. package/lib/node/nsfw-watcher/index.d.ts +3 -3
  177. package/lib/node/nsfw-watcher/index.js +39 -39
  178. package/lib/node/nsfw-watcher/nsfw-filesystem-service.d.ts +191 -191
  179. package/lib/node/nsfw-watcher/nsfw-filesystem-service.js +405 -405
  180. package/lib/node/nsfw-watcher/nsfw-filesystem-service.js.map +1 -1
  181. package/lib/node/nsfw-watcher/nsfw-filesystem-watcher.spec.d.ts +1 -1
  182. package/lib/node/nsfw-watcher/nsfw-filesystem-watcher.spec.js +151 -151
  183. package/lib/node/nsfw-watcher/nsfw-options.d.ts +6 -6
  184. package/lib/node/nsfw-watcher/nsfw-options.js +22 -22
  185. package/package.json +8 -6
  186. package/src/browser/breadcrumbs/filepath-breadcrumb.ts +43 -43
  187. package/src/browser/breadcrumbs/filepath-breadcrumbs-container.ts +65 -65
  188. package/src/browser/breadcrumbs/filepath-breadcrumbs-contribution.ts +129 -129
  189. package/src/browser/download/file-download-command-contribution.ts +83 -83
  190. package/src/browser/download/file-download-frontend-module.ts +25 -25
  191. package/src/browser/download/file-download-service.ts +179 -179
  192. package/src/browser/file-dialog/file-dialog-container.ts +67 -67
  193. package/src/browser/file-dialog/file-dialog-hidden-files-renderer.tsx +59 -59
  194. package/src/browser/file-dialog/file-dialog-model.ts +96 -96
  195. package/src/browser/file-dialog/file-dialog-module.ts +44 -44
  196. package/src/browser/file-dialog/file-dialog-service.ts +99 -99
  197. package/src/browser/file-dialog/file-dialog-tree-filters-renderer.tsx +100 -100
  198. package/src/browser/file-dialog/file-dialog-tree.ts +89 -89
  199. package/src/browser/file-dialog/file-dialog-widget.ts +75 -75
  200. package/src/browser/file-dialog/file-dialog.ts +434 -434
  201. package/src/browser/file-dialog/index.ts +20 -20
  202. package/src/browser/file-resource.ts +373 -361
  203. package/src/browser/file-selection.ts +44 -44
  204. package/src/browser/file-service.ts +1817 -1814
  205. package/src/browser/file-tree/file-tree-container.ts +36 -36
  206. package/src/browser/file-tree/file-tree-decorator-adapter.ts +159 -159
  207. package/src/browser/file-tree/file-tree-label-provider.ts +53 -53
  208. package/src/browser/file-tree/file-tree-model.ts +212 -212
  209. package/src/browser/file-tree/file-tree-widget.tsx +327 -327
  210. package/src/browser/file-tree/file-tree.ts +183 -183
  211. package/src/browser/file-tree/index.ts +22 -22
  212. package/src/browser/file-upload-service.ts +539 -541
  213. package/src/browser/filesystem-frontend-contribution.ts +338 -338
  214. package/src/browser/filesystem-frontend-module.ts +77 -77
  215. package/src/browser/filesystem-preferences.ts +139 -139
  216. package/src/browser/filesystem-save-resource-service.ts +125 -124
  217. package/src/browser/filesystem-watcher-error-handler.ts +60 -60
  218. package/src/browser/index.ts +21 -21
  219. package/src/browser/location/index.ts +18 -18
  220. package/src/browser/location/location-renderer.tsx +404 -404
  221. package/src/browser/location/location-service.ts +22 -22
  222. package/src/browser/remote-file-service-contribution.ts +38 -38
  223. package/src/browser/style/file-dialog.css +208 -208
  224. package/src/browser/style/file-icons.css +64 -64
  225. package/src/browser/style/filepath-breadcrumbs.css +20 -20
  226. package/src/browser/style/index.css +36 -36
  227. package/src/browser-only/browser-only-filesystem-frontend-module.ts +38 -0
  228. package/src/browser-only/browser-only-filesystem-provider-server.ts +32 -0
  229. package/src/browser-only/browserfs-filesystem-initialization.ts +61 -0
  230. package/src/browser-only/browserfs-filesystem-provider.ts +462 -0
  231. package/src/common/delegating-file-system-provider.ts +226 -226
  232. package/src/common/download/README.md +30 -30
  233. package/src/common/download/file-download-data.ts +27 -27
  234. package/src/common/file-upload.ts +17 -17
  235. package/src/common/files.spec.ts +51 -51
  236. package/src/common/files.ts +983 -983
  237. package/src/common/filesystem-utils.spec.ts +411 -411
  238. package/src/common/filesystem-utils.ts +64 -64
  239. package/src/common/filesystem-watcher-protocol.ts +96 -96
  240. package/src/common/filesystem.ts +43 -43
  241. package/src/common/index.ts +18 -18
  242. package/src/common/io.ts +150 -150
  243. package/src/common/remote-file-system-provider.ts +511 -511
  244. package/src/electron-browser/file-dialog/electron-file-dialog-module.ts +24 -24
  245. package/src/electron-browser/file-dialog/electron-file-dialog-service.ts +165 -165
  246. package/src/electron-browser/preload.ts +31 -31
  247. package/src/electron-common/electron-api.ts +55 -55
  248. package/src/electron-main/electron-api-main.ts +78 -78
  249. package/src/electron-main/electron-main-module.ts +23 -23
  250. package/src/node/disk-file-system-provider.spec.ts +142 -109
  251. package/src/node/disk-file-system-provider.ts +915 -910
  252. package/src/node/download/directory-archiver.spec.ts +104 -104
  253. package/src/node/download/directory-archiver.ts +126 -126
  254. package/src/node/download/file-download-backend-module.ts +32 -32
  255. package/src/node/download/file-download-cache.ts +88 -88
  256. package/src/node/download/file-download-endpoint.ts +63 -63
  257. package/src/node/download/file-download-handler.ts +304 -304
  258. package/src/node/download/test/mock-directory-archiver.ts +30 -30
  259. package/src/node/file-change-collection.spec.ts +110 -110
  260. package/src/node/file-change-collection.ts +78 -78
  261. package/src/node/filesystem-backend-module.ts +140 -140
  262. package/src/node/filesystem-watcher-client.ts +72 -72
  263. package/src/node/filesystem-watcher-dispatcher.ts +82 -82
  264. package/src/node/node-file-upload-service.ts +80 -80
  265. package/src/node/nsfw-watcher/index.ts +45 -45
  266. package/src/node/nsfw-watcher/nsfw-filesystem-service.ts +481 -481
  267. package/src/node/nsfw-watcher/nsfw-filesystem-watcher.spec.ts +182 -182
  268. package/src/node/nsfw-watcher/nsfw-options.ts +23 -23
  269. package/src/typings/dom.webkit.d.ts +77 -77
  270. package/src/typings/mv/index.d.ts +21 -21
  271. package/src/typings/nsfw/index.d.ts +18 -18
  272. package/src/typings/trash/index.d.ts +20 -20
@@ -1,1814 +1,1817 @@
1
- // *****************************************************************************
2
- // Copyright (C) 2020 TypeFox 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/common/fileService.ts
21
- // and https://github.com/microsoft/vscode/blob/04c36be045a94fee58e5f8992d3e3fd980294a84/src/vs/workbench/services/textfile/browser/textFileService.ts
22
- // and https://github.com/microsoft/vscode/blob/04c36be045a94fee58e5f8992d3e3fd980294a84/src/vs/workbench/services/textfile/electron-browser/nativeTextFileService.ts
23
- // and https://github.com/microsoft/vscode/blob/04c36be045a94fee58e5f8992d3e3fd980294a84/src/vs/workbench/services/workingCopy/common/workingCopyFileService.ts
24
- // and https://github.com/microsoft/vscode/blob/04c36be045a94fee58e5f8992d3e3fd980294a84/src/vs/workbench/services/workingCopy/common/workingCopyFileOperationParticipant.ts
25
-
26
- /* eslint-disable max-len */
27
- /* eslint-disable @typescript-eslint/no-shadow */
28
- /* eslint-disable no-null/no-null */
29
- /* eslint-disable @typescript-eslint/tslint/config */
30
- /* eslint-disable @typescript-eslint/no-explicit-any */
31
-
32
- import { injectable, inject, named, postConstruct } from '@theia/core/shared/inversify';
33
- import URI from '@theia/core/lib/common/uri';
34
- import { timeout, Deferred } from '@theia/core/lib/common/promise-util';
35
- import { CancellationToken, CancellationTokenSource } from '@theia/core/lib/common/cancellation';
36
- import { Disposable, DisposableCollection } from '@theia/core/lib/common/disposable';
37
- import { WaitUntilEvent, Emitter, AsyncEmitter, Event } from '@theia/core/lib/common/event';
38
- import { ContributionProvider } from '@theia/core/lib/common/contribution-provider';
39
- import { TernarySearchTree } from '@theia/core/lib/common/ternary-search-tree';
40
- import {
41
- ensureFileSystemProviderError, etag, ETAG_DISABLED,
42
- FileChangesEvent,
43
- FileOperation, FileOperationError,
44
- FileOperationEvent, FileOperationResult, FileSystemProviderCapabilities,
45
- FileSystemProviderErrorCode, FileType, hasFileFolderCopyCapability, hasOpenReadWriteCloseCapability, hasReadWriteCapability,
46
- CreateFileOptions, FileContent, FileStat, FileStatWithMetadata,
47
- FileStreamContent, FileSystemProvider,
48
- FileSystemProviderWithFileReadWriteCapability, FileSystemProviderWithOpenReadWriteCloseCapability,
49
- ReadFileOptions, ResolveFileOptions, ResolveMetadataFileOptions,
50
- Stat, WatchOptions, WriteFileOptions,
51
- toFileOperationResult, toFileSystemProviderErrorCode,
52
- ResolveFileResult, ResolveFileResultWithMetadata,
53
- MoveFileOptions, CopyFileOptions, BaseStatWithMetadata, FileDeleteOptions, FileOperationOptions, hasAccessCapability, hasUpdateCapability,
54
- hasFileReadStreamCapability, FileSystemProviderWithFileReadStreamCapability
55
- } from '../common/files';
56
- import { BinaryBuffer, BinaryBufferReadable, BinaryBufferReadableStream, BinaryBufferReadableBufferedStream, BinaryBufferWriteableStream } from '@theia/core/lib/common/buffer';
57
- import { ReadableStream, isReadableStream, isReadableBufferedStream, transform, consumeStream, peekStream, peekReadable, Readable } from '@theia/core/lib/common/stream';
58
- import { LabelProvider } from '@theia/core/lib/browser/label-provider';
59
- import { FileSystemPreferences } from './filesystem-preferences';
60
- import { ProgressService } from '@theia/core/lib/common/progress-service';
61
- import { DelegatingFileSystemProvider } from '../common/delegating-file-system-provider';
62
- import type { TextDocumentContentChangeEvent } from '@theia/core/shared/vscode-languageserver-protocol';
63
- import { EncodingRegistry } from '@theia/core/lib/browser/encoding-registry';
64
- import { UTF8, UTF8_with_bom } from '@theia/core/lib/common/encodings';
65
- import { EncodingService, ResourceEncoding, DecodeStreamResult } from '@theia/core/lib/common/encoding-service';
66
- import { Mutable } from '@theia/core/lib/common/types';
67
- import { readFileIntoStream } from '../common/io';
68
- import { FileSystemWatcherErrorHandler } from './filesystem-watcher-error-handler';
69
- import { FileSystemUtils } from '../common/filesystem-utils';
70
- import { nls } from '@theia/core';
71
-
72
- export interface FileOperationParticipant {
73
-
74
- /**
75
- * Participate in a file operation of a working copy. Allows to
76
- * change the working copy before it is being saved to disk.
77
- */
78
- participate(
79
- target: URI,
80
- source: URI | undefined,
81
- operation: FileOperation,
82
- timeout: number,
83
- token: CancellationToken
84
- ): Promise<void>;
85
- }
86
-
87
- export interface ReadEncodingOptions {
88
-
89
- /**
90
- * The optional encoding parameter allows to specify the desired encoding when resolving
91
- * the contents of the file.
92
- */
93
- encoding?: string;
94
-
95
- /**
96
- * The optional guessEncoding parameter allows to guess encoding from content of the file.
97
- */
98
- autoGuessEncoding?: boolean;
99
- }
100
-
101
- export interface WriteEncodingOptions {
102
-
103
- /**
104
- * The encoding to use when updating a file.
105
- */
106
- encoding?: string;
107
-
108
- /**
109
- * If set to true, will enforce the selected encoding and not perform any detection using BOMs.
110
- */
111
- overwriteEncoding?: boolean;
112
- }
113
-
114
- export interface ReadTextFileOptions extends ReadEncodingOptions, ReadFileOptions {
115
- /**
116
- * The optional acceptTextOnly parameter allows to fail this request early if the file
117
- * contents are not textual.
118
- */
119
- acceptTextOnly?: boolean;
120
- }
121
-
122
- interface BaseTextFileContent extends BaseStatWithMetadata {
123
-
124
- /**
125
- * The encoding of the content if known.
126
- */
127
- encoding: string;
128
- }
129
-
130
- export interface TextFileContent extends BaseTextFileContent {
131
-
132
- /**
133
- * The content of a text file.
134
- */
135
- value: string;
136
- }
137
-
138
- export interface TextFileStreamContent extends BaseTextFileContent {
139
-
140
- /**
141
- * The line grouped content of a text file.
142
- */
143
- value: ReadableStream<string>;
144
- }
145
-
146
- export interface CreateTextFileOptions extends WriteEncodingOptions, CreateFileOptions { }
147
-
148
- export interface WriteTextFileOptions extends WriteEncodingOptions, WriteFileOptions { }
149
-
150
- export interface UpdateTextFileOptions extends WriteEncodingOptions, WriteFileOptions {
151
- readEncoding: string
152
- }
153
-
154
- export interface UserFileOperationEvent extends WaitUntilEvent {
155
-
156
- /**
157
- * An identifier to correlate the operation through the
158
- * different event types (before, after, error).
159
- */
160
- readonly correlationId: number;
161
-
162
- /**
163
- * The file operation that is taking place.
164
- */
165
- readonly operation: FileOperation;
166
-
167
- /**
168
- * The resource the event is about.
169
- */
170
- readonly target: URI;
171
-
172
- /**
173
- * A property that is defined for move operations.
174
- */
175
- readonly source?: URI;
176
- }
177
-
178
- export const FileServiceContribution = Symbol('FileServiceContribution');
179
-
180
- /**
181
- * A {@link FileServiceContribution} can be used to add custom {@link FileSystemProvider}s.
182
- * For this, the contribution has to listen to the {@link FileSystemProviderActivationEvent} and register
183
- * the custom {@link FileSystemProvider}s according to the scheme when this event is fired.
184
- *
185
- * ### Example usage
186
- * ```ts
187
- * export class MyFileServiceContribution implements FileServiceContribution {
188
- * registerFileSystemProviders(service: FileService): void {
189
- * service.onWillActivateFileSystemProvider(event => {
190
- * if (event.scheme === 'mySyncProviderScheme') {
191
- * service.registerProvider('mySyncProviderScheme', this.mySyncProvider);
192
- * }
193
- * if (event.scheme === 'myAsyncProviderScheme') {
194
- * event.waitUntil((async () => {
195
- * const myAsyncProvider = await this.createAsyncProvider();
196
- * service.registerProvider('myAsyncProviderScheme', myAsyncProvider);
197
- * })());
198
- * }
199
- * });
200
- *
201
- * }
202
- *```
203
- */
204
- export interface FileServiceContribution {
205
- /**
206
- * Register custom file system providers for the given {@link FileService}.
207
- * @param service The file service for which the providers should be registered.
208
- */
209
- registerFileSystemProviders(service: FileService): void;
210
- }
211
-
212
- /**
213
- * Represents the `FileSystemProviderRegistration` event.
214
- * This event is fired by the {@link FileService} if a {@link FileSystemProvider} is
215
- * registered to or unregistered from the service.
216
- */
217
- export interface FileSystemProviderRegistrationEvent {
218
- /** `True` if a new provider has been registered, `false` if a provider has been unregistered. */
219
- added: boolean;
220
- /** The (uri) scheme for which the provider was (previously) registered */
221
- scheme: string;
222
- /** The affected file system provider for which this event was fired. */
223
- provider?: FileSystemProvider;
224
- }
225
-
226
- /**
227
- * Represents the `FileSystemProviderCapabilitiesChange` event.
228
- * This event is fired by the {@link FileService} if the capabilities of one of its managed
229
- * {@link FileSystemProvider}s have changed.
230
- */
231
- export interface FileSystemProviderCapabilitiesChangeEvent {
232
- /** The affected file system provider for which this event was fired. */
233
- provider: FileSystemProvider;
234
- /** The (uri) scheme for which the provider is registered */
235
- scheme: string;
236
- }
237
-
238
- /**
239
- * Represents the `FileSystemProviderActivation` event.
240
- * This event is fired by the {@link FileService} if it wants to activate the
241
- * {@link FileSystemProvider} for a specific scheme.
242
- */
243
- export interface FileSystemProviderActivationEvent extends WaitUntilEvent {
244
- /** The (uri) scheme for which the provider should be activated */
245
- scheme: string;
246
- }
247
-
248
- export const enum TextFileOperationResult {
249
- FILE_IS_BINARY
250
- }
251
-
252
- export class TextFileOperationError extends FileOperationError {
253
-
254
- constructor(
255
- message: string,
256
- public textFileOperationResult: TextFileOperationResult,
257
- override options?: ReadTextFileOptions & WriteTextFileOptions
258
- ) {
259
- super(message, FileOperationResult.FILE_OTHER_ERROR);
260
- Object.setPrototypeOf(this, TextFileOperationError.prototype);
261
- }
262
-
263
- }
264
-
265
- /**
266
- * The {@link FileService} is the common facade responsible for all interactions with file systems.
267
- * It manages all registered {@link FileSystemProvider}s and
268
- * forwards calls to the responsible {@link FileSystemProvider}, determined by the scheme.
269
- * For additional documentation regarding the provided functions see also {@link FileSystemProvider}.
270
- */
271
- @injectable()
272
- export class FileService {
273
-
274
- private readonly BUFFER_SIZE = 64 * 1024;
275
-
276
- @inject(LabelProvider)
277
- protected readonly labelProvider: LabelProvider;
278
-
279
- @inject(FileSystemPreferences)
280
- protected readonly preferences: FileSystemPreferences;
281
-
282
- @inject(ProgressService)
283
- protected readonly progressService: ProgressService;
284
-
285
- @inject(EncodingRegistry)
286
- protected readonly encodingRegistry: EncodingRegistry;
287
-
288
- @inject(EncodingService)
289
- protected readonly encodingService: EncodingService;
290
-
291
- @inject(ContributionProvider) @named(FileServiceContribution)
292
- protected readonly contributions: ContributionProvider<FileServiceContribution>;
293
-
294
- @inject(FileSystemWatcherErrorHandler)
295
- protected readonly watcherErrorHandler: FileSystemWatcherErrorHandler;
296
-
297
- @postConstruct()
298
- protected init(): void {
299
- for (const contribution of this.contributions.getContributions()) {
300
- contribution.registerFileSystemProviders(this);
301
- }
302
- }
303
-
304
- // #region Events
305
-
306
- private correlationIds = 0;
307
-
308
- private readonly onWillRunUserOperationEmitter = new AsyncEmitter<UserFileOperationEvent>();
309
- /**
310
- * An event that is emitted when file operation is being performed.
311
- * This event is triggered by user gestures.
312
- */
313
- readonly onWillRunUserOperation = this.onWillRunUserOperationEmitter.event;
314
-
315
- private readonly onDidFailUserOperationEmitter = new AsyncEmitter<UserFileOperationEvent>();
316
- /**
317
- * An event that is emitted when file operation is failed.
318
- * This event is triggered by user gestures.
319
- */
320
- readonly onDidFailUserOperation = this.onDidFailUserOperationEmitter.event;
321
-
322
- private readonly onDidRunUserOperationEmitter = new AsyncEmitter<UserFileOperationEvent>();
323
- /**
324
- * An event that is emitted when file operation is finished.
325
- * This event is triggered by user gestures.
326
- */
327
- readonly onDidRunUserOperation = this.onDidRunUserOperationEmitter.event;
328
-
329
- // #endregion
330
-
331
- // #region File System Provider
332
-
333
- private onDidChangeFileSystemProviderRegistrationsEmitter = new Emitter<FileSystemProviderRegistrationEvent>();
334
- readonly onDidChangeFileSystemProviderRegistrations = this.onDidChangeFileSystemProviderRegistrationsEmitter.event;
335
-
336
- private onWillActivateFileSystemProviderEmitter = new Emitter<FileSystemProviderActivationEvent>();
337
- /**
338
- * See `FileServiceContribution.registerProviders`.
339
- */
340
- readonly onWillActivateFileSystemProvider = this.onWillActivateFileSystemProviderEmitter.event;
341
-
342
- private onDidChangeFileSystemProviderCapabilitiesEmitter = new Emitter<FileSystemProviderCapabilitiesChangeEvent>();
343
- readonly onDidChangeFileSystemProviderCapabilities = this.onDidChangeFileSystemProviderCapabilitiesEmitter.event;
344
-
345
- private readonly providers = new Map<string, FileSystemProvider>();
346
- private readonly activations = new Map<string, Promise<FileSystemProvider>>();
347
-
348
- /**
349
- * Registers a new {@link FileSystemProvider} for the given scheme.
350
- * @param scheme The (uri) scheme for which the provider should be registered.
351
- * @param provider The file system provider that should be registered.
352
- *
353
- * @returns A `Disposable` that can be invoked to unregister the given provider.
354
- */
355
- registerProvider(scheme: string, provider: FileSystemProvider): Disposable {
356
- if (this.providers.has(scheme)) {
357
- throw new Error(`A filesystem provider for the scheme '${scheme}' is already registered.`);
358
- }
359
-
360
- this.providers.set(scheme, provider);
361
- this.onDidChangeFileSystemProviderRegistrationsEmitter.fire({ added: true, scheme, provider });
362
-
363
- const providerDisposables = new DisposableCollection();
364
- providerDisposables.push(provider.onDidChangeFile(changes => this.onDidFilesChangeEmitter.fire(new FileChangesEvent(changes))));
365
- providerDisposables.push(provider.onFileWatchError(() => this.handleFileWatchError()));
366
- providerDisposables.push(provider.onDidChangeCapabilities(() => this.onDidChangeFileSystemProviderCapabilitiesEmitter.fire({ provider, scheme })));
367
-
368
- return Disposable.create(() => {
369
- this.onDidChangeFileSystemProviderRegistrationsEmitter.fire({ added: false, scheme, provider });
370
- this.providers.delete(scheme);
371
-
372
- providerDisposables.dispose();
373
- });
374
- }
375
-
376
- /**
377
- * Try to activate the registered provider for the given scheme
378
- * @param scheme The uri scheme for which the responsible provider should be activated.
379
- *
380
- * @returns A promise of the activated file system provider. Only resolves if a provider is available for this scheme, gets rejected otherwise.
381
- */
382
- async activateProvider(scheme: string): Promise<FileSystemProvider> {
383
- let provider = this.providers.get(scheme);
384
- if (provider) {
385
- return provider;
386
- }
387
- let activation = this.activations.get(scheme);
388
- if (!activation) {
389
- const deferredActivation = new Deferred<FileSystemProvider>();
390
- this.activations.set(scheme, activation = deferredActivation.promise);
391
- WaitUntilEvent.fire(this.onWillActivateFileSystemProviderEmitter, { scheme }).then(() => {
392
- provider = this.providers.get(scheme);
393
- if (!provider) {
394
- const error = new Error();
395
- error.name = 'ENOPRO';
396
- error.message = `No file system provider found for scheme ${scheme}`;
397
- throw error;
398
- } else {
399
- deferredActivation.resolve(provider);
400
- }
401
- }).catch(e => deferredActivation.reject(e));
402
- }
403
- return activation;
404
- }
405
-
406
- /**
407
- * Tests if the service (i.e. any of its registered {@link FileSystemProvider}s) can handle the given resource.
408
- * @param resource `URI` of the resource to test.
409
- *
410
- * @returns `true` if the resource can be handled, `false` otherwise.
411
- */
412
- canHandleResource(resource: URI): boolean {
413
- return this.providers.has(resource.scheme);
414
- }
415
-
416
- /**
417
- * Tests if the service (i.e the {@link FileSystemProvider} registered for the given uri scheme) provides the given capability.
418
- * @param resource `URI` of the resource to test.
419
- * @param capability The required capability.
420
- *
421
- * @returns `true` if the resource can be handled and the required capability can be provided.
422
- */
423
- hasCapability(resource: URI, capability: FileSystemProviderCapabilities): boolean {
424
- const provider = this.providers.get(resource.scheme);
425
-
426
- return !!(provider && (provider.capabilities & capability));
427
- }
428
-
429
- /**
430
- * List the schemes and capabilities for registered file system providers
431
- */
432
- listCapabilities(): { scheme: string; capabilities: FileSystemProviderCapabilities }[] {
433
- return Array.from(this.providers.entries()).map(([scheme, provider]) => ({
434
- scheme,
435
- capabilities: provider.capabilities
436
- }));
437
- }
438
-
439
- protected async withProvider(resource: URI): Promise<FileSystemProvider> {
440
- // Assert path is absolute
441
- if (!resource.path.isAbsolute) {
442
- throw new FileOperationError(nls.localizeByDefault("Unable to resolve filesystem provider with relative file path '{0}'", this.resourceForError(resource)), FileOperationResult.FILE_INVALID_PATH);
443
- }
444
-
445
- return this.activateProvider(resource.scheme);
446
- }
447
-
448
- private async withReadProvider(resource: URI): Promise<FileSystemProviderWithFileReadWriteCapability | FileSystemProviderWithOpenReadWriteCloseCapability> {
449
- const provider = await this.withProvider(resource);
450
-
451
- if (hasOpenReadWriteCloseCapability(provider) || hasReadWriteCapability(provider)) {
452
- return provider;
453
- }
454
-
455
- throw new Error(`Filesystem provider for scheme '${resource.scheme}' neither has FileReadWrite, FileReadStream nor FileOpenReadWriteClose capability which is needed for the read operation.`);
456
- }
457
-
458
- private async withWriteProvider(resource: URI): Promise<FileSystemProviderWithFileReadWriteCapability | FileSystemProviderWithOpenReadWriteCloseCapability> {
459
- const provider = await this.withProvider(resource);
460
- if (hasOpenReadWriteCloseCapability(provider) || hasReadWriteCapability(provider)) {
461
- return provider;
462
- }
463
-
464
- throw new Error(`Filesystem provider for scheme '${resource.scheme}' neither has FileReadWrite nor FileOpenReadWriteClose capability which is needed for the write operation.`);
465
- }
466
-
467
- // #endregion
468
-
469
- private onDidRunOperationEmitter = new Emitter<FileOperationEvent>();
470
- /**
471
- * An event that is emitted when operation is finished.
472
- * This event is triggered by user gestures and programmatically.
473
- */
474
- readonly onDidRunOperation = this.onDidRunOperationEmitter.event;
475
-
476
- /**
477
- * Try to resolve file information and metadata for the given resource.
478
- * @param resource `URI` of the resource that should be resolved.
479
- * @param options Options to customize the resolvement process.
480
- *
481
- * @return A promise that resolves if the resource could be successfully resolved.
482
- */
483
- resolve(resource: URI, options: ResolveMetadataFileOptions): Promise<FileStatWithMetadata>;
484
- resolve(resource: URI, options?: ResolveFileOptions | undefined): Promise<FileStat>;
485
- async resolve(resource: any, options?: any) {
486
- try {
487
- return await this.doResolveFile(resource, options);
488
- } catch (error) {
489
-
490
- // Specially handle file not found case as file operation result
491
- if (toFileSystemProviderErrorCode(error) === FileSystemProviderErrorCode.FileNotFound) {
492
- throw new FileOperationError(nls.localizeByDefault("Unable to resolve nonexistent file '{0}'", this.resourceForError(resource)), FileOperationResult.FILE_NOT_FOUND);
493
- }
494
-
495
- // Bubble up any other error as is
496
- throw ensureFileSystemProviderError(error);
497
- }
498
- }
499
-
500
- private async doResolveFile(resource: URI, options: ResolveMetadataFileOptions): Promise<FileStatWithMetadata>;
501
- private async doResolveFile(resource: URI, options?: ResolveFileOptions): Promise<FileStat>;
502
- private async doResolveFile(resource: URI, options?: ResolveFileOptions): Promise<FileStat> {
503
- const provider = await this.withProvider(resource);
504
-
505
- const resolveTo = options?.resolveTo;
506
- const resolveSingleChildDescendants = options?.resolveSingleChildDescendants;
507
- const resolveMetadata = options?.resolveMetadata;
508
-
509
- const stat = await provider.stat(resource);
510
-
511
- let trie: TernarySearchTree<URI, boolean> | undefined;
512
-
513
- return this.toFileStat(provider, resource, stat, undefined, !!resolveMetadata, (stat, siblings) => {
514
-
515
- // lazy trie to check for recursive resolving
516
- if (!trie) {
517
- trie = TernarySearchTree.forUris<true>(!!(provider.capabilities & FileSystemProviderCapabilities.PathCaseSensitive));
518
- trie.set(resource, true);
519
- if (Array.isArray(resolveTo) && resolveTo.length) {
520
- resolveTo.forEach(uri => trie!.set(uri, true));
521
- }
522
- }
523
-
524
- // check for recursive resolving
525
- if (Boolean(trie.findSuperstr(stat.resource) || trie.get(stat.resource))) {
526
- return true;
527
- }
528
-
529
- // check for resolving single child folders
530
- if (stat.isDirectory && resolveSingleChildDescendants) {
531
- return siblings === 1;
532
- }
533
-
534
- return false;
535
- });
536
- }
537
-
538
- private async toFileStat(provider: FileSystemProvider, resource: URI, stat: Stat | { type: FileType } & Partial<Stat>, siblings: number | undefined, resolveMetadata: boolean, recurse: (stat: FileStat, siblings?: number) => boolean): Promise<FileStat>;
539
- private async toFileStat(provider: FileSystemProvider, resource: URI, stat: Stat, siblings: number | undefined, resolveMetadata: true, recurse: (stat: FileStat, siblings?: number) => boolean): Promise<FileStatWithMetadata>;
540
- private async toFileStat(provider: FileSystemProvider, resource: URI, stat: Stat | { type: FileType } & Partial<Stat>, siblings: number | undefined, resolveMetadata: boolean, recurse: (stat: FileStat, siblings?: number) => boolean): Promise<FileStat> {
541
- const fileStat = FileStat.fromStat(resource, stat);
542
-
543
- // check to recurse for directories
544
- if (fileStat.isDirectory && recurse(fileStat, siblings)) {
545
- try {
546
- const entries = await provider.readdir(resource);
547
- const resolvedEntries = await Promise.all(entries.map(async ([name, type]) => {
548
- try {
549
- const childResource = resource.resolve(name);
550
- const childStat = resolveMetadata ? await provider.stat(childResource) : { type };
551
-
552
- return await this.toFileStat(provider, childResource, childStat, entries.length, resolveMetadata, recurse);
553
- } catch (error) {
554
- console.trace(error);
555
-
556
- return null; // can happen e.g. due to permission errors
557
- }
558
- }));
559
-
560
- // make sure to get rid of null values that signal a failure to resolve a particular entry
561
- fileStat.children = resolvedEntries.filter(e => !!e) as FileStat[];
562
- } catch (error) {
563
- console.trace(error);
564
-
565
- fileStat.children = []; // gracefully handle errors, we may not have permissions to read
566
- }
567
-
568
- return fileStat;
569
- }
570
-
571
- return fileStat;
572
- }
573
-
574
- /**
575
- * Try to resolve file information and metadata for all given resource.
576
- * @param toResolve An array of all the resources (and corresponding resolvement options) that should be resolved.
577
- *
578
- * @returns A promise of all resolved resources. The promise is not rejected if any of the given resources cannot be resolved.
579
- * Instead this is reflected with the `success` flag of the corresponding {@link ResolveFileResult}.
580
- */
581
- async resolveAll(toResolve: { resource: URI, options?: ResolveFileOptions }[]): Promise<ResolveFileResult[]>;
582
- async resolveAll(toResolve: { resource: URI, options: ResolveMetadataFileOptions }[]): Promise<ResolveFileResultWithMetadata[]>;
583
- async resolveAll(toResolve: { resource: URI; options?: ResolveFileOptions; }[]): Promise<ResolveFileResult[]> {
584
- return Promise.all(toResolve.map(async entry => {
585
- try {
586
- return { stat: await this.doResolveFile(entry.resource, entry.options), success: true };
587
- } catch (error) {
588
- console.trace(error);
589
-
590
- return { stat: undefined, success: false };
591
- }
592
- }));
593
- }
594
-
595
- /**
596
- * Tests if the given resource exists in the filesystem.
597
- * @param resource `URI` of the resource which should be tested.
598
- * @throws Will throw an error if no {@link FileSystemProvider} is registered for the given resource.
599
- *
600
- * @returns A promise that resolves to `true` if the resource exists.
601
- */
602
- async exists(resource: URI): Promise<boolean> {
603
- const provider = await this.withProvider(resource);
604
-
605
- try {
606
- const stat = await provider.stat(resource);
607
-
608
- return !!stat;
609
- } catch (error) {
610
- return false;
611
- }
612
- }
613
-
614
- /**
615
- * Tests a user's permissions for the given resource.
616
- * @param resource `URI` of the resource which should be tested.
617
- * @param mode An optional integer that specifies the accessibility checks to be performed.
618
- * Check `FileAccess.Constants` for possible values of mode.
619
- * It is possible to create a mask consisting of the bitwise `OR` of two or more values (e.g. FileAccess.Constants.W_OK | FileAccess.Constants.R_OK).
620
- * If `mode` is not defined, `FileAccess.Constants.F_OK` will be used instead.
621
- */
622
- async access(resource: URI, mode?: number): Promise<boolean> {
623
- const provider = await this.withProvider(resource);
624
-
625
- if (!hasAccessCapability(provider)) {
626
- return false;
627
- }
628
- try {
629
- await provider.access(resource, mode);
630
- return true;
631
- } catch (error) {
632
- return false;
633
- }
634
- }
635
-
636
- /**
637
- * Resolves the fs path of the given URI.
638
- *
639
- * USE WITH CAUTION: You should always prefer URIs to paths if possible, as they are
640
- * portable and platform independent. Paths should only be used in cases you directly
641
- * interact with the OS, e.g. when running a command on the shell.
642
- *
643
- * If you need to display human readable simple or long names then use `LabelProvider` instead.
644
- * @param resource `URI` of the resource that should be resolved.
645
- * @throws Will throw an error if no {@link FileSystemProvider} is registered for the given resource.
646
- *
647
- * @returns A promise of the resolved fs path.
648
- */
649
- async fsPath(resource: URI): Promise<string> {
650
- const provider = await this.withProvider(resource);
651
-
652
- if (!hasAccessCapability(provider)) {
653
- return resource.path.toString();
654
- }
655
- return provider.fsPath(resource);
656
- }
657
-
658
- // #region Text File Reading/Writing
659
-
660
- async create(resource: URI, value?: string | Readable<string>, options?: CreateTextFileOptions): Promise<FileStatWithMetadata> {
661
- if (options?.fromUserGesture === false) {
662
- return this.doCreate(resource, value, options);
663
- }
664
- await this.runFileOperationParticipants(resource, undefined, FileOperation.CREATE);
665
-
666
- const event = { correlationId: this.correlationIds++, operation: FileOperation.CREATE, target: resource };
667
- await this.onWillRunUserOperationEmitter.fire(event);
668
-
669
- let stat: FileStatWithMetadata;
670
- try {
671
- stat = await this.doCreate(resource, value, options);
672
- } catch (error) {
673
- await this.onDidFailUserOperationEmitter.fire(event);
674
- throw error;
675
- }
676
-
677
- await this.onDidRunUserOperationEmitter.fire(event);
678
-
679
- return stat;
680
- }
681
-
682
- protected async doCreate(resource: URI, value?: string | Readable<string>, options?: CreateTextFileOptions): Promise<FileStatWithMetadata> {
683
- const encoding = await this.getWriteEncoding(resource, options);
684
- const encoded = await this.encodingService.encodeStream(value, encoding);
685
- return this.createFile(resource, encoded, options);
686
- }
687
-
688
- async write(resource: URI, value: string | Readable<string>, options?: WriteTextFileOptions): Promise<FileStatWithMetadata & { encoding: string }> {
689
- const encoding = await this.getWriteEncoding(resource, options);
690
- const encoded = await this.encodingService.encodeStream(value, encoding);
691
- return Object.assign(await this.writeFile(resource, encoded, options), { encoding: encoding.encoding });
692
- }
693
-
694
- async read(resource: URI, options?: ReadTextFileOptions): Promise<TextFileContent> {
695
- const [bufferStream, decoder] = await this.doRead(resource, {
696
- ...options,
697
- // optimization: since we know that the caller does not
698
- // care about buffering, we indicate this to the reader.
699
- // this reduces all the overhead the buffered reading
700
- // has (open, read, close) if the provider supports
701
- // unbuffered reading.
702
- preferUnbuffered: true
703
- });
704
-
705
- return {
706
- ...bufferStream,
707
- encoding: decoder.detected.encoding || UTF8,
708
- value: await consumeStream(decoder.stream, strings => strings.join(''))
709
- };
710
- }
711
-
712
- async readStream(resource: URI, options?: ReadTextFileOptions): Promise<TextFileStreamContent> {
713
- const [bufferStream, decoder] = await this.doRead(resource, options);
714
-
715
- return {
716
- ...bufferStream,
717
- encoding: decoder.detected.encoding || UTF8,
718
- value: decoder.stream
719
- };
720
- }
721
-
722
- private async doRead(resource: URI, options?: ReadTextFileOptions & { preferUnbuffered?: boolean }): Promise<[FileStreamContent, DecodeStreamResult]> {
723
- options = this.resolveReadOptions(options);
724
-
725
- // read stream raw (either buffered or unbuffered)
726
- let bufferStream: FileStreamContent;
727
- if (options?.preferUnbuffered) {
728
- const content = await this.readFile(resource, options);
729
- bufferStream = {
730
- ...content,
731
- value: BinaryBufferReadableStream.fromBuffer(content.value)
732
- };
733
- } else {
734
- bufferStream = await this.readFileStream(resource, options);
735
- }
736
-
737
- const decoder = await this.encodingService.decodeStream(bufferStream.value, {
738
- guessEncoding: options.autoGuessEncoding,
739
- overwriteEncoding: detectedEncoding => this.getReadEncoding(resource, options, detectedEncoding)
740
- });
741
-
742
- // validate binary
743
- if (options?.acceptTextOnly && decoder.detected.seemsBinary) {
744
- throw new TextFileOperationError(nls.localizeByDefault('File seems to be binary and cannot be opened as text'), TextFileOperationResult.FILE_IS_BINARY, options);
745
- }
746
-
747
- return [bufferStream, decoder];
748
- }
749
-
750
- protected resolveReadOptions(options?: ReadTextFileOptions): ReadTextFileOptions {
751
- options = {
752
- ...options,
753
- autoGuessEncoding: typeof options?.autoGuessEncoding === 'boolean' ? options.autoGuessEncoding : this.preferences['files.autoGuessEncoding']
754
- };
755
- const limits: Mutable<ReadTextFileOptions['limits']> = options.limits = options.limits || {};
756
- if (typeof limits.size !== 'number') {
757
- limits.size = this.preferences['files.maxFileSizeMB'] * 1024 * 1024;
758
- }
759
- return options;
760
- }
761
-
762
- async update(resource: URI, changes: TextDocumentContentChangeEvent[], options: UpdateTextFileOptions): Promise<FileStatWithMetadata & { encoding: string }> {
763
- const provider = this.throwIfFileSystemIsReadonly(await this.withWriteProvider(resource), resource);
764
- try {
765
- await this.validateWriteFile(provider, resource, options);
766
- if (hasUpdateCapability(provider)) {
767
- const encoding = await this.getEncodingForResource(resource, options ? options.encoding : undefined);;
768
- const stat = await provider.updateFile(resource, changes, {
769
- readEncoding: options.readEncoding,
770
- writeEncoding: encoding,
771
- overwriteEncoding: options.overwriteEncoding || false
772
- });
773
- return Object.assign(FileStat.fromStat(resource, stat), { encoding: stat.encoding });
774
- } else {
775
- throw new Error('incremental file update is not supported');
776
- }
777
- } catch (error) {
778
- this.rethrowAsFileOperationError("Unable to write file '{0}' ({1})", resource, error, options);
779
- }
780
- }
781
-
782
- // #endregion
783
-
784
- // #region File Reading/Writing
785
-
786
- async createFile(resource: URI, bufferOrReadableOrStream: BinaryBuffer | BinaryBufferReadable | BinaryBufferReadableStream = BinaryBuffer.fromString(''), options?: CreateFileOptions): Promise<FileStatWithMetadata> {
787
-
788
- // validate overwrite
789
- if (!options?.overwrite && await this.exists(resource)) {
790
- throw new FileOperationError(nls.localizeByDefault("Unable to create file '{0}' that already exists when overwrite flag is not set", this.resourceForError(resource)), FileOperationResult.FILE_MODIFIED_SINCE, options);
791
- }
792
-
793
- // do write into file (this will create it too)
794
- const fileStat = await this.writeFile(resource, bufferOrReadableOrStream);
795
-
796
- // events
797
- this.onDidRunOperationEmitter.fire(new FileOperationEvent(resource, FileOperation.CREATE, fileStat));
798
-
799
- return fileStat;
800
- }
801
-
802
- async writeFile(resource: URI, bufferOrReadableOrStream: BinaryBuffer | BinaryBufferReadable | BinaryBufferReadableStream, options?: WriteFileOptions): Promise<FileStatWithMetadata> {
803
- const provider = this.throwIfFileSystemIsReadonly(await this.withWriteProvider(resource), resource);
804
-
805
- try {
806
-
807
- // validate write
808
- const stat = await this.validateWriteFile(provider, resource, options);
809
-
810
- // mkdir recursively as needed
811
- if (!stat) {
812
- await this.mkdirp(provider, resource.parent);
813
- }
814
-
815
- // optimization: if the provider has unbuffered write capability and the data
816
- // to write is a Readable, we consume up to 3 chunks and try to write the data
817
- // unbuffered to reduce the overhead. If the Readable has more data to provide
818
- // we continue to write buffered.
819
- let bufferOrReadableOrStreamOrBufferedStream: BinaryBuffer | BinaryBufferReadable | BinaryBufferReadableStream | BinaryBufferReadableBufferedStream;
820
- if (hasReadWriteCapability(provider) && !(bufferOrReadableOrStream instanceof BinaryBuffer)) {
821
- if (isReadableStream(bufferOrReadableOrStream)) {
822
- const bufferedStream = await peekStream(bufferOrReadableOrStream, 3);
823
- if (bufferedStream.ended) {
824
- bufferOrReadableOrStreamOrBufferedStream = BinaryBuffer.concat(bufferedStream.buffer);
825
- } else {
826
- bufferOrReadableOrStreamOrBufferedStream = bufferedStream;
827
- }
828
- } else {
829
- bufferOrReadableOrStreamOrBufferedStream = peekReadable(bufferOrReadableOrStream, data => BinaryBuffer.concat(data), 3);
830
- }
831
- } else {
832
- bufferOrReadableOrStreamOrBufferedStream = bufferOrReadableOrStream;
833
- }
834
-
835
- // write file: unbuffered (only if data to write is a buffer, or the provider has no buffered write capability)
836
- if (!hasOpenReadWriteCloseCapability(provider) || (hasReadWriteCapability(provider) && bufferOrReadableOrStreamOrBufferedStream instanceof BinaryBuffer)) {
837
- await this.doWriteUnbuffered(provider, resource, bufferOrReadableOrStreamOrBufferedStream);
838
- }
839
-
840
- // write file: buffered
841
- else {
842
- await this.doWriteBuffered(provider, resource, bufferOrReadableOrStreamOrBufferedStream instanceof BinaryBuffer ? BinaryBufferReadable.fromBuffer(bufferOrReadableOrStreamOrBufferedStream) : bufferOrReadableOrStreamOrBufferedStream);
843
- }
844
- } catch (error) {
845
- this.rethrowAsFileOperationError("Unable to write file '{0}' ({1})", resource, error, options);
846
- }
847
-
848
- return this.resolve(resource, { resolveMetadata: true });
849
- }
850
-
851
- private async validateWriteFile(provider: FileSystemProvider, resource: URI, options?: WriteFileOptions): Promise<Stat | undefined> {
852
- let stat: Stat | undefined = undefined;
853
- try {
854
- stat = await provider.stat(resource);
855
- } catch (error) {
856
- return undefined; // file might not exist
857
- }
858
-
859
- // file cannot be directory
860
- if ((stat.type & FileType.Directory) !== 0) {
861
- throw new FileOperationError(nls.localizeByDefault("Unable to write file '{0}' that is actually a directory", this.resourceForError(resource)), FileOperationResult.FILE_IS_DIRECTORY, options);
862
- }
863
-
864
- if (this.modifiedSince(stat, options)) {
865
- throw new FileOperationError(nls.localizeByDefault('File Modified Since'), FileOperationResult.FILE_MODIFIED_SINCE, options);
866
- }
867
-
868
- return stat;
869
- }
870
-
871
- /**
872
- * Dirty write prevention: if the file on disk has been changed and does not match our expected
873
- * mtime and etag, we bail out to prevent dirty writing.
874
- *
875
- * First, we check for a mtime that is in the future before we do more checks. The assumption is
876
- * that only the mtime is an indicator for a file that has changed on disk.
877
- *
878
- * Second, if the mtime has advanced, we compare the size of the file on disk with our previous
879
- * one using the etag() function. Relying only on the mtime check has proven to produce false
880
- * positives due to file system weirdness (especially around remote file systems). As such, the
881
- * check for size is a weaker check because it can return a false negative if the file has changed
882
- * but to the same length. This is a compromise we take to avoid having to produce checksums of
883
- * the file content for comparison which would be much slower to compute.
884
- */
885
- protected modifiedSince(stat: Stat, options?: WriteFileOptions): boolean {
886
- return !!options && typeof options.mtime === 'number' && typeof options.etag === 'string' && options.etag !== ETAG_DISABLED &&
887
- typeof stat.mtime === 'number' && typeof stat.size === 'number' &&
888
- options.mtime < stat.mtime && options.etag !== etag({ mtime: options.mtime /* not using stat.mtime for a reason, see above */, size: stat.size });
889
- }
890
-
891
- async readFile(resource: URI, options?: ReadFileOptions): Promise<FileContent> {
892
- const provider = await this.withReadProvider(resource);
893
-
894
- const stream = await this.doReadAsFileStream(provider, resource, {
895
- ...options,
896
- // optimization: since we know that the caller does not
897
- // care about buffering, we indicate this to the reader.
898
- // this reduces all the overhead the buffered reading
899
- // has (open, read, close) if the provider supports
900
- // unbuffered reading.
901
- preferUnbuffered: true
902
- });
903
-
904
- return {
905
- ...stream,
906
- value: await BinaryBufferReadableStream.toBuffer(stream.value)
907
- };
908
- }
909
-
910
- async readFileStream(resource: URI, options?: ReadFileOptions): Promise<FileStreamContent> {
911
- const provider = await this.withReadProvider(resource);
912
-
913
- return this.doReadAsFileStream(provider, resource, options);
914
- }
915
-
916
- private async doReadAsFileStream(provider: FileSystemProviderWithFileReadWriteCapability | FileSystemProviderWithOpenReadWriteCloseCapability, resource: URI, options?: ReadFileOptions & { preferUnbuffered?: boolean }): Promise<FileStreamContent> {
917
-
918
- // install a cancellation token that gets cancelled
919
- // when any error occurs. this allows us to resolve
920
- // the content of the file while resolving metadata
921
- // but still cancel the operation in certain cases.
922
- const cancellableSource = new CancellationTokenSource();
923
-
924
- // validate read operation
925
- const statPromise = this.validateReadFile(resource, options).then(stat => stat, error => {
926
- cancellableSource.cancel();
927
-
928
- throw error;
929
- });
930
-
931
- try {
932
-
933
- // if the etag is provided, we await the result of the validation
934
- // due to the likelyhood of hitting a NOT_MODIFIED_SINCE result.
935
- // otherwise, we let it run in parallel to the file reading for
936
- // optimal startup performance.
937
- if (options && typeof options.etag === 'string' && options.etag !== ETAG_DISABLED) {
938
- await statPromise;
939
- }
940
-
941
- let fileStreamPromise: Promise<BinaryBufferReadableStream>;
942
-
943
- // read unbuffered (only if either preferred, or the provider has no buffered read capability)
944
- if (!(hasOpenReadWriteCloseCapability(provider) || hasFileReadStreamCapability(provider)) || (hasReadWriteCapability(provider) && options?.preferUnbuffered)) {
945
- fileStreamPromise = this.readFileUnbuffered(provider, resource, options);
946
- }
947
-
948
- // read streamed (always prefer over primitive buffered read)
949
- else if (hasFileReadStreamCapability(provider)) {
950
- fileStreamPromise = Promise.resolve(this.readFileStreamed(provider, resource, cancellableSource.token, options));
951
- }
952
-
953
- // read buffered
954
- else {
955
- fileStreamPromise = Promise.resolve(this.readFileBuffered(provider, resource, cancellableSource.token, options));
956
- }
957
-
958
- const [fileStat, fileStream] = await Promise.all([statPromise, fileStreamPromise]);
959
-
960
- return {
961
- ...fileStat,
962
- value: fileStream
963
- };
964
- } catch (error) {
965
- this.rethrowAsFileOperationError("Unable to read file '{0}' ({1})", resource, error, options);
966
- }
967
- }
968
-
969
- private readFileStreamed(provider: FileSystemProviderWithFileReadStreamCapability, resource: URI, token: CancellationToken, options: ReadFileOptions = Object.create(null)): BinaryBufferReadableStream {
970
- const fileStream = provider.readFileStream(resource, options, token);
971
-
972
- return transform(fileStream, {
973
- data: data => data instanceof BinaryBuffer ? data : BinaryBuffer.wrap(data),
974
- error: error => this.asFileOperationError("Unable to read file '{0}' ({1})", resource, error, options)
975
- }, data => BinaryBuffer.concat(data));
976
- }
977
-
978
- private readFileBuffered(provider: FileSystemProviderWithOpenReadWriteCloseCapability, resource: URI, token: CancellationToken, options: ReadFileOptions = Object.create(null)): BinaryBufferReadableStream {
979
- const stream = BinaryBufferWriteableStream.create();
980
-
981
- readFileIntoStream(provider, resource, stream, data => data, {
982
- ...options,
983
- bufferSize: this.BUFFER_SIZE,
984
- errorTransformer: error => this.asFileOperationError("Unable to read file '{0}' ({1})", resource, error, options)
985
- }, token);
986
-
987
- return stream;
988
- }
989
-
990
- protected rethrowAsFileOperationError(message: string, resource: URI, error: Error, options?: ReadFileOptions & WriteFileOptions & CreateFileOptions): never {
991
- throw this.asFileOperationError(message, resource, error, options);
992
- }
993
- protected asFileOperationError(message: string, resource: URI, error: Error, options?: ReadFileOptions & WriteFileOptions & CreateFileOptions): FileOperationError {
994
- const fileOperationError = new FileOperationError(nls.localizeByDefault(message, this.resourceForError(resource), ensureFileSystemProviderError(error).toString()),
995
- toFileOperationResult(error), options);
996
- fileOperationError.stack = `${fileOperationError.stack}\nCaused by: ${error.stack}`;
997
- return fileOperationError;
998
- }
999
-
1000
- private async readFileUnbuffered(provider: FileSystemProviderWithFileReadWriteCapability, resource: URI, options?: ReadFileOptions): Promise<BinaryBufferReadableStream> {
1001
- let buffer = await provider.readFile(resource);
1002
-
1003
- // respect position option
1004
- if (options && typeof options.position === 'number') {
1005
- buffer = buffer.slice(options.position);
1006
- }
1007
-
1008
- // respect length option
1009
- if (options && typeof options.length === 'number') {
1010
- buffer = buffer.slice(0, options.length);
1011
- }
1012
-
1013
- // Throw if file is too large to load
1014
- this.validateReadFileLimits(resource, buffer.byteLength, options);
1015
-
1016
- return BinaryBufferReadableStream.fromBuffer(BinaryBuffer.wrap(buffer));
1017
- }
1018
-
1019
- private async validateReadFile(resource: URI, options?: ReadFileOptions): Promise<FileStatWithMetadata> {
1020
- const stat = await this.resolve(resource, { resolveMetadata: true });
1021
-
1022
- // Throw if resource is a directory
1023
- if (stat.isDirectory) {
1024
- throw new FileOperationError(nls.localizeByDefault("Unable to read file '{0}' that is actually a directory", this.resourceForError(resource)), FileOperationResult.FILE_IS_DIRECTORY, options);
1025
- }
1026
-
1027
- // Throw if file not modified since (unless disabled)
1028
- if (options && typeof options.etag === 'string' && options.etag !== ETAG_DISABLED && options.etag === stat.etag) {
1029
- throw new FileOperationError(nls.localizeByDefault('File not modified since'), FileOperationResult.FILE_NOT_MODIFIED_SINCE, options);
1030
- }
1031
-
1032
- // Throw if file is too large to load
1033
- this.validateReadFileLimits(resource, stat.size, options);
1034
-
1035
- return stat;
1036
- }
1037
-
1038
- private validateReadFileLimits(resource: URI, size: number, options?: ReadFileOptions): void {
1039
- if (options?.limits) {
1040
- let tooLargeErrorResult: FileOperationResult | undefined = undefined;
1041
-
1042
- if (typeof options.limits.memory === 'number' && size > options.limits.memory) {
1043
- tooLargeErrorResult = FileOperationResult.FILE_EXCEEDS_MEMORY_LIMIT;
1044
- }
1045
-
1046
- if (typeof options.limits.size === 'number' && size > options.limits.size) {
1047
- tooLargeErrorResult = FileOperationResult.FILE_TOO_LARGE;
1048
- }
1049
-
1050
- if (typeof tooLargeErrorResult === 'number') {
1051
- throw new FileOperationError(nls.localizeByDefault("Unable to read file '{0}' that is too large to open", this.resourceForError(resource)), tooLargeErrorResult);
1052
- }
1053
- }
1054
- }
1055
-
1056
- // #endregion
1057
-
1058
- // #region Move/Copy/Delete/Create Folder
1059
-
1060
- async move(source: URI, target: URI, options?: MoveFileOptions): Promise<FileStatWithMetadata> {
1061
- if (options?.fromUserGesture === false) {
1062
- return this.doMove(source, target, options.overwrite);
1063
- }
1064
- await this.runFileOperationParticipants(target, source, FileOperation.MOVE);
1065
-
1066
- const event = { correlationId: this.correlationIds++, operation: FileOperation.MOVE, target, source };
1067
- await this.onWillRunUserOperationEmitter.fire(event);
1068
- let stat: FileStatWithMetadata;
1069
- try {
1070
- stat = await this.doMove(source, target, options?.overwrite);
1071
- } catch (error) {
1072
- await this.onDidFailUserOperationEmitter.fire(event);
1073
- throw error;
1074
- }
1075
-
1076
- await this.onDidRunUserOperationEmitter.fire(event);
1077
- return stat;
1078
- }
1079
-
1080
- protected async doMove(source: URI, target: URI, overwrite?: boolean): Promise<FileStatWithMetadata> {
1081
- const sourceProvider = this.throwIfFileSystemIsReadonly(await this.withWriteProvider(source), source);
1082
- const targetProvider = this.throwIfFileSystemIsReadonly(await this.withWriteProvider(target), target);
1083
-
1084
- // move
1085
- const mode = await this.doMoveCopy(sourceProvider, source, targetProvider, target, 'move', !!overwrite);
1086
-
1087
- // resolve and send events
1088
- const fileStat = await this.resolve(target, { resolveMetadata: true });
1089
- this.onDidRunOperationEmitter.fire(new FileOperationEvent(source, mode === 'move' ? FileOperation.MOVE : FileOperation.COPY, fileStat));
1090
-
1091
- return fileStat;
1092
- }
1093
-
1094
- async copy(source: URI, target: URI, options?: CopyFileOptions): Promise<FileStatWithMetadata> {
1095
- if (options?.fromUserGesture === false) {
1096
- return this.doCopy(source, target, options.overwrite);
1097
- }
1098
- await this.runFileOperationParticipants(target, source, FileOperation.COPY);
1099
-
1100
- const event = { correlationId: this.correlationIds++, operation: FileOperation.COPY, target, source };
1101
- await this.onWillRunUserOperationEmitter.fire(event);
1102
- let stat: FileStatWithMetadata;
1103
- try {
1104
- stat = await this.doCopy(source, target, options?.overwrite);
1105
- } catch (error) {
1106
- await this.onDidFailUserOperationEmitter.fire(event);
1107
- throw error;
1108
- }
1109
-
1110
- await this.onDidRunUserOperationEmitter.fire(event);
1111
- return stat;
1112
- }
1113
-
1114
- protected async doCopy(source: URI, target: URI, overwrite?: boolean): Promise<FileStatWithMetadata> {
1115
- const sourceProvider = await this.withReadProvider(source);
1116
- const targetProvider = this.throwIfFileSystemIsReadonly(await this.withWriteProvider(target), target);
1117
-
1118
- // copy
1119
- const mode = await this.doMoveCopy(sourceProvider, source, targetProvider, target, 'copy', !!overwrite);
1120
-
1121
- // resolve and send events
1122
- const fileStat = await this.resolve(target, { resolveMetadata: true });
1123
- this.onDidRunOperationEmitter.fire(new FileOperationEvent(source, mode === 'copy' ? FileOperation.COPY : FileOperation.MOVE, fileStat));
1124
-
1125
- return fileStat;
1126
- }
1127
-
1128
- private async doMoveCopy(sourceProvider: FileSystemProvider, source: URI, targetProvider: FileSystemProvider, target: URI, mode: 'move' | 'copy', overwrite: boolean): Promise<'move' | 'copy'> {
1129
- if (source.toString() === target.toString()) {
1130
- return mode; // simulate node.js behaviour here and do a no-op if paths match
1131
- }
1132
-
1133
- // validation
1134
- const { exists, isSameResourceWithDifferentPathCase } = await this.doValidateMoveCopy(sourceProvider, source, targetProvider, target, mode, overwrite);
1135
-
1136
- // if target exists get valid target
1137
- if (exists && !overwrite) {
1138
- const parent = await this.resolve(target.parent);
1139
- const targetFileStat = await this.resolve(target);
1140
- target = FileSystemUtils.generateUniqueResourceURI(parent, target, targetFileStat.isDirectory, isSameResourceWithDifferentPathCase ? 'copy' : undefined);
1141
- }
1142
-
1143
- // delete as needed (unless target is same resource with different path case)
1144
- if (exists && !isSameResourceWithDifferentPathCase && overwrite) {
1145
- await this.delete(target, { recursive: true });
1146
- }
1147
-
1148
- // create parent folders
1149
- await this.mkdirp(targetProvider, target.parent);
1150
-
1151
- // copy source => target
1152
- if (mode === 'copy') {
1153
-
1154
- // same provider with fast copy: leverage copy() functionality
1155
- if (sourceProvider === targetProvider && hasFileFolderCopyCapability(sourceProvider)) {
1156
- await sourceProvider.copy(source, target, { overwrite });
1157
- }
1158
-
1159
- // when copying via buffer/unbuffered, we have to manually
1160
- // traverse the source if it is a folder and not a file
1161
- else {
1162
- const sourceFile = await this.resolve(source);
1163
- if (sourceFile.isDirectory) {
1164
- await this.doCopyFolder(sourceProvider, sourceFile, targetProvider, target);
1165
- } else {
1166
- await this.doCopyFile(sourceProvider, source, targetProvider, target);
1167
- }
1168
- }
1169
-
1170
- return mode;
1171
- }
1172
-
1173
- // move source => target
1174
- else {
1175
-
1176
- // same provider: leverage rename() functionality
1177
- if (sourceProvider === targetProvider) {
1178
- await sourceProvider.rename(source, target, { overwrite });
1179
-
1180
- return mode;
1181
- }
1182
-
1183
- // across providers: copy to target & delete at source
1184
- else {
1185
- await this.doMoveCopy(sourceProvider, source, targetProvider, target, 'copy', overwrite);
1186
-
1187
- await this.delete(source, { recursive: true });
1188
-
1189
- return 'copy';
1190
- }
1191
- }
1192
- }
1193
-
1194
- private async doCopyFile(sourceProvider: FileSystemProvider, source: URI, targetProvider: FileSystemProvider, target: URI): Promise<void> {
1195
-
1196
- // copy: source (buffered) => target (buffered)
1197
- if (hasOpenReadWriteCloseCapability(sourceProvider) && hasOpenReadWriteCloseCapability(targetProvider)) {
1198
- return this.doPipeBuffered(sourceProvider, source, targetProvider, target);
1199
- }
1200
-
1201
- // copy: source (buffered) => target (unbuffered)
1202
- if (hasOpenReadWriteCloseCapability(sourceProvider) && hasReadWriteCapability(targetProvider)) {
1203
- return this.doPipeBufferedToUnbuffered(sourceProvider, source, targetProvider, target);
1204
- }
1205
-
1206
- // copy: source (unbuffered) => target (buffered)
1207
- if (hasReadWriteCapability(sourceProvider) && hasOpenReadWriteCloseCapability(targetProvider)) {
1208
- return this.doPipeUnbufferedToBuffered(sourceProvider, source, targetProvider, target);
1209
- }
1210
-
1211
- // copy: source (unbuffered) => target (unbuffered)
1212
- if (hasReadWriteCapability(sourceProvider) && hasReadWriteCapability(targetProvider)) {
1213
- return this.doPipeUnbuffered(sourceProvider, source, targetProvider, target);
1214
- }
1215
- }
1216
-
1217
- private async doCopyFolder(sourceProvider: FileSystemProvider, sourceFolder: FileStat, targetProvider: FileSystemProvider, targetFolder: URI): Promise<void> {
1218
-
1219
- // create folder in target
1220
- await targetProvider.mkdir(targetFolder);
1221
-
1222
- // create children in target
1223
- if (Array.isArray(sourceFolder.children)) {
1224
- await Promise.all(sourceFolder.children.map(async sourceChild => {
1225
- const targetChild = targetFolder.resolve(sourceChild.name);
1226
- if (sourceChild.isDirectory) {
1227
- return this.doCopyFolder(sourceProvider, await this.resolve(sourceChild.resource), targetProvider, targetChild);
1228
- } else {
1229
- return this.doCopyFile(sourceProvider, sourceChild.resource, targetProvider, targetChild);
1230
- }
1231
- }));
1232
- }
1233
- }
1234
-
1235
- private async doValidateMoveCopy(sourceProvider: FileSystemProvider, source: URI, targetProvider: FileSystemProvider, target: URI, mode: 'move' | 'copy', overwrite?: boolean): Promise<{ exists: boolean, isSameResourceWithDifferentPathCase: boolean }> {
1236
- let isSameResourceWithDifferentPathCase = false;
1237
-
1238
- // Check if source is equal or parent to target (requires providers to be the same)
1239
- if (sourceProvider === targetProvider) {
1240
- const isPathCaseSensitive = !!(sourceProvider.capabilities & FileSystemProviderCapabilities.PathCaseSensitive);
1241
- if (!isPathCaseSensitive) {
1242
- isSameResourceWithDifferentPathCase = source.toString().toLowerCase() === target.toString().toLowerCase();
1243
- }
1244
-
1245
- if (isSameResourceWithDifferentPathCase && mode === 'copy') {
1246
- throw new Error(nls.localizeByDefault("Unable to move/copy when source '{0}' is parent of target '{1}'.", this.resourceForError(source), this.resourceForError(target)));
1247
- }
1248
-
1249
- if (!isSameResourceWithDifferentPathCase && target.isEqualOrParent(source, isPathCaseSensitive)) {
1250
- throw new Error(nls.localizeByDefault("Unable to move/copy when source '{0}' is parent of target '{1}'.", this.resourceForError(source), this.resourceForError(target)));
1251
- }
1252
- }
1253
-
1254
- // Extra checks if target exists and this is not a rename
1255
- const exists = await this.exists(target);
1256
- if (exists && !isSameResourceWithDifferentPathCase) {
1257
-
1258
- // Special case: if the target is a parent of the source, we cannot delete
1259
- // it as it would delete the source as well. In this case we have to throw
1260
- if (sourceProvider === targetProvider) {
1261
- const isPathCaseSensitive = !!(sourceProvider.capabilities & FileSystemProviderCapabilities.PathCaseSensitive);
1262
- if (source.isEqualOrParent(target, isPathCaseSensitive)) {
1263
- throw new Error(nls.localizeByDefault("Unable to move/copy '{0}' into '{1}' since a file would replace the folder it is contained in.", this.resourceForError(source), this.resourceForError(target)));
1264
- }
1265
- }
1266
- }
1267
-
1268
- return { exists, isSameResourceWithDifferentPathCase };
1269
- }
1270
-
1271
- async createFolder(resource: URI, options: FileOperationOptions = {}): Promise<FileStatWithMetadata> {
1272
- const {
1273
- fromUserGesture = true,
1274
- } = options;
1275
-
1276
- const provider = this.throwIfFileSystemIsReadonly(await this.withProvider(resource), resource);
1277
-
1278
- // mkdir recursively
1279
- await this.mkdirp(provider, resource);
1280
-
1281
- // events
1282
- const fileStat = await this.resolve(resource, { resolveMetadata: true });
1283
-
1284
- if (fromUserGesture) {
1285
- this.onDidRunUserOperationEmitter.fire({ correlationId: this.correlationIds++, operation: FileOperation.CREATE, target: resource });
1286
- } else {
1287
- this.onDidRunOperationEmitter.fire(new FileOperationEvent(resource, FileOperation.CREATE, fileStat));
1288
- }
1289
-
1290
- return fileStat;
1291
- }
1292
-
1293
- private async mkdirp(provider: FileSystemProvider, directory: URI): Promise<void> {
1294
- const directoriesToCreate: string[] = [];
1295
-
1296
- // mkdir until we reach root
1297
- while (!directory.path.isRoot) {
1298
- try {
1299
- const stat = await provider.stat(directory);
1300
- if ((stat.type & FileType.Directory) === 0) {
1301
- throw new Error(nls.localizeByDefault("Unable to create folder '{0}' that already exists but is not a directory", this.resourceForError(directory)));
1302
- }
1303
-
1304
- break; // we have hit a directory that exists -> good
1305
- } catch (error) {
1306
-
1307
- // Bubble up any other error that is not file not found
1308
- if (toFileSystemProviderErrorCode(error) !== FileSystemProviderErrorCode.FileNotFound) {
1309
- throw error;
1310
- }
1311
-
1312
- // Upon error, remember directories that need to be created
1313
- directoriesToCreate.push(directory.path.base);
1314
-
1315
- // Continue up
1316
- directory = directory.parent;
1317
- }
1318
- }
1319
-
1320
- // Create directories as needed
1321
- for (let i = directoriesToCreate.length - 1; i >= 0; i--) {
1322
- directory = directory.resolve(directoriesToCreate[i]);
1323
-
1324
- try {
1325
- await provider.mkdir(directory);
1326
- } catch (error) {
1327
- if (toFileSystemProviderErrorCode(error) !== FileSystemProviderErrorCode.FileExists) {
1328
- // For mkdirp() we tolerate that the mkdir() call fails
1329
- // in case the folder already exists. This follows node.js
1330
- // own implementation of fs.mkdir({ recursive: true }) and
1331
- // reduces the chances of race conditions leading to errors
1332
- // if multiple calls try to create the same folders
1333
- // As such, we only throw an error here if it is other than
1334
- // the fact that the file already exists.
1335
- // (see also https://github.com/microsoft/vscode/issues/89834)
1336
- throw error;
1337
- }
1338
- }
1339
- }
1340
- }
1341
-
1342
- async delete(resource: URI, options?: FileOperationOptions & Partial<FileDeleteOptions>): Promise<void> {
1343
- if (options?.fromUserGesture === false) {
1344
- return this.doDelete(resource, options);
1345
- }
1346
- await this.runFileOperationParticipants(resource, undefined, FileOperation.DELETE);
1347
-
1348
- const event = { correlationId: this.correlationIds++, operation: FileOperation.DELETE, target: resource };
1349
- await this.onWillRunUserOperationEmitter.fire(event);
1350
- try {
1351
- await this.doDelete(resource, options);
1352
- } catch (error) {
1353
- await this.onDidFailUserOperationEmitter.fire(event);
1354
- throw error;
1355
- }
1356
-
1357
- await this.onDidRunUserOperationEmitter.fire(event);
1358
- }
1359
-
1360
- protected async doDelete(resource: URI, options?: Partial<FileDeleteOptions>): Promise<void> {
1361
- const provider = this.throwIfFileSystemIsReadonly(await this.withProvider(resource), resource);
1362
-
1363
- // Validate trash support
1364
- const useTrash = !!options?.useTrash;
1365
- if (useTrash && !(provider.capabilities & FileSystemProviderCapabilities.Trash)) {
1366
- throw new Error(nls.localizeByDefault("Unable to delete file '{0}' via trash because provider does not support it.", this.resourceForError(resource)));
1367
- }
1368
-
1369
- // Validate delete
1370
- const exists = await this.exists(resource);
1371
- if (!exists) {
1372
- throw new FileOperationError(nls.localizeByDefault("Unable to delete nonexistent file '{0}'", this.resourceForError(resource)), FileOperationResult.FILE_NOT_FOUND);
1373
- }
1374
-
1375
- // Validate recursive
1376
- const recursive = !!options?.recursive;
1377
- if (!recursive && exists) {
1378
- const stat = await this.resolve(resource);
1379
- if (stat.isDirectory && Array.isArray(stat.children) && stat.children.length > 0) {
1380
- throw new Error(nls.localizeByDefault("Unable to delete non-empty folder '{0}'.", this.resourceForError(resource)));
1381
- }
1382
- }
1383
-
1384
- // Delete through provider
1385
- await provider.delete(resource, { recursive, useTrash });
1386
-
1387
- // Events
1388
- this.onDidRunOperationEmitter.fire(new FileOperationEvent(resource, FileOperation.DELETE));
1389
- }
1390
-
1391
- // #endregion
1392
-
1393
- // #region File Watching
1394
-
1395
- private onDidFilesChangeEmitter = new Emitter<FileChangesEvent>();
1396
- /**
1397
- * An event that is emitted when files are changed on the disk.
1398
- */
1399
- get onDidFilesChange(): Event<FileChangesEvent> {
1400
- return this.onDidFilesChangeEmitter.event;
1401
- }
1402
-
1403
- private activeWatchers = new Map<string, { disposable: Disposable, count: number }>();
1404
-
1405
- watch(resource: URI, options: WatchOptions = { recursive: false, excludes: [] }): Disposable {
1406
- const resolvedOptions: WatchOptions = {
1407
- ...options,
1408
- // always ignore temporary upload files
1409
- excludes: options.excludes.concat('**/theia_upload_*')
1410
- };
1411
-
1412
- let watchDisposed = false;
1413
- let watchDisposable = Disposable.create(() => watchDisposed = true);
1414
-
1415
- // Watch and wire in disposable which is async but
1416
- // check if we got disposed meanwhile and forward
1417
- this.doWatch(resource, resolvedOptions).then(disposable => {
1418
- if (watchDisposed) {
1419
- disposable.dispose();
1420
- } else {
1421
- watchDisposable = disposable;
1422
- }
1423
- }, error => console.error(error));
1424
-
1425
- return Disposable.create(() => watchDisposable.dispose());
1426
- }
1427
-
1428
- async doWatch(resource: URI, options: WatchOptions): Promise<Disposable> {
1429
- const provider = await this.withProvider(resource);
1430
- const key = this.toWatchKey(provider, resource, options);
1431
-
1432
- // Only start watching if we are the first for the given key
1433
- const watcher = this.activeWatchers.get(key) || { count: 0, disposable: provider.watch(resource, options) };
1434
- if (!this.activeWatchers.has(key)) {
1435
- this.activeWatchers.set(key, watcher);
1436
- }
1437
-
1438
- // Increment usage counter
1439
- watcher.count += 1;
1440
-
1441
- return Disposable.create(() => {
1442
-
1443
- // Unref
1444
- watcher.count--;
1445
-
1446
- // Dispose only when last user is reached
1447
- if (watcher.count === 0) {
1448
- watcher.disposable.dispose();
1449
- this.activeWatchers.delete(key);
1450
- }
1451
- });
1452
- }
1453
-
1454
- private toWatchKey(provider: FileSystemProvider, resource: URI, options: WatchOptions): string {
1455
- return [
1456
- this.toMapKey(provider, resource), // lowercase path if the provider is case insensitive
1457
- String(options.recursive), // use recursive: true | false as part of the key
1458
- options.excludes.join() // use excludes as part of the key
1459
- ].join();
1460
- }
1461
-
1462
- // #endregion
1463
-
1464
- // #region Helpers
1465
-
1466
- private writeQueues: Map<string, Promise<void>> = new Map();
1467
-
1468
- private ensureWriteQueue(provider: FileSystemProvider, resource: URI, task: () => Promise<void>): Promise<void> {
1469
- // ensure to never write to the same resource without finishing
1470
- // the one write. this ensures a write finishes consistently
1471
- // (even with error) before another write is done.
1472
- const queueKey = this.toMapKey(provider, resource);
1473
- const writeQueue = (this.writeQueues.get(queueKey) || Promise.resolve()).then(task, task);
1474
- this.writeQueues.set(queueKey, writeQueue);
1475
- return writeQueue;
1476
- }
1477
-
1478
- private toMapKey(provider: FileSystemProvider, resource: URI): string {
1479
- const isPathCaseSensitive = !!(provider.capabilities & FileSystemProviderCapabilities.PathCaseSensitive);
1480
-
1481
- return isPathCaseSensitive ? resource.toString() : resource.toString().toLowerCase();
1482
- }
1483
-
1484
- private async doWriteBuffered(provider: FileSystemProviderWithOpenReadWriteCloseCapability, resource: URI, readableOrStreamOrBufferedStream: BinaryBufferReadable | BinaryBufferReadableStream | BinaryBufferReadableBufferedStream): Promise<void> {
1485
- return this.ensureWriteQueue(provider, resource, async () => {
1486
-
1487
- // open handle
1488
- const handle = await provider.open(resource, { create: true });
1489
-
1490
- // write into handle until all bytes from buffer have been written
1491
- try {
1492
- if (isReadableStream(readableOrStreamOrBufferedStream) || isReadableBufferedStream(readableOrStreamOrBufferedStream)) {
1493
- await this.doWriteStreamBufferedQueued(provider, handle, readableOrStreamOrBufferedStream);
1494
- } else {
1495
- await this.doWriteReadableBufferedQueued(provider, handle, readableOrStreamOrBufferedStream);
1496
- }
1497
- } catch (error) {
1498
- throw ensureFileSystemProviderError(error);
1499
- } finally {
1500
-
1501
- // close handle always
1502
- await provider.close(handle);
1503
- }
1504
- });
1505
- }
1506
-
1507
- private async doWriteStreamBufferedQueued(provider: FileSystemProviderWithOpenReadWriteCloseCapability, handle: number, streamOrBufferedStream: BinaryBufferReadableStream | BinaryBufferReadableBufferedStream): Promise<void> {
1508
- let posInFile = 0;
1509
- let stream: BinaryBufferReadableStream;
1510
-
1511
- // Buffered stream: consume the buffer first by writing
1512
- // it to the target before reading from the stream.
1513
- if (isReadableBufferedStream(streamOrBufferedStream)) {
1514
- if (streamOrBufferedStream.buffer.length > 0) {
1515
- const chunk = BinaryBuffer.concat(streamOrBufferedStream.buffer);
1516
- await this.doWriteBuffer(provider, handle, chunk, chunk.byteLength, posInFile, 0);
1517
-
1518
- posInFile += chunk.byteLength;
1519
- }
1520
-
1521
- // If the stream has been consumed, return early
1522
- if (streamOrBufferedStream.ended) {
1523
- return;
1524
- }
1525
-
1526
- stream = streamOrBufferedStream.stream;
1527
- }
1528
-
1529
- // Unbuffered stream - just take as is
1530
- else {
1531
- stream = streamOrBufferedStream;
1532
- }
1533
-
1534
- return new Promise(async (resolve, reject) => {
1535
-
1536
- stream.on('data', async chunk => {
1537
-
1538
- // pause stream to perform async write operation
1539
- stream.pause();
1540
-
1541
- try {
1542
- await this.doWriteBuffer(provider, handle, chunk, chunk.byteLength, posInFile, 0);
1543
- } catch (error) {
1544
- return reject(error);
1545
- }
1546
-
1547
- posInFile += chunk.byteLength;
1548
-
1549
- // resume stream now that we have successfully written
1550
- // run this on the next tick to prevent increasing the
1551
- // execution stack because resume() may call the event
1552
- // handler again before finishing.
1553
- setTimeout(() => stream.resume());
1554
- });
1555
-
1556
- stream.on('error', error => reject(error));
1557
- stream.on('end', () => resolve());
1558
- });
1559
- }
1560
-
1561
- private async doWriteReadableBufferedQueued(provider: FileSystemProviderWithOpenReadWriteCloseCapability, handle: number, readable: BinaryBufferReadable): Promise<void> {
1562
- let posInFile = 0;
1563
-
1564
- let chunk: BinaryBuffer | null;
1565
- while ((chunk = readable.read()) !== null) {
1566
- await this.doWriteBuffer(provider, handle, chunk, chunk.byteLength, posInFile, 0);
1567
-
1568
- posInFile += chunk.byteLength;
1569
- }
1570
- }
1571
-
1572
- private async doWriteBuffer(provider: FileSystemProviderWithOpenReadWriteCloseCapability, handle: number, buffer: BinaryBuffer, length: number, posInFile: number, posInBuffer: number): Promise<void> {
1573
- let totalBytesWritten = 0;
1574
- while (totalBytesWritten < length) {
1575
- const bytesWritten = await provider.write(handle, posInFile + totalBytesWritten, buffer.buffer, posInBuffer + totalBytesWritten, length - totalBytesWritten);
1576
- totalBytesWritten += bytesWritten;
1577
- }
1578
- }
1579
-
1580
- private async doWriteUnbuffered(provider: FileSystemProviderWithFileReadWriteCapability, resource: URI, bufferOrReadableOrStreamOrBufferedStream: BinaryBuffer | BinaryBufferReadable | BinaryBufferReadableStream | BinaryBufferReadableBufferedStream): Promise<void> {
1581
- return this.ensureWriteQueue(provider, resource, () => this.doWriteUnbufferedQueued(provider, resource, bufferOrReadableOrStreamOrBufferedStream));
1582
- }
1583
-
1584
- private async doWriteUnbufferedQueued(provider: FileSystemProviderWithFileReadWriteCapability, resource: URI, bufferOrReadableOrStreamOrBufferedStream: BinaryBuffer | BinaryBufferReadable | BinaryBufferReadableStream | BinaryBufferReadableBufferedStream): Promise<void> {
1585
- let buffer: BinaryBuffer;
1586
- if (bufferOrReadableOrStreamOrBufferedStream instanceof BinaryBuffer) {
1587
- buffer = bufferOrReadableOrStreamOrBufferedStream;
1588
- } else if (isReadableStream(bufferOrReadableOrStreamOrBufferedStream)) {
1589
- buffer = await BinaryBufferReadableStream.toBuffer(bufferOrReadableOrStreamOrBufferedStream);
1590
- } else if (isReadableBufferedStream(bufferOrReadableOrStreamOrBufferedStream)) {
1591
- buffer = await BinaryBufferReadableBufferedStream.toBuffer(bufferOrReadableOrStreamOrBufferedStream);
1592
- } else {
1593
- buffer = BinaryBufferReadable.toBuffer(bufferOrReadableOrStreamOrBufferedStream);
1594
- }
1595
-
1596
- return provider.writeFile(resource, buffer.buffer, { create: true, overwrite: true });
1597
- }
1598
-
1599
- private async doPipeBuffered(sourceProvider: FileSystemProviderWithOpenReadWriteCloseCapability, source: URI, targetProvider: FileSystemProviderWithOpenReadWriteCloseCapability, target: URI): Promise<void> {
1600
- return this.ensureWriteQueue(targetProvider, target, () => this.doPipeBufferedQueued(sourceProvider, source, targetProvider, target));
1601
- }
1602
-
1603
- private async doPipeBufferedQueued(sourceProvider: FileSystemProviderWithOpenReadWriteCloseCapability, source: URI, targetProvider: FileSystemProviderWithOpenReadWriteCloseCapability, target: URI): Promise<void> {
1604
- let sourceHandle: number | undefined = undefined;
1605
- let targetHandle: number | undefined = undefined;
1606
-
1607
- try {
1608
-
1609
- // Open handles
1610
- sourceHandle = await sourceProvider.open(source, { create: false });
1611
- targetHandle = await targetProvider.open(target, { create: true });
1612
-
1613
- const buffer = BinaryBuffer.alloc(this.BUFFER_SIZE);
1614
-
1615
- let posInFile = 0;
1616
- let posInBuffer = 0;
1617
- let bytesRead = 0;
1618
- do {
1619
- // read from source (sourceHandle) at current position (posInFile) into buffer (buffer) at
1620
- // buffer position (posInBuffer) up to the size of the buffer (buffer.byteLength).
1621
- bytesRead = await sourceProvider.read(sourceHandle, posInFile, buffer.buffer, posInBuffer, buffer.byteLength - posInBuffer);
1622
-
1623
- // write into target (targetHandle) at current position (posInFile) from buffer (buffer) at
1624
- // buffer position (posInBuffer) all bytes we read (bytesRead).
1625
- await this.doWriteBuffer(targetProvider, targetHandle, buffer, bytesRead, posInFile, posInBuffer);
1626
-
1627
- posInFile += bytesRead;
1628
- posInBuffer += bytesRead;
1629
-
1630
- // when buffer full, fill it again from the beginning
1631
- if (posInBuffer === buffer.byteLength) {
1632
- posInBuffer = 0;
1633
- }
1634
- } while (bytesRead > 0);
1635
- } catch (error) {
1636
- throw ensureFileSystemProviderError(error);
1637
- } finally {
1638
- await Promise.all([
1639
- typeof sourceHandle === 'number' ? sourceProvider.close(sourceHandle) : Promise.resolve(),
1640
- typeof targetHandle === 'number' ? targetProvider.close(targetHandle) : Promise.resolve(),
1641
- ]);
1642
- }
1643
- }
1644
-
1645
- private async doPipeUnbuffered(sourceProvider: FileSystemProviderWithFileReadWriteCapability, source: URI, targetProvider: FileSystemProviderWithFileReadWriteCapability, target: URI): Promise<void> {
1646
- return this.ensureWriteQueue(targetProvider, target, () => this.doPipeUnbufferedQueued(sourceProvider, source, targetProvider, target));
1647
- }
1648
-
1649
- private async doPipeUnbufferedQueued(sourceProvider: FileSystemProviderWithFileReadWriteCapability, source: URI, targetProvider: FileSystemProviderWithFileReadWriteCapability, target: URI): Promise<void> {
1650
- return targetProvider.writeFile(target, await sourceProvider.readFile(source), { create: true, overwrite: true });
1651
- }
1652
-
1653
- private async doPipeUnbufferedToBuffered(sourceProvider: FileSystemProviderWithFileReadWriteCapability, source: URI, targetProvider: FileSystemProviderWithOpenReadWriteCloseCapability, target: URI): Promise<void> {
1654
- return this.ensureWriteQueue(targetProvider, target, () => this.doPipeUnbufferedToBufferedQueued(sourceProvider, source, targetProvider, target));
1655
- }
1656
-
1657
- private async doPipeUnbufferedToBufferedQueued(sourceProvider: FileSystemProviderWithFileReadWriteCapability, source: URI, targetProvider: FileSystemProviderWithOpenReadWriteCloseCapability, target: URI): Promise<void> {
1658
-
1659
- // Open handle
1660
- const targetHandle = await targetProvider.open(target, { create: true });
1661
-
1662
- // Read entire buffer from source and write buffered
1663
- try {
1664
- const buffer = await sourceProvider.readFile(source);
1665
- await this.doWriteBuffer(targetProvider, targetHandle, BinaryBuffer.wrap(buffer), buffer.byteLength, 0, 0);
1666
- } catch (error) {
1667
- throw ensureFileSystemProviderError(error);
1668
- } finally {
1669
- await targetProvider.close(targetHandle);
1670
- }
1671
- }
1672
-
1673
- private async doPipeBufferedToUnbuffered(sourceProvider: FileSystemProviderWithOpenReadWriteCloseCapability, source: URI, targetProvider: FileSystemProviderWithFileReadWriteCapability, target: URI): Promise<void> {
1674
-
1675
- // Read buffer via stream buffered
1676
- const buffer = await BinaryBufferReadableStream.toBuffer(this.readFileBuffered(sourceProvider, source, CancellationToken.None));
1677
-
1678
- // Write buffer into target at once
1679
- await this.doWriteUnbuffered(targetProvider, target, buffer);
1680
- }
1681
-
1682
- protected throwIfFileSystemIsReadonly<T extends FileSystemProvider>(provider: T, resource: URI): T {
1683
- if (provider.capabilities & FileSystemProviderCapabilities.Readonly) {
1684
- throw new FileOperationError(nls.localizeByDefault("Unable to modify read-only file '{0}'", this.resourceForError(resource)), FileOperationResult.FILE_PERMISSION_DENIED);
1685
- }
1686
-
1687
- return provider;
1688
- }
1689
-
1690
- private resourceForError(resource: URI): string {
1691
- return this.labelProvider.getLongName(resource);
1692
- }
1693
-
1694
- // #endregion
1695
-
1696
- // #region File operation participants
1697
-
1698
- private readonly participants: FileOperationParticipant[] = [];
1699
-
1700
- addFileOperationParticipant(participant: FileOperationParticipant): Disposable {
1701
- this.participants.push(participant);
1702
-
1703
- return Disposable.create(() => {
1704
- const index = this.participants.indexOf(participant);
1705
- if (index > -1) {
1706
- this.participants.splice(index, 1);
1707
- }
1708
- });
1709
- }
1710
-
1711
- async runFileOperationParticipants(target: URI, source: URI | undefined, operation: FileOperation): Promise<void> {
1712
- const participantsTimeout = this.preferences['files.participants.timeout'];
1713
- if (participantsTimeout <= 0 || this.participants.length === 0) {
1714
- return;
1715
- }
1716
-
1717
- const cancellationTokenSource = new CancellationTokenSource();
1718
-
1719
- return this.progressService.withProgress(
1720
- this.progressLabel(operation),
1721
- 'notification',
1722
- async () => {
1723
- for (const participant of this.participants) {
1724
- if (cancellationTokenSource.token.isCancellationRequested) {
1725
- break;
1726
- }
1727
-
1728
- try {
1729
- const promise = participant.participate(target, source, operation, participantsTimeout, cancellationTokenSource.token);
1730
- await Promise.race([
1731
- promise,
1732
- timeout(participantsTimeout, cancellationTokenSource.token).then(() => cancellationTokenSource.dispose(), () => { /* no-op if cancelled */ })
1733
- ]);
1734
- } catch (err) {
1735
- console.warn(err);
1736
- }
1737
- }
1738
- },
1739
- () => {
1740
- cancellationTokenSource.cancel();
1741
- });
1742
- }
1743
-
1744
- private progressLabel(operation: FileOperation): string {
1745
- switch (operation) {
1746
- case FileOperation.CREATE:
1747
- return nls.localizeByDefault("Running 'File Create' participants...");
1748
- case FileOperation.MOVE:
1749
- return nls.localizeByDefault("Running 'File Rename' participants...");
1750
- case FileOperation.COPY:
1751
- return nls.localizeByDefault("Running 'File Copy' participants...");
1752
- case FileOperation.DELETE:
1753
- return nls.localizeByDefault("Running 'File Delete' participants...");
1754
- }
1755
- }
1756
-
1757
- // #endregion
1758
-
1759
- // #region encoding
1760
-
1761
- protected async getWriteEncoding(resource: URI, options?: WriteEncodingOptions): Promise<ResourceEncoding> {
1762
- const encoding = await this.getEncodingForResource(resource, options ? options.encoding : undefined);
1763
- return this.encodingService.toResourceEncoding(encoding, {
1764
- overwriteEncoding: options?.overwriteEncoding,
1765
- read: async length => {
1766
- const buffer = await BinaryBufferReadableStream.toBuffer((await this.readFileStream(resource, { length })).value);
1767
- return buffer.buffer;
1768
- }
1769
- });
1770
- }
1771
-
1772
- protected getReadEncoding(resource: URI, options?: ReadEncodingOptions, detectedEncoding?: string): Promise<string> {
1773
- let preferredEncoding: string | undefined;
1774
-
1775
- // Encoding passed in as option
1776
- if (options?.encoding) {
1777
- if (detectedEncoding === UTF8_with_bom && options.encoding === UTF8) {
1778
- preferredEncoding = UTF8_with_bom; // indicate the file has BOM if we are to resolve with UTF 8
1779
- } else {
1780
- preferredEncoding = options.encoding; // give passed in encoding highest priority
1781
- }
1782
- } else if (detectedEncoding) {
1783
- preferredEncoding = detectedEncoding;
1784
- }
1785
-
1786
- return this.getEncodingForResource(resource, preferredEncoding);
1787
- }
1788
-
1789
- protected async getEncodingForResource(resource: URI, preferredEncoding?: string): Promise<string> {
1790
- resource = await this.toUnderlyingResource(resource);
1791
- return this.encodingRegistry.getEncodingForResource(resource, preferredEncoding);
1792
- }
1793
-
1794
- /**
1795
- * Converts to an underlying fs provider resource format.
1796
- *
1797
- * For example converting `user-storage` resources to `file` resources under a user home:
1798
- * user-storage:/user/settings.json => file://home/.theia/settings.json
1799
- */
1800
- async toUnderlyingResource(resource: URI): Promise<URI> {
1801
- let provider = await this.withProvider(resource);
1802
- while (provider instanceof DelegatingFileSystemProvider) {
1803
- resource = provider.toUnderlyingResource(resource);
1804
- provider = await this.withProvider(resource);
1805
- }
1806
- return resource;
1807
- }
1808
-
1809
- // #endregion
1810
-
1811
- protected handleFileWatchError(): void {
1812
- this.watcherErrorHandler.handleError();
1813
- }
1814
- }
1
+ // *****************************************************************************
2
+ // Copyright (C) 2020 TypeFox 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/common/fileService.ts
21
+ // and https://github.com/microsoft/vscode/blob/04c36be045a94fee58e5f8992d3e3fd980294a84/src/vs/workbench/services/textfile/browser/textFileService.ts
22
+ // and https://github.com/microsoft/vscode/blob/04c36be045a94fee58e5f8992d3e3fd980294a84/src/vs/workbench/services/textfile/electron-browser/nativeTextFileService.ts
23
+ // and https://github.com/microsoft/vscode/blob/04c36be045a94fee58e5f8992d3e3fd980294a84/src/vs/workbench/services/workingCopy/common/workingCopyFileService.ts
24
+ // and https://github.com/microsoft/vscode/blob/04c36be045a94fee58e5f8992d3e3fd980294a84/src/vs/workbench/services/workingCopy/common/workingCopyFileOperationParticipant.ts
25
+
26
+ /* eslint-disable max-len */
27
+ /* eslint-disable @typescript-eslint/no-shadow */
28
+ /* eslint-disable no-null/no-null */
29
+ /* eslint-disable @typescript-eslint/tslint/config */
30
+ /* eslint-disable @typescript-eslint/no-explicit-any */
31
+
32
+ import { injectable, inject, named, postConstruct } from '@theia/core/shared/inversify';
33
+ import URI from '@theia/core/lib/common/uri';
34
+ import { timeout, Deferred } from '@theia/core/lib/common/promise-util';
35
+ import { CancellationToken, CancellationTokenSource } from '@theia/core/lib/common/cancellation';
36
+ import { Disposable, DisposableCollection } from '@theia/core/lib/common/disposable';
37
+ import { WaitUntilEvent, Emitter, AsyncEmitter, Event } from '@theia/core/lib/common/event';
38
+ import { ContributionProvider } from '@theia/core/lib/common/contribution-provider';
39
+ import { TernarySearchTree } from '@theia/core/lib/common/ternary-search-tree';
40
+ import {
41
+ ensureFileSystemProviderError, etag, ETAG_DISABLED,
42
+ FileChangesEvent,
43
+ FileOperation, FileOperationError,
44
+ FileOperationEvent, FileOperationResult, FileSystemProviderCapabilities,
45
+ FileSystemProviderErrorCode, FileType, hasFileFolderCopyCapability, hasOpenReadWriteCloseCapability, hasReadWriteCapability,
46
+ CreateFileOptions, FileContent, FileStat, FileStatWithMetadata,
47
+ FileStreamContent, FileSystemProvider,
48
+ FileSystemProviderWithFileReadWriteCapability, FileSystemProviderWithOpenReadWriteCloseCapability,
49
+ ReadFileOptions, ResolveFileOptions, ResolveMetadataFileOptions,
50
+ Stat, WatchOptions, WriteFileOptions,
51
+ toFileOperationResult, toFileSystemProviderErrorCode,
52
+ ResolveFileResult, ResolveFileResultWithMetadata,
53
+ MoveFileOptions, CopyFileOptions, BaseStatWithMetadata, FileDeleteOptions, FileOperationOptions, hasAccessCapability, hasUpdateCapability,
54
+ hasFileReadStreamCapability, FileSystemProviderWithFileReadStreamCapability
55
+ } from '../common/files';
56
+ import { BinaryBuffer, BinaryBufferReadable, BinaryBufferReadableStream, BinaryBufferReadableBufferedStream, BinaryBufferWriteableStream } from '@theia/core/lib/common/buffer';
57
+ import { ReadableStream, isReadableStream, isReadableBufferedStream, transform, consumeStream, peekStream, peekReadable, Readable } from '@theia/core/lib/common/stream';
58
+ import { LabelProvider } from '@theia/core/lib/browser/label-provider';
59
+ import { FileSystemPreferences } from './filesystem-preferences';
60
+ import { ProgressService } from '@theia/core/lib/common/progress-service';
61
+ import { DelegatingFileSystemProvider } from '../common/delegating-file-system-provider';
62
+ import type { TextDocumentContentChangeEvent } from '@theia/core/shared/vscode-languageserver-protocol';
63
+ import { EncodingRegistry } from '@theia/core/lib/browser/encoding-registry';
64
+ import { UTF8, UTF8_with_bom } from '@theia/core/lib/common/encodings';
65
+ import { EncodingService, ResourceEncoding, DecodeStreamResult } from '@theia/core/lib/common/encoding-service';
66
+ import { Mutable } from '@theia/core/lib/common/types';
67
+ import { readFileIntoStream } from '../common/io';
68
+ import { FileSystemWatcherErrorHandler } from './filesystem-watcher-error-handler';
69
+ import { FileSystemUtils } from '../common/filesystem-utils';
70
+ import { nls } from '@theia/core';
71
+
72
+ export interface FileOperationParticipant {
73
+
74
+ /**
75
+ * Participate in a file operation of a working copy. Allows to
76
+ * change the working copy before it is being saved to disk.
77
+ */
78
+ participate(
79
+ target: URI,
80
+ source: URI | undefined,
81
+ operation: FileOperation,
82
+ timeout: number,
83
+ token: CancellationToken
84
+ ): Promise<void>;
85
+ }
86
+
87
+ export interface ReadEncodingOptions {
88
+
89
+ /**
90
+ * The optional encoding parameter allows to specify the desired encoding when resolving
91
+ * the contents of the file.
92
+ */
93
+ encoding?: string;
94
+
95
+ /**
96
+ * The optional guessEncoding parameter allows to guess encoding from content of the file.
97
+ */
98
+ autoGuessEncoding?: boolean;
99
+ }
100
+
101
+ export interface WriteEncodingOptions {
102
+
103
+ /**
104
+ * The encoding to use when updating a file.
105
+ */
106
+ encoding?: string;
107
+
108
+ /**
109
+ * If set to true, will enforce the selected encoding and not perform any detection using BOMs.
110
+ */
111
+ overwriteEncoding?: boolean;
112
+ }
113
+
114
+ export interface ReadTextFileOptions extends ReadEncodingOptions, ReadFileOptions {
115
+ /**
116
+ * The optional acceptTextOnly parameter allows to fail this request early if the file
117
+ * contents are not textual.
118
+ */
119
+ acceptTextOnly?: boolean;
120
+ }
121
+
122
+ interface BaseTextFileContent extends BaseStatWithMetadata {
123
+
124
+ /**
125
+ * The encoding of the content if known.
126
+ */
127
+ encoding: string;
128
+ }
129
+
130
+ export interface TextFileContent extends BaseTextFileContent {
131
+
132
+ /**
133
+ * The content of a text file.
134
+ */
135
+ value: string;
136
+ }
137
+
138
+ export interface TextFileStreamContent extends BaseTextFileContent {
139
+
140
+ /**
141
+ * The line grouped content of a text file.
142
+ */
143
+ value: ReadableStream<string>;
144
+ }
145
+
146
+ export interface CreateTextFileOptions extends WriteEncodingOptions, CreateFileOptions { }
147
+
148
+ export interface WriteTextFileOptions extends WriteEncodingOptions, WriteFileOptions { }
149
+
150
+ export interface UpdateTextFileOptions extends WriteEncodingOptions, WriteFileOptions {
151
+ readEncoding: string
152
+ }
153
+
154
+ export interface UserFileOperationEvent extends WaitUntilEvent {
155
+
156
+ /**
157
+ * An identifier to correlate the operation through the
158
+ * different event types (before, after, error).
159
+ */
160
+ readonly correlationId: number;
161
+
162
+ /**
163
+ * The file operation that is taking place.
164
+ */
165
+ readonly operation: FileOperation;
166
+
167
+ /**
168
+ * The resource the event is about.
169
+ */
170
+ readonly target: URI;
171
+
172
+ /**
173
+ * A property that is defined for move operations.
174
+ */
175
+ readonly source?: URI;
176
+ }
177
+
178
+ export const FileServiceContribution = Symbol('FileServiceContribution');
179
+
180
+ /**
181
+ * A {@link FileServiceContribution} can be used to add custom {@link FileSystemProvider}s.
182
+ * For this, the contribution has to listen to the {@link FileSystemProviderActivationEvent} and register
183
+ * the custom {@link FileSystemProvider}s according to the scheme when this event is fired.
184
+ *
185
+ * ### Example usage
186
+ * ```ts
187
+ * export class MyFileServiceContribution implements FileServiceContribution {
188
+ * registerFileSystemProviders(service: FileService): void {
189
+ * service.onWillActivateFileSystemProvider(event => {
190
+ * if (event.scheme === 'mySyncProviderScheme') {
191
+ * service.registerProvider('mySyncProviderScheme', this.mySyncProvider);
192
+ * }
193
+ * if (event.scheme === 'myAsyncProviderScheme') {
194
+ * event.waitUntil((async () => {
195
+ * const myAsyncProvider = await this.createAsyncProvider();
196
+ * service.registerProvider('myAsyncProviderScheme', myAsyncProvider);
197
+ * })());
198
+ * }
199
+ * });
200
+ *
201
+ * }
202
+ *```
203
+ */
204
+ export interface FileServiceContribution {
205
+ /**
206
+ * Register custom file system providers for the given {@link FileService}.
207
+ * @param service The file service for which the providers should be registered.
208
+ */
209
+ registerFileSystemProviders(service: FileService): void;
210
+ }
211
+
212
+ /**
213
+ * Represents the `FileSystemProviderRegistration` event.
214
+ * This event is fired by the {@link FileService} if a {@link FileSystemProvider} is
215
+ * registered to or unregistered from the service.
216
+ */
217
+ export interface FileSystemProviderRegistrationEvent {
218
+ /** `True` if a new provider has been registered, `false` if a provider has been unregistered. */
219
+ added: boolean;
220
+ /** The (uri) scheme for which the provider was (previously) registered */
221
+ scheme: string;
222
+ /** The affected file system provider for which this event was fired. */
223
+ provider?: FileSystemProvider;
224
+ }
225
+
226
+ /**
227
+ * Represents the `FileSystemProviderCapabilitiesChange` event.
228
+ * This event is fired by the {@link FileService} if the capabilities of one of its managed
229
+ * {@link FileSystemProvider}s have changed.
230
+ */
231
+ export interface FileSystemProviderCapabilitiesChangeEvent {
232
+ /** The affected file system provider for which this event was fired. */
233
+ provider: FileSystemProvider;
234
+ /** The (uri) scheme for which the provider is registered */
235
+ scheme: string;
236
+ }
237
+
238
+ /**
239
+ * Represents the `FileSystemProviderActivation` event.
240
+ * This event is fired by the {@link FileService} if it wants to activate the
241
+ * {@link FileSystemProvider} for a specific scheme.
242
+ */
243
+ export interface FileSystemProviderActivationEvent extends WaitUntilEvent {
244
+ /** The (uri) scheme for which the provider should be activated */
245
+ scheme: string;
246
+ }
247
+
248
+ export const enum TextFileOperationResult {
249
+ FILE_IS_BINARY
250
+ }
251
+
252
+ export class TextFileOperationError extends FileOperationError {
253
+
254
+ constructor(
255
+ message: string,
256
+ public textFileOperationResult: TextFileOperationResult,
257
+ override options?: ReadTextFileOptions & WriteTextFileOptions
258
+ ) {
259
+ super(message, FileOperationResult.FILE_OTHER_ERROR);
260
+ Object.setPrototypeOf(this, TextFileOperationError.prototype);
261
+ }
262
+
263
+ }
264
+
265
+ /**
266
+ * The {@link FileService} is the common facade responsible for all interactions with file systems.
267
+ * It manages all registered {@link FileSystemProvider}s and
268
+ * forwards calls to the responsible {@link FileSystemProvider}, determined by the scheme.
269
+ * For additional documentation regarding the provided functions see also {@link FileSystemProvider}.
270
+ */
271
+ @injectable()
272
+ export class FileService {
273
+
274
+ private readonly BUFFER_SIZE = 64 * 1024;
275
+
276
+ @inject(LabelProvider)
277
+ protected readonly labelProvider: LabelProvider;
278
+
279
+ @inject(FileSystemPreferences)
280
+ protected readonly preferences: FileSystemPreferences;
281
+
282
+ @inject(ProgressService)
283
+ protected readonly progressService: ProgressService;
284
+
285
+ @inject(EncodingRegistry)
286
+ protected readonly encodingRegistry: EncodingRegistry;
287
+
288
+ @inject(EncodingService)
289
+ protected readonly encodingService: EncodingService;
290
+
291
+ @inject(ContributionProvider) @named(FileServiceContribution)
292
+ protected readonly contributions: ContributionProvider<FileServiceContribution>;
293
+
294
+ @inject(FileSystemWatcherErrorHandler)
295
+ protected readonly watcherErrorHandler: FileSystemWatcherErrorHandler;
296
+
297
+ @postConstruct()
298
+ protected init(): void {
299
+ for (const contribution of this.contributions.getContributions()) {
300
+ contribution.registerFileSystemProviders(this);
301
+ }
302
+ }
303
+
304
+ // #region Events
305
+
306
+ private correlationIds = 0;
307
+
308
+ private readonly onWillRunUserOperationEmitter = new AsyncEmitter<UserFileOperationEvent>();
309
+ /**
310
+ * An event that is emitted when file operation is being performed.
311
+ * This event is triggered by user gestures.
312
+ */
313
+ readonly onWillRunUserOperation = this.onWillRunUserOperationEmitter.event;
314
+
315
+ private readonly onDidFailUserOperationEmitter = new AsyncEmitter<UserFileOperationEvent>();
316
+ /**
317
+ * An event that is emitted when file operation is failed.
318
+ * This event is triggered by user gestures.
319
+ */
320
+ readonly onDidFailUserOperation = this.onDidFailUserOperationEmitter.event;
321
+
322
+ private readonly onDidRunUserOperationEmitter = new AsyncEmitter<UserFileOperationEvent>();
323
+ /**
324
+ * An event that is emitted when file operation is finished.
325
+ * This event is triggered by user gestures.
326
+ */
327
+ readonly onDidRunUserOperation = this.onDidRunUserOperationEmitter.event;
328
+
329
+ // #endregion
330
+
331
+ // #region File System Provider
332
+
333
+ private onDidChangeFileSystemProviderRegistrationsEmitter = new Emitter<FileSystemProviderRegistrationEvent>();
334
+ readonly onDidChangeFileSystemProviderRegistrations = this.onDidChangeFileSystemProviderRegistrationsEmitter.event;
335
+
336
+ private onWillActivateFileSystemProviderEmitter = new Emitter<FileSystemProviderActivationEvent>();
337
+ /**
338
+ * See `FileServiceContribution.registerProviders`.
339
+ */
340
+ readonly onWillActivateFileSystemProvider = this.onWillActivateFileSystemProviderEmitter.event;
341
+
342
+ private onDidChangeFileSystemProviderCapabilitiesEmitter = new Emitter<FileSystemProviderCapabilitiesChangeEvent>();
343
+ readonly onDidChangeFileSystemProviderCapabilities = this.onDidChangeFileSystemProviderCapabilitiesEmitter.event;
344
+
345
+ private readonly providers = new Map<string, FileSystemProvider>();
346
+ private readonly activations = new Map<string, Promise<FileSystemProvider>>();
347
+
348
+ /**
349
+ * Registers a new {@link FileSystemProvider} for the given scheme.
350
+ * @param scheme The (uri) scheme for which the provider should be registered.
351
+ * @param provider The file system provider that should be registered.
352
+ *
353
+ * @returns A `Disposable` that can be invoked to unregister the given provider.
354
+ */
355
+ registerProvider(scheme: string, provider: FileSystemProvider): Disposable {
356
+ if (this.providers.has(scheme)) {
357
+ throw new Error(`A filesystem provider for the scheme '${scheme}' is already registered.`);
358
+ }
359
+
360
+ this.providers.set(scheme, provider);
361
+ this.onDidChangeFileSystemProviderRegistrationsEmitter.fire({ added: true, scheme, provider });
362
+
363
+ const providerDisposables = new DisposableCollection();
364
+ providerDisposables.push(provider.onDidChangeFile(changes => this.onDidFilesChangeEmitter.fire(new FileChangesEvent(changes))));
365
+ providerDisposables.push(provider.onFileWatchError(() => this.handleFileWatchError()));
366
+ providerDisposables.push(provider.onDidChangeCapabilities(() => this.onDidChangeFileSystemProviderCapabilitiesEmitter.fire({ provider, scheme })));
367
+
368
+ return Disposable.create(() => {
369
+ this.onDidChangeFileSystemProviderRegistrationsEmitter.fire({ added: false, scheme, provider });
370
+ this.providers.delete(scheme);
371
+
372
+ providerDisposables.dispose();
373
+ });
374
+ }
375
+
376
+ /**
377
+ * Try to activate the registered provider for the given scheme
378
+ * @param scheme The uri scheme for which the responsible provider should be activated.
379
+ *
380
+ * @returns A promise of the activated file system provider. Only resolves if a provider is available for this scheme, gets rejected otherwise.
381
+ */
382
+ async activateProvider(scheme: string): Promise<FileSystemProvider> {
383
+ let provider = this.providers.get(scheme);
384
+ if (provider) {
385
+ return provider;
386
+ }
387
+ let activation = this.activations.get(scheme);
388
+ if (!activation) {
389
+ const deferredActivation = new Deferred<FileSystemProvider>();
390
+ this.activations.set(scheme, activation = deferredActivation.promise);
391
+ WaitUntilEvent.fire(this.onWillActivateFileSystemProviderEmitter, { scheme }).then(() => {
392
+ provider = this.providers.get(scheme);
393
+ if (!provider) {
394
+ const error = new Error();
395
+ error.name = 'ENOPRO';
396
+ error.message = `No file system provider found for scheme ${scheme}`;
397
+ throw error;
398
+ } else {
399
+ deferredActivation.resolve(provider);
400
+ }
401
+ }).catch(e => deferredActivation.reject(e));
402
+ }
403
+ return activation;
404
+ }
405
+
406
+ /**
407
+ * Tests if the service (i.e. any of its registered {@link FileSystemProvider}s) can handle the given resource.
408
+ * @param resource `URI` of the resource to test.
409
+ *
410
+ * @returns `true` if the resource can be handled, `false` otherwise.
411
+ */
412
+ canHandleResource(resource: URI): boolean {
413
+ return this.providers.has(resource.scheme);
414
+ }
415
+
416
+ /**
417
+ * Tests if the service (i.e the {@link FileSystemProvider} registered for the given uri scheme) provides the given capability.
418
+ * @param resource `URI` of the resource to test.
419
+ * @param capability The required capability.
420
+ *
421
+ * @returns `true` if the resource can be handled and the required capability can be provided.
422
+ */
423
+ hasCapability(resource: URI, capability: FileSystemProviderCapabilities): boolean {
424
+ const provider = this.providers.get(resource.scheme);
425
+
426
+ return !!(provider && (provider.capabilities & capability));
427
+ }
428
+
429
+ /**
430
+ * List the schemes and capabilities for registered file system providers
431
+ */
432
+ listCapabilities(): { scheme: string; capabilities: FileSystemProviderCapabilities }[] {
433
+ return Array.from(this.providers.entries()).map(([scheme, provider]) => ({
434
+ scheme,
435
+ capabilities: provider.capabilities
436
+ }));
437
+ }
438
+
439
+ protected async withProvider(resource: URI): Promise<FileSystemProvider> {
440
+ // Assert path is absolute
441
+ if (!resource.path.isAbsolute) {
442
+ throw new FileOperationError(nls.localizeByDefault("Unable to resolve filesystem provider with relative file path '{0}'", this.resourceForError(resource)), FileOperationResult.FILE_INVALID_PATH);
443
+ }
444
+
445
+ return this.activateProvider(resource.scheme);
446
+ }
447
+
448
+ private async withReadProvider(resource: URI): Promise<FileSystemProviderWithFileReadWriteCapability | FileSystemProviderWithOpenReadWriteCloseCapability> {
449
+ const provider = await this.withProvider(resource);
450
+
451
+ if (hasOpenReadWriteCloseCapability(provider) || hasReadWriteCapability(provider)) {
452
+ return provider;
453
+ }
454
+
455
+ throw new Error(`Filesystem provider for scheme '${resource.scheme}' neither has FileReadWrite, FileReadStream nor FileOpenReadWriteClose capability which is needed for the read operation.`);
456
+ }
457
+
458
+ private async withWriteProvider(resource: URI): Promise<FileSystemProviderWithFileReadWriteCapability | FileSystemProviderWithOpenReadWriteCloseCapability> {
459
+ const provider = await this.withProvider(resource);
460
+ if (hasOpenReadWriteCloseCapability(provider) || hasReadWriteCapability(provider)) {
461
+ return provider;
462
+ }
463
+
464
+ throw new Error(`Filesystem provider for scheme '${resource.scheme}' neither has FileReadWrite nor FileOpenReadWriteClose capability which is needed for the write operation.`);
465
+ }
466
+
467
+ // #endregion
468
+
469
+ private onDidRunOperationEmitter = new Emitter<FileOperationEvent>();
470
+ /**
471
+ * An event that is emitted when operation is finished.
472
+ * This event is triggered by user gestures and programmatically.
473
+ */
474
+ readonly onDidRunOperation = this.onDidRunOperationEmitter.event;
475
+
476
+ /**
477
+ * Try to resolve file information and metadata for the given resource.
478
+ * @param resource `URI` of the resource that should be resolved.
479
+ * @param options Options to customize the resolvement process.
480
+ *
481
+ * @return A promise that resolves if the resource could be successfully resolved.
482
+ */
483
+ resolve(resource: URI, options: ResolveMetadataFileOptions): Promise<FileStatWithMetadata>;
484
+ resolve(resource: URI, options?: ResolveFileOptions | undefined): Promise<FileStat>;
485
+ async resolve(resource: any, options?: any) {
486
+ try {
487
+ return await this.doResolveFile(resource, options);
488
+ } catch (error) {
489
+
490
+ // Specially handle file not found case as file operation result
491
+ if (toFileSystemProviderErrorCode(error) === FileSystemProviderErrorCode.FileNotFound) {
492
+ throw new FileOperationError(nls.localizeByDefault("Unable to resolve nonexistent file '{0}'", this.resourceForError(resource)), FileOperationResult.FILE_NOT_FOUND);
493
+ }
494
+
495
+ // Bubble up any other error as is
496
+ throw ensureFileSystemProviderError(error);
497
+ }
498
+ }
499
+
500
+ private async doResolveFile(resource: URI, options: ResolveMetadataFileOptions): Promise<FileStatWithMetadata>;
501
+ private async doResolveFile(resource: URI, options?: ResolveFileOptions): Promise<FileStat>;
502
+ private async doResolveFile(resource: URI, options?: ResolveFileOptions): Promise<FileStat> {
503
+ const provider = await this.withProvider(resource);
504
+
505
+ const resolveTo = options?.resolveTo;
506
+ const resolveSingleChildDescendants = options?.resolveSingleChildDescendants;
507
+ const resolveMetadata = options?.resolveMetadata;
508
+
509
+ const stat = await provider.stat(resource);
510
+
511
+ let trie: TernarySearchTree<URI, boolean> | undefined;
512
+
513
+ return this.toFileStat(provider, resource, stat, undefined, !!resolveMetadata, (stat, siblings) => {
514
+
515
+ // lazy trie to check for recursive resolving
516
+ if (!trie) {
517
+ trie = TernarySearchTree.forUris<true>(!!(provider.capabilities & FileSystemProviderCapabilities.PathCaseSensitive));
518
+ trie.set(resource, true);
519
+ if (Array.isArray(resolveTo) && resolveTo.length) {
520
+ resolveTo.forEach(uri => trie!.set(uri, true));
521
+ }
522
+ }
523
+
524
+ // check for recursive resolving
525
+ if (Boolean(trie.findSuperstr(stat.resource) || trie.get(stat.resource))) {
526
+ return true;
527
+ }
528
+
529
+ // check for resolving single child folders
530
+ if (stat.isDirectory && resolveSingleChildDescendants) {
531
+ return siblings === 1;
532
+ }
533
+
534
+ return false;
535
+ });
536
+ }
537
+
538
+ private async toFileStat(provider: FileSystemProvider, resource: URI, stat: Stat | { type: FileType } & Partial<Stat>, siblings: number | undefined, resolveMetadata: boolean, recurse: (stat: FileStat, siblings?: number) => boolean): Promise<FileStat>;
539
+ private async toFileStat(provider: FileSystemProvider, resource: URI, stat: Stat, siblings: number | undefined, resolveMetadata: true, recurse: (stat: FileStat, siblings?: number) => boolean): Promise<FileStatWithMetadata>;
540
+ private async toFileStat(provider: FileSystemProvider, resource: URI, stat: Stat | { type: FileType } & Partial<Stat>, siblings: number | undefined, resolveMetadata: boolean, recurse: (stat: FileStat, siblings?: number) => boolean): Promise<FileStat> {
541
+ const fileStat = FileStat.fromStat(resource, stat);
542
+
543
+ // check to recurse for directories
544
+ if (fileStat.isDirectory && recurse(fileStat, siblings)) {
545
+ try {
546
+ const entries = await provider.readdir(resource);
547
+ const resolvedEntries = await Promise.all(entries.map(async ([name, type]) => {
548
+ try {
549
+ const childResource = resource.resolve(name);
550
+ const childStat = resolveMetadata ? await provider.stat(childResource) : { type };
551
+
552
+ return await this.toFileStat(provider, childResource, childStat, entries.length, resolveMetadata, recurse);
553
+ } catch (error) {
554
+ console.trace(error);
555
+
556
+ return null; // can happen e.g. due to permission errors
557
+ }
558
+ }));
559
+
560
+ // make sure to get rid of null values that signal a failure to resolve a particular entry
561
+ fileStat.children = resolvedEntries.filter(e => !!e) as FileStat[];
562
+ } catch (error) {
563
+ console.trace(error);
564
+
565
+ fileStat.children = []; // gracefully handle errors, we may not have permissions to read
566
+ }
567
+
568
+ return fileStat;
569
+ }
570
+
571
+ return fileStat;
572
+ }
573
+
574
+ /**
575
+ * Try to resolve file information and metadata for all given resource.
576
+ * @param toResolve An array of all the resources (and corresponding resolvement options) that should be resolved.
577
+ *
578
+ * @returns A promise of all resolved resources. The promise is not rejected if any of the given resources cannot be resolved.
579
+ * Instead this is reflected with the `success` flag of the corresponding {@link ResolveFileResult}.
580
+ */
581
+ async resolveAll(toResolve: { resource: URI, options?: ResolveFileOptions }[]): Promise<ResolveFileResult[]>;
582
+ async resolveAll(toResolve: { resource: URI, options: ResolveMetadataFileOptions }[]): Promise<ResolveFileResultWithMetadata[]>;
583
+ async resolveAll(toResolve: { resource: URI; options?: ResolveFileOptions; }[]): Promise<ResolveFileResult[]> {
584
+ return Promise.all(toResolve.map(async entry => {
585
+ try {
586
+ return { stat: await this.doResolveFile(entry.resource, entry.options), success: true };
587
+ } catch (error) {
588
+ console.trace(error);
589
+
590
+ return { stat: undefined, success: false };
591
+ }
592
+ }));
593
+ }
594
+
595
+ /**
596
+ * Tests if the given resource exists in the filesystem.
597
+ * @param resource `URI` of the resource which should be tested.
598
+ * @throws Will throw an error if no {@link FileSystemProvider} is registered for the given resource.
599
+ *
600
+ * @returns A promise that resolves to `true` if the resource exists.
601
+ */
602
+ async exists(resource: URI): Promise<boolean> {
603
+ const provider = await this.withProvider(resource);
604
+
605
+ try {
606
+ const stat = await provider.stat(resource);
607
+
608
+ return !!stat;
609
+ } catch (error) {
610
+ return false;
611
+ }
612
+ }
613
+
614
+ /**
615
+ * Tests a user's permissions for the given resource.
616
+ * @param resource `URI` of the resource which should be tested.
617
+ * @param mode An optional integer that specifies the accessibility checks to be performed.
618
+ * Check `FileAccess.Constants` for possible values of mode.
619
+ * It is possible to create a mask consisting of the bitwise `OR` of two or more values (e.g. FileAccess.Constants.W_OK | FileAccess.Constants.R_OK).
620
+ * If `mode` is not defined, `FileAccess.Constants.F_OK` will be used instead.
621
+ */
622
+ async access(resource: URI, mode?: number): Promise<boolean> {
623
+ const provider = await this.withProvider(resource);
624
+
625
+ if (!hasAccessCapability(provider)) {
626
+ return false;
627
+ }
628
+ try {
629
+ await provider.access(resource, mode);
630
+ return true;
631
+ } catch (error) {
632
+ return false;
633
+ }
634
+ }
635
+
636
+ /**
637
+ * Resolves the fs path of the given URI.
638
+ *
639
+ * USE WITH CAUTION: You should always prefer URIs to paths if possible, as they are
640
+ * portable and platform independent. Paths should only be used in cases you directly
641
+ * interact with the OS, e.g. when running a command on the shell.
642
+ *
643
+ * If you need to display human readable simple or long names then use `LabelProvider` instead.
644
+ * @param resource `URI` of the resource that should be resolved.
645
+ * @throws Will throw an error if no {@link FileSystemProvider} is registered for the given resource.
646
+ *
647
+ * @returns A promise of the resolved fs path.
648
+ */
649
+ async fsPath(resource: URI): Promise<string> {
650
+ const provider = await this.withProvider(resource);
651
+
652
+ if (!hasAccessCapability(provider)) {
653
+ return resource.path.toString();
654
+ }
655
+ return provider.fsPath(resource);
656
+ }
657
+
658
+ // #region Text File Reading/Writing
659
+
660
+ async create(resource: URI, value?: string | Readable<string>, options?: CreateTextFileOptions): Promise<FileStatWithMetadata> {
661
+ if (options?.fromUserGesture === false) {
662
+ return this.doCreate(resource, value, options);
663
+ }
664
+ await this.runFileOperationParticipants(resource, undefined, FileOperation.CREATE);
665
+
666
+ const event = { correlationId: this.correlationIds++, operation: FileOperation.CREATE, target: resource };
667
+ await this.onWillRunUserOperationEmitter.fire(event);
668
+
669
+ let stat: FileStatWithMetadata;
670
+ try {
671
+ stat = await this.doCreate(resource, value, options);
672
+ } catch (error) {
673
+ await this.onDidFailUserOperationEmitter.fire(event);
674
+ throw error;
675
+ }
676
+
677
+ await this.onDidRunUserOperationEmitter.fire(event);
678
+
679
+ return stat;
680
+ }
681
+
682
+ protected async doCreate(resource: URI, value?: string | Readable<string>, options?: CreateTextFileOptions): Promise<FileStatWithMetadata> {
683
+ const encoding = await this.getWriteEncoding(resource, options);
684
+ const encoded = await this.encodingService.encodeStream(value, encoding);
685
+ return this.createFile(resource, encoded, options);
686
+ }
687
+
688
+ async write(resource: URI, value: string | Readable<string>, options?: WriteTextFileOptions): Promise<FileStatWithMetadata & { encoding: string }> {
689
+ const encoding = await this.getWriteEncoding(resource, options);
690
+ const encoded = await this.encodingService.encodeStream(value, encoding);
691
+ return Object.assign(await this.writeFile(resource, encoded, options), { encoding: encoding.encoding });
692
+ }
693
+
694
+ async read(resource: URI, options?: ReadTextFileOptions): Promise<TextFileContent> {
695
+ const [bufferStream, decoder] = await this.doRead(resource, {
696
+ ...options,
697
+ preferUnbuffered: this.shouldReadUnbuffered(options)
698
+ });
699
+
700
+ return {
701
+ ...bufferStream,
702
+ encoding: decoder.detected.encoding || UTF8,
703
+ value: await consumeStream(decoder.stream, strings => strings.join(''))
704
+ };
705
+ }
706
+
707
+ async readStream(resource: URI, options?: ReadTextFileOptions): Promise<TextFileStreamContent> {
708
+ const [bufferStream, decoder] = await this.doRead(resource, options);
709
+
710
+ return {
711
+ ...bufferStream,
712
+ encoding: decoder.detected.encoding || UTF8,
713
+ value: decoder.stream
714
+ };
715
+ }
716
+
717
+ private async doRead(resource: URI, options?: ReadTextFileOptions & { preferUnbuffered?: boolean }): Promise<[FileStreamContent, DecodeStreamResult]> {
718
+ options = this.resolveReadOptions(options);
719
+
720
+ // read stream raw (either buffered or unbuffered)
721
+ let bufferStream: FileStreamContent;
722
+ if (options?.preferUnbuffered) {
723
+ const content = await this.readFile(resource, options);
724
+ bufferStream = {
725
+ ...content,
726
+ value: BinaryBufferReadableStream.fromBuffer(content.value)
727
+ };
728
+ } else {
729
+ bufferStream = await this.readFileStream(resource, options);
730
+ }
731
+
732
+ const decoder = await this.encodingService.decodeStream(bufferStream.value, {
733
+ guessEncoding: options.autoGuessEncoding,
734
+ overwriteEncoding: detectedEncoding => this.getReadEncoding(resource, options, detectedEncoding)
735
+ });
736
+
737
+ // validate binary
738
+ if (options?.acceptTextOnly && decoder.detected.seemsBinary) {
739
+ throw new TextFileOperationError(nls.localizeByDefault('File seems to be binary and cannot be opened as text'), TextFileOperationResult.FILE_IS_BINARY, options);
740
+ }
741
+
742
+ return [bufferStream, decoder];
743
+ }
744
+
745
+ protected resolveReadOptions(options?: ReadTextFileOptions): ReadTextFileOptions {
746
+ options = {
747
+ ...options,
748
+ autoGuessEncoding: typeof options?.autoGuessEncoding === 'boolean' ? options.autoGuessEncoding : this.preferences['files.autoGuessEncoding']
749
+ };
750
+ const limits: Mutable<ReadTextFileOptions['limits']> = options.limits = options.limits || {};
751
+ if (typeof limits.size !== 'number') {
752
+ limits.size = this.preferences['files.maxFileSizeMB'] * 1024 * 1024;
753
+ }
754
+ return options;
755
+ }
756
+
757
+ async update(resource: URI, changes: TextDocumentContentChangeEvent[], options: UpdateTextFileOptions): Promise<FileStatWithMetadata & { encoding: string }> {
758
+ const provider = this.throwIfFileSystemIsReadonly(await this.withWriteProvider(resource), resource);
759
+ try {
760
+ await this.validateWriteFile(provider, resource, options);
761
+ if (hasUpdateCapability(provider)) {
762
+ const encoding = await this.getEncodingForResource(resource, options ? options.encoding : undefined);;
763
+ const stat = await provider.updateFile(resource, changes, {
764
+ readEncoding: options.readEncoding,
765
+ writeEncoding: encoding,
766
+ overwriteEncoding: options.overwriteEncoding || false
767
+ });
768
+ return Object.assign(FileStat.fromStat(resource, stat), { encoding: stat.encoding });
769
+ } else {
770
+ throw new Error('incremental file update is not supported');
771
+ }
772
+ } catch (error) {
773
+ this.rethrowAsFileOperationError("Unable to write file '{0}' ({1})", resource, error, options);
774
+ }
775
+ }
776
+
777
+ // #endregion
778
+
779
+ // #region File Reading/Writing
780
+
781
+ async createFile(resource: URI, bufferOrReadableOrStream: BinaryBuffer | BinaryBufferReadable | BinaryBufferReadableStream = BinaryBuffer.fromString(''), options?: CreateFileOptions): Promise<FileStatWithMetadata> {
782
+
783
+ // validate overwrite
784
+ if (!options?.overwrite && await this.exists(resource)) {
785
+ throw new FileOperationError(nls.localizeByDefault("Unable to create file '{0}' that already exists when overwrite flag is not set", this.resourceForError(resource)), FileOperationResult.FILE_MODIFIED_SINCE, options);
786
+ }
787
+
788
+ // do write into file (this will create it too)
789
+ const fileStat = await this.writeFile(resource, bufferOrReadableOrStream);
790
+
791
+ // events
792
+ this.onDidRunOperationEmitter.fire(new FileOperationEvent(resource, FileOperation.CREATE, fileStat));
793
+
794
+ return fileStat;
795
+ }
796
+
797
+ async writeFile(resource: URI, bufferOrReadableOrStream: BinaryBuffer | BinaryBufferReadable | BinaryBufferReadableStream, options?: WriteFileOptions): Promise<FileStatWithMetadata> {
798
+ const provider = this.throwIfFileSystemIsReadonly(await this.withWriteProvider(resource), resource);
799
+
800
+ try {
801
+
802
+ // validate write
803
+ const stat = await this.validateWriteFile(provider, resource, options);
804
+
805
+ // mkdir recursively as needed
806
+ if (!stat) {
807
+ await this.mkdirp(provider, resource.parent);
808
+ }
809
+
810
+ // optimization: if the provider has unbuffered write capability and the data
811
+ // to write is a Readable, we consume up to 3 chunks and try to write the data
812
+ // unbuffered to reduce the overhead. If the Readable has more data to provide
813
+ // we continue to write buffered.
814
+ let bufferOrReadableOrStreamOrBufferedStream: BinaryBuffer | BinaryBufferReadable | BinaryBufferReadableStream | BinaryBufferReadableBufferedStream;
815
+ if (hasReadWriteCapability(provider) && !(bufferOrReadableOrStream instanceof BinaryBuffer)) {
816
+ if (isReadableStream(bufferOrReadableOrStream)) {
817
+ const bufferedStream = await peekStream(bufferOrReadableOrStream, 3);
818
+ if (bufferedStream.ended) {
819
+ bufferOrReadableOrStreamOrBufferedStream = BinaryBuffer.concat(bufferedStream.buffer);
820
+ } else {
821
+ bufferOrReadableOrStreamOrBufferedStream = bufferedStream;
822
+ }
823
+ } else {
824
+ bufferOrReadableOrStreamOrBufferedStream = peekReadable(bufferOrReadableOrStream, data => BinaryBuffer.concat(data), 3);
825
+ }
826
+ } else {
827
+ bufferOrReadableOrStreamOrBufferedStream = bufferOrReadableOrStream;
828
+ }
829
+
830
+ // write file: unbuffered (only if data to write is a buffer, or the provider has no buffered write capability)
831
+ if (!hasOpenReadWriteCloseCapability(provider) || (hasReadWriteCapability(provider) && bufferOrReadableOrStreamOrBufferedStream instanceof BinaryBuffer)) {
832
+ await this.doWriteUnbuffered(provider, resource, bufferOrReadableOrStreamOrBufferedStream);
833
+ }
834
+
835
+ // write file: buffered
836
+ else {
837
+ await this.doWriteBuffered(provider, resource, bufferOrReadableOrStreamOrBufferedStream instanceof BinaryBuffer ? BinaryBufferReadable.fromBuffer(bufferOrReadableOrStreamOrBufferedStream) : bufferOrReadableOrStreamOrBufferedStream);
838
+ }
839
+ } catch (error) {
840
+ this.rethrowAsFileOperationError("Unable to write file '{0}' ({1})", resource, error, options);
841
+ }
842
+
843
+ return this.resolve(resource, { resolveMetadata: true });
844
+ }
845
+
846
+ private async validateWriteFile(provider: FileSystemProvider, resource: URI, options?: WriteFileOptions): Promise<Stat | undefined> {
847
+ let stat: Stat | undefined = undefined;
848
+ try {
849
+ stat = await provider.stat(resource);
850
+ } catch (error) {
851
+ return undefined; // file might not exist
852
+ }
853
+
854
+ // file cannot be directory
855
+ if ((stat.type & FileType.Directory) !== 0) {
856
+ throw new FileOperationError(nls.localizeByDefault("Unable to write file '{0}' that is actually a directory", this.resourceForError(resource)), FileOperationResult.FILE_IS_DIRECTORY, options);
857
+ }
858
+
859
+ if (this.modifiedSince(stat, options)) {
860
+ throw new FileOperationError(nls.localizeByDefault('File Modified Since'), FileOperationResult.FILE_MODIFIED_SINCE, options);
861
+ }
862
+
863
+ return stat;
864
+ }
865
+
866
+ /**
867
+ * Dirty write prevention: if the file on disk has been changed and does not match our expected
868
+ * mtime and etag, we bail out to prevent dirty writing.
869
+ *
870
+ * First, we check for a mtime that is in the future before we do more checks. The assumption is
871
+ * that only the mtime is an indicator for a file that has changed on disk.
872
+ *
873
+ * Second, if the mtime has advanced, we compare the size of the file on disk with our previous
874
+ * one using the etag() function. Relying only on the mtime check has proven to produce false
875
+ * positives due to file system weirdness (especially around remote file systems). As such, the
876
+ * check for size is a weaker check because it can return a false negative if the file has changed
877
+ * but to the same length. This is a compromise we take to avoid having to produce checksums of
878
+ * the file content for comparison which would be much slower to compute.
879
+ */
880
+ protected modifiedSince(stat: Stat, options?: WriteFileOptions): boolean {
881
+ return !!options && typeof options.mtime === 'number' && typeof options.etag === 'string' && options.etag !== ETAG_DISABLED &&
882
+ typeof stat.mtime === 'number' && typeof stat.size === 'number' &&
883
+ options.mtime < stat.mtime && options.etag !== etag({ mtime: options.mtime /* not using stat.mtime for a reason, see above */, size: stat.size });
884
+ }
885
+
886
+ protected shouldReadUnbuffered(options?: ReadFileOptions): boolean {
887
+ // optimization: since we know that the caller does not
888
+ // care about buffering, we indicate this to the reader.
889
+ // this reduces all the overhead the buffered reading
890
+ // has (open, read, close) if the provider supports
891
+ // unbuffered reading.
892
+ //
893
+ // However, if we read only part of the file we still
894
+ // want buffered reading as otherwise we need to read
895
+ // the whole file and cut out the specified part later.
896
+ return options?.position === undefined && options?.length === undefined;
897
+ }
898
+
899
+ async readFile(resource: URI, options?: ReadFileOptions): Promise<FileContent> {
900
+ const provider = await this.withReadProvider(resource);
901
+
902
+ const stream = await this.doReadAsFileStream(provider, resource, {
903
+ ...options,
904
+ preferUnbuffered: this.shouldReadUnbuffered(options)
905
+ });
906
+
907
+ return {
908
+ ...stream,
909
+ value: await BinaryBufferReadableStream.toBuffer(stream.value)
910
+ };
911
+ }
912
+
913
+ async readFileStream(resource: URI, options?: ReadFileOptions): Promise<FileStreamContent> {
914
+ const provider = await this.withReadProvider(resource);
915
+
916
+ return this.doReadAsFileStream(provider, resource, options);
917
+ }
918
+
919
+ private async doReadAsFileStream(provider: FileSystemProviderWithFileReadWriteCapability | FileSystemProviderWithOpenReadWriteCloseCapability, resource: URI, options?: ReadFileOptions & { preferUnbuffered?: boolean }): Promise<FileStreamContent> {
920
+
921
+ // install a cancellation token that gets cancelled
922
+ // when any error occurs. this allows us to resolve
923
+ // the content of the file while resolving metadata
924
+ // but still cancel the operation in certain cases.
925
+ const cancellableSource = new CancellationTokenSource();
926
+
927
+ // validate read operation
928
+ const statPromise = this.validateReadFile(resource, options).then(stat => stat, error => {
929
+ cancellableSource.cancel();
930
+
931
+ throw error;
932
+ });
933
+
934
+ try {
935
+
936
+ // if the etag is provided, we await the result of the validation
937
+ // due to the likelyhood of hitting a NOT_MODIFIED_SINCE result.
938
+ // otherwise, we let it run in parallel to the file reading for
939
+ // optimal startup performance.
940
+ if (options && typeof options.etag === 'string' && options.etag !== ETAG_DISABLED) {
941
+ await statPromise;
942
+ }
943
+
944
+ let fileStreamPromise: Promise<BinaryBufferReadableStream>;
945
+
946
+ // read unbuffered (only if either preferred, or the provider has no buffered read capability)
947
+ if (!(hasOpenReadWriteCloseCapability(provider) || hasFileReadStreamCapability(provider)) || (hasReadWriteCapability(provider) && options?.preferUnbuffered)) {
948
+ fileStreamPromise = this.readFileUnbuffered(provider, resource, options);
949
+ }
950
+
951
+ // read streamed (always prefer over primitive buffered read)
952
+ else if (hasFileReadStreamCapability(provider)) {
953
+ fileStreamPromise = Promise.resolve(this.readFileStreamed(provider, resource, cancellableSource.token, options));
954
+ }
955
+
956
+ // read buffered
957
+ else {
958
+ fileStreamPromise = Promise.resolve(this.readFileBuffered(provider, resource, cancellableSource.token, options));
959
+ }
960
+
961
+ const [fileStat, fileStream] = await Promise.all([statPromise, fileStreamPromise]);
962
+
963
+ return {
964
+ ...fileStat,
965
+ value: fileStream
966
+ };
967
+ } catch (error) {
968
+ this.rethrowAsFileOperationError("Unable to read file '{0}' ({1})", resource, error, options);
969
+ }
970
+ }
971
+
972
+ private readFileStreamed(provider: FileSystemProviderWithFileReadStreamCapability, resource: URI, token: CancellationToken, options: ReadFileOptions = Object.create(null)): BinaryBufferReadableStream {
973
+ const fileStream = provider.readFileStream(resource, options, token);
974
+
975
+ return transform(fileStream, {
976
+ data: data => data instanceof BinaryBuffer ? data : BinaryBuffer.wrap(data),
977
+ error: error => this.asFileOperationError("Unable to read file '{0}' ({1})", resource, error, options)
978
+ }, data => BinaryBuffer.concat(data));
979
+ }
980
+
981
+ private readFileBuffered(provider: FileSystemProviderWithOpenReadWriteCloseCapability, resource: URI, token: CancellationToken, options: ReadFileOptions = Object.create(null)): BinaryBufferReadableStream {
982
+ const stream = BinaryBufferWriteableStream.create();
983
+
984
+ readFileIntoStream(provider, resource, stream, data => data, {
985
+ ...options,
986
+ bufferSize: this.BUFFER_SIZE,
987
+ errorTransformer: error => this.asFileOperationError("Unable to read file '{0}' ({1})", resource, error, options)
988
+ }, token);
989
+
990
+ return stream;
991
+ }
992
+
993
+ protected rethrowAsFileOperationError(message: string, resource: URI, error: Error, options?: ReadFileOptions & WriteFileOptions & CreateFileOptions): never {
994
+ throw this.asFileOperationError(message, resource, error, options);
995
+ }
996
+ protected asFileOperationError(message: string, resource: URI, error: Error, options?: ReadFileOptions & WriteFileOptions & CreateFileOptions): FileOperationError {
997
+ const fileOperationError = new FileOperationError(nls.localizeByDefault(message, this.resourceForError(resource), ensureFileSystemProviderError(error).toString()),
998
+ toFileOperationResult(error), options);
999
+ fileOperationError.stack = `${fileOperationError.stack}\nCaused by: ${error.stack}`;
1000
+ return fileOperationError;
1001
+ }
1002
+
1003
+ private async readFileUnbuffered(provider: FileSystemProviderWithFileReadWriteCapability, resource: URI, options?: ReadFileOptions): Promise<BinaryBufferReadableStream> {
1004
+ let buffer = await provider.readFile(resource);
1005
+
1006
+ // respect position option
1007
+ if (options && typeof options.position === 'number') {
1008
+ buffer = buffer.slice(options.position);
1009
+ }
1010
+
1011
+ // respect length option
1012
+ if (options && typeof options.length === 'number') {
1013
+ buffer = buffer.slice(0, options.length);
1014
+ }
1015
+
1016
+ // Throw if file is too large to load
1017
+ this.validateReadFileLimits(resource, buffer.byteLength, options);
1018
+
1019
+ return BinaryBufferReadableStream.fromBuffer(BinaryBuffer.wrap(buffer));
1020
+ }
1021
+
1022
+ private async validateReadFile(resource: URI, options?: ReadFileOptions): Promise<FileStatWithMetadata> {
1023
+ const stat = await this.resolve(resource, { resolveMetadata: true });
1024
+
1025
+ // Throw if resource is a directory
1026
+ if (stat.isDirectory) {
1027
+ throw new FileOperationError(nls.localizeByDefault("Unable to read file '{0}' that is actually a directory", this.resourceForError(resource)), FileOperationResult.FILE_IS_DIRECTORY, options);
1028
+ }
1029
+
1030
+ // Throw if file not modified since (unless disabled)
1031
+ if (options && typeof options.etag === 'string' && options.etag !== ETAG_DISABLED && options.etag === stat.etag) {
1032
+ throw new FileOperationError(nls.localizeByDefault('File not modified since'), FileOperationResult.FILE_NOT_MODIFIED_SINCE, options);
1033
+ }
1034
+
1035
+ // Throw if file is too large to load
1036
+ this.validateReadFileLimits(resource, stat.size, options);
1037
+
1038
+ return stat;
1039
+ }
1040
+
1041
+ private validateReadFileLimits(resource: URI, size: number, options?: ReadFileOptions): void {
1042
+ if (options?.limits) {
1043
+ let tooLargeErrorResult: FileOperationResult | undefined = undefined;
1044
+
1045
+ if (typeof options.limits.memory === 'number' && size > options.limits.memory) {
1046
+ tooLargeErrorResult = FileOperationResult.FILE_EXCEEDS_MEMORY_LIMIT;
1047
+ }
1048
+
1049
+ if (typeof options.limits.size === 'number' && size > options.limits.size) {
1050
+ tooLargeErrorResult = FileOperationResult.FILE_TOO_LARGE;
1051
+ }
1052
+
1053
+ if (typeof tooLargeErrorResult === 'number') {
1054
+ throw new FileOperationError(nls.localizeByDefault("Unable to read file '{0}' that is too large to open", this.resourceForError(resource)), tooLargeErrorResult);
1055
+ }
1056
+ }
1057
+ }
1058
+
1059
+ // #endregion
1060
+
1061
+ // #region Move/Copy/Delete/Create Folder
1062
+
1063
+ async move(source: URI, target: URI, options?: MoveFileOptions): Promise<FileStatWithMetadata> {
1064
+ if (options?.fromUserGesture === false) {
1065
+ return this.doMove(source, target, options.overwrite);
1066
+ }
1067
+ await this.runFileOperationParticipants(target, source, FileOperation.MOVE);
1068
+
1069
+ const event = { correlationId: this.correlationIds++, operation: FileOperation.MOVE, target, source };
1070
+ await this.onWillRunUserOperationEmitter.fire(event);
1071
+ let stat: FileStatWithMetadata;
1072
+ try {
1073
+ stat = await this.doMove(source, target, options?.overwrite);
1074
+ } catch (error) {
1075
+ await this.onDidFailUserOperationEmitter.fire(event);
1076
+ throw error;
1077
+ }
1078
+
1079
+ await this.onDidRunUserOperationEmitter.fire(event);
1080
+ return stat;
1081
+ }
1082
+
1083
+ protected async doMove(source: URI, target: URI, overwrite?: boolean): Promise<FileStatWithMetadata> {
1084
+ const sourceProvider = this.throwIfFileSystemIsReadonly(await this.withWriteProvider(source), source);
1085
+ const targetProvider = this.throwIfFileSystemIsReadonly(await this.withWriteProvider(target), target);
1086
+
1087
+ // move
1088
+ const mode = await this.doMoveCopy(sourceProvider, source, targetProvider, target, 'move', !!overwrite);
1089
+
1090
+ // resolve and send events
1091
+ const fileStat = await this.resolve(target, { resolveMetadata: true });
1092
+ this.onDidRunOperationEmitter.fire(new FileOperationEvent(source, mode === 'move' ? FileOperation.MOVE : FileOperation.COPY, fileStat));
1093
+
1094
+ return fileStat;
1095
+ }
1096
+
1097
+ async copy(source: URI, target: URI, options?: CopyFileOptions): Promise<FileStatWithMetadata> {
1098
+ if (options?.fromUserGesture === false) {
1099
+ return this.doCopy(source, target, options.overwrite);
1100
+ }
1101
+ await this.runFileOperationParticipants(target, source, FileOperation.COPY);
1102
+
1103
+ const event = { correlationId: this.correlationIds++, operation: FileOperation.COPY, target, source };
1104
+ await this.onWillRunUserOperationEmitter.fire(event);
1105
+ let stat: FileStatWithMetadata;
1106
+ try {
1107
+ stat = await this.doCopy(source, target, options?.overwrite);
1108
+ } catch (error) {
1109
+ await this.onDidFailUserOperationEmitter.fire(event);
1110
+ throw error;
1111
+ }
1112
+
1113
+ await this.onDidRunUserOperationEmitter.fire(event);
1114
+ return stat;
1115
+ }
1116
+
1117
+ protected async doCopy(source: URI, target: URI, overwrite?: boolean): Promise<FileStatWithMetadata> {
1118
+ const sourceProvider = await this.withReadProvider(source);
1119
+ const targetProvider = this.throwIfFileSystemIsReadonly(await this.withWriteProvider(target), target);
1120
+
1121
+ // copy
1122
+ const mode = await this.doMoveCopy(sourceProvider, source, targetProvider, target, 'copy', !!overwrite);
1123
+
1124
+ // resolve and send events
1125
+ const fileStat = await this.resolve(target, { resolveMetadata: true });
1126
+ this.onDidRunOperationEmitter.fire(new FileOperationEvent(source, mode === 'copy' ? FileOperation.COPY : FileOperation.MOVE, fileStat));
1127
+
1128
+ return fileStat;
1129
+ }
1130
+
1131
+ private async doMoveCopy(sourceProvider: FileSystemProvider, source: URI, targetProvider: FileSystemProvider, target: URI, mode: 'move' | 'copy', overwrite: boolean): Promise<'move' | 'copy'> {
1132
+ if (source.toString() === target.toString()) {
1133
+ return mode; // simulate node.js behaviour here and do a no-op if paths match
1134
+ }
1135
+
1136
+ // validation
1137
+ const { exists, isSameResourceWithDifferentPathCase } = await this.doValidateMoveCopy(sourceProvider, source, targetProvider, target, mode, overwrite);
1138
+
1139
+ // if target exists get valid target
1140
+ if (exists && !overwrite) {
1141
+ const parent = await this.resolve(target.parent);
1142
+ const targetFileStat = await this.resolve(target);
1143
+ target = FileSystemUtils.generateUniqueResourceURI(parent, target, targetFileStat.isDirectory, isSameResourceWithDifferentPathCase ? 'copy' : undefined);
1144
+ }
1145
+
1146
+ // delete as needed (unless target is same resource with different path case)
1147
+ if (exists && !isSameResourceWithDifferentPathCase && overwrite) {
1148
+ await this.delete(target, { recursive: true });
1149
+ }
1150
+
1151
+ // create parent folders
1152
+ await this.mkdirp(targetProvider, target.parent);
1153
+
1154
+ // copy source => target
1155
+ if (mode === 'copy') {
1156
+
1157
+ // same provider with fast copy: leverage copy() functionality
1158
+ if (sourceProvider === targetProvider && hasFileFolderCopyCapability(sourceProvider)) {
1159
+ await sourceProvider.copy(source, target, { overwrite });
1160
+ }
1161
+
1162
+ // when copying via buffer/unbuffered, we have to manually
1163
+ // traverse the source if it is a folder and not a file
1164
+ else {
1165
+ const sourceFile = await this.resolve(source);
1166
+ if (sourceFile.isDirectory) {
1167
+ await this.doCopyFolder(sourceProvider, sourceFile, targetProvider, target);
1168
+ } else {
1169
+ await this.doCopyFile(sourceProvider, source, targetProvider, target);
1170
+ }
1171
+ }
1172
+
1173
+ return mode;
1174
+ }
1175
+
1176
+ // move source => target
1177
+ else {
1178
+
1179
+ // same provider: leverage rename() functionality
1180
+ if (sourceProvider === targetProvider) {
1181
+ await sourceProvider.rename(source, target, { overwrite });
1182
+
1183
+ return mode;
1184
+ }
1185
+
1186
+ // across providers: copy to target & delete at source
1187
+ else {
1188
+ await this.doMoveCopy(sourceProvider, source, targetProvider, target, 'copy', overwrite);
1189
+
1190
+ await this.delete(source, { recursive: true });
1191
+
1192
+ return 'copy';
1193
+ }
1194
+ }
1195
+ }
1196
+
1197
+ private async doCopyFile(sourceProvider: FileSystemProvider, source: URI, targetProvider: FileSystemProvider, target: URI): Promise<void> {
1198
+
1199
+ // copy: source (buffered) => target (buffered)
1200
+ if (hasOpenReadWriteCloseCapability(sourceProvider) && hasOpenReadWriteCloseCapability(targetProvider)) {
1201
+ return this.doPipeBuffered(sourceProvider, source, targetProvider, target);
1202
+ }
1203
+
1204
+ // copy: source (buffered) => target (unbuffered)
1205
+ if (hasOpenReadWriteCloseCapability(sourceProvider) && hasReadWriteCapability(targetProvider)) {
1206
+ return this.doPipeBufferedToUnbuffered(sourceProvider, source, targetProvider, target);
1207
+ }
1208
+
1209
+ // copy: source (unbuffered) => target (buffered)
1210
+ if (hasReadWriteCapability(sourceProvider) && hasOpenReadWriteCloseCapability(targetProvider)) {
1211
+ return this.doPipeUnbufferedToBuffered(sourceProvider, source, targetProvider, target);
1212
+ }
1213
+
1214
+ // copy: source (unbuffered) => target (unbuffered)
1215
+ if (hasReadWriteCapability(sourceProvider) && hasReadWriteCapability(targetProvider)) {
1216
+ return this.doPipeUnbuffered(sourceProvider, source, targetProvider, target);
1217
+ }
1218
+ }
1219
+
1220
+ private async doCopyFolder(sourceProvider: FileSystemProvider, sourceFolder: FileStat, targetProvider: FileSystemProvider, targetFolder: URI): Promise<void> {
1221
+
1222
+ // create folder in target
1223
+ await targetProvider.mkdir(targetFolder);
1224
+
1225
+ // create children in target
1226
+ if (Array.isArray(sourceFolder.children)) {
1227
+ await Promise.all(sourceFolder.children.map(async sourceChild => {
1228
+ const targetChild = targetFolder.resolve(sourceChild.name);
1229
+ if (sourceChild.isDirectory) {
1230
+ return this.doCopyFolder(sourceProvider, await this.resolve(sourceChild.resource), targetProvider, targetChild);
1231
+ } else {
1232
+ return this.doCopyFile(sourceProvider, sourceChild.resource, targetProvider, targetChild);
1233
+ }
1234
+ }));
1235
+ }
1236
+ }
1237
+
1238
+ private async doValidateMoveCopy(sourceProvider: FileSystemProvider, source: URI, targetProvider: FileSystemProvider, target: URI, mode: 'move' | 'copy', overwrite?: boolean): Promise<{ exists: boolean, isSameResourceWithDifferentPathCase: boolean }> {
1239
+ let isSameResourceWithDifferentPathCase = false;
1240
+
1241
+ // Check if source is equal or parent to target (requires providers to be the same)
1242
+ if (sourceProvider === targetProvider) {
1243
+ const isPathCaseSensitive = !!(sourceProvider.capabilities & FileSystemProviderCapabilities.PathCaseSensitive);
1244
+ if (!isPathCaseSensitive) {
1245
+ isSameResourceWithDifferentPathCase = source.toString().toLowerCase() === target.toString().toLowerCase();
1246
+ }
1247
+
1248
+ if (isSameResourceWithDifferentPathCase && mode === 'copy') {
1249
+ throw new Error(nls.localizeByDefault("Unable to move/copy when source '{0}' is parent of target '{1}'.", this.resourceForError(source), this.resourceForError(target)));
1250
+ }
1251
+
1252
+ if (!isSameResourceWithDifferentPathCase && target.isEqualOrParent(source, isPathCaseSensitive)) {
1253
+ throw new Error(nls.localizeByDefault("Unable to move/copy when source '{0}' is parent of target '{1}'.", this.resourceForError(source), this.resourceForError(target)));
1254
+ }
1255
+ }
1256
+
1257
+ // Extra checks if target exists and this is not a rename
1258
+ const exists = await this.exists(target);
1259
+ if (exists && !isSameResourceWithDifferentPathCase) {
1260
+
1261
+ // Special case: if the target is a parent of the source, we cannot delete
1262
+ // it as it would delete the source as well. In this case we have to throw
1263
+ if (sourceProvider === targetProvider) {
1264
+ const isPathCaseSensitive = !!(sourceProvider.capabilities & FileSystemProviderCapabilities.PathCaseSensitive);
1265
+ if (source.isEqualOrParent(target, isPathCaseSensitive)) {
1266
+ throw new Error(nls.localizeByDefault("Unable to move/copy '{0}' into '{1}' since a file would replace the folder it is contained in.", this.resourceForError(source), this.resourceForError(target)));
1267
+ }
1268
+ }
1269
+ }
1270
+
1271
+ return { exists, isSameResourceWithDifferentPathCase };
1272
+ }
1273
+
1274
+ async createFolder(resource: URI, options: FileOperationOptions = {}): Promise<FileStatWithMetadata> {
1275
+ const {
1276
+ fromUserGesture = true,
1277
+ } = options;
1278
+
1279
+ const provider = this.throwIfFileSystemIsReadonly(await this.withProvider(resource), resource);
1280
+
1281
+ // mkdir recursively
1282
+ await this.mkdirp(provider, resource);
1283
+
1284
+ // events
1285
+ const fileStat = await this.resolve(resource, { resolveMetadata: true });
1286
+
1287
+ if (fromUserGesture) {
1288
+ this.onDidRunUserOperationEmitter.fire({ correlationId: this.correlationIds++, operation: FileOperation.CREATE, target: resource });
1289
+ } else {
1290
+ this.onDidRunOperationEmitter.fire(new FileOperationEvent(resource, FileOperation.CREATE, fileStat));
1291
+ }
1292
+
1293
+ return fileStat;
1294
+ }
1295
+
1296
+ private async mkdirp(provider: FileSystemProvider, directory: URI): Promise<void> {
1297
+ const directoriesToCreate: string[] = [];
1298
+
1299
+ // mkdir until we reach root
1300
+ while (!directory.path.isRoot) {
1301
+ try {
1302
+ const stat = await provider.stat(directory);
1303
+ if ((stat.type & FileType.Directory) === 0) {
1304
+ throw new Error(nls.localizeByDefault("Unable to create folder '{0}' that already exists but is not a directory", this.resourceForError(directory)));
1305
+ }
1306
+
1307
+ break; // we have hit a directory that exists -> good
1308
+ } catch (error) {
1309
+
1310
+ // Bubble up any other error that is not file not found
1311
+ if (toFileSystemProviderErrorCode(error) !== FileSystemProviderErrorCode.FileNotFound) {
1312
+ throw error;
1313
+ }
1314
+
1315
+ // Upon error, remember directories that need to be created
1316
+ directoriesToCreate.push(directory.path.base);
1317
+
1318
+ // Continue up
1319
+ directory = directory.parent;
1320
+ }
1321
+ }
1322
+
1323
+ // Create directories as needed
1324
+ for (let i = directoriesToCreate.length - 1; i >= 0; i--) {
1325
+ directory = directory.resolve(directoriesToCreate[i]);
1326
+
1327
+ try {
1328
+ await provider.mkdir(directory);
1329
+ } catch (error) {
1330
+ if (toFileSystemProviderErrorCode(error) !== FileSystemProviderErrorCode.FileExists) {
1331
+ // For mkdirp() we tolerate that the mkdir() call fails
1332
+ // in case the folder already exists. This follows node.js
1333
+ // own implementation of fs.mkdir({ recursive: true }) and
1334
+ // reduces the chances of race conditions leading to errors
1335
+ // if multiple calls try to create the same folders
1336
+ // As such, we only throw an error here if it is other than
1337
+ // the fact that the file already exists.
1338
+ // (see also https://github.com/microsoft/vscode/issues/89834)
1339
+ throw error;
1340
+ }
1341
+ }
1342
+ }
1343
+ }
1344
+
1345
+ async delete(resource: URI, options?: FileOperationOptions & Partial<FileDeleteOptions>): Promise<void> {
1346
+ if (options?.fromUserGesture === false) {
1347
+ return this.doDelete(resource, options);
1348
+ }
1349
+ await this.runFileOperationParticipants(resource, undefined, FileOperation.DELETE);
1350
+
1351
+ const event = { correlationId: this.correlationIds++, operation: FileOperation.DELETE, target: resource };
1352
+ await this.onWillRunUserOperationEmitter.fire(event);
1353
+ try {
1354
+ await this.doDelete(resource, options);
1355
+ } catch (error) {
1356
+ await this.onDidFailUserOperationEmitter.fire(event);
1357
+ throw error;
1358
+ }
1359
+
1360
+ await this.onDidRunUserOperationEmitter.fire(event);
1361
+ }
1362
+
1363
+ protected async doDelete(resource: URI, options?: Partial<FileDeleteOptions>): Promise<void> {
1364
+ const provider = this.throwIfFileSystemIsReadonly(await this.withProvider(resource), resource);
1365
+
1366
+ // Validate trash support
1367
+ const useTrash = !!options?.useTrash;
1368
+ if (useTrash && !(provider.capabilities & FileSystemProviderCapabilities.Trash)) {
1369
+ throw new Error(nls.localizeByDefault("Unable to delete file '{0}' via trash because provider does not support it.", this.resourceForError(resource)));
1370
+ }
1371
+
1372
+ // Validate delete
1373
+ const exists = await this.exists(resource);
1374
+ if (!exists) {
1375
+ throw new FileOperationError(nls.localizeByDefault("Unable to delete nonexistent file '{0}'", this.resourceForError(resource)), FileOperationResult.FILE_NOT_FOUND);
1376
+ }
1377
+
1378
+ // Validate recursive
1379
+ const recursive = !!options?.recursive;
1380
+ if (!recursive && exists) {
1381
+ const stat = await this.resolve(resource);
1382
+ if (stat.isDirectory && Array.isArray(stat.children) && stat.children.length > 0) {
1383
+ throw new Error(nls.localizeByDefault("Unable to delete non-empty folder '{0}'.", this.resourceForError(resource)));
1384
+ }
1385
+ }
1386
+
1387
+ // Delete through provider
1388
+ await provider.delete(resource, { recursive, useTrash });
1389
+
1390
+ // Events
1391
+ this.onDidRunOperationEmitter.fire(new FileOperationEvent(resource, FileOperation.DELETE));
1392
+ }
1393
+
1394
+ // #endregion
1395
+
1396
+ // #region File Watching
1397
+
1398
+ private onDidFilesChangeEmitter = new Emitter<FileChangesEvent>();
1399
+ /**
1400
+ * An event that is emitted when files are changed on the disk.
1401
+ */
1402
+ get onDidFilesChange(): Event<FileChangesEvent> {
1403
+ return this.onDidFilesChangeEmitter.event;
1404
+ }
1405
+
1406
+ private activeWatchers = new Map<string, { disposable: Disposable, count: number }>();
1407
+
1408
+ watch(resource: URI, options: WatchOptions = { recursive: false, excludes: [] }): Disposable {
1409
+ const resolvedOptions: WatchOptions = {
1410
+ ...options,
1411
+ // always ignore temporary upload files
1412
+ excludes: options.excludes.concat('**/theia_upload_*')
1413
+ };
1414
+
1415
+ let watchDisposed = false;
1416
+ let watchDisposable = Disposable.create(() => watchDisposed = true);
1417
+
1418
+ // Watch and wire in disposable which is async but
1419
+ // check if we got disposed meanwhile and forward
1420
+ this.doWatch(resource, resolvedOptions).then(disposable => {
1421
+ if (watchDisposed) {
1422
+ disposable.dispose();
1423
+ } else {
1424
+ watchDisposable = disposable;
1425
+ }
1426
+ }, error => console.error(error));
1427
+
1428
+ return Disposable.create(() => watchDisposable.dispose());
1429
+ }
1430
+
1431
+ async doWatch(resource: URI, options: WatchOptions): Promise<Disposable> {
1432
+ const provider = await this.withProvider(resource);
1433
+ const key = this.toWatchKey(provider, resource, options);
1434
+
1435
+ // Only start watching if we are the first for the given key
1436
+ const watcher = this.activeWatchers.get(key) || { count: 0, disposable: provider.watch(resource, options) };
1437
+ if (!this.activeWatchers.has(key)) {
1438
+ this.activeWatchers.set(key, watcher);
1439
+ }
1440
+
1441
+ // Increment usage counter
1442
+ watcher.count += 1;
1443
+
1444
+ return Disposable.create(() => {
1445
+
1446
+ // Unref
1447
+ watcher.count--;
1448
+
1449
+ // Dispose only when last user is reached
1450
+ if (watcher.count === 0) {
1451
+ watcher.disposable.dispose();
1452
+ this.activeWatchers.delete(key);
1453
+ }
1454
+ });
1455
+ }
1456
+
1457
+ private toWatchKey(provider: FileSystemProvider, resource: URI, options: WatchOptions): string {
1458
+ return [
1459
+ this.toMapKey(provider, resource), // lowercase path if the provider is case insensitive
1460
+ String(options.recursive), // use recursive: true | false as part of the key
1461
+ options.excludes.join() // use excludes as part of the key
1462
+ ].join();
1463
+ }
1464
+
1465
+ // #endregion
1466
+
1467
+ // #region Helpers
1468
+
1469
+ private writeQueues: Map<string, Promise<void>> = new Map();
1470
+
1471
+ private ensureWriteQueue(provider: FileSystemProvider, resource: URI, task: () => Promise<void>): Promise<void> {
1472
+ // ensure to never write to the same resource without finishing
1473
+ // the one write. this ensures a write finishes consistently
1474
+ // (even with error) before another write is done.
1475
+ const queueKey = this.toMapKey(provider, resource);
1476
+ const writeQueue = (this.writeQueues.get(queueKey) || Promise.resolve()).then(task, task);
1477
+ this.writeQueues.set(queueKey, writeQueue);
1478
+ return writeQueue;
1479
+ }
1480
+
1481
+ private toMapKey(provider: FileSystemProvider, resource: URI): string {
1482
+ const isPathCaseSensitive = !!(provider.capabilities & FileSystemProviderCapabilities.PathCaseSensitive);
1483
+
1484
+ return isPathCaseSensitive ? resource.toString() : resource.toString().toLowerCase();
1485
+ }
1486
+
1487
+ private async doWriteBuffered(provider: FileSystemProviderWithOpenReadWriteCloseCapability, resource: URI, readableOrStreamOrBufferedStream: BinaryBufferReadable | BinaryBufferReadableStream | BinaryBufferReadableBufferedStream): Promise<void> {
1488
+ return this.ensureWriteQueue(provider, resource, async () => {
1489
+
1490
+ // open handle
1491
+ const handle = await provider.open(resource, { create: true });
1492
+
1493
+ // write into handle until all bytes from buffer have been written
1494
+ try {
1495
+ if (isReadableStream(readableOrStreamOrBufferedStream) || isReadableBufferedStream(readableOrStreamOrBufferedStream)) {
1496
+ await this.doWriteStreamBufferedQueued(provider, handle, readableOrStreamOrBufferedStream);
1497
+ } else {
1498
+ await this.doWriteReadableBufferedQueued(provider, handle, readableOrStreamOrBufferedStream);
1499
+ }
1500
+ } catch (error) {
1501
+ throw ensureFileSystemProviderError(error);
1502
+ } finally {
1503
+
1504
+ // close handle always
1505
+ await provider.close(handle);
1506
+ }
1507
+ });
1508
+ }
1509
+
1510
+ private async doWriteStreamBufferedQueued(provider: FileSystemProviderWithOpenReadWriteCloseCapability, handle: number, streamOrBufferedStream: BinaryBufferReadableStream | BinaryBufferReadableBufferedStream): Promise<void> {
1511
+ let posInFile = 0;
1512
+ let stream: BinaryBufferReadableStream;
1513
+
1514
+ // Buffered stream: consume the buffer first by writing
1515
+ // it to the target before reading from the stream.
1516
+ if (isReadableBufferedStream(streamOrBufferedStream)) {
1517
+ if (streamOrBufferedStream.buffer.length > 0) {
1518
+ const chunk = BinaryBuffer.concat(streamOrBufferedStream.buffer);
1519
+ await this.doWriteBuffer(provider, handle, chunk, chunk.byteLength, posInFile, 0);
1520
+
1521
+ posInFile += chunk.byteLength;
1522
+ }
1523
+
1524
+ // If the stream has been consumed, return early
1525
+ if (streamOrBufferedStream.ended) {
1526
+ return;
1527
+ }
1528
+
1529
+ stream = streamOrBufferedStream.stream;
1530
+ }
1531
+
1532
+ // Unbuffered stream - just take as is
1533
+ else {
1534
+ stream = streamOrBufferedStream;
1535
+ }
1536
+
1537
+ return new Promise(async (resolve, reject) => {
1538
+
1539
+ stream.on('data', async chunk => {
1540
+
1541
+ // pause stream to perform async write operation
1542
+ stream.pause();
1543
+
1544
+ try {
1545
+ await this.doWriteBuffer(provider, handle, chunk, chunk.byteLength, posInFile, 0);
1546
+ } catch (error) {
1547
+ return reject(error);
1548
+ }
1549
+
1550
+ posInFile += chunk.byteLength;
1551
+
1552
+ // resume stream now that we have successfully written
1553
+ // run this on the next tick to prevent increasing the
1554
+ // execution stack because resume() may call the event
1555
+ // handler again before finishing.
1556
+ setTimeout(() => stream.resume());
1557
+ });
1558
+
1559
+ stream.on('error', error => reject(error));
1560
+ stream.on('end', () => resolve());
1561
+ });
1562
+ }
1563
+
1564
+ private async doWriteReadableBufferedQueued(provider: FileSystemProviderWithOpenReadWriteCloseCapability, handle: number, readable: BinaryBufferReadable): Promise<void> {
1565
+ let posInFile = 0;
1566
+
1567
+ let chunk: BinaryBuffer | null;
1568
+ while ((chunk = readable.read()) !== null) {
1569
+ await this.doWriteBuffer(provider, handle, chunk, chunk.byteLength, posInFile, 0);
1570
+
1571
+ posInFile += chunk.byteLength;
1572
+ }
1573
+ }
1574
+
1575
+ private async doWriteBuffer(provider: FileSystemProviderWithOpenReadWriteCloseCapability, handle: number, buffer: BinaryBuffer, length: number, posInFile: number, posInBuffer: number): Promise<void> {
1576
+ let totalBytesWritten = 0;
1577
+ while (totalBytesWritten < length) {
1578
+ const bytesWritten = await provider.write(handle, posInFile + totalBytesWritten, buffer.buffer, posInBuffer + totalBytesWritten, length - totalBytesWritten);
1579
+ totalBytesWritten += bytesWritten;
1580
+ }
1581
+ }
1582
+
1583
+ private async doWriteUnbuffered(provider: FileSystemProviderWithFileReadWriteCapability, resource: URI, bufferOrReadableOrStreamOrBufferedStream: BinaryBuffer | BinaryBufferReadable | BinaryBufferReadableStream | BinaryBufferReadableBufferedStream): Promise<void> {
1584
+ return this.ensureWriteQueue(provider, resource, () => this.doWriteUnbufferedQueued(provider, resource, bufferOrReadableOrStreamOrBufferedStream));
1585
+ }
1586
+
1587
+ private async doWriteUnbufferedQueued(provider: FileSystemProviderWithFileReadWriteCapability, resource: URI, bufferOrReadableOrStreamOrBufferedStream: BinaryBuffer | BinaryBufferReadable | BinaryBufferReadableStream | BinaryBufferReadableBufferedStream): Promise<void> {
1588
+ let buffer: BinaryBuffer;
1589
+ if (bufferOrReadableOrStreamOrBufferedStream instanceof BinaryBuffer) {
1590
+ buffer = bufferOrReadableOrStreamOrBufferedStream;
1591
+ } else if (isReadableStream(bufferOrReadableOrStreamOrBufferedStream)) {
1592
+ buffer = await BinaryBufferReadableStream.toBuffer(bufferOrReadableOrStreamOrBufferedStream);
1593
+ } else if (isReadableBufferedStream(bufferOrReadableOrStreamOrBufferedStream)) {
1594
+ buffer = await BinaryBufferReadableBufferedStream.toBuffer(bufferOrReadableOrStreamOrBufferedStream);
1595
+ } else {
1596
+ buffer = BinaryBufferReadable.toBuffer(bufferOrReadableOrStreamOrBufferedStream);
1597
+ }
1598
+
1599
+ return provider.writeFile(resource, buffer.buffer, { create: true, overwrite: true });
1600
+ }
1601
+
1602
+ private async doPipeBuffered(sourceProvider: FileSystemProviderWithOpenReadWriteCloseCapability, source: URI, targetProvider: FileSystemProviderWithOpenReadWriteCloseCapability, target: URI): Promise<void> {
1603
+ return this.ensureWriteQueue(targetProvider, target, () => this.doPipeBufferedQueued(sourceProvider, source, targetProvider, target));
1604
+ }
1605
+
1606
+ private async doPipeBufferedQueued(sourceProvider: FileSystemProviderWithOpenReadWriteCloseCapability, source: URI, targetProvider: FileSystemProviderWithOpenReadWriteCloseCapability, target: URI): Promise<void> {
1607
+ let sourceHandle: number | undefined = undefined;
1608
+ let targetHandle: number | undefined = undefined;
1609
+
1610
+ try {
1611
+
1612
+ // Open handles
1613
+ sourceHandle = await sourceProvider.open(source, { create: false });
1614
+ targetHandle = await targetProvider.open(target, { create: true });
1615
+
1616
+ const buffer = BinaryBuffer.alloc(this.BUFFER_SIZE);
1617
+
1618
+ let posInFile = 0;
1619
+ let posInBuffer = 0;
1620
+ let bytesRead = 0;
1621
+ do {
1622
+ // read from source (sourceHandle) at current position (posInFile) into buffer (buffer) at
1623
+ // buffer position (posInBuffer) up to the size of the buffer (buffer.byteLength).
1624
+ bytesRead = await sourceProvider.read(sourceHandle, posInFile, buffer.buffer, posInBuffer, buffer.byteLength - posInBuffer);
1625
+
1626
+ // write into target (targetHandle) at current position (posInFile) from buffer (buffer) at
1627
+ // buffer position (posInBuffer) all bytes we read (bytesRead).
1628
+ await this.doWriteBuffer(targetProvider, targetHandle, buffer, bytesRead, posInFile, posInBuffer);
1629
+
1630
+ posInFile += bytesRead;
1631
+ posInBuffer += bytesRead;
1632
+
1633
+ // when buffer full, fill it again from the beginning
1634
+ if (posInBuffer === buffer.byteLength) {
1635
+ posInBuffer = 0;
1636
+ }
1637
+ } while (bytesRead > 0);
1638
+ } catch (error) {
1639
+ throw ensureFileSystemProviderError(error);
1640
+ } finally {
1641
+ await Promise.all([
1642
+ typeof sourceHandle === 'number' ? sourceProvider.close(sourceHandle) : Promise.resolve(),
1643
+ typeof targetHandle === 'number' ? targetProvider.close(targetHandle) : Promise.resolve(),
1644
+ ]);
1645
+ }
1646
+ }
1647
+
1648
+ private async doPipeUnbuffered(sourceProvider: FileSystemProviderWithFileReadWriteCapability, source: URI, targetProvider: FileSystemProviderWithFileReadWriteCapability, target: URI): Promise<void> {
1649
+ return this.ensureWriteQueue(targetProvider, target, () => this.doPipeUnbufferedQueued(sourceProvider, source, targetProvider, target));
1650
+ }
1651
+
1652
+ private async doPipeUnbufferedQueued(sourceProvider: FileSystemProviderWithFileReadWriteCapability, source: URI, targetProvider: FileSystemProviderWithFileReadWriteCapability, target: URI): Promise<void> {
1653
+ return targetProvider.writeFile(target, await sourceProvider.readFile(source), { create: true, overwrite: true });
1654
+ }
1655
+
1656
+ private async doPipeUnbufferedToBuffered(sourceProvider: FileSystemProviderWithFileReadWriteCapability, source: URI, targetProvider: FileSystemProviderWithOpenReadWriteCloseCapability, target: URI): Promise<void> {
1657
+ return this.ensureWriteQueue(targetProvider, target, () => this.doPipeUnbufferedToBufferedQueued(sourceProvider, source, targetProvider, target));
1658
+ }
1659
+
1660
+ private async doPipeUnbufferedToBufferedQueued(sourceProvider: FileSystemProviderWithFileReadWriteCapability, source: URI, targetProvider: FileSystemProviderWithOpenReadWriteCloseCapability, target: URI): Promise<void> {
1661
+
1662
+ // Open handle
1663
+ const targetHandle = await targetProvider.open(target, { create: true });
1664
+
1665
+ // Read entire buffer from source and write buffered
1666
+ try {
1667
+ const buffer = await sourceProvider.readFile(source);
1668
+ await this.doWriteBuffer(targetProvider, targetHandle, BinaryBuffer.wrap(buffer), buffer.byteLength, 0, 0);
1669
+ } catch (error) {
1670
+ throw ensureFileSystemProviderError(error);
1671
+ } finally {
1672
+ await targetProvider.close(targetHandle);
1673
+ }
1674
+ }
1675
+
1676
+ private async doPipeBufferedToUnbuffered(sourceProvider: FileSystemProviderWithOpenReadWriteCloseCapability, source: URI, targetProvider: FileSystemProviderWithFileReadWriteCapability, target: URI): Promise<void> {
1677
+
1678
+ // Read buffer via stream buffered
1679
+ const buffer = await BinaryBufferReadableStream.toBuffer(this.readFileBuffered(sourceProvider, source, CancellationToken.None));
1680
+
1681
+ // Write buffer into target at once
1682
+ await this.doWriteUnbuffered(targetProvider, target, buffer);
1683
+ }
1684
+
1685
+ protected throwIfFileSystemIsReadonly<T extends FileSystemProvider>(provider: T, resource: URI): T {
1686
+ if (provider.capabilities & FileSystemProviderCapabilities.Readonly) {
1687
+ throw new FileOperationError(nls.localizeByDefault("Unable to modify read-only file '{0}'", this.resourceForError(resource)), FileOperationResult.FILE_PERMISSION_DENIED);
1688
+ }
1689
+
1690
+ return provider;
1691
+ }
1692
+
1693
+ private resourceForError(resource: URI): string {
1694
+ return this.labelProvider.getLongName(resource);
1695
+ }
1696
+
1697
+ // #endregion
1698
+
1699
+ // #region File operation participants
1700
+
1701
+ private readonly participants: FileOperationParticipant[] = [];
1702
+
1703
+ addFileOperationParticipant(participant: FileOperationParticipant): Disposable {
1704
+ this.participants.push(participant);
1705
+
1706
+ return Disposable.create(() => {
1707
+ const index = this.participants.indexOf(participant);
1708
+ if (index > -1) {
1709
+ this.participants.splice(index, 1);
1710
+ }
1711
+ });
1712
+ }
1713
+
1714
+ async runFileOperationParticipants(target: URI, source: URI | undefined, operation: FileOperation): Promise<void> {
1715
+ const participantsTimeout = this.preferences['files.participants.timeout'];
1716
+ if (participantsTimeout <= 0 || this.participants.length === 0) {
1717
+ return;
1718
+ }
1719
+
1720
+ const cancellationTokenSource = new CancellationTokenSource();
1721
+
1722
+ return this.progressService.withProgress(
1723
+ this.progressLabel(operation),
1724
+ 'notification',
1725
+ async () => {
1726
+ for (const participant of this.participants) {
1727
+ if (cancellationTokenSource.token.isCancellationRequested) {
1728
+ break;
1729
+ }
1730
+
1731
+ try {
1732
+ const promise = participant.participate(target, source, operation, participantsTimeout, cancellationTokenSource.token);
1733
+ await Promise.race([
1734
+ promise,
1735
+ timeout(participantsTimeout, cancellationTokenSource.token).then(() => cancellationTokenSource.dispose(), () => { /* no-op if cancelled */ })
1736
+ ]);
1737
+ } catch (err) {
1738
+ console.warn(err);
1739
+ }
1740
+ }
1741
+ },
1742
+ () => {
1743
+ cancellationTokenSource.cancel();
1744
+ });
1745
+ }
1746
+
1747
+ private progressLabel(operation: FileOperation): string {
1748
+ switch (operation) {
1749
+ case FileOperation.CREATE:
1750
+ return nls.localizeByDefault("Running 'File Create' participants...");
1751
+ case FileOperation.MOVE:
1752
+ return nls.localizeByDefault("Running 'File Rename' participants...");
1753
+ case FileOperation.COPY:
1754
+ return nls.localizeByDefault("Running 'File Copy' participants...");
1755
+ case FileOperation.DELETE:
1756
+ return nls.localizeByDefault("Running 'File Delete' participants...");
1757
+ }
1758
+ }
1759
+
1760
+ // #endregion
1761
+
1762
+ // #region encoding
1763
+
1764
+ protected async getWriteEncoding(resource: URI, options?: WriteEncodingOptions): Promise<ResourceEncoding> {
1765
+ const encoding = await this.getEncodingForResource(resource, options ? options.encoding : undefined);
1766
+ return this.encodingService.toResourceEncoding(encoding, {
1767
+ overwriteEncoding: options?.overwriteEncoding,
1768
+ read: async length => {
1769
+ const buffer = await BinaryBufferReadableStream.toBuffer((await this.readFileStream(resource, { length })).value);
1770
+ return buffer.buffer;
1771
+ }
1772
+ });
1773
+ }
1774
+
1775
+ protected getReadEncoding(resource: URI, options?: ReadEncodingOptions, detectedEncoding?: string): Promise<string> {
1776
+ let preferredEncoding: string | undefined;
1777
+
1778
+ // Encoding passed in as option
1779
+ if (options?.encoding) {
1780
+ if (detectedEncoding === UTF8_with_bom && options.encoding === UTF8) {
1781
+ preferredEncoding = UTF8_with_bom; // indicate the file has BOM if we are to resolve with UTF 8
1782
+ } else {
1783
+ preferredEncoding = options.encoding; // give passed in encoding highest priority
1784
+ }
1785
+ } else if (detectedEncoding) {
1786
+ preferredEncoding = detectedEncoding;
1787
+ }
1788
+
1789
+ return this.getEncodingForResource(resource, preferredEncoding);
1790
+ }
1791
+
1792
+ protected async getEncodingForResource(resource: URI, preferredEncoding?: string): Promise<string> {
1793
+ resource = await this.toUnderlyingResource(resource);
1794
+ return this.encodingRegistry.getEncodingForResource(resource, preferredEncoding);
1795
+ }
1796
+
1797
+ /**
1798
+ * Converts to an underlying fs provider resource format.
1799
+ *
1800
+ * For example converting `user-storage` resources to `file` resources under a user home:
1801
+ * user-storage:/user/settings.json => file://home/.theia/settings.json
1802
+ */
1803
+ async toUnderlyingResource(resource: URI): Promise<URI> {
1804
+ let provider = await this.withProvider(resource);
1805
+ while (provider instanceof DelegatingFileSystemProvider) {
1806
+ resource = provider.toUnderlyingResource(resource);
1807
+ provider = await this.withProvider(resource);
1808
+ }
1809
+ return resource;
1810
+ }
1811
+
1812
+ // #endregion
1813
+
1814
+ protected handleFileWatchError(): void {
1815
+ this.watcherErrorHandler.handleError();
1816
+ }
1817
+ }