-
Notifications
You must be signed in to change notification settings - Fork 2
/
FSTORAGE.PAS
522 lines (472 loc) · 13.7 KB
/
FSTORAGE.PAS
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
{/////////////////////////////////////////////////////////////////////////
//
// Dos Navigator Version 1.51 Copyright (C) 1991-99 RIT Research Labs
//
// This programs is free for commercial and non-commercial use as long as
// the following conditions are aheared to.
//
// Copyright remains RIT Research Labs, and as such any Copyright notices
// in the code are not to be removed. If this package is used in a
// product, RIT Research Labs should be given attribution as the RIT Research
// Labs of the parts of the library used. This can be in the form of a textual
// message at program startup or in documentation (online or textual)
// provided with the package.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// 1. Redistributions of source code must retain the copyright
// notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// 3. All advertising materials mentioning features or use of this software
// must display the following acknowledgement:
// "Based on Dos Navigator by RIT Research Labs."
//
// THIS SOFTWARE IS PROVIDED BY RIT RESEARCH LABS "AS IS" AND ANY EXPRESS
// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// The licence and distribution terms for any publically available
// version or derivative of this code cannot be changed. i.e. this code
// cannot simply be copied and put under another distribution licence
// (including the GNU Public Licence).
//
//////////////////////////////////////////////////////////////////////////}
unit FStorage;
interface
uses Objects, Advance, DOS, FilesCol, ObjType;
type
TStoredID = (siUndefined, siEmpty, siDirectory, siFile, siEnd);
TStored = record
ID: TStoredID;
Len: Byte;
end;
TStoredFile = record
T: TStored;
Name: String[12];
Size: LongInt;
CSize: LongInt;
Date: LongInt;
Attr: Byte;
end;
TStoredDir = record
T: TStored;
Name: PathStr;
end;
TSwapLevel = (slNone, slXMS, slEMS, slDsk, slCnv, slFail);
PDirStorage = ^TDirStorage;
TDirStorage = object(TObject)
SwapLevel: TSwapLevel;
Files: LongInt;
Items: LongInt;
Stream: PStream;
FilePtr: LongInt;
CurFile: TStoredFile;
Last: Boolean;
CurDir, LastDir: PathStr;
CurPos: LongInt;
TotalLength, TotalCLength: LongInt;
vSavePos, vSize: LongInt;
constructor Init;
constructor Load(var S: TStream);
procedure Store(var S: TStream);
destructor Done; virtual;
procedure AddFile(const FName: String; Size, CSize, Date: LongInt; Attr: Byte);
procedure ResetPointer(const Dir: String);
function GetNextFile: Boolean;
procedure UpdateRecord;
procedure DosReread;
procedure DeleteFile(P: PFileRec);
procedure InitStream(X,E,M: LongInt);
procedure Truncate(N: LongInt);
procedure WipeCur;
procedure vWrite(var Buf; Count: Word);
procedure vRead(var Buf; Count: Word);
procedure vSeek(Pos: LongInt);
procedure vTruncate;
procedure FixError;
procedure TryStream(Sz: LongInt);
end;
const
RDirStorage: TStreamRec = (
ObjType: otDirStorage;
VmtLink: Ofs(TypeOf(TDirStorage)^);
Load: @TDirStorage.Load;
Store: @TDirStorage.Store);
implementation
uses ExtraMemory, DNApp, StartUp;
type
PTempFile = ^TTempFile;
TTempFile = object(TBufStream)
FName: string;
constructor Init;
destructor Done; virtual;
end;
constructor TTempFile.Init;
var
s: string;
begin
s := CalcTmpFName(CalcTmpId, 'SWD');
inherited Init(s, stCreate, 4096);
FName := s;
end;
destructor TTempFile.Done;
var
s: string;
begin
s := FName;
inherited Done;
EraseFile(s);
end;
procedure __Error;
begin
RunError(224);
end;
procedure TDirStorage.TryStream;
begin
repeat
SwapLevel := Succ(SwapLevel);
if SwapLevel = slFail then __Error;
if Sz = -1 then InitStream(4096, 16384, 2048) else InitStream(Sz, Sz, Sz);
until Stream <> nil;
end;
constructor TDirStorage.Init;
begin
inherited Init;
SwapLevel := slNone;
TryStream(-1);
TotalLength := 0;
TotalCLength := 0;
vSize := 0;
vSavePos := 0;
end;
procedure TDirStorage.FixError;
label
LLL;
const
BufSize = 512;
var
OldStream: PStream;
SavePos, Count: LongInt;
Buffer: array[0..BufSize-1] of Byte;
N: Word;
begin
OldStream := Stream; OldStream^.Status := stOK;
LLL:
TryStream(-1);
Count := vSize;
OldStream^.Seek(0);
while Count > 0 do
begin
if Count > BufSize then N := BufSize else N := Count;
OldStream^.Read(Buffer, N);
if OldStream^.Status <> stOK then __Error;
Stream^.Write(Buffer, N);
if Stream^.Status <> stOK then begin FreeObject(Stream); goto LLL end;
Dec(Count, N);
end;
FreeObject(OldStream);
Stream^.Seek(vSavePos);
end;
procedure TDirStorage.InitStream;
begin
Stream := nil;
Last := True; CurPos := -1;
case SwapLevel of
slXMS: if XMSFound then
begin
Stream := New(PXMSStream, Init(X, X));
if Stream^.Status <> stOK then FreeObject(Stream);
end;
slEMS: if EMSFound then
begin
Stream := New(PEMSStream, Init(E, E));
if Stream^.Status <> stOK then FreeObject(Stream);
end;
slCnv: begin
Stream := New(PMemoryStream, Init(M, 2048));
if Stream^.Status <> stOK then FreeObject(Stream);
end;
slDsk: begin
Stream := New(PTempFile, Init);
if Stream^.Status <> stOK then FreeObject(Stream);
end;
end;
end;
destructor TDirStorage.Done;
begin
if Stream <> nil then Dispose(Stream, Done);
end;
procedure TDirStorage.AddFile;
var Dr: PathStr;
Nm: NameStr;
Xt: ExtStr;
SD: TStoredDir;
SF: TStoredFile absolute SD;
TS: TStored absolute SD;
B: Array[0..512] of Byte;
L: Word;
begin
if Stream = nil then Exit;
Stream^.Status := stOk;
vSeek(Max(0, (vSize)-SizeOf(TStored)));
L := 0;
if FName <> #0 then
begin
FSplit(FName, Dr, Nm, Xt);
if (CurDir <> Dr) then
begin
if Dr[Length(Dr)] <> '\' then AddStr(Dr, '\'); UpStr(Dr);
CurDir := Dr;
SD.T.ID := siDirectory;
L := SizeOf(TStored) + 1 + Length(Dr);
SD.Name := Dr;
SD.T.Len := L;
Move(SD, B, L);
Inc(Items);
end;
SF.T.ID := siFile;
SF.T.Len := SizeOf(SF);
SF.Name := Nm+Xt;
SF.Size := Size;
Inc(TotalLength, Size);
Inc(TotalCLength, CSize);
SF.CSize := CSize;
SF.Date := Date;
SF.Attr := Attr;
Move(SF, B[L], SizeOf(SF)); Inc(L, SizeOf(SF));
Inc(Files); Inc(Items);
end;
TS.ID := siEnd;
TS.Len := SizeOf(TStored);
Move(TS, B[L], SizeOf(TStored)); Inc(L, SizeOf(TStored));
vWrite(B, L);
Last := True;
end;
procedure TDirStorage.ResetPointer;
begin
if Stream = nil then Exit;
CurPos := 0;
FilePtr := -1; CurPos := -1;
Last := vSize = 0;
vSeek(0);
FillChar(CurFile, SizeOf(CurFile), 0);
CurDir := Dir; LastDir := '';
if (CurDir <> '') and (CurDir[Length(CurDir)] <> '\') then AddStr(CurDir, '\');
end;
function TDirStorage.GetNextFile;
var SD: TStoredDir;
SF: TStoredFile absolute SD;
Dr: PathStr;
begin
Dr := LastDir; GetNextFile := False;
FillChar(CurFile, SizeOf(CurFile), 0); Last := True; CurPos := -1;
if Stream = nil then Exit;
while Stream^.Status = stOK do
begin
FilePtr := vSavePos;
vRead(SD, SizeOf(TStored));
if SD.T.Len > SizeOf(TStored) then vRead(SD.Name, SD.T.Len - SizeOf(TStored));
case SD.T.ID of
siDirectory: begin Dr := SD.Name; LastDir := Dr end;
siEnd: Break;
siFile: begin
CurPos := vSavePos - SD.T.Len;
if (CurDir = '') or (Dr = CurDir) then
begin
CurFile := SF;
Last := vSize - vSavePos <= SizeOf(TStored);
LastDir := Dr;
GetNextFile := True;
Break;
end;
end;
end;
end;
end;
procedure TDirStorage.UpdateRecord;
begin
if Stream = nil then Exit;
if (CurPos > 0) and (CurFile.Name <> '') then
begin
Stream^.Status := stOK;
vSeek(CurPos);
vWrite(CurFile, SizeOf(CurFile));
end;
end;
constructor TDirStorage.Load;
var P, L: LongInt;
begin
TObject.Init;
S.Read(Files, SizeOf(Files));
S.Read(TotalLength, SizeOf(TotalLength));
S.Read(TotalCLength, SizeOf(TotalCLength));
S.Read(L, SizeOf(L)); vSize := L;
TryStream(L);
P := S.GetPos;
repeat
Stream^.CopyFrom(S, L);
if Stream^.Status = stOK then Break else
begin
FreeObject(Stream);
TryStream(L);
S.Seek(P);
end;
until False;
end;
procedure TDirStorage.Store;
var L: LongInt;
begin
AddFile(#0,0,0,0,0);
S.Write(Files, SizeOf(Files));
S.Write(TotalLength, SizeOf(TotalLength));
S.Write(TotalCLength, SizeOf(TotalCLength));
L := vSize;
S.Write(L, SizeOf(L));
vSeek(0); Stream^.Status := stOK;
S.CopyFrom(Stream^, vSize);
end;
procedure TDirStorage.DosReread;
var SD: TStoredDir;
SF: TStoredFile absolute SD;
ST: TStored absolute SD;
Dr: PathStr;
L, B: LongInt;
SR: SearchRec;
begin
Last := Off; Dr := '';
Stream^.Status := stOK;
vSeek(0);
while Stream^.Status = stOK do
begin
L := vSavePos;
vRead(SD, SizeOf(TStored));
if ST.Len > SizeOf(TStored) then vRead(SD.Name, ST.Len - SizeOf(TStored));
case ST.ID of
siDirectory: Dr := SD.Name;
siEnd: Break;
siFile: begin
Abort := Off; DosError := 0;
FindFirst(Dr+SF.Name, $3F xor VolumeID, SR);
Dec(TotalLength, SF.Size);
if (DosError <> 0) or Abort then
begin
Dec(Files);
ST.ID := siEmpty;
vSeek(L);
vWrite(SF, ST.Len);
end else
if (SR.Size <> SF.Size) or (SR.Time <> SF.Date) or (SR.Attr <> SF.Attr) then
begin
Dec(TotalLength, SR.Size);
SF.Size := SR.Size;
SF.CSize := SR.Size;
SF.Date := SR.Time;
SF.Attr := SR.Attr;
vSeek(L);
vWrite(SF, ST.Len);
end;
end;
end;
end;
end;
procedure TDirStorage.DeleteFile;
var Dr: PathStr;
Nm: String[12];
begin
Dr := P^.Owner^; if Dr[Length(Dr)] <> '\' then AddStr(Dr, '\'); UpStr(Dr);
ResetPointer(''); Nm := MakeFileName(P^.Name); UpStr(Nm);
While not Last and GetNextFile do
if (CurFile.Name = Nm) and (LastDir = Dr) then WipeCur;
end;
procedure TDirStorage.Truncate(N: LongInt);
var
ST: TStored;
L, NF, NI: LongInt;
begin
ResetPointer('');
NF := 0; NI := 0;
while Stream^.Status = stOK do
begin
Inc(NI);
L := vSavePos;
vRead(ST, SizeOf(TStored));
if ST.Len > SizeOf(TStored) then vSeek(L + ST.Len);
case ST.ID of
siEnd : Break;
siFile :
begin
Dec(N);
if N > 0 then
begin
Inc(NF);
end else
begin
ST.ID := siEnd;
ST.Len := SizeOf(TStored);
vSeek(L);
vWrite(ST, SizeOf(TStored));
vTruncate;
Files := NF;
Items := NI;
Break;
end
end;
end;
end;
end;
procedure TDirStorage.WipeCur;
var
L: LongInt;
begin
L := vSavePos;
Stream^.Status := stOK;
vSeek(FilePtr);
CurFile.T.Id := siEmpty;
vWrite(CurFile, SizeOf(CurFile));
vSeek(L);
Dec(Files);
end;
procedure TDirStorage.vWrite;
begin
repeat
Stream^.Write(Buf, Count);
if Stream^.Status = stOK
then
begin
vSize := Max(vSize, vSavePos+Count); Inc(vSavePos, Count);
Break;
end
else FixError;
until False;
end;
procedure TDirStorage.vSeek;
begin
Stream^.Seek(Pos); vSavePos := Pos;
end;
procedure TDirStorage.vTruncate;
begin
Stream^.Truncate;
if Stream^.Status = stOK
then begin vSize := Stream^.GetPos; vSavePos := vSize end
else FixError;
end;
procedure TDirStorage.vRead;
begin
Stream^.Read(Buf, Count);
if Stream^.Status = stOK
then Inc(vSavePos, Count)
else __Error;
end;
end.