/
files.ts
966 lines (821 loc) · 33.8 KB
/
files.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
// *****************************************************************************
// Copyright (C) 2020 TypeFox and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0.
//
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
// with the GNU Classpath Exception which is available at
// https://www.gnu.org/software/classpath/license.html.
//
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
// *****************************************************************************
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
// based on https://github.com/microsoft/vscode/blob/04c36be045a94fee58e5f8992d3e3fd980294a84/src/vs/platform/files/common/files.ts
import URI from '@theia/core/lib/common/uri';
import { Event } from '@theia/core/lib/common/event';
import { Disposable as IDisposable } from '@theia/core/lib/common/disposable';
import { BinaryBuffer, BinaryBufferReadableStream } from '@theia/core/lib/common/buffer';
import type { TextDocumentContentChangeEvent } from '@theia/core/shared/vscode-languageserver-protocol';
import { ReadableStreamEvents } from '@theia/core/lib/common/stream';
import { CancellationToken } from '@theia/core/lib/common/cancellation';
export const enum FileOperation {
CREATE,
DELETE,
MOVE,
COPY
}
export class FileOperationEvent {
constructor(resource: URI, operation: FileOperation.DELETE);
constructor(resource: URI, operation: FileOperation.CREATE | FileOperation.MOVE | FileOperation.COPY, target: FileStatWithMetadata);
constructor(public readonly resource: URI, public readonly operation: FileOperation, public readonly target?: FileStatWithMetadata) { }
isOperation(operation: FileOperation.DELETE): boolean;
isOperation(operation: FileOperation.MOVE | FileOperation.COPY | FileOperation.CREATE): this is { readonly target: FileStatWithMetadata };
isOperation(operation: FileOperation): boolean {
return this.operation === operation;
}
}
/**
* Possible changes that can occur to a file.
*/
export const enum FileChangeType {
UPDATED = 0,
ADDED = 1,
DELETED = 2
}
/**
* Identifies a single change in a file.
*/
export interface FileChange {
/**
* The type of change that occurred to the file.
*/
readonly type: FileChangeType;
/**
* The unified resource identifier of the file that changed.
*/
readonly resource: URI;
}
export class FileChangesEvent {
constructor(public readonly changes: readonly FileChange[]) { }
/**
* Returns true if this change event contains the provided file with the given change type (if provided). In case of
* type DELETED, this method will also return true if a folder got deleted that is the parent of the
* provided file path.
*/
contains(resource: URI, type?: FileChangeType): boolean {
if (!resource) {
return false;
}
const checkForChangeType = typeof type === 'number';
return this.changes.some(change => {
if (checkForChangeType && change.type !== type) {
return false;
}
// For deleted also return true when deleted folder is parent of target path
if (change.type === FileChangeType.DELETED) {
return change.resource.isEqualOrParent(resource);
}
return change.resource.toString() === resource.toString();
});
}
/**
* Returns the changes that describe added files.
*/
getAdded(): FileChange[] {
return this.getOfType(FileChangeType.ADDED);
}
/**
* Returns if this event contains added files.
*/
gotAdded(): boolean {
return this.hasType(FileChangeType.ADDED);
}
/**
* Returns the changes that describe deleted files.
*/
getDeleted(): FileChange[] {
return this.getOfType(FileChangeType.DELETED);
}
/**
* Returns if this event contains deleted files.
*/
gotDeleted(): boolean {
return this.hasType(FileChangeType.DELETED);
}
/**
* Returns the changes that describe updated files.
*/
getUpdated(): FileChange[] {
return this.getOfType(FileChangeType.UPDATED);
}
/**
* Returns if this event contains updated files.
*/
gotUpdated(): boolean {
return this.hasType(FileChangeType.UPDATED);
}
private getOfType(type: FileChangeType): FileChange[] {
return this.changes.filter(change => change.type === type);
}
private hasType(type: FileChangeType): boolean {
return this.changes.some(change => change.type === type);
}
}
export interface BaseStat {
/**
* The unified resource identifier of this file or folder.
*/
resource: URI;
/**
* The name which is the last segment
* of the {{path}}.
*/
name: string;
/**
* The size of the file.
*
* The value may or may not be resolved as
* it is optional.
*/
size?: number;
/**
* The last modification date represented as millis from unix epoch.
*
* The value may or may not be resolved as
* it is optional.
*/
mtime?: number;
/**
* The creation date represented as millis from unix epoch.
*
* The value may or may not be resolved as
* it is optional.
*/
ctime?: number;
/**
* A unique identifier that represents the
* current state of the file or directory.
*
* The value may or may not be resolved as
* it is optional.
*/
etag?: string;
}
export namespace BaseStat {
export function is(arg: unknown): arg is BaseStat {
return !!arg && typeof arg === 'object'
&& ('resource' in arg && (arg as BaseStat).resource instanceof URI)
&& ('name' in arg && typeof (arg as BaseStat).name === 'string');
}
}
export interface BaseStatWithMetadata extends BaseStat {
mtime: number;
ctime: number;
etag: string;
size: number;
}
/**
* A file resource with meta information.
*/
export interface FileStat extends BaseStat {
/**
* The resource is a file.
*/
isFile: boolean;
/**
* The resource is a directory.
*/
isDirectory: boolean;
/**
* The resource is a symbolic link.
*/
isSymbolicLink: boolean;
/**
* The children of the file stat or undefined if none.
*/
children?: FileStat[];
}
export namespace FileStat {
export function is(arg: unknown): arg is FileStat {
const fileStat = arg as FileStat;
return BaseStat.is(fileStat) &&
('isFile' in fileStat && typeof fileStat.isFile === 'boolean') &&
('isDirectory' in fileStat && typeof fileStat.isDirectory === 'boolean') &&
('isSymbolicLink' in fileStat && typeof fileStat.isSymbolicLink === 'boolean');
}
export function asFileType(stat: FileStat): FileType {
let res = 0;
if (stat.isFile) {
res += FileType.File;
} else if (stat.isDirectory) {
res += FileType.Directory;
}
if (stat.isSymbolicLink) {
res += FileType.SymbolicLink;
}
return res;
}
export function toStat(stat: FileStat): Stat | { type: FileType } & Partial<Stat> {
return {
type: asFileType(stat),
ctime: stat.ctime,
mtime: stat.mtime,
size: stat.size
};
}
export function fromStat(resource: URI, stat: Stat): FileStatWithMetadata;
export function fromStat(resource: URI, stat: { type: FileType } & Partial<Stat>): FileStat;
export function fromStat(resource: URI, stat: Stat | { type: FileType } & Partial<Stat>): FileStat {
return {
resource,
name: resource.path.base || resource.path.toString(),
isFile: (stat.type & FileType.File) !== 0,
isDirectory: (stat.type & FileType.Directory) !== 0,
isSymbolicLink: (stat.type & FileType.SymbolicLink) !== 0,
mtime: stat.mtime,
ctime: stat.ctime,
size: stat.size,
etag: etag({ mtime: stat.mtime, size: stat.size })
};
}
export function dir(resource: string | URI, stat?: Partial<Omit<Stat, 'type'>>): FileStat {
return fromStat(resource instanceof URI ? resource : new URI(resource), { type: FileType.Directory, ...stat });
}
export function file(resource: string | URI, stat?: Partial<Omit<Stat, 'type'>>): FileStat {
return fromStat(resource instanceof URI ? resource : new URI(resource), { type: FileType.File, ...stat });
}
}
export interface FileStatWithMetadata extends FileStat, BaseStatWithMetadata {
mtime: number;
ctime: number;
etag: string;
size: number;
children?: FileStatWithMetadata[];
}
export interface ResolveFileResult {
stat?: FileStat;
success: boolean;
}
export interface ResolveFileResultWithMetadata extends ResolveFileResult {
stat?: FileStatWithMetadata;
}
export interface FileContent extends BaseStatWithMetadata {
/**
* The content of a file as buffer.
*/
value: BinaryBuffer;
}
export interface FileStreamContent extends BaseStatWithMetadata {
/**
* The content of a file as stream.
*/
value: BinaryBufferReadableStream;
}
export interface WriteFileOptions {
/**
* The last known modification time of the file. This can be used to prevent dirty writes.
*/
readonly mtime?: number;
/**
* The etag of the file. This can be used to prevent dirty writes.
*/
readonly etag?: string;
}
export interface ReadFileOptions extends FileReadStreamOptions {
/**
* The optional etag parameter allows to return early from resolving the resource if
* the contents on disk match the etag. This prevents accumulated reading of resources
* that have been read already with the same etag.
* It is the task of the caller to makes sure to handle this error case from the promise.
*/
readonly etag?: string;
}
export interface WriteFileOptions {
/**
* The last known modification time of the file. This can be used to prevent dirty writes.
*/
readonly mtime?: number;
/**
* The etag of the file. This can be used to prevent dirty writes.
*/
readonly etag?: string;
}
export interface ResolveFileOptions {
/**
* Automatically continue resolving children of a directory until the provided resources
* are found.
*/
readonly resolveTo?: readonly URI[];
/**
* Automatically continue resolving children of a directory if the number of children is 1.
*/
readonly resolveSingleChildDescendants?: boolean;
/**
* Will resolve mtime, ctime, size and etag of files if enabled. This can have a negative impact
* on performance and thus should only be used when these values are required.
*/
readonly resolveMetadata?: boolean;
}
export interface ResolveMetadataFileOptions extends ResolveFileOptions {
readonly resolveMetadata: true;
}
export interface FileOperationOptions {
/**
* Indicates that a user action triggered the opening, e.g.
* via mouse or keyboard use. Default is true.
*/
fromUserGesture?: boolean
}
export interface MoveFileOptions extends FileOperationOptions, Partial<FileOverwriteOptions> {
}
export interface CopyFileOptions extends FileOperationOptions, Partial<FileOverwriteOptions> {
}
export interface CreateFileOptions extends FileOperationOptions, Partial<FileOverwriteOptions> {
}
export class FileOperationError extends Error {
constructor(message: string, public fileOperationResult: FileOperationResult, public options?: ReadFileOptions & WriteFileOptions & CreateFileOptions) {
super(message);
Object.setPrototypeOf(this, FileOperationError.prototype);
}
}
export const enum FileOperationResult {
FILE_IS_DIRECTORY,
FILE_NOT_FOUND,
FILE_NOT_MODIFIED_SINCE,
FILE_MODIFIED_SINCE,
FILE_MOVE_CONFLICT,
FILE_READ_ONLY,
FILE_PERMISSION_DENIED,
FILE_TOO_LARGE,
FILE_INVALID_PATH,
FILE_EXCEEDS_MEMORY_LIMIT,
FILE_NOT_DIRECTORY,
FILE_OTHER_ERROR
}
export interface FileOverwriteOptions {
/**
* Overwrite the file to create if it already exists on disk. Otherwise
* an error will be thrown (FILE_MODIFIED_SINCE).
*/
overwrite: boolean;
}
export interface FileReadStreamOptions {
/**
* Is an integer specifying where to begin reading from in the file. If position is undefined,
* data will be read from the current file position.
*/
readonly position?: number;
/**
* Is an integer specifying how many bytes to read from the file. By default, all bytes
* will be read.
*/
readonly length?: number;
/**
* If provided, the size of the file will be checked against the limits.
*/
limits?: {
readonly size?: number;
readonly memory?: number;
};
}
export interface FileUpdateOptions {
readEncoding: string;
writeEncoding: string;
overwriteEncoding: boolean;
}
export interface FileUpdateResult extends Stat {
encoding: string;
}
export interface FileWriteOptions {
overwrite: boolean;
create: boolean;
}
export interface FileOpenOptions {
create: boolean;
}
export interface FileDeleteOptions {
recursive: boolean;
useTrash: boolean;
}
export enum FileType {
Unknown = 0,
File = 1,
Directory = 2,
SymbolicLink = 64
}
export interface Stat {
type: FileType;
/**
* The last modification date represented as millis from unix epoch.
*/
mtime: number;
/**
* The creation date represented as millis from unix epoch.
*/
ctime: number;
size: number;
}
export interface WatchOptions {
recursive: boolean;
excludes: string[];
}
export const enum FileSystemProviderCapabilities {
FileReadWrite = 1 << 1,
FileOpenReadWriteClose = 1 << 2,
FileReadStream = 1 << 4,
FileFolderCopy = 1 << 3,
PathCaseSensitive = 1 << 10,
Readonly = 1 << 11,
Trash = 1 << 12,
Access = 1 << 24,
Update = 1 << 25
}
export enum FileSystemProviderErrorCode {
FileExists = 'EntryExists',
FileNotFound = 'EntryNotFound',
FileNotADirectory = 'EntryNotADirectory',
FileIsADirectory = 'EntryIsADirectory',
FileExceedsMemoryLimit = 'EntryExceedsMemoryLimit',
FileTooLarge = 'EntryTooLarge',
NoPermissions = 'NoPermissions',
Unavailable = 'Unavailable',
Unknown = 'Unknown'
}
export class FileSystemProviderError extends Error {
constructor(message: string, public readonly code: FileSystemProviderErrorCode) {
super(message);
Object.setPrototypeOf(this, FileSystemProviderError.prototype);
}
}
export function createFileSystemProviderError(error: Error | string, code: FileSystemProviderErrorCode): FileSystemProviderError {
const providerError = new FileSystemProviderError(error.toString(), code);
markAsFileSystemProviderError(providerError, code);
return providerError;
}
export function ensureFileSystemProviderError(error?: Error): Error {
if (!error) {
return createFileSystemProviderError('Unknown Error', FileSystemProviderErrorCode.Unknown); // https://github.com/Microsoft/vscode/issues/72798
}
return error;
}
export const FileSystemProvider = Symbol('FileSystemProvider');
/**
* A {@link FileSystemProvider} provides the capabilities to read, write, discover, and to manage files and folders
* of the underlying (potentially virtual) file system. {@link FileSystemProvider}s can be used to serve files from both the
* local disk as well as remote locations like ftp-servers, REST-services etc. A {@link FileSystemProvider} is registered for a certain
* scheme and can handle all resources whose uri does conform to that scheme.
*/
export interface FileSystemProvider {
/** The {@link FileSystemProviderCapabilities} for this provider. */
readonly capabilities: FileSystemProviderCapabilities;
/** * Event that is fired if the capabilities of this provider have changed. */
readonly onDidChangeCapabilities: Event<void>;
/** Event that is fired if a (watched) file in the filesystem of this provider has changed. */
readonly onDidChangeFile: Event<readonly FileChange[]>;
/** Event that is fired if an error occurred when watching files in the filesystem of this provider. */
readonly onFileWatchError: Event<void>;
/**
* Watch the given resource and react to changes by firing the {@link FileSystemProvider#onDidChangeFile} event.
* @param resource `URI` of the resource to be watched.
* @param opts Options to define if the resource should be watched recursively and to
* provide a set of resources that should be excluded from watching.
*
* @returns A `Disposable` that can be invoked to stop watching the resource.
*/
watch(resource: URI, opts: WatchOptions): IDisposable;
/**
* Retrieve metadata about a given file.
*
* @param uri The `URI` of the file to retrieve meta data about.
* @returns A promise of the metadata about the resource.
*/
stat(resource: URI): Promise<Stat>;
/**
* Create a new directory using the given resource uri.
* @param resource The `URI` of the new folder.
*/
mkdir(resource: URI): Promise<void>;
/**
* Retrieve the content of a given directory.
* @param resource The `URI` of the directory.
*
* @returns A map containing the {@link FileType} for each child resource, identified by name.
*/
readdir(resource: URI): Promise<[string, FileType][]>;
/**
* Delete the given resource.
* @param resource The `URI` of the resource to delete.
* @param opts Options to define if files should be deleted recursively and if the trash should be used.
*/
delete(resource: URI, opts: FileDeleteOptions): Promise<void>;
/**
* Rename a file or folder.
* @param from `URI` of the existing file or folder.
* @param to `URI` of the target location.
* @param opts Options to define if existing files should be overwritten.
*/
rename(from: URI, to: URI, opts: FileOverwriteOptions): Promise<void>;
/**
* Optional function that has to be implemented by {@link FileSystemProviderWithFileFolderCopyCapability}.
* See {@link FileSystemProviderWithFileFolderCopyCapability#copy}} for additional documentation.
*/
copy?(from: URI, to: URI, opts: FileOverwriteOptions): Promise<void>;
/**
* Optional function that has to be implemented by {@link FileSystemProviderWithFileReadWriteCapability}.
* See {@link FileSystemProviderWithFileReadWriteCapability#readFile} for additional documentation.
*/
readFile?(resource: URI): Promise<Uint8Array>;
/**
* Optional function that has to be implemented by {@link FileSystemProviderWithFileReadWriteCapability}.
* See {@link FileSystemProviderWithFileReadWriteCapability#writeFile} for additional documentation.
*/
writeFile?(resource: URI, content: Uint8Array, opts: FileWriteOptions): Promise<void>;
/**
* Optional function that has to be implemented by {@link FileSystemProviderWithFileReadStreamCapability}.
* See {@link FileSystemProviderWithFileReadStreamCapability#readFileStream} for additional documentation.
*/
readFileStream?(resource: URI, opts: FileReadStreamOptions, token: CancellationToken): ReadableStreamEvents<Uint8Array>;
/**
* Optional function that has to be implemented by {@link FileSystemProviderWithOpenReadWriteCloseCapability}.
* See {@link FileSystemProviderWithOpenReadWriteCloseCapability#open} for additional documentation.
*/
open?(resource: URI, opts: FileOpenOptions): Promise<number>;
/**
* Optional function that has to be implemented by {@link FileSystemProviderWithOpenReadWriteCloseCapability}.
* See {@link FileSystemProviderWithOpenReadWriteCloseCapability#close} for additional documentation.
*/
close?(fd: number): Promise<void>;
/**
* Optional function that has to be implemented by {@link FileSystemProviderWithOpenReadWriteCloseCapability}.
* See {@link FileSystemProviderWithOpenReadWriteCloseCapability#read} for additional documentation.
*/
read?(fd: number, pos: number, data: Uint8Array, offset: number, length: number): Promise<number>;
/**
* Optional function that has to be implemented by {@link FileSystemProviderWithOpenReadWriteCloseCapability}.
* See {@link FileSystemProviderWithOpenReadWriteCloseCapability#write} for additional documentation.
*/
write?(fd: number, pos: number, data: Uint8Array, offset: number, length: number): Promise<number>;
/**
* Optional function that has to be implemented by {@link FileSystemProviderWithAccessCapability}.
* See {@link FileSystemProviderWithAccessCapability#access} for additional documentation.
*/
access?(resource: URI, mode?: number): Promise<void>;
/**
* Optional function that has to be implemented by {@link FileSystemProviderWithAccessCapability}.
* See {@link FileSystemProviderWithAccessCapability#fsPath} for additional documentation.
*/
fsPath?(resource: URI): Promise<string>;
/**
* Optional function that has to be implemented by {@link FileSystemProviderWithUpdateCapability}.
* See {@link FileSystemProviderWithUpdateCapability#updateFile} for additional documentation.
*/
updateFile?(resource: URI, changes: TextDocumentContentChangeEvent[], opts: FileUpdateOptions): Promise<FileUpdateResult>;
}
/**
* Subtype of {@link FileSystemProvider} that ensures that the optional functions needed for providers, that should be
* able access files, are implemented.
*/
export interface FileSystemProviderWithAccessCapability extends FileSystemProvider {
/**
* Tests a user's permissions for the file or directory specified by URI.
* @param resource The `URI` of the file that should be tested.
* @param mode An optional integer that specifies the accessibility checks to be performed.
* Check `FileAccess.Constants` for possible values of mode.
* 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).
* If `mode` is not defined, `FileAccess.Constants.F_OK` will be used instead.
*
* @returns A promise that resolves if the user has the required permissions, should be rejected otherwise.
*/
access(resource: URI, mode?: number): Promise<void>;
/**
* Returns the path of the given file URI, specific to the backend's operating system.
* If the URI is not a file URI, undefined is returned.
*
* USE WITH CAUTION: You should always prefer URIs to paths if possible, as they are
* portable and platform independent. Paths should only be used in cases you directly
* interact with the OS, e.g. when running a command on the shell.
*
* @param resource `URI` of the resource to derive the path from.
*
* @returns A promise of the corresponding file system path.
*/
fsPath(resource: URI): Promise<string>;
}
export function hasAccessCapability(provider: FileSystemProvider): provider is FileSystemProviderWithAccessCapability {
return !!(provider.capabilities & FileSystemProviderCapabilities.Access);
}
/**
* Subtype of {@link FileSystemProvider} that ensures that the optional functions needed, for providers that should be
* able to update (text) files, are implemented.
*/
export interface FileSystemProviderWithUpdateCapability extends FileSystemProvider {
/**
* Update the content of the given (text) file according to the given text document changes.
* @param resource `URI` of the resource to update.
* @param changes Array of events describing the changes to the file.
* @param opts The encoding options.
*
* @returns A promise of the file metadata that resolves after the update process has completed.
*/
updateFile(resource: URI, changes: TextDocumentContentChangeEvent[], opts: FileUpdateOptions): Promise<FileUpdateResult>;
}
export function hasUpdateCapability(provider: FileSystemProvider): provider is FileSystemProviderWithUpdateCapability {
return !!(provider.capabilities & FileSystemProviderCapabilities.Update);
}
/**
* Subtype of {@link FileSystemProvider} that ensures that the optional functions, needed for providers
* that should be able to read & write files, are implemented.
*/
export interface FileSystemProviderWithFileReadWriteCapability extends FileSystemProvider {
/**
* Read the contents of the given file as stream.
* @param resource The `URI` of the file.
*
* @return The `ReadableStreamEvents` for the readable stream of the given file.
*/
readFile(resource: URI): Promise<Uint8Array>;
/**
* Write data to a file, replacing its entire contents.
* @param resource The uri of the file.
* @param content The new content of the file.
* @param opts Options to define if the file should be created if missing and if an existing file should be overwritten.
*/
writeFile(resource: URI, content: Uint8Array, opts: FileWriteOptions): Promise<void>;
}
export function hasReadWriteCapability(provider: FileSystemProvider): provider is FileSystemProviderWithFileReadWriteCapability {
return !!(provider.capabilities & FileSystemProviderCapabilities.FileReadWrite);
}
/**
* Subtype of {@link FileSystemProvider} that ensures that the optional functions, needed for providers that should be able to copy
* file folders, are implemented.
*/
export interface FileSystemProviderWithFileFolderCopyCapability extends FileSystemProvider {
/**
* Copy files or folders.
* @param from `URI` of the existing file or folder.
* @param to `URI` of the destination location.
* @param opts Options to define if existing files should be overwritten.
*/
copy(from: URI, to: URI, opts: FileOverwriteOptions): Promise<void>;
}
export function hasFileFolderCopyCapability(provider: FileSystemProvider): provider is FileSystemProviderWithFileFolderCopyCapability {
return !!(provider.capabilities & FileSystemProviderCapabilities.FileFolderCopy);
}
/**
* Subtype of {@link FileSystemProvider} that ensures that the optional functions, needed for providers that should be able to open,read, write
* or close files, are implemented.
*/
export interface FileSystemProviderWithOpenReadWriteCloseCapability extends FileSystemProvider {
/**
* Open the give file.
* @param resource The `URI` of the file to open.
* @param opts Options to define if the file should be created if it does not exist yet.
*
* @returns A promise of the file descriptor that resolves after the file is open.
*/
open(resource: URI, opts: FileOpenOptions): Promise<number>;
/**
* Close the file with the given file descriptor.
* @param fd the file descriptor to close.
*/
close(fd: number): Promise<void>;
/**
* Read specified content from a given file descriptor into a data buffer.
* @param fd The file descriptor referencing the file to read from.
* @param pos The offset from the beginning of the file from which data should be read.
* @param data The buffer that the data will be written to.
* @param offset The offset in the buffer at which to start writing.
* @param length The number of bytes to read.
*
* @returns A promise of the number of bytes read.
*/
read(fd: number, pos: number, data: Uint8Array, offset: number, length: number): Promise<number>;
/**
* Write specified content from the data buffer to the file referenced by the given file descriptor.
* @param fd The file descriptor referencing the file to write to.
* @param pos The offset from the beginning of the file where this data should be written.
* @param offset The part of the buffer to be read from.
* @param length The number of bytes to write.
*
* @returns A promise of the number of bytes written.
*/
write(fd: number, pos: number, data: Uint8Array, offset: number, length: number): Promise<number>;
}
export function hasOpenReadWriteCloseCapability(provider: FileSystemProvider): provider is FileSystemProviderWithOpenReadWriteCloseCapability {
return !!(provider.capabilities & FileSystemProviderCapabilities.FileOpenReadWriteClose);
}
/**
* Subtype of {@link FileSystemProvider} that ensures that the optional functions, needed for providers that should be able to read
* files as streams, are implemented.
*/
export interface FileSystemProviderWithFileReadStreamCapability extends FileSystemProvider {
/**
* Read the contents of the given file as stream.
* @param resource The `URI` of the file.
*
* @return The `ReadableStreamEvents` for the readable stream of the given file.
*/
readFileStream(resource: URI, opts: FileReadStreamOptions, token: CancellationToken): ReadableStreamEvents<Uint8Array>;
}
export function hasFileReadStreamCapability(provider: FileSystemProvider): provider is FileSystemProviderWithFileReadStreamCapability {
return !!(provider.capabilities & FileSystemProviderCapabilities.FileReadStream);
}
export function markAsFileSystemProviderError(error: Error, code: FileSystemProviderErrorCode): Error {
error.name = code ? `${code} (FileSystemError)` : 'FileSystemError';
return error;
}
export function toFileSystemProviderErrorCode(error: Error | undefined | null): FileSystemProviderErrorCode {
// Guard against abuse
if (!error) {
return FileSystemProviderErrorCode.Unknown;
}
// FileSystemProviderError comes with the code
if (error instanceof FileSystemProviderError) {
return error.code;
}
// Any other error, check for name match by assuming that the error
// went through the markAsFileSystemProviderError() method
const match = /^(.+) \(FileSystemError\)$/.exec(error.name);
if (!match) {
return FileSystemProviderErrorCode.Unknown;
}
switch (match[1]) {
case FileSystemProviderErrorCode.FileExists: return FileSystemProviderErrorCode.FileExists;
case FileSystemProviderErrorCode.FileIsADirectory: return FileSystemProviderErrorCode.FileIsADirectory;
case FileSystemProviderErrorCode.FileNotADirectory: return FileSystemProviderErrorCode.FileNotADirectory;
case FileSystemProviderErrorCode.FileNotFound: return FileSystemProviderErrorCode.FileNotFound;
case FileSystemProviderErrorCode.FileExceedsMemoryLimit: return FileSystemProviderErrorCode.FileExceedsMemoryLimit;
case FileSystemProviderErrorCode.FileTooLarge: return FileSystemProviderErrorCode.FileTooLarge;
case FileSystemProviderErrorCode.NoPermissions: return FileSystemProviderErrorCode.NoPermissions;
case FileSystemProviderErrorCode.Unavailable: return FileSystemProviderErrorCode.Unavailable;
}
return FileSystemProviderErrorCode.Unknown;
}
export function toFileOperationResult(error: Error): FileOperationResult {
// FileSystemProviderError comes with the result already
if (error instanceof FileOperationError) {
return error.fileOperationResult;
}
// Otherwise try to find from code
switch (toFileSystemProviderErrorCode(error)) {
case FileSystemProviderErrorCode.FileNotFound:
return FileOperationResult.FILE_NOT_FOUND;
case FileSystemProviderErrorCode.FileIsADirectory:
return FileOperationResult.FILE_IS_DIRECTORY;
case FileSystemProviderErrorCode.FileNotADirectory:
return FileOperationResult.FILE_NOT_DIRECTORY;
case FileSystemProviderErrorCode.NoPermissions:
return FileOperationResult.FILE_PERMISSION_DENIED;
case FileSystemProviderErrorCode.FileExists:
return FileOperationResult.FILE_MOVE_CONFLICT;
case FileSystemProviderErrorCode.FileExceedsMemoryLimit:
return FileOperationResult.FILE_EXCEEDS_MEMORY_LIMIT;
case FileSystemProviderErrorCode.FileTooLarge:
return FileOperationResult.FILE_TOO_LARGE;
default:
return FileOperationResult.FILE_OTHER_ERROR;
}
}
/**
* A hint to disable etag checking for reading/writing.
*/
export const ETAG_DISABLED = '';
export function etag(stat: { mtime: number, size: number }): string;
export function etag(stat: { mtime: number | undefined, size: number | undefined }): string | undefined;
export function etag(stat: { mtime: number | undefined, size: number | undefined }): string | undefined {
if (typeof stat.size !== 'number' || typeof stat.mtime !== 'number') {
return undefined;
}
return stat.mtime.toString(29) + stat.size.toString(31);
}
/**
* Helper to format a raw byte size into a human readable label.
*/
export class BinarySize {
static readonly KB = 1024;
static readonly MB = BinarySize.KB * BinarySize.KB;
static readonly GB = BinarySize.MB * BinarySize.KB;
static readonly TB = BinarySize.GB * BinarySize.KB;
static formatSize(size: number): string {
if (size < BinarySize.KB) {
return size + 'B';
}
if (size < BinarySize.MB) {
return (size / BinarySize.KB).toFixed(2) + 'KB';
}
if (size < BinarySize.GB) {
return (size / BinarySize.MB).toFixed(2) + 'MB';
}
if (size < BinarySize.TB) {
return (size / BinarySize.GB).toFixed(2) + 'GB';
}
return (size / BinarySize.TB).toFixed(2) + 'TB';
}
}