@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,790 +1,796 @@
1
- "use strict";
2
- // *****************************************************************************
3
- // Copyright (C) 2020 TypeFox and others.
4
- //
5
- // This program and the accompanying materials are made available under the
6
- // terms of the Eclipse Public License v. 2.0 which is available at
7
- // http://www.eclipse.org/legal/epl-2.0.
8
- //
9
- // This Source Code may also be made available under the following Secondary
10
- // Licenses when the conditions for such availability set forth in the Eclipse
11
- // Public License v. 2.0 are satisfied: GNU General Public License, version 2
12
- // with the GNU Classpath Exception which is available at
13
- // https://www.gnu.org/software/classpath/license.html.
14
- //
15
- // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
16
- // *****************************************************************************
17
- /*---------------------------------------------------------------------------------------------
18
- * Copyright (c) Microsoft Corporation. All rights reserved.
19
- * Licensed under the MIT License. See License.txt in the project root for license information.
20
- *--------------------------------------------------------------------------------------------*/
21
- // based on https://github.com/microsoft/vscode/blob/04c36be045a94fee58e5f8992d3e3fd980294a84/src/vs/platform/files/node/diskFileSystemProvider.ts
22
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
23
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
24
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
25
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
26
- return c > 3 && r && Object.defineProperty(target, key, r), r;
27
- };
28
- var __metadata = (this && this.__metadata) || function (k, v) {
29
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
30
- };
31
- Object.defineProperty(exports, "__esModule", { value: true });
32
- exports.DiskFileSystemProvider = void 0;
33
- /* eslint-disable no-null/no-null */
34
- /* eslint-disable @typescript-eslint/no-shadow */
35
- const inversify_1 = require("@theia/core/shared/inversify");
36
- const path_1 = require("path");
37
- const uuid_1 = require("uuid");
38
- const os = require("os");
39
- const fs = require("fs");
40
- const fs_1 = require("fs");
41
- const util_1 = require("util");
42
- const uri_1 = require("@theia/core/lib/common/uri");
43
- const path_2 = require("@theia/core/lib/common/path");
44
- const file_uri_1 = require("@theia/core/lib/node/file-uri");
45
- const event_1 = require("@theia/core/lib/common/event");
46
- const disposable_1 = require("@theia/core/lib/common/disposable");
47
- const os_1 = require("@theia/core/lib/common/os");
48
- const promise_util_1 = require("@theia/core/lib/common/promise-util");
49
- const files_1 = require("../common/files");
50
- const filesystem_watcher_protocol_1 = require("../common/filesystem-watcher-protocol");
51
- const trash = require("trash");
52
- const vscode_languageserver_textdocument_1 = require("vscode-languageserver-textdocument");
53
- const encoding_service_1 = require("@theia/core/lib/common/encoding-service");
54
- const buffer_1 = require("@theia/core/lib/common/buffer");
55
- const stream_1 = require("@theia/core/lib/common/stream");
56
- const io_1 = require("../common/io");
57
- const stat_mode_1 = require("stat-mode");
58
- let DiskFileSystemProvider = class DiskFileSystemProvider {
59
- constructor() {
60
- this.BUFFER_SIZE = 64 * 1024;
61
- this.onDidChangeFileEmitter = new event_1.Emitter();
62
- this.onDidChangeFile = this.onDidChangeFileEmitter.event;
63
- this.onFileWatchErrorEmitter = new event_1.Emitter();
64
- this.onFileWatchError = this.onFileWatchErrorEmitter.event;
65
- this.toDispose = new disposable_1.DisposableCollection(this.onDidChangeFileEmitter);
66
- // #region File Capabilities
67
- this.onDidChangeCapabilities = event_1.Event.None;
68
- this.mapHandleToPos = new Map();
69
- this.writeHandles = new Set();
70
- this.canFlush = true;
71
- }
72
- init() {
73
- this.toDispose.push(this.watcher);
74
- this.watcher.setClient({
75
- onDidFilesChanged: params => this.onDidChangeFileEmitter.fire(params.changes.map(({ uri, type }) => ({
76
- resource: new uri_1.default(uri),
77
- type
78
- }))),
79
- onError: () => this.onFileWatchErrorEmitter.fire()
80
- });
81
- }
82
- get capabilities() {
83
- if (!this._capabilities) {
84
- this._capabilities =
85
- 2 /* FileReadWrite */ |
86
- 4 /* FileOpenReadWriteClose */ |
87
- 16 /* FileReadStream */ |
88
- 8 /* FileFolderCopy */ |
89
- 16777216 /* Access */ |
90
- 4096 /* Trash */ |
91
- 33554432 /* Update */;
92
- if (os_1.OS.type() === os_1.OS.Type.Linux) {
93
- this._capabilities |= 1024 /* PathCaseSensitive */;
94
- }
95
- }
96
- return this._capabilities;
97
- }
98
- // #endregion
99
- // #region File Metadata Resolving
100
- async stat(resource) {
101
- try {
102
- const { stat, symbolicLink } = await this.statLink(this.toFilePath(resource)); // cannot use fs.stat() here to support links properly
103
- const mode = new stat_mode_1.Mode(stat);
104
- return {
105
- type: this.toType(stat, symbolicLink),
106
- ctime: stat.birthtime.getTime(),
107
- mtime: stat.mtime.getTime(),
108
- size: stat.size,
109
- permissions: !mode.owner.write ? files_1.FilePermission.Readonly : undefined,
110
- };
111
- }
112
- catch (error) {
113
- throw this.toFileSystemProviderError(error);
114
- }
115
- }
116
- async access(resource, mode) {
117
- try {
118
- await (0, util_1.promisify)(fs.access)(this.toFilePath(resource), mode);
119
- }
120
- catch (error) {
121
- throw this.toFileSystemProviderError(error);
122
- }
123
- }
124
- async fsPath(resource) {
125
- return file_uri_1.FileUri.fsPath(resource);
126
- }
127
- async statLink(path) {
128
- // First stat the link
129
- let lstats;
130
- try {
131
- lstats = await (0, util_1.promisify)(fs_1.lstat)(path);
132
- // Return early if the stat is not a symbolic link at all
133
- if (!lstats.isSymbolicLink()) {
134
- return { stat: lstats };
135
- }
136
- }
137
- catch (error) {
138
- /* ignore - use stat() instead */
139
- }
140
- // If the stat is a symbolic link or failed to stat, use fs.stat()
141
- // which for symbolic links will stat the target they point to
142
- try {
143
- const stats = await (0, util_1.promisify)(fs_1.stat)(path);
144
- return { stat: stats, symbolicLink: (lstats === null || lstats === void 0 ? void 0 : lstats.isSymbolicLink()) ? { dangling: false } : undefined };
145
- }
146
- catch (error) {
147
- // If the link points to a non-existing file we still want
148
- // to return it as result while setting dangling: true flag
149
- if (error.code === 'ENOENT' && lstats) {
150
- return { stat: lstats, symbolicLink: { dangling: true } };
151
- }
152
- throw error;
153
- }
154
- }
155
- async readdir(resource) {
156
- try {
157
- const children = await (0, util_1.promisify)(fs.readdir)(this.toFilePath(resource));
158
- const result = [];
159
- await Promise.all(children.map(async (child) => {
160
- try {
161
- const stat = await this.stat(resource.resolve(child));
162
- result.push([child, stat.type]);
163
- }
164
- catch (error) {
165
- console.trace(error); // ignore errors for individual entries that can arise from permission denied
166
- }
167
- }));
168
- return result;
169
- }
170
- catch (error) {
171
- throw this.toFileSystemProviderError(error);
172
- }
173
- }
174
- toType(entry, symbolicLink) {
175
- // Signal file type by checking for file / directory, except:
176
- // - symbolic links pointing to non-existing files are FileType.Unknown
177
- // - files that are neither file nor directory are FileType.Unknown
178
- let type;
179
- if (symbolicLink === null || symbolicLink === void 0 ? void 0 : symbolicLink.dangling) {
180
- type = files_1.FileType.Unknown;
181
- }
182
- else if (entry.isFile()) {
183
- type = files_1.FileType.File;
184
- }
185
- else if (entry.isDirectory()) {
186
- type = files_1.FileType.Directory;
187
- }
188
- else {
189
- type = files_1.FileType.Unknown;
190
- }
191
- // Always signal symbolic link as file type additionally
192
- if (symbolicLink) {
193
- type |= files_1.FileType.SymbolicLink;
194
- }
195
- return type;
196
- }
197
- // #endregion
198
- // #region File Reading/Writing
199
- async readFile(resource) {
200
- try {
201
- const filePath = this.toFilePath(resource);
202
- return await (0, util_1.promisify)(fs_1.readFile)(filePath);
203
- }
204
- catch (error) {
205
- throw this.toFileSystemProviderError(error);
206
- }
207
- }
208
- readFileStream(resource, opts, token) {
209
- const stream = (0, stream_1.newWriteableStream)(data => buffer_1.BinaryBuffer.concat(data.map(data => buffer_1.BinaryBuffer.wrap(data))).buffer);
210
- (0, io_1.readFileIntoStream)(this, resource, stream, data => data.buffer, {
211
- ...opts,
212
- bufferSize: this.BUFFER_SIZE
213
- }, token);
214
- return stream;
215
- }
216
- async writeFile(resource, content, opts) {
217
- let handle = undefined;
218
- try {
219
- const filePath = this.toFilePath(resource);
220
- // Validate target unless { create: true, overwrite: true }
221
- if (!opts.create || !opts.overwrite) {
222
- const fileExists = await (0, util_1.promisify)(fs_1.exists)(filePath);
223
- if (fileExists) {
224
- if (!opts.overwrite) {
225
- throw (0, files_1.createFileSystemProviderError)('File already exists', files_1.FileSystemProviderErrorCode.FileExists);
226
- }
227
- }
228
- else {
229
- if (!opts.create) {
230
- throw (0, files_1.createFileSystemProviderError)('File does not exist', files_1.FileSystemProviderErrorCode.FileNotFound);
231
- }
232
- }
233
- }
234
- // Open
235
- handle = await this.open(resource, { create: true });
236
- // Write content at once
237
- await this.write(handle, 0, content, 0, content.byteLength);
238
- }
239
- catch (error) {
240
- throw this.toFileSystemProviderError(error);
241
- }
242
- finally {
243
- if (typeof handle === 'number') {
244
- await this.close(handle);
245
- }
246
- }
247
- }
248
- async open(resource, opts) {
249
- try {
250
- const filePath = this.toFilePath(resource);
251
- let flags = undefined;
252
- if (opts.create) {
253
- if (os_1.isWindows && await (0, util_1.promisify)(fs_1.exists)(filePath)) {
254
- try {
255
- // On Windows and if the file exists, we use a different strategy of saving the file
256
- // by first truncating the file and then writing with r+ flag. This helps to save hidden files on Windows
257
- // (see https://github.com/Microsoft/vscode/issues/931) and prevent removing alternate data streams
258
- // (see https://github.com/Microsoft/vscode/issues/6363)
259
- await (0, util_1.promisify)(fs_1.truncate)(filePath, 0);
260
- // After a successful truncate() the flag can be set to 'r+' which will not truncate.
261
- flags = 'r+';
262
- }
263
- catch (error) {
264
- console.trace(error);
265
- }
266
- }
267
- // we take opts.create as a hint that the file is opened for writing
268
- // as such we use 'w' to truncate an existing or create the
269
- // file otherwise. we do not allow reading.
270
- if (!flags) {
271
- flags = 'w';
272
- }
273
- }
274
- else {
275
- // otherwise we assume the file is opened for reading
276
- // as such we use 'r' to neither truncate, nor create
277
- // the file.
278
- flags = 'r';
279
- }
280
- const handle = await (0, util_1.promisify)(fs_1.open)(filePath, flags);
281
- // remember this handle to track file position of the handle
282
- // we init the position to 0 since the file descriptor was
283
- // just created and the position was not moved so far (see
284
- // also http://man7.org/linux/man-pages/man2/open.2.html -
285
- // "The file offset is set to the beginning of the file.")
286
- this.mapHandleToPos.set(handle, 0);
287
- // remember that this handle was used for writing
288
- if (opts.create) {
289
- this.writeHandles.add(handle);
290
- }
291
- return handle;
292
- }
293
- catch (error) {
294
- throw this.toFileSystemProviderError(error);
295
- }
296
- }
297
- async close(fd) {
298
- try {
299
- // remove this handle from map of positions
300
- this.mapHandleToPos.delete(fd);
301
- // if a handle is closed that was used for writing, ensure
302
- // to flush the contents to disk if possible.
303
- if (this.writeHandles.delete(fd) && this.canFlush) {
304
- try {
305
- await (0, util_1.promisify)(fs_1.fdatasync)(fd);
306
- }
307
- catch (error) {
308
- // In some exotic setups it is well possible that node fails to sync
309
- // In that case we disable flushing and log the error to our logger
310
- this.canFlush = false;
311
- console.error(error);
312
- }
313
- }
314
- return await (0, util_1.promisify)(fs_1.close)(fd);
315
- }
316
- catch (error) {
317
- throw this.toFileSystemProviderError(error);
318
- }
319
- }
320
- async read(fd, pos, data, offset, length) {
321
- const normalizedPos = this.normalizePos(fd, pos);
322
- let bytesRead = null;
323
- try {
324
- const result = await (0, util_1.promisify)(fs_1.read)(fd, data, offset, length, normalizedPos);
325
- if (typeof result === 'number') {
326
- bytesRead = result; // node.d.ts fail
327
- }
328
- else {
329
- bytesRead = result.bytesRead;
330
- }
331
- return bytesRead;
332
- }
333
- catch (error) {
334
- throw this.toFileSystemProviderError(error);
335
- }
336
- finally {
337
- this.updatePos(fd, normalizedPos, bytesRead);
338
- }
339
- }
340
- normalizePos(fd, pos) {
341
- // when calling fs.read/write we try to avoid passing in the "pos" argument and
342
- // rather prefer to pass in "null" because this avoids an extra seek(pos)
343
- // call that in some cases can even fail (e.g. when opening a file over FTP -
344
- // see https://github.com/microsoft/vscode/issues/73884).
345
- //
346
- // as such, we compare the passed in position argument with our last known
347
- // position for the file descriptor and use "null" if they match.
348
- if (pos === this.mapHandleToPos.get(fd)) {
349
- return null;
350
- }
351
- return pos;
352
- }
353
- updatePos(fd, pos, bytesLength) {
354
- const lastKnownPos = this.mapHandleToPos.get(fd);
355
- if (typeof lastKnownPos === 'number') {
356
- // pos !== null signals that previously a position was used that is
357
- // not null. node.js documentation explains, that in this case
358
- // the internal file pointer is not moving and as such we do not move
359
- // our position pointer.
360
- //
361
- // Docs: "If position is null, data will be read from the current file position,
362
- // and the file position will be updated. If position is an integer, the file position
363
- // will remain unchanged."
364
- if (typeof pos === 'number') {
365
- // do not modify the position
366
- }
367
- else if (typeof bytesLength === 'number') {
368
- this.mapHandleToPos.set(fd, lastKnownPos + bytesLength);
369
- }
370
- else {
371
- this.mapHandleToPos.delete(fd);
372
- }
373
- }
374
- }
375
- async write(fd, pos, data, offset, length) {
376
- // we know at this point that the file to write to is truncated and thus empty
377
- // if the write now fails, the file remains empty. as such we really try hard
378
- // to ensure the write succeeds by retrying up to three times.
379
- return (0, promise_util_1.retry)(() => this.doWrite(fd, pos, data, offset, length), 100 /* ms delay */, 3 /* retries */);
380
- }
381
- async doWrite(fd, pos, data, offset, length) {
382
- const normalizedPos = this.normalizePos(fd, pos);
383
- let bytesWritten = null;
384
- try {
385
- const result = await (0, util_1.promisify)(fs_1.write)(fd, data, offset, length, normalizedPos);
386
- if (typeof result === 'number') {
387
- bytesWritten = result; // node.d.ts fail
388
- }
389
- else {
390
- bytesWritten = result.bytesWritten;
391
- }
392
- return bytesWritten;
393
- }
394
- catch (error) {
395
- throw this.toFileSystemProviderError(error);
396
- }
397
- finally {
398
- this.updatePos(fd, normalizedPos, bytesWritten);
399
- }
400
- }
401
- // #endregion
402
- // #region Move/Copy/Delete/Create Folder
403
- async mkdir(resource) {
404
- try {
405
- await (0, util_1.promisify)(fs_1.mkdir)(this.toFilePath(resource));
406
- }
407
- catch (error) {
408
- throw this.toFileSystemProviderError(error);
409
- }
410
- }
411
- async delete(resource, opts) {
412
- try {
413
- const filePath = this.toFilePath(resource);
414
- await this.doDelete(filePath, opts);
415
- }
416
- catch (error) {
417
- throw this.toFileSystemProviderError(error);
418
- }
419
- }
420
- async doDelete(filePath, opts) {
421
- if (!opts.useTrash) {
422
- if (opts.recursive) {
423
- await this.rimraf(filePath);
424
- }
425
- else {
426
- await (0, util_1.promisify)(fs_1.unlink)(filePath);
427
- }
428
- }
429
- else {
430
- await trash(filePath);
431
- }
432
- }
433
- rimraf(path) {
434
- if (new path_2.Path(path).isRoot) {
435
- throw new Error('rimraf - will refuse to recursively delete root');
436
- }
437
- return this.rimrafMove(path);
438
- }
439
- async rimrafMove(path) {
440
- try {
441
- const pathInTemp = (0, path_1.join)(os.tmpdir(), (0, uuid_1.v4)());
442
- try {
443
- await (0, util_1.promisify)(fs_1.rename)(path, pathInTemp);
444
- }
445
- catch (error) {
446
- return this.rimrafUnlink(path); // if rename fails, delete without tmp dir
447
- }
448
- // Delete but do not return as promise
449
- this.rimrafUnlink(pathInTemp);
450
- }
451
- catch (error) {
452
- if (error.code !== 'ENOENT') {
453
- throw error;
454
- }
455
- }
456
- }
457
- async rimrafUnlink(path) {
458
- try {
459
- const stat = await (0, util_1.promisify)(fs_1.lstat)(path);
460
- // Folder delete (recursive) - NOT for symbolic links though!
461
- if (stat.isDirectory() && !stat.isSymbolicLink()) {
462
- // Children
463
- const children = await (0, util_1.promisify)(fs_1.readdir)(path);
464
- await Promise.all(children.map(child => this.rimrafUnlink((0, path_1.join)(path, child))));
465
- // Folder
466
- await (0, util_1.promisify)(fs_1.rmdir)(path);
467
- }
468
- else {
469
- // chmod as needed to allow for unlink
470
- const mode = stat.mode;
471
- if (!(mode & 128)) { // 128 === 0200
472
- await (0, util_1.promisify)(fs_1.chmod)(path, mode | 128);
473
- }
474
- return (0, util_1.promisify)(fs_1.unlink)(path);
475
- }
476
- }
477
- catch (error) {
478
- if (error.code !== 'ENOENT') {
479
- throw error;
480
- }
481
- }
482
- }
483
- async rename(from, to, opts) {
484
- const fromFilePath = this.toFilePath(from);
485
- const toFilePath = this.toFilePath(to);
486
- if (fromFilePath === toFilePath) {
487
- return; // simulate node.js behaviour here and do a no-op if paths match
488
- }
489
- try {
490
- // Ensure target does not exist
491
- await this.validateTargetDeleted(from, to, 'move', opts.overwrite);
492
- // Move
493
- await this.move(fromFilePath, toFilePath);
494
- }
495
- catch (error) {
496
- // rewrite some typical errors that can happen especially around symlinks
497
- // to something the user can better understand
498
- if (error.code === 'EINVAL' || error.code === 'EBUSY' || error.code === 'ENAMETOOLONG') {
499
- error = new Error(`Unable to move '${(0, path_1.basename)(fromFilePath)}' into '${(0, path_1.basename)((0, path_1.dirname)(toFilePath))}' (${error.toString()}).`);
500
- }
501
- throw this.toFileSystemProviderError(error);
502
- }
503
- }
504
- async move(source, target) {
505
- if (source === target) {
506
- return Promise.resolve();
507
- }
508
- async function updateMtime(path) {
509
- const stat = await (0, util_1.promisify)(fs_1.lstat)(path);
510
- if (stat.isDirectory() || stat.isSymbolicLink()) {
511
- return Promise.resolve(); // only for files
512
- }
513
- const fd = await (0, util_1.promisify)(fs_1.open)(path, 'a');
514
- try {
515
- await (0, util_1.promisify)(fs_1.futimes)(fd, stat.atime, new Date());
516
- }
517
- catch (error) {
518
- // ignore
519
- }
520
- return (0, util_1.promisify)(fs_1.close)(fd);
521
- }
522
- try {
523
- await (0, util_1.promisify)(fs_1.rename)(source, target);
524
- await updateMtime(target);
525
- }
526
- catch (error) {
527
- // In two cases we fallback to classic copy and delete:
528
- //
529
- // 1.) The EXDEV error indicates that source and target are on different devices
530
- // In this case, fallback to using a copy() operation as there is no way to
531
- // rename() between different devices.
532
- //
533
- // 2.) The user tries to rename a file/folder that ends with a dot. This is not
534
- // really possible to move then, at least on UNC devices.
535
- if (source.toLowerCase() !== target.toLowerCase() && error.code === 'EXDEV' || source.endsWith('.')) {
536
- await this.doCopy(source, target);
537
- await this.rimraf(source);
538
- await updateMtime(target);
539
- }
540
- else {
541
- throw error;
542
- }
543
- }
544
- }
545
- async copy(from, to, opts) {
546
- const fromFilePath = this.toFilePath(from);
547
- const toFilePath = this.toFilePath(to);
548
- if (fromFilePath === toFilePath) {
549
- return; // simulate node.js behaviour here and do a no-op if paths match
550
- }
551
- try {
552
- // Ensure target does not exist
553
- await this.validateTargetDeleted(from, to, 'copy', opts.overwrite);
554
- // Copy
555
- await this.doCopy(fromFilePath, toFilePath);
556
- }
557
- catch (error) {
558
- // rewrite some typical errors that can happen especially around symlinks
559
- // to something the user can better understand
560
- if (error.code === 'EINVAL' || error.code === 'EBUSY' || error.code === 'ENAMETOOLONG') {
561
- error = new Error(`Unable to copy '${(0, path_1.basename)(fromFilePath)}' into '${(0, path_1.basename)((0, path_1.dirname)(toFilePath))}' (${error.toString()}).`);
562
- }
563
- throw this.toFileSystemProviderError(error);
564
- }
565
- }
566
- async validateTargetDeleted(from, to, mode, overwrite) {
567
- const isPathCaseSensitive = !!(this.capabilities & 1024 /* PathCaseSensitive */);
568
- const fromFilePath = this.toFilePath(from);
569
- const toFilePath = this.toFilePath(to);
570
- let isSameResourceWithDifferentPathCase = false;
571
- if (!isPathCaseSensitive) {
572
- isSameResourceWithDifferentPathCase = fromFilePath.toLowerCase() === toFilePath.toLowerCase();
573
- }
574
- if (isSameResourceWithDifferentPathCase && mode === 'copy') {
575
- throw (0, files_1.createFileSystemProviderError)("'File cannot be copied to same path with different path case", files_1.FileSystemProviderErrorCode.FileExists);
576
- }
577
- // handle existing target (unless this is a case change)
578
- if (!isSameResourceWithDifferentPathCase && await (0, util_1.promisify)(fs_1.exists)(toFilePath)) {
579
- if (!overwrite) {
580
- throw (0, files_1.createFileSystemProviderError)('File at target already exists', files_1.FileSystemProviderErrorCode.FileExists);
581
- }
582
- // Delete target
583
- await this.delete(to, { recursive: true, useTrash: false });
584
- }
585
- }
586
- async doCopy(source, target, copiedSourcesIn) {
587
- const copiedSources = copiedSourcesIn ? copiedSourcesIn : Object.create(null);
588
- const fileStat = await (0, util_1.promisify)(fs_1.stat)(source);
589
- if (!fileStat.isDirectory()) {
590
- return this.doCopyFile(source, target, fileStat.mode & 511);
591
- }
592
- if (copiedSources[source]) {
593
- return Promise.resolve(); // escape when there are cycles (can happen with symlinks)
594
- }
595
- copiedSources[source] = true; // remember as copied
596
- // Create folder
597
- await this.mkdirp(target, fileStat.mode & 511);
598
- // Copy each file recursively
599
- const files = await (0, util_1.promisify)(fs_1.readdir)(source);
600
- for (let i = 0; i < files.length; i++) {
601
- const file = files[i];
602
- await this.doCopy((0, path_1.join)(source, file), (0, path_1.join)(target, file), copiedSources);
603
- }
604
- }
605
- async mkdirp(path, mode) {
606
- const mkdir = async () => {
607
- try {
608
- await (0, util_1.promisify)(fs.mkdir)(path, mode);
609
- }
610
- catch (error) {
611
- // ENOENT: a parent folder does not exist yet
612
- if (error.code === 'ENOENT') {
613
- throw error;
614
- }
615
- // Any other error: check if folder exists and
616
- // return normally in that case if its a folder
617
- let targetIsFile = false;
618
- try {
619
- const fileStat = await (0, util_1.promisify)(fs.stat)(path);
620
- targetIsFile = !fileStat.isDirectory();
621
- }
622
- catch (statError) {
623
- throw error; // rethrow original error if stat fails
624
- }
625
- if (targetIsFile) {
626
- throw new Error(`'${path}' exists and is not a directory.`);
627
- }
628
- }
629
- };
630
- // stop at root
631
- if (path === (0, path_1.dirname)(path)) {
632
- return;
633
- }
634
- try {
635
- await mkdir();
636
- }
637
- catch (error) {
638
- // ENOENT: a parent folder does not exist yet, continue
639
- // to create the parent folder and then try again.
640
- if (error.code === 'ENOENT') {
641
- await this.mkdirp((0, path_1.dirname)(path), mode);
642
- return mkdir();
643
- }
644
- // Any other error
645
- throw error;
646
- }
647
- }
648
- doCopyFile(source, target, mode) {
649
- return new Promise((resolve, reject) => {
650
- const reader = fs.createReadStream(source);
651
- const writer = fs.createWriteStream(target, { mode });
652
- let finished = false;
653
- const finish = (error) => {
654
- if (!finished) {
655
- finished = true;
656
- // in error cases, pass to callback
657
- if (error) {
658
- return reject(error);
659
- }
660
- // we need to explicitly chmod because of https://github.com/nodejs/node/issues/1104
661
- fs.chmod(target, mode, error => error ? reject(error) : resolve());
662
- }
663
- };
664
- // handle errors properly
665
- reader.once('error', error => finish(error));
666
- writer.once('error', error => finish(error));
667
- // we are done (underlying fd has been closed)
668
- writer.once('close', () => finish());
669
- // start piping
670
- reader.pipe(writer);
671
- });
672
- }
673
- // #endregion
674
- // #region File Watching
675
- watch(resource, opts) {
676
- const watcherService = this.watcher;
677
- /**
678
- * Disposable handle. Can be disposed early (before the watcher is allocated.)
679
- */
680
- const handle = {
681
- disposed: false,
682
- watcherId: undefined,
683
- dispose() {
684
- if (this.disposed) {
685
- return;
686
- }
687
- if (this.watcherId !== undefined) {
688
- watcherService.unwatchFileChanges(this.watcherId);
689
- }
690
- this.disposed = true;
691
- },
692
- };
693
- watcherService.watchFileChanges(resource.toString(), {
694
- // Convert from `files.WatchOptions` to internal `watcher-protocol.WatchOptions`:
695
- ignored: opts.excludes
696
- }).then(watcherId => {
697
- if (handle.disposed) {
698
- watcherService.unwatchFileChanges(watcherId);
699
- }
700
- else {
701
- handle.watcherId = watcherId;
702
- }
703
- });
704
- this.toDispose.push(handle);
705
- return handle;
706
- }
707
- // #endregion
708
- async updateFile(resource, changes, opts) {
709
- try {
710
- const content = await this.readFile(resource);
711
- const decoded = this.encodingService.decode(buffer_1.BinaryBuffer.wrap(content), opts.readEncoding);
712
- const newContent = vscode_languageserver_textdocument_1.TextDocument.update(vscode_languageserver_textdocument_1.TextDocument.create('', '', 1, decoded), changes, 2).getText();
713
- const encoding = await this.encodingService.toResourceEncoding(opts.writeEncoding, {
714
- overwriteEncoding: opts.overwriteEncoding,
715
- read: async (length) => {
716
- const fd = await this.open(resource, { create: false });
717
- try {
718
- const data = new Uint8Array(length);
719
- await this.read(fd, 0, data, 0, length);
720
- return data;
721
- }
722
- finally {
723
- await this.close(fd);
724
- }
725
- }
726
- });
727
- const encoded = this.encodingService.encode(newContent, encoding);
728
- await this.writeFile(resource, encoded.buffer, { create: false, overwrite: true });
729
- const stat = await this.stat(resource);
730
- return Object.assign(stat, { encoding: encoding.encoding });
731
- }
732
- catch (error) {
733
- throw this.toFileSystemProviderError(error);
734
- }
735
- }
736
- // #region Helpers
737
- toFilePath(resource) {
738
- return (0, path_1.normalize)(file_uri_1.FileUri.fsPath(resource));
739
- }
740
- toFileSystemProviderError(error) {
741
- if (error instanceof files_1.FileSystemProviderError) {
742
- return error; // avoid double conversion
743
- }
744
- let code;
745
- switch (error.code) {
746
- case 'ENOENT':
747
- code = files_1.FileSystemProviderErrorCode.FileNotFound;
748
- break;
749
- case 'EISDIR':
750
- code = files_1.FileSystemProviderErrorCode.FileIsADirectory;
751
- break;
752
- case 'ENOTDIR':
753
- code = files_1.FileSystemProviderErrorCode.FileNotADirectory;
754
- break;
755
- case 'EEXIST':
756
- code = files_1.FileSystemProviderErrorCode.FileExists;
757
- break;
758
- case 'EPERM':
759
- case 'EACCES':
760
- code = files_1.FileSystemProviderErrorCode.NoPermissions;
761
- break;
762
- default:
763
- code = files_1.FileSystemProviderErrorCode.Unknown;
764
- }
765
- return (0, files_1.createFileSystemProviderError)(error, code);
766
- }
767
- // #endregion
768
- dispose() {
769
- this.toDispose.dispose();
770
- }
771
- };
772
- __decorate([
773
- (0, inversify_1.inject)(filesystem_watcher_protocol_1.FileSystemWatcherServer),
774
- __metadata("design:type", Object)
775
- ], DiskFileSystemProvider.prototype, "watcher", void 0);
776
- __decorate([
777
- (0, inversify_1.inject)(encoding_service_1.EncodingService),
778
- __metadata("design:type", encoding_service_1.EncodingService)
779
- ], DiskFileSystemProvider.prototype, "encodingService", void 0);
780
- __decorate([
781
- (0, inversify_1.postConstruct)(),
782
- __metadata("design:type", Function),
783
- __metadata("design:paramtypes", []),
784
- __metadata("design:returntype", void 0)
785
- ], DiskFileSystemProvider.prototype, "init", null);
786
- DiskFileSystemProvider = __decorate([
787
- (0, inversify_1.injectable)()
788
- ], DiskFileSystemProvider);
789
- exports.DiskFileSystemProvider = DiskFileSystemProvider;
1
+ "use strict";
2
+ // *****************************************************************************
3
+ // Copyright (C) 2020 TypeFox and others.
4
+ //
5
+ // This program and the accompanying materials are made available under the
6
+ // terms of the Eclipse Public License v. 2.0 which is available at
7
+ // http://www.eclipse.org/legal/epl-2.0.
8
+ //
9
+ // This Source Code may also be made available under the following Secondary
10
+ // Licenses when the conditions for such availability set forth in the Eclipse
11
+ // Public License v. 2.0 are satisfied: GNU General Public License, version 2
12
+ // with the GNU Classpath Exception which is available at
13
+ // https://www.gnu.org/software/classpath/license.html.
14
+ //
15
+ // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
16
+ // *****************************************************************************
17
+ /*---------------------------------------------------------------------------------------------
18
+ * Copyright (c) Microsoft Corporation. All rights reserved.
19
+ * Licensed under the MIT License. See License.txt in the project root for license information.
20
+ *--------------------------------------------------------------------------------------------*/
21
+ // based on https://github.com/microsoft/vscode/blob/04c36be045a94fee58e5f8992d3e3fd980294a84/src/vs/platform/files/node/diskFileSystemProvider.ts
22
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
23
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
24
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
25
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
26
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
27
+ };
28
+ var __metadata = (this && this.__metadata) || function (k, v) {
29
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
30
+ };
31
+ Object.defineProperty(exports, "__esModule", { value: true });
32
+ exports.DiskFileSystemProvider = void 0;
33
+ /* eslint-disable no-null/no-null */
34
+ /* eslint-disable @typescript-eslint/no-shadow */
35
+ const inversify_1 = require("@theia/core/shared/inversify");
36
+ const path_1 = require("path");
37
+ const uuid_1 = require("@theia/core/lib/common/uuid");
38
+ const os = require("os");
39
+ const fs = require("fs");
40
+ const fs_1 = require("fs");
41
+ const util_1 = require("util");
42
+ const uri_1 = require("@theia/core/lib/common/uri");
43
+ const path_2 = require("@theia/core/lib/common/path");
44
+ const file_uri_1 = require("@theia/core/lib/common/file-uri");
45
+ const event_1 = require("@theia/core/lib/common/event");
46
+ const disposable_1 = require("@theia/core/lib/common/disposable");
47
+ const os_1 = require("@theia/core/lib/common/os");
48
+ const promise_util_1 = require("@theia/core/lib/common/promise-util");
49
+ const files_1 = require("../common/files");
50
+ const filesystem_watcher_protocol_1 = require("../common/filesystem-watcher-protocol");
51
+ const trash = require("trash");
52
+ const vscode_languageserver_textdocument_1 = require("vscode-languageserver-textdocument");
53
+ const encoding_service_1 = require("@theia/core/lib/common/encoding-service");
54
+ const buffer_1 = require("@theia/core/lib/common/buffer");
55
+ const stream_1 = require("@theia/core/lib/common/stream");
56
+ const io_1 = require("../common/io");
57
+ const stat_mode_1 = require("stat-mode");
58
+ let DiskFileSystemProvider = class DiskFileSystemProvider {
59
+ constructor() {
60
+ this.BUFFER_SIZE = 64 * 1024;
61
+ this.onDidChangeFileEmitter = new event_1.Emitter();
62
+ this.onDidChangeFile = this.onDidChangeFileEmitter.event;
63
+ this.onFileWatchErrorEmitter = new event_1.Emitter();
64
+ this.onFileWatchError = this.onFileWatchErrorEmitter.event;
65
+ this.toDispose = new disposable_1.DisposableCollection(this.onDidChangeFileEmitter);
66
+ // #region File Capabilities
67
+ this.onDidChangeCapabilities = event_1.Event.None;
68
+ this.mapHandleToPos = new Map();
69
+ this.writeHandles = new Set();
70
+ this.canFlush = true;
71
+ }
72
+ init() {
73
+ this.toDispose.push(this.watcher);
74
+ this.watcher.setClient({
75
+ onDidFilesChanged: params => this.onDidChangeFileEmitter.fire(params.changes.map(({ uri, type }) => ({
76
+ resource: new uri_1.default(uri),
77
+ type
78
+ }))),
79
+ onError: () => this.onFileWatchErrorEmitter.fire()
80
+ });
81
+ }
82
+ get capabilities() {
83
+ if (!this._capabilities) {
84
+ this._capabilities =
85
+ 2 /* FileReadWrite */ |
86
+ 4 /* FileOpenReadWriteClose */ |
87
+ 16 /* FileReadStream */ |
88
+ 8 /* FileFolderCopy */ |
89
+ 16777216 /* Access */ |
90
+ 4096 /* Trash */ |
91
+ 33554432 /* Update */;
92
+ if (os_1.OS.type() === os_1.OS.Type.Linux) {
93
+ this._capabilities |= 1024 /* PathCaseSensitive */;
94
+ }
95
+ }
96
+ return this._capabilities;
97
+ }
98
+ // #endregion
99
+ // #region File Metadata Resolving
100
+ async stat(resource) {
101
+ try {
102
+ const { stat, symbolicLink } = await this.statLink(this.toFilePath(resource)); // cannot use fs.stat() here to support links properly
103
+ const mode = new stat_mode_1.Mode(stat);
104
+ return {
105
+ type: this.toType(stat, symbolicLink),
106
+ ctime: stat.birthtime.getTime(),
107
+ mtime: stat.mtime.getTime(),
108
+ size: stat.size,
109
+ permissions: !mode.owner.write ? files_1.FilePermission.Readonly : undefined,
110
+ };
111
+ }
112
+ catch (error) {
113
+ throw this.toFileSystemProviderError(error);
114
+ }
115
+ }
116
+ async access(resource, mode) {
117
+ try {
118
+ await (0, util_1.promisify)(fs.access)(this.toFilePath(resource), mode);
119
+ }
120
+ catch (error) {
121
+ throw this.toFileSystemProviderError(error);
122
+ }
123
+ }
124
+ async fsPath(resource) {
125
+ return file_uri_1.FileUri.fsPath(resource);
126
+ }
127
+ async statLink(path) {
128
+ // First stat the link
129
+ let lstats;
130
+ try {
131
+ lstats = await (0, util_1.promisify)(fs_1.lstat)(path);
132
+ // Return early if the stat is not a symbolic link at all
133
+ if (!lstats.isSymbolicLink()) {
134
+ return { stat: lstats };
135
+ }
136
+ }
137
+ catch (error) {
138
+ /* ignore - use stat() instead */
139
+ }
140
+ // If the stat is a symbolic link or failed to stat, use fs.stat()
141
+ // which for symbolic links will stat the target they point to
142
+ try {
143
+ const stats = await (0, util_1.promisify)(fs_1.stat)(path);
144
+ return { stat: stats, symbolicLink: (lstats === null || lstats === void 0 ? void 0 : lstats.isSymbolicLink()) ? { dangling: false } : undefined };
145
+ }
146
+ catch (error) {
147
+ // If the link points to a non-existing file we still want
148
+ // to return it as result while setting dangling: true flag
149
+ if (error.code === 'ENOENT' && lstats) {
150
+ return { stat: lstats, symbolicLink: { dangling: true } };
151
+ }
152
+ throw error;
153
+ }
154
+ }
155
+ async readdir(resource) {
156
+ try {
157
+ const children = await (0, util_1.promisify)(fs.readdir)(this.toFilePath(resource));
158
+ const result = [];
159
+ await Promise.all(children.map(async (child) => {
160
+ try {
161
+ const stat = await this.stat(resource.resolve(child));
162
+ result.push([child, stat.type]);
163
+ }
164
+ catch (error) {
165
+ console.trace(error); // ignore errors for individual entries that can arise from permission denied
166
+ }
167
+ }));
168
+ return result;
169
+ }
170
+ catch (error) {
171
+ throw this.toFileSystemProviderError(error);
172
+ }
173
+ }
174
+ toType(entry, symbolicLink) {
175
+ // Signal file type by checking for file / directory, except:
176
+ // - symbolic links pointing to non-existing files are FileType.Unknown
177
+ // - files that are neither file nor directory are FileType.Unknown
178
+ let type;
179
+ if (symbolicLink === null || symbolicLink === void 0 ? void 0 : symbolicLink.dangling) {
180
+ type = files_1.FileType.Unknown;
181
+ }
182
+ else if (entry.isFile()) {
183
+ type = files_1.FileType.File;
184
+ }
185
+ else if (entry.isDirectory()) {
186
+ type = files_1.FileType.Directory;
187
+ }
188
+ else {
189
+ type = files_1.FileType.Unknown;
190
+ }
191
+ // Always signal symbolic link as file type additionally
192
+ if (symbolicLink) {
193
+ type |= files_1.FileType.SymbolicLink;
194
+ }
195
+ return type;
196
+ }
197
+ // #endregion
198
+ // #region File Reading/Writing
199
+ async readFile(resource) {
200
+ try {
201
+ const filePath = this.toFilePath(resource);
202
+ return await (0, util_1.promisify)(fs_1.readFile)(filePath);
203
+ }
204
+ catch (error) {
205
+ throw this.toFileSystemProviderError(error);
206
+ }
207
+ }
208
+ readFileStream(resource, opts, token) {
209
+ const stream = (0, stream_1.newWriteableStream)(data => buffer_1.BinaryBuffer.concat(data.map(data => buffer_1.BinaryBuffer.wrap(data))).buffer);
210
+ (0, io_1.readFileIntoStream)(this, resource, stream, data => data.buffer, {
211
+ ...opts,
212
+ bufferSize: this.BUFFER_SIZE
213
+ }, token);
214
+ return stream;
215
+ }
216
+ async writeFile(resource, content, opts) {
217
+ let handle = undefined;
218
+ try {
219
+ const filePath = this.toFilePath(resource);
220
+ // Validate target unless { create: true, overwrite: true }
221
+ if (!opts.create || !opts.overwrite) {
222
+ const fileExists = await (0, util_1.promisify)(fs_1.exists)(filePath);
223
+ if (fileExists) {
224
+ if (!opts.overwrite) {
225
+ throw (0, files_1.createFileSystemProviderError)('File already exists', files_1.FileSystemProviderErrorCode.FileExists);
226
+ }
227
+ }
228
+ else {
229
+ if (!opts.create) {
230
+ throw (0, files_1.createFileSystemProviderError)('File does not exist', files_1.FileSystemProviderErrorCode.FileNotFound);
231
+ }
232
+ }
233
+ }
234
+ // Open
235
+ handle = await this.open(resource, { create: true });
236
+ // Write content at once
237
+ await this.write(handle, 0, content, 0, content.byteLength);
238
+ }
239
+ catch (error) {
240
+ throw this.toFileSystemProviderError(error);
241
+ }
242
+ finally {
243
+ if (typeof handle === 'number') {
244
+ await this.close(handle);
245
+ }
246
+ }
247
+ }
248
+ async open(resource, opts) {
249
+ try {
250
+ const filePath = this.toFilePath(resource);
251
+ let flags = undefined;
252
+ if (opts.create) {
253
+ if (os_1.isWindows && await (0, util_1.promisify)(fs_1.exists)(filePath)) {
254
+ try {
255
+ // On Windows and if the file exists, we use a different strategy of saving the file
256
+ // by first truncating the file and then writing with r+ flag. This helps to save hidden files on Windows
257
+ // (see https://github.com/Microsoft/vscode/issues/931) and prevent removing alternate data streams
258
+ // (see https://github.com/Microsoft/vscode/issues/6363)
259
+ await (0, util_1.promisify)(fs_1.truncate)(filePath, 0);
260
+ // After a successful truncate() the flag can be set to 'r+' which will not truncate.
261
+ flags = 'r+';
262
+ }
263
+ catch (error) {
264
+ console.trace(error);
265
+ }
266
+ }
267
+ // we take opts.create as a hint that the file is opened for writing
268
+ // as such we use 'w' to truncate an existing or create the
269
+ // file otherwise. we do not allow reading.
270
+ if (!flags) {
271
+ flags = 'w';
272
+ }
273
+ }
274
+ else {
275
+ // otherwise we assume the file is opened for reading
276
+ // as such we use 'r' to neither truncate, nor create
277
+ // the file.
278
+ flags = 'r';
279
+ }
280
+ const handle = await (0, util_1.promisify)(fs_1.open)(filePath, flags);
281
+ // remember this handle to track file position of the handle
282
+ // we init the position to 0 since the file descriptor was
283
+ // just created and the position was not moved so far (see
284
+ // also http://man7.org/linux/man-pages/man2/open.2.html -
285
+ // "The file offset is set to the beginning of the file.")
286
+ this.mapHandleToPos.set(handle, 0);
287
+ // remember that this handle was used for writing
288
+ if (opts.create) {
289
+ this.writeHandles.add(handle);
290
+ }
291
+ return handle;
292
+ }
293
+ catch (error) {
294
+ throw this.toFileSystemProviderError(error);
295
+ }
296
+ }
297
+ async close(fd) {
298
+ try {
299
+ // remove this handle from map of positions
300
+ this.mapHandleToPos.delete(fd);
301
+ // if a handle is closed that was used for writing, ensure
302
+ // to flush the contents to disk if possible.
303
+ if (this.writeHandles.delete(fd) && this.canFlush) {
304
+ try {
305
+ await (0, util_1.promisify)(fs_1.fdatasync)(fd);
306
+ }
307
+ catch (error) {
308
+ // In some exotic setups it is well possible that node fails to sync
309
+ // In that case we disable flushing and log the error to our logger
310
+ this.canFlush = false;
311
+ console.error(error);
312
+ }
313
+ }
314
+ return await (0, util_1.promisify)(fs_1.close)(fd);
315
+ }
316
+ catch (error) {
317
+ throw this.toFileSystemProviderError(error);
318
+ }
319
+ }
320
+ async read(fd, pos, data, offset, length) {
321
+ const normalizedPos = this.normalizePos(fd, pos);
322
+ let bytesRead = null;
323
+ try {
324
+ const result = await (0, util_1.promisify)(fs_1.read)(fd, data, offset, length, normalizedPos);
325
+ if (typeof result === 'number') {
326
+ bytesRead = result; // node.d.ts fail
327
+ }
328
+ else {
329
+ bytesRead = result.bytesRead;
330
+ }
331
+ return bytesRead;
332
+ }
333
+ catch (error) {
334
+ throw this.toFileSystemProviderError(error);
335
+ }
336
+ finally {
337
+ this.updatePos(fd, normalizedPos, bytesRead);
338
+ }
339
+ }
340
+ normalizePos(fd, pos) {
341
+ // when calling fs.read/write we try to avoid passing in the "pos" argument and
342
+ // rather prefer to pass in "null" because this avoids an extra seek(pos)
343
+ // call that in some cases can even fail (e.g. when opening a file over FTP -
344
+ // see https://github.com/microsoft/vscode/issues/73884).
345
+ //
346
+ // as such, we compare the passed in position argument with our last known
347
+ // position for the file descriptor and use "null" if they match.
348
+ if (pos === this.mapHandleToPos.get(fd)) {
349
+ return null;
350
+ }
351
+ return pos;
352
+ }
353
+ updatePos(fd, pos, bytesLength) {
354
+ const lastKnownPos = this.mapHandleToPos.get(fd);
355
+ if (typeof lastKnownPos === 'number') {
356
+ // pos !== null signals that previously a position was used that is
357
+ // not null. node.js documentation explains, that in this case
358
+ // the internal file pointer is not moving and as such we do not move
359
+ // our position pointer.
360
+ //
361
+ // Docs: "If position is null, data will be read from the current file position,
362
+ // and the file position will be updated. If position is an integer, the file position
363
+ // will remain unchanged."
364
+ if (typeof pos === 'number') {
365
+ // do not modify the position
366
+ }
367
+ else if (typeof bytesLength === 'number') {
368
+ this.mapHandleToPos.set(fd, lastKnownPos + bytesLength);
369
+ }
370
+ else {
371
+ this.mapHandleToPos.delete(fd);
372
+ }
373
+ }
374
+ }
375
+ async write(fd, pos, data, offset, length) {
376
+ // we know at this point that the file to write to is truncated and thus empty
377
+ // if the write now fails, the file remains empty. as such we really try hard
378
+ // to ensure the write succeeds by retrying up to three times.
379
+ return (0, promise_util_1.retry)(() => this.doWrite(fd, pos, data, offset, length), 100 /* ms delay */, 3 /* retries */);
380
+ }
381
+ async doWrite(fd, pos, data, offset, length) {
382
+ const normalizedPos = this.normalizePos(fd, pos);
383
+ let bytesWritten = null;
384
+ try {
385
+ const result = await (0, util_1.promisify)(fs_1.write)(fd, data, offset, length, normalizedPos);
386
+ if (typeof result === 'number') {
387
+ bytesWritten = result; // node.d.ts fail
388
+ }
389
+ else {
390
+ bytesWritten = result.bytesWritten;
391
+ }
392
+ return bytesWritten;
393
+ }
394
+ catch (error) {
395
+ throw this.toFileSystemProviderError(error);
396
+ }
397
+ finally {
398
+ this.updatePos(fd, normalizedPos, bytesWritten);
399
+ }
400
+ }
401
+ // #endregion
402
+ // #region Move/Copy/Delete/Create Folder
403
+ async mkdir(resource) {
404
+ try {
405
+ await (0, util_1.promisify)(fs_1.mkdir)(this.toFilePath(resource));
406
+ }
407
+ catch (error) {
408
+ throw this.toFileSystemProviderError(error);
409
+ }
410
+ }
411
+ async delete(resource, opts) {
412
+ try {
413
+ const filePath = this.toFilePath(resource);
414
+ await this.doDelete(filePath, opts);
415
+ }
416
+ catch (error) {
417
+ throw this.toFileSystemProviderError(error);
418
+ }
419
+ }
420
+ async doDelete(filePath, opts) {
421
+ if (!opts.useTrash) {
422
+ if (opts.recursive) {
423
+ await this.rimraf(filePath);
424
+ }
425
+ else {
426
+ const stat = await (0, util_1.promisify)(fs_1.lstat)(filePath);
427
+ if (stat.isDirectory() && !stat.isSymbolicLink()) {
428
+ await (0, util_1.promisify)(fs_1.rmdir)(filePath);
429
+ }
430
+ else {
431
+ await (0, util_1.promisify)(fs_1.unlink)(filePath);
432
+ }
433
+ }
434
+ }
435
+ else {
436
+ await trash(filePath);
437
+ }
438
+ }
439
+ rimraf(path) {
440
+ if (new path_2.Path(path).isRoot) {
441
+ throw new Error('rimraf - will refuse to recursively delete root');
442
+ }
443
+ return this.rimrafMove(path);
444
+ }
445
+ async rimrafMove(path) {
446
+ try {
447
+ const pathInTemp = (0, path_1.join)(os.tmpdir(), (0, uuid_1.generateUuid)());
448
+ try {
449
+ await (0, util_1.promisify)(fs_1.rename)(path, pathInTemp);
450
+ }
451
+ catch (error) {
452
+ return this.rimrafUnlink(path); // if rename fails, delete without tmp dir
453
+ }
454
+ // Delete but do not return as promise
455
+ this.rimrafUnlink(pathInTemp);
456
+ }
457
+ catch (error) {
458
+ if (error.code !== 'ENOENT') {
459
+ throw error;
460
+ }
461
+ }
462
+ }
463
+ async rimrafUnlink(path) {
464
+ try {
465
+ const stat = await (0, util_1.promisify)(fs_1.lstat)(path);
466
+ // Folder delete (recursive) - NOT for symbolic links though!
467
+ if (stat.isDirectory() && !stat.isSymbolicLink()) {
468
+ // Children
469
+ const children = await (0, util_1.promisify)(fs_1.readdir)(path);
470
+ await Promise.all(children.map(child => this.rimrafUnlink((0, path_1.join)(path, child))));
471
+ // Folder
472
+ await (0, util_1.promisify)(fs_1.rmdir)(path);
473
+ }
474
+ else {
475
+ // chmod as needed to allow for unlink
476
+ const mode = stat.mode;
477
+ if (!(mode & 128)) { // 128 === 0200
478
+ await (0, util_1.promisify)(fs_1.chmod)(path, mode | 128);
479
+ }
480
+ return (0, util_1.promisify)(fs_1.unlink)(path);
481
+ }
482
+ }
483
+ catch (error) {
484
+ if (error.code !== 'ENOENT') {
485
+ throw error;
486
+ }
487
+ }
488
+ }
489
+ async rename(from, to, opts) {
490
+ const fromFilePath = this.toFilePath(from);
491
+ const toFilePath = this.toFilePath(to);
492
+ if (fromFilePath === toFilePath) {
493
+ return; // simulate node.js behaviour here and do a no-op if paths match
494
+ }
495
+ try {
496
+ // Ensure target does not exist
497
+ await this.validateTargetDeleted(from, to, 'move', opts.overwrite);
498
+ // Move
499
+ await this.move(fromFilePath, toFilePath);
500
+ }
501
+ catch (error) {
502
+ // rewrite some typical errors that can happen especially around symlinks
503
+ // to something the user can better understand
504
+ if (error.code === 'EINVAL' || error.code === 'EBUSY' || error.code === 'ENAMETOOLONG') {
505
+ error = new Error(`Unable to move '${(0, path_1.basename)(fromFilePath)}' into '${(0, path_1.basename)((0, path_1.dirname)(toFilePath))}' (${error.toString()}).`);
506
+ }
507
+ throw this.toFileSystemProviderError(error);
508
+ }
509
+ }
510
+ async move(source, target) {
511
+ if (source === target) {
512
+ return Promise.resolve();
513
+ }
514
+ async function updateMtime(path) {
515
+ const stat = await (0, util_1.promisify)(fs_1.lstat)(path);
516
+ if (stat.isDirectory() || stat.isSymbolicLink()) {
517
+ return Promise.resolve(); // only for files
518
+ }
519
+ const fd = await (0, util_1.promisify)(fs_1.open)(path, 'a');
520
+ try {
521
+ await (0, util_1.promisify)(fs_1.futimes)(fd, stat.atime, new Date());
522
+ }
523
+ catch (error) {
524
+ // ignore
525
+ }
526
+ return (0, util_1.promisify)(fs_1.close)(fd);
527
+ }
528
+ try {
529
+ await (0, util_1.promisify)(fs_1.rename)(source, target);
530
+ await updateMtime(target);
531
+ }
532
+ catch (error) {
533
+ // In two cases we fallback to classic copy and delete:
534
+ //
535
+ // 1.) The EXDEV error indicates that source and target are on different devices
536
+ // In this case, fallback to using a copy() operation as there is no way to
537
+ // rename() between different devices.
538
+ //
539
+ // 2.) The user tries to rename a file/folder that ends with a dot. This is not
540
+ // really possible to move then, at least on UNC devices.
541
+ if (source.toLowerCase() !== target.toLowerCase() && error.code === 'EXDEV' || source.endsWith('.')) {
542
+ await this.doCopy(source, target);
543
+ await this.rimraf(source);
544
+ await updateMtime(target);
545
+ }
546
+ else {
547
+ throw error;
548
+ }
549
+ }
550
+ }
551
+ async copy(from, to, opts) {
552
+ const fromFilePath = this.toFilePath(from);
553
+ const toFilePath = this.toFilePath(to);
554
+ if (fromFilePath === toFilePath) {
555
+ return; // simulate node.js behaviour here and do a no-op if paths match
556
+ }
557
+ try {
558
+ // Ensure target does not exist
559
+ await this.validateTargetDeleted(from, to, 'copy', opts.overwrite);
560
+ // Copy
561
+ await this.doCopy(fromFilePath, toFilePath);
562
+ }
563
+ catch (error) {
564
+ // rewrite some typical errors that can happen especially around symlinks
565
+ // to something the user can better understand
566
+ if (error.code === 'EINVAL' || error.code === 'EBUSY' || error.code === 'ENAMETOOLONG') {
567
+ error = new Error(`Unable to copy '${(0, path_1.basename)(fromFilePath)}' into '${(0, path_1.basename)((0, path_1.dirname)(toFilePath))}' (${error.toString()}).`);
568
+ }
569
+ throw this.toFileSystemProviderError(error);
570
+ }
571
+ }
572
+ async validateTargetDeleted(from, to, mode, overwrite) {
573
+ const isPathCaseSensitive = !!(this.capabilities & 1024 /* PathCaseSensitive */);
574
+ const fromFilePath = this.toFilePath(from);
575
+ const toFilePath = this.toFilePath(to);
576
+ let isSameResourceWithDifferentPathCase = false;
577
+ if (!isPathCaseSensitive) {
578
+ isSameResourceWithDifferentPathCase = fromFilePath.toLowerCase() === toFilePath.toLowerCase();
579
+ }
580
+ if (isSameResourceWithDifferentPathCase && mode === 'copy') {
581
+ throw (0, files_1.createFileSystemProviderError)("'File cannot be copied to same path with different path case", files_1.FileSystemProviderErrorCode.FileExists);
582
+ }
583
+ // handle existing target (unless this is a case change)
584
+ if (!isSameResourceWithDifferentPathCase && await (0, util_1.promisify)(fs_1.exists)(toFilePath)) {
585
+ if (!overwrite) {
586
+ throw (0, files_1.createFileSystemProviderError)('File at target already exists', files_1.FileSystemProviderErrorCode.FileExists);
587
+ }
588
+ // Delete target
589
+ await this.delete(to, { recursive: true, useTrash: false });
590
+ }
591
+ }
592
+ async doCopy(source, target, copiedSourcesIn) {
593
+ const copiedSources = copiedSourcesIn ? copiedSourcesIn : Object.create(null);
594
+ const fileStat = await (0, util_1.promisify)(fs_1.stat)(source);
595
+ if (!fileStat.isDirectory()) {
596
+ return this.doCopyFile(source, target, fileStat.mode & 511);
597
+ }
598
+ if (copiedSources[source]) {
599
+ return Promise.resolve(); // escape when there are cycles (can happen with symlinks)
600
+ }
601
+ copiedSources[source] = true; // remember as copied
602
+ // Create folder
603
+ await this.mkdirp(target, fileStat.mode & 511);
604
+ // Copy each file recursively
605
+ const files = await (0, util_1.promisify)(fs_1.readdir)(source);
606
+ for (let i = 0; i < files.length; i++) {
607
+ const file = files[i];
608
+ await this.doCopy((0, path_1.join)(source, file), (0, path_1.join)(target, file), copiedSources);
609
+ }
610
+ }
611
+ async mkdirp(path, mode) {
612
+ const mkdir = async () => {
613
+ try {
614
+ await (0, util_1.promisify)(fs.mkdir)(path, mode);
615
+ }
616
+ catch (error) {
617
+ // ENOENT: a parent folder does not exist yet
618
+ if (error.code === 'ENOENT') {
619
+ throw error;
620
+ }
621
+ // Any other error: check if folder exists and
622
+ // return normally in that case if its a folder
623
+ let targetIsFile = false;
624
+ try {
625
+ const fileStat = await (0, util_1.promisify)(fs.stat)(path);
626
+ targetIsFile = !fileStat.isDirectory();
627
+ }
628
+ catch (statError) {
629
+ throw error; // rethrow original error if stat fails
630
+ }
631
+ if (targetIsFile) {
632
+ throw new Error(`'${path}' exists and is not a directory.`);
633
+ }
634
+ }
635
+ };
636
+ // stop at root
637
+ if (path === (0, path_1.dirname)(path)) {
638
+ return;
639
+ }
640
+ try {
641
+ await mkdir();
642
+ }
643
+ catch (error) {
644
+ // ENOENT: a parent folder does not exist yet, continue
645
+ // to create the parent folder and then try again.
646
+ if (error.code === 'ENOENT') {
647
+ await this.mkdirp((0, path_1.dirname)(path), mode);
648
+ return mkdir();
649
+ }
650
+ // Any other error
651
+ throw error;
652
+ }
653
+ }
654
+ doCopyFile(source, target, mode) {
655
+ return new Promise((resolve, reject) => {
656
+ const reader = fs.createReadStream(source);
657
+ const writer = fs.createWriteStream(target, { mode });
658
+ let finished = false;
659
+ const finish = (error) => {
660
+ if (!finished) {
661
+ finished = true;
662
+ // in error cases, pass to callback
663
+ if (error) {
664
+ return reject(error);
665
+ }
666
+ // we need to explicitly chmod because of https://github.com/nodejs/node/issues/1104
667
+ fs.chmod(target, mode, error => error ? reject(error) : resolve());
668
+ }
669
+ };
670
+ // handle errors properly
671
+ reader.once('error', error => finish(error));
672
+ writer.once('error', error => finish(error));
673
+ // we are done (underlying fd has been closed)
674
+ writer.once('close', () => finish());
675
+ // start piping
676
+ reader.pipe(writer);
677
+ });
678
+ }
679
+ // #endregion
680
+ // #region File Watching
681
+ watch(resource, opts) {
682
+ const watcherService = this.watcher;
683
+ /**
684
+ * Disposable handle. Can be disposed early (before the watcher is allocated.)
685
+ */
686
+ const handle = {
687
+ disposed: false,
688
+ watcherId: undefined,
689
+ dispose() {
690
+ if (this.disposed) {
691
+ return;
692
+ }
693
+ if (this.watcherId !== undefined) {
694
+ watcherService.unwatchFileChanges(this.watcherId);
695
+ }
696
+ this.disposed = true;
697
+ },
698
+ };
699
+ watcherService.watchFileChanges(resource.toString(), {
700
+ // Convert from `files.WatchOptions` to internal `watcher-protocol.WatchOptions`:
701
+ ignored: opts.excludes
702
+ }).then(watcherId => {
703
+ if (handle.disposed) {
704
+ watcherService.unwatchFileChanges(watcherId);
705
+ }
706
+ else {
707
+ handle.watcherId = watcherId;
708
+ }
709
+ });
710
+ this.toDispose.push(handle);
711
+ return handle;
712
+ }
713
+ // #endregion
714
+ async updateFile(resource, changes, opts) {
715
+ try {
716
+ const content = await this.readFile(resource);
717
+ const decoded = this.encodingService.decode(buffer_1.BinaryBuffer.wrap(content), opts.readEncoding);
718
+ const newContent = vscode_languageserver_textdocument_1.TextDocument.update(vscode_languageserver_textdocument_1.TextDocument.create('', '', 1, decoded), changes, 2).getText();
719
+ const encoding = await this.encodingService.toResourceEncoding(opts.writeEncoding, {
720
+ overwriteEncoding: opts.overwriteEncoding,
721
+ read: async (length) => {
722
+ const fd = await this.open(resource, { create: false });
723
+ try {
724
+ const data = new Uint8Array(length);
725
+ await this.read(fd, 0, data, 0, length);
726
+ return data;
727
+ }
728
+ finally {
729
+ await this.close(fd);
730
+ }
731
+ }
732
+ });
733
+ const encoded = this.encodingService.encode(newContent, encoding);
734
+ await this.writeFile(resource, encoded.buffer, { create: false, overwrite: true });
735
+ const stat = await this.stat(resource);
736
+ return Object.assign(stat, { encoding: encoding.encoding });
737
+ }
738
+ catch (error) {
739
+ throw this.toFileSystemProviderError(error);
740
+ }
741
+ }
742
+ // #region Helpers
743
+ toFilePath(resource) {
744
+ return (0, path_1.normalize)(file_uri_1.FileUri.fsPath(resource));
745
+ }
746
+ toFileSystemProviderError(error) {
747
+ if (error instanceof files_1.FileSystemProviderError) {
748
+ return error; // avoid double conversion
749
+ }
750
+ let code;
751
+ switch (error.code) {
752
+ case 'ENOENT':
753
+ code = files_1.FileSystemProviderErrorCode.FileNotFound;
754
+ break;
755
+ case 'EISDIR':
756
+ code = files_1.FileSystemProviderErrorCode.FileIsADirectory;
757
+ break;
758
+ case 'ENOTDIR':
759
+ code = files_1.FileSystemProviderErrorCode.FileNotADirectory;
760
+ break;
761
+ case 'EEXIST':
762
+ code = files_1.FileSystemProviderErrorCode.FileExists;
763
+ break;
764
+ case 'EPERM':
765
+ case 'EACCES':
766
+ code = files_1.FileSystemProviderErrorCode.NoPermissions;
767
+ break;
768
+ default:
769
+ code = files_1.FileSystemProviderErrorCode.Unknown;
770
+ }
771
+ return (0, files_1.createFileSystemProviderError)(error, code);
772
+ }
773
+ // #endregion
774
+ dispose() {
775
+ this.toDispose.dispose();
776
+ }
777
+ };
778
+ __decorate([
779
+ (0, inversify_1.inject)(filesystem_watcher_protocol_1.FileSystemWatcherServer),
780
+ __metadata("design:type", Object)
781
+ ], DiskFileSystemProvider.prototype, "watcher", void 0);
782
+ __decorate([
783
+ (0, inversify_1.inject)(encoding_service_1.EncodingService),
784
+ __metadata("design:type", encoding_service_1.EncodingService)
785
+ ], DiskFileSystemProvider.prototype, "encodingService", void 0);
786
+ __decorate([
787
+ (0, inversify_1.postConstruct)(),
788
+ __metadata("design:type", Function),
789
+ __metadata("design:paramtypes", []),
790
+ __metadata("design:returntype", void 0)
791
+ ], DiskFileSystemProvider.prototype, "init", null);
792
+ DiskFileSystemProvider = __decorate([
793
+ (0, inversify_1.injectable)()
794
+ ], DiskFileSystemProvider);
795
+ exports.DiskFileSystemProvider = DiskFileSystemProvider;
790
796
  //# sourceMappingURL=disk-file-system-provider.js.map