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