-
Notifications
You must be signed in to change notification settings - Fork 30.1k
/
Copy pathfiles.ts
1326 lines (1084 loc) · 38.6 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
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { VSBuffer, VSBufferReadable, VSBufferReadableStream } from 'vs/base/common/buffer';
import { CancellationToken } from 'vs/base/common/cancellation';
import { Event } from 'vs/base/common/event';
import { IExpression, IRelativePattern } from 'vs/base/common/glob';
import { IDisposable } from 'vs/base/common/lifecycle';
import { TernarySearchTree } from 'vs/base/common/ternarySearchTree';
import { sep } from 'vs/base/common/path';
import { ReadableStreamEvents } from 'vs/base/common/stream';
import { startsWithIgnoreCase } from 'vs/base/common/strings';
import { isNumber } from 'vs/base/common/types';
import { URI } from 'vs/base/common/uri';
import { localize } from 'vs/nls';
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
//#region file service & providers
export const IFileService = createDecorator<IFileService>('fileService');
export interface IFileService {
readonly _serviceBrand: undefined;
/**
* An event that is fired when a file system provider is added or removed
*/
readonly onDidChangeFileSystemProviderRegistrations: Event<IFileSystemProviderRegistrationEvent>;
/**
* An event that is fired when a registered file system provider changes its capabilities.
*/
readonly onDidChangeFileSystemProviderCapabilities: Event<IFileSystemProviderCapabilitiesChangeEvent>;
/**
* An event that is fired when a file system provider is about to be activated. Listeners
* can join this event with a long running promise to help in the activation process.
*/
readonly onWillActivateFileSystemProvider: Event<IFileSystemProviderActivationEvent>;
/**
* Registers a file system provider for a certain scheme.
*/
registerProvider(scheme: string, provider: IFileSystemProvider): IDisposable;
/**
* Returns a file system provider for a certain scheme.
*/
getProvider(scheme: string): IFileSystemProvider | undefined;
/**
* Tries to activate a provider with the given scheme.
*/
activateProvider(scheme: string): Promise<void>;
/**
* Checks if this file service can handle the given resource by
* first activating any extension that wants to be activated
* on the provided resource scheme to include extensions that
* contribute file system providers for the given resource.
*/
canHandleResource(resource: URI): Promise<boolean>;
/**
* Checks if the file service has a registered provider for the
* provided resource.
*
* Note: this does NOT account for contributed providers from
* extensions that have not been activated yet. To include those,
* consider to call `await fileService.canHandleResource(resource)`.
*/
hasProvider(resource: URI): boolean;
/**
* Checks if the provider for the provided resource has the provided file system capability.
*/
hasCapability(resource: URI, capability: FileSystemProviderCapabilities): boolean;
/**
* List the schemes and capabilities for registered file system providers
*/
listCapabilities(): Iterable<{ scheme: string; capabilities: FileSystemProviderCapabilities }>;
/**
* Allows to listen for file changes. The event will fire for every file within the opened workspace
* (if any) as well as all files that have been watched explicitly using the #watch() API.
*/
readonly onDidFilesChange: Event<FileChangesEvent>;
/**
* An event that is fired upon successful completion of a certain file operation.
*/
readonly onDidRunOperation: Event<FileOperationEvent>;
/**
* Resolve the properties of a file/folder identified by the resource. For a folder, children
* information is resolved as well depending on the provided options. Use `stat()` method if
* you do not need children information.
*
* If the optional parameter "resolveTo" is specified in options, the stat service is asked
* to provide a stat object that should contain the full graph of folders up to all of the
* target resources.
*
* If the optional parameter "resolveSingleChildDescendants" is specified in options,
* the stat service is asked to automatically resolve child folders that only
* contain a single element.
*
* If the optional parameter "resolveMetadata" is specified in options,
* the stat will contain metadata information such as size, mtime and etag.
*/
resolve(resource: URI, options: IResolveMetadataFileOptions): Promise<IFileStatWithMetadata>;
resolve(resource: URI, options?: IResolveFileOptions): Promise<IFileStat>;
/**
* Same as `resolve()` but supports resolving multiple resources in parallel.
*
* If one of the resolve targets fails to resolve returns a fake `IFileStat` instead of
* making the whole call fail.
*/
resolveAll(toResolve: { resource: URI; options: IResolveMetadataFileOptions }[]): Promise<IFileStatResult[]>;
resolveAll(toResolve: { resource: URI; options?: IResolveFileOptions }[]): Promise<IFileStatResult[]>;
/**
* Same as `resolve()` but without resolving the children of a folder if the
* resource is pointing to a folder.
*/
stat(resource: URI): Promise<IFileStatWithPartialMetadata>;
/**
* Finds out if a file/folder identified by the resource exists.
*/
exists(resource: URI): Promise<boolean>;
/**
* Read the contents of the provided resource unbuffered.
*/
readFile(resource: URI, options?: IReadFileOptions, token?: CancellationToken): Promise<IFileContent>;
/**
* Read the contents of the provided resource buffered as stream.
*/
readFileStream(resource: URI, options?: IReadFileStreamOptions, token?: CancellationToken): Promise<IFileStreamContent>;
/**
* Updates the content replacing its previous value.
*
* Emits a `FileOperation.WRITE` file operation event when successful.
*/
writeFile(resource: URI, bufferOrReadableOrStream: VSBuffer | VSBufferReadable | VSBufferReadableStream, options?: IWriteFileOptions): Promise<IFileStatWithMetadata>;
/**
* Moves the file/folder to a new path identified by the resource.
*
* The optional parameter overwrite can be set to replace an existing file at the location.
*
* Emits a `FileOperation.MOVE` file operation event when successful.
*/
move(source: URI, target: URI, overwrite?: boolean): Promise<IFileStatWithMetadata>;
/**
* Find out if a move operation is possible given the arguments. No changes on disk will
* be performed. Returns an Error if the operation cannot be done.
*/
canMove(source: URI, target: URI, overwrite?: boolean): Promise<Error | true>;
/**
* Copies the file/folder to a path identified by the resource. A folder is copied
* recursively.
*
* Emits a `FileOperation.COPY` file operation event when successful.
*/
copy(source: URI, target: URI, overwrite?: boolean): Promise<IFileStatWithMetadata>;
/**
* Find out if a copy operation is possible given the arguments. No changes on disk will
* be performed. Returns an Error if the operation cannot be done.
*/
canCopy(source: URI, target: URI, overwrite?: boolean): Promise<Error | true>;
/**
* Clones a file to a path identified by the resource. Folders are not supported.
*
* If the target path exists, it will be overwritten.
*/
cloneFile(source: URI, target: URI): Promise<void>;
/**
* Creates a new file with the given path and optional contents. The returned promise
* will have the stat model object as a result.
*
* The optional parameter content can be used as value to fill into the new file.
*
* Emits a `FileOperation.CREATE` file operation event when successful.
*/
createFile(resource: URI, bufferOrReadableOrStream?: VSBuffer | VSBufferReadable | VSBufferReadableStream, options?: ICreateFileOptions): Promise<IFileStatWithMetadata>;
/**
* Find out if a file create operation is possible given the arguments. No changes on disk will
* be performed. Returns an Error if the operation cannot be done.
*/
canCreateFile(resource: URI, options?: ICreateFileOptions): Promise<Error | true>;
/**
* Creates a new folder with the given path. The returned promise
* will have the stat model object as a result.
*
* Emits a `FileOperation.CREATE` file operation event when successful.
*/
createFolder(resource: URI): Promise<IFileStatWithMetadata>;
/**
* Deletes the provided file. The optional useTrash parameter allows to
* move the file to trash. The optional recursive parameter allows to delete
* non-empty folders recursively.
*
* Emits a `FileOperation.DELETE` file operation event when successful.
*/
del(resource: URI, options?: Partial<IFileDeleteOptions>): Promise<void>;
/**
* Find out if a delete operation is possible given the arguments. No changes on disk will
* be performed. Returns an Error if the operation cannot be done.
*/
canDelete(resource: URI, options?: Partial<IFileDeleteOptions>): Promise<Error | true>;
/**
* An event that signals an error when watching for file changes.
*/
readonly onDidWatchError: Event<Error>;
/**
* Allows to start a watcher that reports file/folder change events on the provided resource.
*
* Note: recursive file watching is not supported from this method. Only events from files
* that are direct children of the provided resource will be reported.
*/
watch(resource: URI, options?: IWatchOptions): IDisposable;
/**
* Frees up any resources occupied by this service.
*/
dispose(): void;
}
export interface IFileOverwriteOptions {
/**
* Set to `true` to overwrite a file if it exists. Will
* throw an error otherwise if the file does exist.
*/
readonly overwrite: boolean;
}
export interface IFileUnlockOptions {
/**
* Set to `true` to try to remove any write locks the file might
* have. A file that is write locked will throw an error for any
* attempt to write to unless `unlock: true` is provided.
*/
readonly unlock: boolean;
}
export interface IFileAtomicReadOptions {
/**
* The optional `atomic` flag can be used to make sure
* the `readFile` method is not running in parallel with
* any `write` operations in the same process.
*
* Typically you should not need to use this flag but if
* for example you are quickly reading a file right after
* a file event occurred and the file changes a lot, there
* is a chance that a read returns an empty or partial file
* because a pending write has not finished yet.
*
* Note: this does not prevent the file from being written
* to from a different process. If you need such atomic
* operations, you better use a real database as storage.
*/
readonly atomic: true;
}
export interface IFileReadStreamOptions {
/**
* 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 IFileWriteOptions extends IFileOverwriteOptions, IFileUnlockOptions {
/**
* Set to `true` to create a file when it does not exist. Will
* throw an error otherwise if the file does not exist.
*/
readonly create: boolean;
}
export type IFileOpenOptions = IFileOpenForReadOptions | IFileOpenForWriteOptions;
export function isFileOpenForWriteOptions(options: IFileOpenOptions): options is IFileOpenForWriteOptions {
return options.create === true;
}
export interface IFileOpenForReadOptions {
/**
* A hint that the file should be opened for reading only.
*/
readonly create: false;
}
export interface IFileOpenForWriteOptions extends IFileUnlockOptions {
/**
* A hint that the file should be opened for reading and writing.
*/
readonly create: true;
}
export interface IFileDeleteOptions {
/**
* Set to `true` to recursively delete any children of the file. This
* only applies to folders and can lead to an error unless provided
* if the folder is not empty.
*/
readonly recursive: boolean;
/**
* Set to `true` to attempt to move the file to trash
* instead of deleting it permanently from disk. This
* option maybe not be supported on all providers.
*/
readonly useTrash: boolean;
}
export enum FileType {
/**
* File is unknown (neither file, directory nor symbolic link).
*/
Unknown = 0,
/**
* File is a normal file.
*/
File = 1,
/**
* File is a directory.
*/
Directory = 2,
/**
* File is a symbolic link.
*
* Note: even when the file is a symbolic link, you can test for
* `FileType.File` and `FileType.Directory` to know the type of
* the target the link points to.
*/
SymbolicLink = 64
}
export enum FilePermission {
/**
* File is readonly.
*/
Readonly = 1
}
export interface IStat {
/**
* The file type.
*/
readonly type: FileType;
/**
* The last modification date represented as millis from unix epoch.
*/
readonly mtime: number;
/**
* The creation date represented as millis from unix epoch.
*/
readonly ctime: number;
/**
* The size of the file in bytes.
*/
readonly size: number;
/**
* The file permissions.
*/
readonly permissions?: FilePermission;
}
export interface IWatchOptions {
/**
* Set to `true` to watch for changes recursively in a folder
* and all of its children.
*/
readonly recursive: boolean;
/**
* A set of glob patterns or paths to exclude from watching.
*/
excludes: string[];
/**
* An optional set of glob patterns or paths to include for
* watching. If not provided, all paths are considered for
* events.
*/
includes?: Array<string | IRelativePattern>;
}
export const enum FileSystemProviderCapabilities {
/**
* No capabilities.
*/
None = 0,
/**
* Provider supports unbuffered read/write.
*/
FileReadWrite = 1 << 1,
/**
* Provider supports open/read/write/close low level file operations.
*/
FileOpenReadWriteClose = 1 << 2,
/**
* Provider supports stream based reading.
*/
FileReadStream = 1 << 4,
/**
* Provider supports copy operation.
*/
FileFolderCopy = 1 << 3,
/**
* Provider is path case sensitive.
*/
PathCaseSensitive = 1 << 10,
/**
* All files of the provider are readonly.
*/
Readonly = 1 << 11,
/**
* Provider supports to delete via trash.
*/
Trash = 1 << 12,
/**
* Provider support to unlock files for writing.
*/
FileWriteUnlock = 1 << 13,
/**
* Provider support to read files atomically. This implies the
* provider provides the `FileReadWrite` capability too.
*/
FileAtomicRead = 1 << 14,
/**
* Provider support to clone files atomically.
*/
FileClone = 1 << 15
}
export interface IFileSystemProvider {
readonly capabilities: FileSystemProviderCapabilities;
readonly onDidChangeCapabilities: Event<void>;
readonly onDidChangeFile: Event<readonly IFileChange[]>;
readonly onDidWatchError?: Event<string>;
watch(resource: URI, opts: IWatchOptions): IDisposable;
stat(resource: URI): Promise<IStat>;
mkdir(resource: URI): Promise<void>;
readdir(resource: URI): Promise<[string, FileType][]>;
delete(resource: URI, opts: IFileDeleteOptions): Promise<void>;
rename(from: URI, to: URI, opts: IFileOverwriteOptions): Promise<void>;
copy?(from: URI, to: URI, opts: IFileOverwriteOptions): Promise<void>;
readFile?(resource: URI): Promise<Uint8Array>;
writeFile?(resource: URI, content: Uint8Array, opts: IFileWriteOptions): Promise<void>;
readFileStream?(resource: URI, opts: IFileReadStreamOptions, token: CancellationToken): ReadableStreamEvents<Uint8Array>;
open?(resource: URI, opts: IFileOpenOptions): Promise<number>;
close?(fd: number): Promise<void>;
read?(fd: number, pos: number, data: Uint8Array, offset: number, length: number): Promise<number>;
write?(fd: number, pos: number, data: Uint8Array, offset: number, length: number): Promise<number>;
cloneFile?(from: URI, to: URI): Promise<void>;
}
export interface IFileSystemProviderWithFileReadWriteCapability extends IFileSystemProvider {
readFile(resource: URI): Promise<Uint8Array>;
writeFile(resource: URI, content: Uint8Array, opts: IFileWriteOptions): Promise<void>;
}
export function hasReadWriteCapability(provider: IFileSystemProvider): provider is IFileSystemProviderWithFileReadWriteCapability {
return !!(provider.capabilities & FileSystemProviderCapabilities.FileReadWrite);
}
export interface IFileSystemProviderWithFileFolderCopyCapability extends IFileSystemProvider {
copy(from: URI, to: URI, opts: IFileOverwriteOptions): Promise<void>;
}
export function hasFileFolderCopyCapability(provider: IFileSystemProvider): provider is IFileSystemProviderWithFileFolderCopyCapability {
return !!(provider.capabilities & FileSystemProviderCapabilities.FileFolderCopy);
}
export interface IFileSystemProviderWithFileCloneCapability extends IFileSystemProvider {
cloneFile(from: URI, to: URI): Promise<void>;
}
export function hasFileCloneCapability(provider: IFileSystemProvider): provider is IFileSystemProviderWithFileCloneCapability {
return !!(provider.capabilities & FileSystemProviderCapabilities.FileClone);
}
export interface IFileSystemProviderWithOpenReadWriteCloseCapability extends IFileSystemProvider {
open(resource: URI, opts: IFileOpenOptions): Promise<number>;
close(fd: number): Promise<void>;
read(fd: number, pos: number, data: Uint8Array, offset: number, length: number): Promise<number>;
write(fd: number, pos: number, data: Uint8Array, offset: number, length: number): Promise<number>;
}
export function hasOpenReadWriteCloseCapability(provider: IFileSystemProvider): provider is IFileSystemProviderWithOpenReadWriteCloseCapability {
return !!(provider.capabilities & FileSystemProviderCapabilities.FileOpenReadWriteClose);
}
export interface IFileSystemProviderWithFileReadStreamCapability extends IFileSystemProvider {
readFileStream(resource: URI, opts: IFileReadStreamOptions, token: CancellationToken): ReadableStreamEvents<Uint8Array>;
}
export function hasFileReadStreamCapability(provider: IFileSystemProvider): provider is IFileSystemProviderWithFileReadStreamCapability {
return !!(provider.capabilities & FileSystemProviderCapabilities.FileReadStream);
}
export interface IFileSystemProviderWithFileAtomicReadCapability extends IFileSystemProvider {
readFile(resource: URI, opts?: IFileAtomicReadOptions): Promise<Uint8Array>;
}
export function hasFileAtomicReadCapability(provider: IFileSystemProvider): provider is IFileSystemProviderWithFileAtomicReadCapability {
if (!hasReadWriteCapability(provider)) {
return false; // we require the `FileReadWrite` capability too
}
return !!(provider.capabilities & FileSystemProviderCapabilities.FileAtomicRead);
}
export enum FileSystemProviderErrorCode {
FileExists = 'EntryExists',
FileNotFound = 'EntryNotFound',
FileNotADirectory = 'EntryNotADirectory',
FileIsADirectory = 'EntryIsADirectory',
FileExceedsMemoryLimit = 'EntryExceedsMemoryLimit',
FileTooLarge = 'EntryTooLarge',
FileWriteLocked = 'EntryWriteLocked',
NoPermissions = 'NoPermissions',
Unavailable = 'Unavailable',
Unknown = 'Unknown'
}
export interface IFileSystemProviderError extends Error {
readonly name: string;
readonly code: FileSystemProviderErrorCode;
}
export class FileSystemProviderError extends Error implements IFileSystemProviderError {
constructor(message: string, readonly code: FileSystemProviderErrorCode) {
super(message);
}
}
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(localize('unknownError', "Unknown Error"), FileSystemProviderErrorCode.Unknown); // https://github.com/microsoft/vscode/issues/72798
}
return error;
}
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.FileWriteLocked: return FileSystemProviderErrorCode.FileWriteLocked;
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.FileWriteLocked:
return FileOperationResult.FILE_WRITE_LOCKED;
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;
}
}
export interface IFileSystemProviderRegistrationEvent {
readonly added: boolean;
readonly scheme: string;
readonly provider?: IFileSystemProvider;
}
export interface IFileSystemProviderCapabilitiesChangeEvent {
readonly provider: IFileSystemProvider;
readonly scheme: string;
}
export interface IFileSystemProviderActivationEvent {
readonly scheme: string;
join(promise: Promise<void>): void;
}
export const enum FileOperation {
CREATE,
DELETE,
MOVE,
COPY,
WRITE
}
export interface IFileOperationEvent {
readonly resource: URI;
readonly operation: FileOperation;
isOperation(operation: FileOperation.DELETE | FileOperation.WRITE): boolean;
isOperation(operation: FileOperation.CREATE | FileOperation.MOVE | FileOperation.COPY): this is IFileOperationEventWithMetadata;
}
export interface IFileOperationEventWithMetadata extends IFileOperationEvent {
readonly target: IFileStatWithMetadata;
}
export class FileOperationEvent implements IFileOperationEvent {
constructor(resource: URI, operation: FileOperation.DELETE | FileOperation.WRITE);
constructor(resource: URI, operation: FileOperation.CREATE | FileOperation.MOVE | FileOperation.COPY, target: IFileStatWithMetadata);
constructor(readonly resource: URI, readonly operation: FileOperation, readonly target?: IFileStatWithMetadata) { }
isOperation(operation: FileOperation.DELETE | FileOperation.WRITE): boolean;
isOperation(operation: FileOperation.CREATE | FileOperation.MOVE | FileOperation.COPY): this is IFileOperationEventWithMetadata;
isOperation(operation: FileOperation): boolean {
return this.operation === operation;
}
}
/**
* Possible changes that can occur to a file.
*/
export const enum FileChangeType {
UPDATED,
ADDED,
DELETED
}
/**
* Identifies a single change in a file.
*/
export interface IFileChange {
/**
* 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 {
private readonly added: TernarySearchTree<URI, IFileChange> | undefined = undefined;
private readonly updated: TernarySearchTree<URI, IFileChange> | undefined = undefined;
private readonly deleted: TernarySearchTree<URI, IFileChange> | undefined = undefined;
constructor(changes: readonly IFileChange[], ignorePathCasing: boolean) {
const entriesByType = new Map<FileChangeType, [URI, IFileChange][]>();
for (const change of changes) {
const array = entriesByType.get(change.type);
if (array) {
array.push([change.resource, change]);
} else {
entriesByType.set(change.type, [[change.resource, change]]);
}
switch (change.type) {
case FileChangeType.ADDED:
this.rawAdded.push(change.resource);
break;
case FileChangeType.UPDATED:
this.rawUpdated.push(change.resource);
break;
case FileChangeType.DELETED:
this.rawDeleted.push(change.resource);
break;
}
}
for (const [key, value] of entriesByType) {
switch (key) {
case FileChangeType.ADDED:
this.added = TernarySearchTree.forUris<IFileChange>(() => ignorePathCasing);
this.added.fill(value);
break;
case FileChangeType.UPDATED:
this.updated = TernarySearchTree.forUris<IFileChange>(() => ignorePathCasing);
this.updated.fill(value);
break;
case FileChangeType.DELETED:
this.deleted = TernarySearchTree.forUris<IFileChange>(() => ignorePathCasing);
this.deleted.fill(value);
break;
}
}
}
/**
* Find out if the file change events match the provided resource.
*
* Note: when passing `FileChangeType.DELETED`, we consider a match
* also when the parent of the resource got deleted.
*/
contains(resource: URI, ...types: FileChangeType[]): boolean {
return this.doContains(resource, { includeChildren: false }, ...types);
}
/**
* Find out if the file change events either match the provided
* resource, or contain a child of this resource.
*/
affects(resource: URI, ...types: FileChangeType[]): boolean {
return this.doContains(resource, { includeChildren: true }, ...types);
}
private doContains(resource: URI, options: { includeChildren: boolean }, ...types: FileChangeType[]): boolean {
if (!resource) {
return false;
}
const hasTypesFilter = types.length > 0;
// Added
if (!hasTypesFilter || types.includes(FileChangeType.ADDED)) {
if (this.added?.get(resource)) {
return true;
}
if (options.includeChildren && this.added?.findSuperstr(resource)) {
return true;
}
}
// Updated
if (!hasTypesFilter || types.includes(FileChangeType.UPDATED)) {
if (this.updated?.get(resource)) {
return true;
}
if (options.includeChildren && this.updated?.findSuperstr(resource)) {
return true;
}
}
// Deleted
if (!hasTypesFilter || types.includes(FileChangeType.DELETED)) {
if (this.deleted?.findSubstr(resource) /* deleted also considers parent folders */) {
return true;
}
if (options.includeChildren && this.deleted?.findSuperstr(resource)) {
return true;
}
}
return false;
}
/**
* Returns if this event contains added files.
*/
gotAdded(): boolean {
return !!this.added;
}
/**
* Returns if this event contains deleted files.
*/
gotDeleted(): boolean {
return !!this.deleted;
}
/**
* Returns if this event contains updated files.
*/
gotUpdated(): boolean {
return !!this.updated;
}
/**
* @deprecated use the `contains` or `affects` method to efficiently find
* out if the event relates to a given resource. these methods ensure:
* - that there is no expensive lookup needed (by using a `TernarySearchTree`)
* - correctly handles `FileChangeType.DELETED` events
*/
readonly rawAdded: URI[] = [];
/**
* @deprecated use the `contains` or `affects` method to efficiently find
* out if the event relates to a given resource. these methods ensure:
* - that there is no expensive lookup needed (by using a `TernarySearchTree`)
* - correctly handles `FileChangeType.DELETED` events
*/
readonly rawUpdated: URI[] = [];
/**
* @deprecated use the `contains` or `affects` method to efficiently find
* out if the event relates to a given resource. these methods ensure:
* - that there is no expensive lookup needed (by using a `TernarySearchTree`)
* - correctly handles `FileChangeType.DELETED` events
*/
readonly rawDeleted: URI[] = [];
}
export function isParent(path: string, candidate: string, ignoreCase?: boolean): boolean {
if (!path || !candidate || path === candidate) {
return false;
}
if (candidate.length > path.length) {
return false;
}
if (candidate.charAt(candidate.length - 1) !== sep) {
candidate += sep;
}
if (ignoreCase) {
return startsWithIgnoreCase(path, candidate);
}
return path.indexOf(candidate) === 0;
}
interface IBaseFileStat {
/**
* The unified resource identifier of this file or folder.
*/
readonly resource: URI;
/**
* The name which is the last segment
* of the {{path}}.
*/
readonly name: string;
/**
* The size of the file.
*
* The value may or may not be resolved as
* it is optional.
*/
readonly size?: number;
/**
* The last modification date represented as millis from unix epoch.
*
* The value may or may not be resolved as
* it is optional.
*/
readonly mtime?: number;
/**
* The creation date represented as millis from unix epoch.
*
* The value may or may not be resolved as
* it is optional.
*/
readonly 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.
*/
readonly etag?: string;
/**
* The file is read-only.
*/
readonly readonly?: boolean;
}
export interface IBaseFileStatWithMetadata extends Required<IBaseFileStat> { }