forked from andersmelander/DWScriptStudio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathamScript.FileSystem.pas
More file actions
870 lines (723 loc) · 26.1 KB
/
amScript.FileSystem.pas
File metadata and controls
870 lines (723 loc) · 26.1 KB
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
unit amScript.FileSystem;
(*
* Copyright © 2012 Anders Melander
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*)
interface
uses
Generics.Collections,
Classes,
Zip,
SysUtils,
dwsFileSystem,
amScript.FileSystem.API,
amScript.Provider.API;
// -----------------------------------------------------------------------------
//
// TScriptFileSystemFactory
//
// -----------------------------------------------------------------------------
// High level file system factory
// -----------------------------------------------------------------------------
type
TScriptFileSystemFactory = class(TdwsCustomFileSystem)
strict private
FAllowProtected: boolean;
FRoot: string;
FRootFileSystem: IScriptFileSystem;
FFileSystems: TList<IScriptFileSystem>;
FSearchPath: TStrings;
strict protected
property AllowProtected: boolean read FAllowProtected;
property Root: string read FRoot;
property RootFileSystem: IScriptFileSystem read FRootFileSystem;
property FileSystems: TList<IScriptFileSystem> read FFileSystems;
procedure SetSearchPath(const Value: TStrings);
public
constructor Create(AAllowProtected: boolean = False); reintroduce;
destructor Destroy; override;
function AllocateFileSystem: IdwsFileSystem; override;
procedure SetRoot(const ARoot: string);
procedure SetRootFileSystem(const FileSystem: IScriptFileSystem);
procedure MountFileSystem(const FileSystem: IScriptFileSystem);
procedure UnmountFileSystem(const FileSystem: IScriptFileSystem);
property SearchPath: TStrings read FSearchPath write SetSearchPath;
end;
// -----------------------------------------------------------------------------
//
// TCustomScriptFileSystem
//
// -----------------------------------------------------------------------------
// Works around the silly need to implement LoadTextFile in every implementation.
// -----------------------------------------------------------------------------
type
TCustomScriptFileSystem = class abstract(TInterfacedObject, IdwsFileSystem)
private
protected
// IdwsFileSystem
function ValidateFileName(const Filename: TFilename): TFilename; virtual; abstract;
function FileExists(const Filename: TFilename) : boolean; virtual; abstract;
function OpenFileStream(const Filename: TFilename; const Mode: TdwsFileOpenMode): TStream; virtual; abstract;
function LoadTextFile(const Filename: TFilename): UnicodeString;
// TODO -cRevival :Missing implementation
function FindFileName(const fileName : TFileName) : TFileName;
function GetSearchPaths : TStrings;
procedure SetSearchPaths(const val : TStrings);
public
end;
// -----------------------------------------------------------------------------
//
// TScriptFileSystem
//
// -----------------------------------------------------------------------------
// Delegates IdwsFileSystem to IScriptFileSystem where applicable.
// -----------------------------------------------------------------------------
type
TScriptFileSystem = class abstract(TCustomScriptFileSystem, IScriptFileSystem)
private
protected
// IdwsFileSystem
function ValidateFileName(const Filename: TFilename): TFilename; override;
function FileExists(const Filename: TFilename) : boolean; override;
// IScriptFileSystem
function OpenScriptProvider(const Filename: string; FileMustExist: boolean): IScriptProvider; virtual; abstract;
function ValidateFileNameEx(const FileName: string; FileMustExist: boolean): string; virtual; abstract;
public
end;
// -----------------------------------------------------------------------------
//
// TScriptRestrictedFileSystem
//
// -----------------------------------------------------------------------------
// Implements a physical file system with search paths.
// -----------------------------------------------------------------------------
type
TScriptRestrictedFileSystem = class(TScriptFileSystem)
private
FRoot: string;
FSearchPath: TStrings;
FRestricted: boolean;
protected
// IdwsFileSystem
function OpenFileStream(const Filename: TFilename; const Mode: TdwsFileOpenMode): TStream; override;
// IScriptFileSystem
function OpenScriptProvider(const Filename: string; FileMustExist: boolean): IScriptProvider; override;
function ValidateFileNameEx(const FileName: string; FileMustExist: boolean): string; override;
public
constructor Create(const ARoot: string); reintroduce; overload;
constructor Create(const ARoot: string; ASearchPath: TStrings); reintroduce; overload;
destructor Destroy; override;
property Restricted: boolean read FRestricted write FRestricted;
end;
// -----------------------------------------------------------------------------
//
// TScriptBundleFileSystem
//
// -----------------------------------------------------------------------------
type
TScriptBundleFileSystem = class(TScriptFileSystem)
private
FFilename: string;
FZipFileStream: TStream;
FZipFile: TZipFile;
FAllowProtected: boolean;
protected
function GetZipFile: TZipFile;
property ZipFile: TZipFile read GetZipFile;
// IdwsFileSystem
function OpenFileStream(const Filename: TFilename; const Mode: TdwsFileOpenMode): TStream; override;
// IScriptFileSystem
function OpenScriptProvider(const Filename: string; FileMustExist: boolean): IScriptProvider; override;
function ValidateFileNameEx(const FileName: string; FileMustExist: boolean): string; override;
public
constructor Create(const AFilename: string; AAllowProtected: boolean = False); reintroduce;
destructor Destroy; override;
end;
// -----------------------------------------------------------------------------
//
// TScriptDelegatingFileSystem
//
// -----------------------------------------------------------------------------
type
TScriptDelegatingFileSystem = class(TScriptFileSystem, IScriptDelegatingFileSystem)
private
FFileSystems: TList<IScriptFileSystem>;
// FRestrictedFileSystem: TScriptRestrictedFileSystem;
FRoot: string;
FSearchPath: TStrings;
FAllowProtected: boolean;
protected
property FileSystems: TList<IScriptFileSystem> read FFileSystems;
// IdwsFileSystem
function FileExists(const Filename: TFilename): Boolean; override;
function OpenFileStream(const Filename : TFilename; const Mode: TdwsFileOpenMode): TStream; override;
function ValidateFileName(const Filename: TFilename): TFilename; override;
// IScriptFileSystem
function OpenScriptProvider(const Filename: string; FileMustExist: boolean): IScriptProvider; override;
function ValidateFileNameEx(const FileName: string; FileMustExist: boolean): string; override;
// IScriptDelegatingFileSystem
procedure MountFileSystem(const FileSystem: IScriptFileSystem);
procedure UnmountFileSystem(const FileSystem: IScriptFileSystem);
public
constructor Create(const ARoot: string; ASearchPath: TStrings; AAllowProtected: boolean = False); reintroduce;
destructor Destroy; override;
end;
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
implementation
uses
StrUtils,
Windows,
RTLConsts,
dwsXPlatform, // must be before IOUtils to avoid TPath conflict
IOUtils,
amPath,
amEnvironment,
amScript.API,
amScript.Module,
amScript.Host.Provider,
amScript.Provider;
// Work around for TZipFile.IndexOf() being case sensitive
// This also handles the fact that zip paths can use either back- or forward slashes. The name parameter is assumed to use backslashes only.
function ZipFileIndexOf(ZipFile: TZipFile; const Name: string): integer;
begin
Result := ZipFile.FileCount-1;
while (Result >= 0) do
begin
if (AnsiSameText(StringReplace(ZipFile.FileName[Result], '/', '\', [rfReplaceAll]), Name)) then
break;
Dec(Result);
end;
end;
// -----------------------------------------------------------------------------
//
// TCustomScriptFileSystem
//
// -----------------------------------------------------------------------------
function TCustomScriptFileSystem.FindFileName(const fileName: TFileName): TFileName;
begin
// TODO -cRevival :Missing implementation
Result := '';
end;
function TCustomScriptFileSystem.GetSearchPaths: TStrings;
begin
// TODO -cRevival :Missing implementation
Result := nil;
end;
procedure TCustomScriptFileSystem.SetSearchPaths(const val: TStrings);
begin
// TODO -cRevival :Missing implementation
end;
function TCustomScriptFileSystem.LoadTextFile(const Filename: TFilename): UnicodeString;
var
Stream : TStream;
begin
Stream := OpenFileStream(Filename, fomFastSequentialRead);
if (Stream <> nil) then
begin
try
Result := LoadTextFromStream(Stream);
finally
Stream.Free;
end;
end else
Result := '';
end;
// -----------------------------------------------------------------------------
//
// TScriptFileSystem
//
// -----------------------------------------------------------------------------
function TScriptFileSystem.FileExists(const Filename: TFilename): boolean;
var
ValidFilename: string;
begin
ValidFilename := ValidateFileNameEx(Filename, True);
Result := (ValidFilename <> '');
end;
function TScriptFileSystem.ValidateFileName(const Filename: TFilename): TFilename;
begin
Result := ValidateFileNameEx(Filename, True);
end;
// -----------------------------------------------------------------------------
//
// TScriptRestrictedFileSystem
//
// -----------------------------------------------------------------------------
constructor TScriptRestrictedFileSystem.Create(const ARoot: string; ASearchPath: TStrings);
begin
Create(ARoot);
FSearchPath.AddStrings(ASearchPath);
end;
constructor TScriptRestrictedFileSystem.Create(const ARoot: string);
begin
inherited Create;
FRoot := ARoot;
FSearchPath := TStringList.Create;
TStringList(FSearchPath).CaseSensitive := False;
TStringList(FSearchPath).Duplicates := dupIgnore;
FSearchPath.Add('.\');
end;
destructor TScriptRestrictedFileSystem.Destroy;
begin
FSearchPath.Free;
inherited;
end;
// -----------------------------------------------------------------------------
type
TFileHandleStream = class(THandleStream)
public
destructor Destroy; override;
end;
destructor TFileHandleStream.Destroy;
begin
if (Handle <> INVALID_HANDLE_VALUE) then
CloseHandle(Handle);
inherited;
end;
function TScriptRestrictedFileSystem.OpenFileStream(const Filename: TFilename; const Mode: TdwsFileOpenMode): TStream;
var
ValidFilename: string;
FileHandle: THandle;
Retries: integer;
LastError: integer;
const
// See http://support.microsoft.com/kb/316609
MaxRetry = 40;
RetryDelay = 250;
begin
ValidFilename := ValidateFileNameEx(Filename, (Mode <> fomCreate));
// It is assumed that a previous call to ValidateFileNameEx has determined if the filename applies to this file system.
// Thus if the above ValidateFileNameEx indicates that the filename is invalid we raise an error.
if (ValidFilename = '') then
begin
if (Mode <> fomCreate) then
raise EFOpenError.CreateFmt(SFOpenErrorEx, [Filename, SFileNotFound])
else
raise EFCreateError.CreateFmt(SFCreateErrorEx, [Filename, SFileNotFound]);
end;
Retries := 0;
FileHandle := INVALID_HANDLE_VALUE;
while (FileHandle = INVALID_HANDLE_VALUE) do
begin
case Mode of
fomReadOnly:
FileHandle := Integer(CreateFile(PChar(ValidFilename),
GENERIC_READ,
FILE_SHARE_READ or FILE_SHARE_WRITE,
nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0));
fomReadWrite:
FileHandle := Integer(CreateFile(PChar(ValidFilename),
GENERIC_READ or GENERIC_WRITE,
FILE_SHARE_READ or FILE_SHARE_WRITE,
nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0));
fomCreate:
FileHandle := Integer(CreateFile(PChar(ValidFilename),
GENERIC_READ or GENERIC_WRITE,
0,
nil, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0));
fomFastSequentialRead:
FileHandle := Integer(CreateFile(PChar(ValidFilename),
GENERIC_READ,
FILE_SHARE_READ or FILE_SHARE_WRITE,
nil, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, 0));
else
ASSERT(False, 'Invalid file open mode');
end;
if (FileHandle = INVALID_HANDLE_VALUE) then
begin
LastError := GetLastError;
if (LastError = ERROR_SHARING_VIOLATION) then
begin
Inc(Retries);
if (Retries < MaxRetry) then
begin
Sleep(RetryDelay);
continue;
end;
end;
if (Mode <> fomCreate) then
raise EFOpenError.CreateFmt(SFOpenErrorEx, [ValidFilename, SysErrorMessage(LastError)])
else
raise EFCreateError.CreateFmt(SFCreateErrorEx, [ValidFilename, SysErrorMessage(LastError)]);
end;
end;
Result := TFileHandleStream.Create(FileHandle);
if (Mode = fomCreate) and (Result.Size > 0) then
Result.Size := 0;
end;
// -----------------------------------------------------------------------------
function TScriptRestrictedFileSystem.OpenScriptProvider(const Filename: string; FileMustExist: boolean): IScriptProvider;
var
ValidFilename: string;
begin
ValidFilename := ValidateFileNameEx(Filename, FileMustExist);
// Must return nil if filename doesn't apply to this filesystem or delegating filesystem will not work
if (ValidFilename = '') then
Exit(nil);
// raise EScript.CreateFmt('File not found: %s', [Filename]);
Result := TFileScriptProvider.Create(ValidFilename);
end;
// -----------------------------------------------------------------------------
function TScriptRestrictedFileSystem.ValidateFileNameEx(const FileName: string; FileMustExist: boolean): string;
var
ValidFilename: string;
Path, RelativeRoot: string;
AbsoluteFilename: string;
begin
Result := '';
// Filename can contain environment vars, but must still be located under one of the roots
ValidFilename := ExpandEnvironmentVariable(Filename);
if (ValidFilename = '') then
exit;
// If file system is unrestricted file is allowed to be located outside seartch path
if (not Restricted) and (not PathIsRelative(ValidFilename)) then
begin
if (not FileMustExist) or (SysUtils.FileExists(ValidFilename)) then
Result := ValidFilename;
exit;
end;
for Path in FSearchPath do
begin
// Root can contain environment vars
RelativeRoot := ExpandEnvironmentVariable(Path);
// Relative root is relative to script root
if (PathIsRelative(RelativeRoot)) then
begin
if (FRoot <> '') then
RelativeRoot := PathCombinePath(FRoot, RelativeRoot);
end;
// Relative filename is relative to root
if (PathIsRelative(ValidFilename)) then
AbsoluteFilename := PathCombinePath(RelativeRoot, ValidFilename)
else
AbsoluteFilename := ValidFilename;
// Remove double quotes
AbsoluteFilename := RemoveDoubleBackslashesFromPath(AbsoluteFilename);
// If file system is restricted then file must be located under one of the search path folders
if (not Restricted) or (AnsiStartsText(RelativeRoot, AbsoluteFilename)) then
begin
if (not FileMustExist) or (SysUtils.FileExists(AbsoluteFilename)) then
begin
Result := AbsoluteFilename;
exit;
end;
end;
end;
end;
// -----------------------------------------------------------------------------
//
// TScriptBundleFileSystem
//
// -----------------------------------------------------------------------------
constructor TScriptBundleFileSystem.Create(const AFilename: string; AAllowProtected: boolean);
begin
inherited Create;
FFilename := AFilename;
FAllowProtected := AAllowProtected;
end;
destructor TScriptBundleFileSystem.Destroy;
begin
FZipFile.Free;
FZipFileStream.Free;
inherited;
end;
// -----------------------------------------------------------------------------
function TScriptBundleFileSystem.GetZipFile: TZipFile;
begin
if (FZipFile = nil) then
begin
// Must use TZipFile.Open(TFileStream) to prevent file from being locked
FZipFileStream := TFileStream.Create(FFilename, fmOpenRead or fmShareDenyWrite);
FZipFile := TZipFile.Create;
FZipFile.Open(FZipFileStream, zmRead);
end;
Result := FZipFile;
end;
// -----------------------------------------------------------------------------
function TScriptBundleFileSystem.OpenFileStream(const Filename: TFilename; const mode: TdwsFileOpenMode): TStream;
var
ValidFilename: string;
Index: integer;
LocalHeader: TZipHeader;
{$ifdef FEATURE_SCRIPT_BUNDLE}
SourceStream: TMemoryStream;
{$endif FEATURE_SCRIPT_BUNDLE}
Stream: TStream;
begin
if (not (Mode in [fomReadOnly, fomFastSequentialRead])) then
raise EScript.Create('Invalid file open mode');
ValidFilename := ValidateFileNameEx(Filename, True);
if (ValidFilename <> '') then
Index := ZipFileIndexOf(ZipFile, ValidFilename)
else
Index := -1;
if (Index = -1) then
raise EScript.CreateFmt('File not found in bundle: %s (%s)', [Filename, FFilename]);
{$ifdef FEATURE_SCRIPT_BUNDLE}
if (FAllowProtected) and (AnsiSameText(Copy(ZipFile.FileComment[Index], 1, ZipFile.FileInfo[Index].FileCommentLength) , 'protected')) then
begin
Result := TMemoryStream.Create;
try
SourceStream := TMemoryStream.Create;
try
ZipFile.Read(Index, Stream, LocalHeader);
try
SourceStream.CopyFrom(Stream, LocalHeader.UncompressedSize);
SourceStream.Position := 0;
ScriptDecryptStream(SourceStream, Result);
//ScriptDecryptStream(Stream, Result);
finally
Stream.Free;
end;
Result.Position := 0;
finally
SourceStream.Free;
end;
except
Result.Free;
raise;
end;
end else
{$endif FEATURE_SCRIPT_BUNDLE}
begin
Result := TMemoryStream.Create;
try
ZipFile.Read(Index, Stream, LocalHeader);
try
Result.CopyFrom(Stream, LocalHeader.UncompressedSize);
finally
Stream.Free;
end;
Result.Position := 0;
except
Result.Free;
raise;
end;
end;
end;
// -----------------------------------------------------------------------------
function TScriptBundleFileSystem.OpenScriptProvider(const Filename: string; FileMustExist: boolean): IScriptProvider;
var
ValidFilename: string;
Stream: TStream;
begin
ValidFilename := ValidateFileNameEx(Filename, True); // File must exist
// Must return nil if filename doesn't apply to this filesystem or delegating filesystem will not work
if (ValidFilename = '') then
Exit(nil);
// raise EScript.CreateFmt('File not found in zip: %s (%s)', [Filename, FFilename]);
Stream := OpenFileStream(ValidFilename, fomReadOnly);
Result := TBundleScriptProvider.Create(FFilename, ValidFilename, FAllowProtected, Stream);
end;
// -----------------------------------------------------------------------------
function TScriptBundleFileSystem.ValidateFileNameEx(const FileName: string; FileMustExist: boolean): string;
var
ValidFilename: string;
Index: integer;
begin
Result := '';
// Make sure we have a valid Windows path (Zip can use either / or \)
ValidFilename := StringReplace(Filename, '/', '\', [rfReplaceAll]);
// Remove double slashes
ValidFilename := RemoveDoubleBackslashesFromPath(ValidFilename);
if (ValidFilename = '') then
exit;
if (not TPath.IsRelativePath(ValidFilename)) then
Exit;
// Remove leading \
if (ValidFilename[1] = '\') then
Delete(ValidFilename, 1, 1);
Index := ZipFileIndexOf(ZipFile, ValidFilename);
if (Index <> -1) then
begin
Result := ZipFile.FileName[Index];
{$ifdef FEATURE_SCRIPT_BUNDLE}
if (not FAllowProtected) and (AnsiSameText(Copy(ZipFile.FileComment[Index], 1, ZipFile.FileInfo[Index].FileCommentLength) , 'protected')) then
Result := '';
{$endif FEATURE_SCRIPT_BUNDLE}
end;
end;
// -----------------------------------------------------------------------------
//
// TScriptDelegatingFileSystem
//
// -----------------------------------------------------------------------------
constructor TScriptDelegatingFileSystem.Create(const ARoot: string; ASearchPath: TStrings; AAllowProtected: boolean);
begin
inherited Create;
FAllowProtected := AAllowProtected;
FRoot := ARoot;
FSearchPath := TStringList.Create;
if (ASearchPath <> nil) then
FSearchPath.Assign(ASearchPath);
FFileSystems := TList<IScriptFileSystem>.Create;
FFileSystems.Add(TScriptRestrictedFileSystem.Create(FRoot, FSearchPath));
end;
destructor TScriptDelegatingFileSystem.Destroy;
begin
FSearchPath.Free;
FFileSystems.Free;
inherited;
end;
// -----------------------------------------------------------------------------
function TScriptDelegatingFileSystem.FileExists(const Filename: TFilename): Boolean;
var
i: integer;
begin
i := 0;
Result := False;
while (not Result) and (i < FFileSystems.Count) do
begin
Result := FFileSystems[i].FileExists(Filename);
Inc(i);
end;
end;
// -----------------------------------------------------------------------------
procedure TScriptDelegatingFileSystem.MountFileSystem(const FileSystem: IScriptFileSystem);
begin
FFileSystems.Insert(0, FileSystem);
end;
procedure TScriptDelegatingFileSystem.UnmountFileSystem(const FileSystem: IScriptFileSystem);
begin
FFileSystems.Remove(FileSystem);
end;
// -----------------------------------------------------------------------------
function TScriptDelegatingFileSystem.OpenFileStream(const Filename: TFilename; const Mode: TdwsFileOpenMode): TStream;
var
i: integer;
ValidFilename: string;
FileMustExist: boolean;
begin
Result := nil;
// In case the filename is valid in multiple filesystems we give precedence to any filesystem where the file already
// exists. First try looking for an existing file.
FileMustExist := True;
while (True) do
begin
i := 0;
while (Result = nil) and (i < FFileSystems.Count) do
begin
// Test to determine if filename is valid for file system. We do not test if file exists here.
ValidFilename := FFileSystems[i].ValidateFileNameEx(Filename, FileMustExist);
if (ValidFilename <> '') then
Result := FFileSystems[i].OpenFileStream(ValidFilename, Mode);
Inc(i);
end;
if (Result <> nil) or (not FileMustExist) or (Mode <> fomCreate) then
break;
// Retry with FileMustExist=False if asked to create file
FileMustExist := False;
end;
if (Result = nil) then
begin
if (Mode <> fomCreate) then
raise EFOpenError.CreateFmt(SFOpenErrorEx, [Filename, SFileNotFound])
else
raise EFOpenError.CreateFmt(SFCreateErrorEx, [Filename, SFileNotFound]);
end;
end;
// -----------------------------------------------------------------------------
function TScriptDelegatingFileSystem.ValidateFileName(const Filename: TFilename): TFilename;
var
i: integer;
begin
i := 0;
Result := '';
while (Result = '') and (i < FFileSystems.Count) do
begin
Result := FFileSystems[i].ValidateFileName(Filename);
Inc(i);
end;
end;
// -----------------------------------------------------------------------------
function TScriptDelegatingFileSystem.OpenScriptProvider(const Filename: string; FileMustExist: boolean): IScriptProvider;
var
i: integer;
ValidFilename: string;
begin
i := 0;
Result := nil;
while (Result = nil) and (i < FFileSystems.Count) do
begin
ValidFilename := FFileSystems[i].ValidateFileNameEx(Filename, FileMustExist);
if (ValidFilename <> '') then
Result := FFileSystems[i].OpenScriptProvider(ValidFilename, FileMustExist);
Inc(i);
end;
end;
// -----------------------------------------------------------------------------
function TScriptDelegatingFileSystem.ValidateFileNameEx(const FileName: string; FileMustExist: boolean): string;
var
i: integer;
begin
i := 0;
Result := '';
while (Result = '') and (i < FFileSystems.Count) do
begin
Result := FFileSystems[i].ValidateFileNameEx(Filename, FileMustExist);
Inc(i);
end;
end;
// -----------------------------------------------------------------------------
//
// TScriptFileSystemFactory
//
// -----------------------------------------------------------------------------
constructor TScriptFileSystemFactory.Create(AAllowProtected: boolean);
begin
inherited Create(nil);
FAllowProtected := AAllowProtected;
FSearchPath := TStringList.Create;
FSearchPath.Delimiter := ';';
FSearchPath.StrictDelimiter := True;
FFileSystems := TList<IScriptFileSystem>.Create;
end;
destructor TScriptFileSystemFactory.Destroy;
begin
FSearchPath.Free;
FFileSystems.Free;
inherited;
end;
// -----------------------------------------------------------------------------
procedure TScriptFileSystemFactory.MountFileSystem(const FileSystem: IScriptFileSystem);
begin
FFileSystems.Add(FileSystem);
end;
procedure TScriptFileSystemFactory.UnmountFileSystem(const FileSystem: IScriptFileSystem);
begin
FFileSystems.Remove(FileSystem);
end;
procedure TScriptFileSystemFactory.SetRootFileSystem(const FileSystem: IScriptFileSystem);
begin
FRootFileSystem := FileSystem;
end;
// -----------------------------------------------------------------------------
function TScriptFileSystemFactory.AllocateFileSystem: IdwsFileSystem;
var
i: integer;
begin
Result := TScriptDelegatingFileSystem.Create(FRoot, FSearchPath, FAllowProtected);
if (FRootFileSystem <> nil) then
TScriptDelegatingFileSystem(Result).MountFileSystem(FRootFileSystem);
for i := 0 to FFileSystems.Count-1 do
TScriptDelegatingFileSystem(Result).MountFileSystem(FFileSystems[i]);
end;
// -----------------------------------------------------------------------------
procedure TScriptFileSystemFactory.SetRoot(const ARoot: string);
begin
FRoot := ARoot;
end;
// -----------------------------------------------------------------------------
procedure TScriptFileSystemFactory.SetSearchPath(const Value: TStrings);
begin
FSearchPath.Assign(Value);
end;
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
end.