-
Notifications
You must be signed in to change notification settings - Fork 137
/
Copy pathdiskimage.js
executable file
·1300 lines (1235 loc) · 57.4 KB
/
diskimage.js
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
#!/usr/bin/env node
/**
* @fileoverview Command-line interface to disk image processing module
* @author Jeff Parsons <[email protected]>
* @copyright © 2012-2025 Jeff Parsons
* @license MIT <https://www.pcjs.org/LICENSE.txt>
*
* This file is part of PCjs, a computer emulation software project at <https://www.pcjs.org>.
*/
import fs from "fs";
import glob from "glob";
import path from "path";
import DiskLib from "../modules/disklib.js";
import PCJSLib from "../modules/pcjslib.js";
import StreamZip from "../modules/streamzip.js"; // PCjs replacement for "node-stream-zip"
import JSONLib from "../../machines/modules/v2/jsonlib.js";
import StrLib from "../../machines/modules/v2/strlib.js";
import CharSet from "../../machines/pcx86/modules/v2/charset.js";
import Device from "../../machines/modules/v3/device.js";
import MESSAGE from "../../machines/modules/v3/message.js";
import DiskInfo from "../../machines/pcx86/modules/v3/diskinfo.js";
let device = new Device("node");
let printf = device.printf.bind(device);
let sprintf = device.sprintf.bind(device);
let diskLib = new DiskLib(device);
let rootDir, sFileIndexCache;
/**
* compareDisks(sDisk1, sDisk2)
*
* @param {string} sDisk1
* @param {string} sDisk2
* @returns {boolean} (true if the contents of this buffer are equal to the contents of the specified buffer, false otherwise)
*/
function compareDisks(sDisk1, sDisk2)
{
/**
* Passing null for the encoding parameter tells readFileSync() to return a buffer (which, in our case, is a DataBuffer).
*/
let db1 = diskLib.readFileSync(sDisk1, null);
let db2 = diskLib.readFileSync(sDisk2, null);
return db1 && db2 && db1.compare(db2) || false;
}
/**
* createDisk(diskFile, diskette, argv, done)
*
* @param {string} diskFile
* @param {Object} diskette
* @param {Array} argv
* @param {function(DiskInfo)} done
*/
function createDisk(diskFile, diskette, argv, done)
{
let sArchiveFolder = "archive/";
if (path.dirname(diskFile).endsWith("/disks")) {
sArchiveFolder = "../archive/";
}
let sArchiveFile = path.join(path.dirname(diskFile), sArchiveFolder, path.basename(diskFile).replace(".json", ".img"));
if (diskette.archive) {
/**
* The "archive" property determines what we look for in an "archive/" folder alongside the JSON disk image:
*
* 1) If it begins with a period, then we assume it's a file extension (eg, ".img", ".psi", etc)
* 2) If it's "folder", then the name of the diskette is used as a folder name (with trailing slash)
* 3) Anything else is more or less used as-is (and unless it contains a period, we add a trailing slash)
*/
if (diskette.archive[0] == '/') {
sArchiveFile = path.sep + diskette.archive.slice(1);
} else if (diskette.archive[0] == '.') {
if (diskette.archive != ".img") {
sArchiveFile = sArchiveFile.replace(".img", diskette.archive.toUpperCase());
}
} else if (diskette.archive == "folder") {
sArchiveFile = sArchiveFile.replace(".img", path.sep);
} else {
sArchiveFile = path.join(path.dirname(sArchiveFile), diskette.archive) + (diskette.archive.indexOf(".") < 0 && !diskette.archive.endsWith(path.sep)? path.sep : "");
}
} else if (!diskLib.existsFile(sArchiveFile)) {
/**
* Try automatically switching from a "--disk" to a "--dir" operation if there's no IMG file.
*/
sArchiveFile = sArchiveFile.replace(".img", path.sep);
}
let name = path.basename(sArchiveFile);
let fDir = false, arcType = 0, sExt = StrLib.getExtension(sArchiveFile);
if (sArchiveFile.endsWith(path.sep)) {
fDir = true;
diskette.command = "--dir=" + name;
}
else if (sExt == "arc") {
arcType = 1;
diskette.command = "--arc=" + name;
}
else if (sExt == "zip") {
arcType = 2;
diskette.command = "--zip=" + name;
}
else {
diskette.command = "--disk=" + name;
}
let driveInfo = createDriveInfo(argv, diskette);
diskette.archive = sArchiveFile;
printf("checking archive: %s\n", sArchiveFile);
if (fDir || arcType) {
let arcOffset = +argv['offset'] || 0;
let label = diskette.label || argv['label'];
let password = argv['password'];
let normalize = diskette.normalize || argv['normalize'];
let target = diskLib.getTargetValue(diskette.format);
let verbose = argv['verbose'];
diskLib.readDir(sArchiveFile, arcType, arcOffset, label, password, normalize, target, undefined, verbose, driveInfo, done);
} else {
done(diskLib.readDiskSync(sArchiveFile, false, driveInfo));
}
}
/**
* createDriveInfo(argv, diskette)
*
* @param {Object} argv
* @param {Object} [diskette]
* @returns {DiskInfo}
*/
function createDriveInfo(argv, diskette)
{
let driveInfo = {};
let typeDrive = argv['drivetype'];
if (typeof typeDrive == "string") {
let match = typeDrive.match(/^([0-9]+):([0-9]+):([0-9]+):?([0-9]*)$/i);
if (match) {
driveInfo.driveCtrl = "PCJS"; // this pseudo drive controller is required for custom drive geometries
driveInfo.driveType = 0;
driveInfo.nCylinders = +match[1];
driveInfo.nHeads = +match[2];
driveInfo.nSectors = +match[3];
driveInfo.cbSector = +match[4] || 512;
if (argv['partitioned'] !== undefined) {
driveInfo.fPartitioned = !!argv['partitioned'];
} else {
driveInfo.fPartitioned = (driveInfo.nCylinders * driveInfo.nHeads * driveInfo.nSectors * driveInfo.cbSector >= DiskInfo.MIN_PARTITION);
}
} else {
match = typeDrive.match(/^([A-Z]+|):?([0-9]+)$/i);
if (match) {
let driveCtrl = match[1] || driveInfo.driveCtrl || "XT";
let driveType = +match[2];
if (DiskInfo.validateDriveType(driveCtrl, driveType)) {
driveInfo.driveCtrl = driveCtrl;
driveInfo.driveType = driveType;
} else {
match = null;
}
}
}
if (!match) {
printf("unrecognized drive type: %s\n", typeDrive);
}
}
let typeFAT = argv['fat'];
if (typeof typeFAT == "string") {
let match = typeFAT.match(/^([0-9]+):?([0-9]*):?([0-9]*)$/i);
if (match) {
driveInfo.driveCtrl = driveInfo.driveCtrl || "XT";
driveInfo.typeFAT = +match[1];
if (match[2]) driveInfo.clusterSize = +match[2] || 0;
if (match[3]) driveInfo.rootEntries = +match[3] || 0;
}
}
driveInfo.sectorIDs = diskette && diskette.argv['sectorID'] || argv['sectorID'];
driveInfo.sectorErrors = diskette && diskette.argv['sectorError'] || argv['sectorError'];
driveInfo.suppData = diskLib.readFileSync(diskette && diskette.argv['suppData'] || argv['suppData']);
return driveInfo;
}
/**
* dumpSector(di, sector, offset, limit)
*
* @param {DiskInfo} di
* @param {Sector} sector
* @param {number} [offset]
* @param {number} [limit]
* @returns {string}
*/
function dumpSector(di, sector, offset = 0, limit = -1)
{
let sBytes = "", sChars = "", sLines = "";
if (limit < 0) limit = di.cbSector;
while (offset < limit) {
let b = di.read(sector, offset);
if (b < 0) break;
if (!sLines || offset % 16 == 0) sLines += sprintf("%#06x ", offset);
sBytes += sprintf("%02x ", b);
sChars += (b >= 0x20 && b < 0x7f? String.fromCharCode(b) : '.');
if (++offset % 16 == 0) {
sLines += sprintf("%48s %16s\n", sBytes, sChars);
sBytes = sChars = "";
}
}
if (sBytes) sLines += sprintf("%-48s %-16s\n", sBytes, sChars);
return sLines;
}
/**
* printFileDesc(diskFile, diskName, desc)
*
* @param {string} diskFile
* @param {string} diskName
* @param {Object} desc
*/
function printFileDesc(diskFile, diskName, desc)
{
printf("%-32s %-12s %s %#05x %7d %s\n", desc[DiskInfo.FILEDESC.HASH] || "-".repeat(32), desc[DiskInfo.FILEDESC.NAME], desc[DiskInfo.FILEDESC.DATE], +desc[DiskInfo.FILEDESC.ATTR], desc[DiskInfo.FILEDESC.SIZE] || 0, diskName + ':' + desc[DiskInfo.FILEDESC.PATH]);
}
/**
* printManifest(diskFile, diskName, manifest)
*
* @param {string} diskFile
* @param {string} diskName
* @param {Array.<FILEDESC>} manifest
*/
function printManifest(diskFile, diskName, manifest)
{
manifest.forEach(function printManifestFile(desc) {
printFileDesc(diskFile, diskName, desc);
});
}
/**
* processDisk(di, diskFile, argv, diskette, fSingle)
*
* @param {DiskInfo} di
* @param {string} diskFile
* @param {Array} argv
* @param {Object} [diskette] (if present, then we were invoked by readCollection(), so any --output option should be ignored)
* @param {boolean} [fSingle]
*/
function processDisk(di, diskFile, argv, diskette = null, fSingle = false)
{
di.setArgs(argv.slice(1).join(' '));
/**
* Any "--format=xxx" acts as a filter function; if the disk's format doesn't contain
* the specified format, we skip the disk.
*/
if (typeof argv['format'] == "string") {
let sFormat = di.getFormat();
if (sFormat.indexOf(argv['format']) < 0) {
printf("warning: specified format (\"%s\") does not match disk format (\"%s\")\n", argv['format'], sFormat);
return;
}
}
if (argv['all'] || argv['collection'] || argv['verbose']) {
printf("processing: %s (%d bytes, hash %s)\n", di.getName(), di.getSize(), di.getHash());
}
let sFindName = argv['file'];
if (typeof sFindName == "string") {
let sFindText = argv['find'];
if (typeof sFindText != "string") sFindText = false;
/**
* TODO: Implement support for finding text in findFile()....
*/
let desc = di.findFile(sFindName, sFindText);
if (desc) {
printFileDesc(di.getName(), desc);
if (argv['index']) {
/**
* We cheat and search for matching hash values in the provided index; this is much faster than
* opening and searching all the other disk images, even when they DO contain pre-generated file tables.
*/
if (sFileIndexCache === undefined) {
sFileIndexCache = diskLib.readFileSync(argv['index']);
if (!sFileIndexCache) sFileIndexCache = null;
}
let cMatches = 0;
if (sFileIndexCache) {
let re = new RegExp("^" + desc[DiskInfo.FILEDESC.HASH] + ".*$", "gm"), match;
while ((match = re.exec(sFileIndexCache))) {
if (match[0].indexOf(diskFile) >= 0) continue;
if (!cMatches++) printf("see also:\n");
printf("%s\n", match[0]);
}
}
if (!cMatches) printf("no matches\n");
}
}
}
let chs = argv['dump'];
if (chs) {
let iCylinder, iHead, idSector, nSectors;
if (typeof chs == "string") {
let values = chs.split(':');
iCylinder = +values[0], iHead = +values[1], idSector = +values[2], nSectors = +values[3] || 1;
}
if (isNaN(iCylinder) || isNaN(iHead) || !idSector) {
printf("specify --dump=C:H:S[:N]\n");
} else {
while (nSectors-- > 0) {
let sector = di.seek(iCylinder, iHead, idSector);
if (!sector) {
printf("unable to find %d:%d:%d\n", iCylinder, iHead, idSector);
break;
}
let sLines = sprintf("CHS=%d:%d:%d\n", iCylinder, iHead, idSector);
sLines += dumpSector(di, sector, 0);
printf("%s\n", sLines);
idSector++;
}
}
}
if (argv['list']) {
let sLines = "";
let iVolume = +argv['volume'];
if (isNaN(iVolume)) iVolume = -1;
if (argv['list'] == "unused") {
let lba = -1;
while ((lba = di.getUnusedSector(iVolume, lba)) >= 0) {
let sector = di.getSector(lba);
let offset = di.getUnusedSectorData(sector);
sLines += sprintf("\nLBA=%d\n", lba);
/**
* There are two partial sector usage cases: the current sector contains the last N bytes of a file,
* or the sector is COMPLETELY unused (ie, offset is zero). When would a file have a completely unused
* sector? When the disk's cluster size is 2 or more sectors. If a file ends somewhere in the middle
* of a cluster, leaving 1 or more sectors in that cluster unused, we still "flag" all the sectors in
* the cluster as belonging to the file.
*
* This is why we don't differentiate those cases on the basis of whether there's an associated file,
* but simply on whether the offset is zero.
*/
if (offset) {
let iFile = sector[DiskInfo.SECTOR.FILE_INDEX];
device.assert(iFile != undefined);
let file = di.fileTable[iFile];
let cbPartial = file.size - sector[DiskInfo.SECTOR.FILE_OFFSET];
sLines += sprintf("last %d bytes of %s:\n", cbPartial, file.path);
sLines += dumpSector(di, sector, 0, offset);
}
sLines += sprintf("unused %d bytes:\n", di.cbSector - offset);
sLines += dumpSector(di, sector, offset);
}
if (!sLines) sLines = "no unused data space on disk";
} else {
/**
* Other --list options include: "metadata", "sorted"
*/
sLines = di.getFileListing(iVolume, 0, argv['list']) || "\tno listing available\n";
}
printf("%s\n", sLines);
}
/**
* Similar to --extract, --type is another form of extraction (ie, extract file(s) to the console).
*/
let sExtraction, fExtractToFile;
if (argv['type']) {
fExtractToFile = false;
sExtraction = argv['type'];
} else if (argv['extract']) {
fExtractToFile = true;
sExtraction = argv['extract'];
}
let fExtractAll = (typeof sExtraction != "string");
if (sExtraction) {
let extractDir = argv['extdir'];
if (typeof extractDir != "string") {
extractDir = "";
} else if (diskFile.indexOf("http") != 0) {
extractDir = extractDir.replace("%d", path.dirname(diskFile));
}
let extractName = "", extractFolder = "";
if (fExtractAll) {
/**
* Normally, we want every disk to be extracted into its own folder, but if you're just
* extracting a single disk AND you've already specified an extraction dir, then we don't need
* to ALSO put the files inside their own disk-based folder name.
*/
if (!fSingle || !extractDir) {
extractFolder = di.getName();
}
} else {
extractName = sExtraction.toUpperCase();
}
if (argv['collection'] && !extractDir) {
extractFolder = diskLib.getLocalPath(path.join(path.dirname(diskFile), "archive", extractFolder));
if (diskFile.indexOf("/private") == 0 && diskFile.indexOf("/disks") > 0) {
extractFolder = extractFolder.replace("/disks/archive", "/archive");
}
}
diskLib.extractFiles(di, argv, extractName, path.join(extractDir, extractFolder), argv['hidden'] || !fExtractAll, fExtractToFile);
}
if (argv['manifest']) {
let manifest = di.getFileManifest(diskLib.getHash, argv['sorted'], argv['metadata']);
printManifest(diskFile, di.getName(), manifest);
}
/**
* If --rewrite, then rewrite the JSON disk image. --overwrite is implicit.
*/
if (argv['rewrite']) {
if (StrLib.getExtension(diskFile) == "json") {
diskLib.writeDiskSync(diskFile, di, argv['legacy'], 0, true, argv['quiet'], undefined, argv['source']);
}
}
/**
* If --checkdisk, then let's load the corresponding archived disk image (.IMG) as well, convert it to JSON,
* load the JSON as a disk image, save it as a temp .IMG, and verify that temp image and archived image are identical.
*
* You must ALSO specify --rebuild if you want the JSON disk image updated as well.
*/
if (argv['checkdisk'] && diskette) {
if (diskette.format) {
let matchFormat = diskette.format.match(/PC([0-9]+)K/);
if (matchFormat) {
let diskSize = di.getSize();
if (+matchFormat[1] * 1024 != diskSize) {
printf("warning: format '%s' does not match disk size (%d) for %s\n", diskette.format, diskSize, diskFile);
}
}
}
/**
* If a JSON disk image was originally built from kryoflux data AND included special args (eg, for copy-protection),
* then don't bother with the rebuild, because those disks can't be saved as IMG disk images.
*/
if (StrLib.getExtension(diskFile) == "json" && !(diskette.kryoflux && diskette.args)) {
if (typeof argv['checkdisk'] == "string" && diskFile.indexOf(argv['checkdisk']) < 0) return;
createDisk(diskFile, diskette, argv, function(diTemp) {
if (diTemp) {
let sTempJSON = path.join(rootDir, "disks", "tmp", path.basename(diskFile).replace(/\.[a-z]+$/, "") + ".json");
diTemp.setArgs(sprintf("%s --output %s%s", diskette.command, sTempJSON, diskette.args));
diskLib.writeDiskSync(sTempJSON, diTemp, argv['legacy'], 0, true, true, undefined, diskette.source);
let warning = false;
if (StrLib.getExtension(diskette.archive) == "img") {
let json = diTemp.getJSON();
diTemp.buildDiskFromJSON(json);
let sTempIMG = sTempJSON.replace(".json",".img");
diskLib.writeDiskSync(sTempIMG, diTemp, true, 0, true, true, undefined, diskette.source);
if (!compareDisks(sTempIMG, diskette.archive)) {
printf("warning: %s unsuccessfully rebuilt\n", diskette.archive);
warning = true;
} else {
fs.unlinkSync(sTempIMG);
}
}
if (!warning) {
if (argv['rebuild']) {
printf("rebuilding %s\n", diskFile);
fs.renameSync(sTempJSON, diskLib.getLocalPath(diskFile));
} else {
fs.unlinkSync(sTempJSON);
}
}
}
});
}
}
/**
* If --checkpage, then get the disk's listing and see if it's up-to-date in the website's README.md.
*
* Additionally, if the page doesn't have a machine, add one, tailored to the software's requirements as best we can.
*
* You must ALSO specify --rebuild if you want the README.md updated (or created) as well.
*/
if (argv['checkpage'] && diskette && !diskette.hidden && !diskette.demos) {
/**
* We don't need/want any software pages checked/built for private diskette collections.
*
* The PCSIG08 software pages (originally at /software/pcx86/shareware/pcsig08/ and later moved
* to /software/pcx86/sw/misc/pcsig08/) were hand-generated, so it would take some extra effort
* to automatically rebuild those. However, those pages no longer use their own set of diskette
* images at pcsig8a-disks.pcjs.org and pcsig8b-disks.pcjs.org, and the pages themselves are now
* deprecated in favor of the more complete set of pages at /software/pcx86/sw/misc/pcsig/, so the
* "pcsig8" exception below is a bit moot now.
*
* 2023-09-12 UPDATE: GitHub Pages has started giving me deployment errors, due to the large number
* of pages the website contains when a page for every PC-SIG diskette is added to the site. Even
* though the pcjs repository itself is well under the "soft" 1Gb limit that GitHub recommends, there
* is apparently also a "soft" (undocumented?) 1Gb limit on the amount of data GitHub Pages can deploy
* as well, and since the web pages are significantly larger than the README files in the repo,
* we were trying to deploy over 1.5Gb. The size itself only generated a warning, but the process of
* deploying that much data was timing out after about 10 minutes. None of this is explained very
* well by GitHub (or their email notifications), but at least their logs hinted at the problem.
*
* So, for now, only PC-SIG pages for diskettes that we have original copies of are being retained,
* and the rest are no longer being automatically generated. Hence the "/pcsig" exception below
* (formerly "/pcsig8").
*/
if (diskFile.indexOf("/private") >= 0 || diskFile.indexOf("/pcsig") >= 0) return;
if (typeof argv['checkpage'] == "string") {
if (argv['verbose']) printf("checking %s for '%s'...\n", diskFile, argv['checkpage']);
if (diskFile.indexOf(argv['checkpage']) < 0) return;
}
let sListing = di.getFileListing(0, 4);
if (!sListing) return;
let sIndex = "", sIndexNew = "", sAction = "";
let sHeading = "\n### Directory of " + diskette.name + "\n";
let sIndexFile = path.join(path.dirname(diskLib.replaceServerPrefix(diskFile, "/software/")), "README.md");
if (diskLib.existsFile(sIndexFile)) {
sIndex = sIndexNew = diskLib.readFileSync(sIndexFile);
sAction = "updated";
} else {
if (diskette.title) {
let sTitle = diskette.title;
if (sTitle.match(/[#:[\]{}]/)) {
sTitle = '"' + sTitle + '"';
}
let permalink = path.dirname(diskette.path.replace(/^\/(disks\/|)[^/]+/, "/software")) + '/';
sIndexNew = "---\nlayout: page\ntitle: " + sTitle + "\npermalink: " + permalink + "\n---\n";
sIndexNew += sHeading + sListing;
sAction = "created";
}
}
/**
* Step 1: make sure there's a machine present to load/examine/test the software.
*/
let sMachineEmbed = "";
let matchFrontMatter = sIndexNew.match(/^---\n([\s\S]*?\n)---\n/);
if (matchFrontMatter && diskette) {
let sFrontMatter = matchFrontMatter[1];
let matchMachines = sFrontMatter.match(/^machines: *\n([\s\S]*?\n)(\S|$)/m);
if (matchMachines) {
/**
* If this was a generated machine and --rebuild is set, then we'll regenerate it.
*/
if (matchMachines[1].indexOf("autoGen: true") >= 0 && matchMachines[1].indexOf(diskette.name) >= 0 && argv['rebuild']) {
sFrontMatter = sFrontMatter.replace(matchMachines[0], matchMachines[2]);
sIndexNew = sIndexNew.replace(/\n\{% include machine.html .*?%\}\n/, "");
matchMachines = null;
}
}
if (!matchMachines) {
/**
* To add a compatible machine, we look at a few aspects of the diskette itself:
*
* if the diskette format > 360K or any file dates are >= 1986, then a PC AT ("5170") is preferred;
* otherwise, if any file dates are >= 1984, a PC XT ("5160") is preferred;
* otherwise, a PC ("5150") should suffice.
*
* However, a diskette's "version" definition can also include a "@hardware" configuration with "options"
* that supplement or override those initial preferences:
*
* manufacturer, such as "ibm" or "compaq"
* model, such as "5150" or "5160"
* video preference, such as "mda" or "cga"
* memory preference, such "256kb" or "640kb"
* hardware preference, such as "com1" or "mouse"
* operating system (aka boot disk) preference, such as "PC DOS 2.00 (Disk 1)"
*
* Browse diskettes.json for more examples (look for "@hardware" properties).
*
* TODO: Finish support for all of the above preferences (eg, mouse support, serial and parallel ports, etc).
*
* TODO: Consider using the @hardware 'machine' property to allow a specific machine to be used; when that property
* is named 'url' instead, the /_includes/explorer/software.html template uses it to create a hardware_url link for the
* software, but we REALLY prefer having dedicated pages for each piece of software.
*/
let diskSize = di.getSize() / 1024;
let dateNewest = di.getNewestDate(true);
let yearNewest = dateNewest && dateNewest.getFullYear() || 1981;
let hardware = diskette.hardware || {}, options = "";
if (hardware) options = hardware.options || "";
let aOptions = options.split(",");
let findOption = function(aPossibleOptions) {
for (let i = 0; i < aPossibleOptions.length; i++) {
if (!aPossibleOptions[i]) continue;
for (let j = 0; j < aOptions.length; j++) {
if (aOptions[j].indexOf(aPossibleOptions[i]) >= 0) return aOptions[j];
}
}
return aPossibleOptions[0];
};
let findConfig = function(configPath) {
configPath = diskLib.getLocalPath(configPath);
let configPossible;
let aPossibleConfigs = glob.sync(configPath);
let optionMemory = findOption(["kb"]);
for (let i = 0; i < aPossibleConfigs.length; i++) {
let configFile = aPossibleConfigs[i];
if (configFile.indexOf("debugger") > 0 || configFile.indexOf("array") > 0) continue;
configPossible = configFile.substr(rootDir.length);
if (configFile.indexOf(optionMemory) >= 0) break;
}
return configPossible;
};
/**
* Now that we have all the raw inputs ("ingredients"), let's toss some defaults together.
*/
let sAutoGen = " autoGen: true\n";
let sAutoType = hardware.autoType;
if (sAutoType == undefined) sAutoType = diskette.autoType;
let manufacturer = findOption(["ibm","compaq"]);
let sDefaultIBMModel = diskSize > 360 || yearNewest >= 1986? "5170" : (yearNewest >= 1984? "5160" : "5150");
let sDefaultCOMPAQModel = diskSize > 360 || yearNewest >= 1986? "deskpro386" : "portable";
let model = findOption({
"ibm": [sDefaultIBMModel, "5150","5160","5170"],
"compaq": [sDefaultCOMPAQModel, "portable","deskpro386"]
}[manufacturer]);
let video = findOption(["*","mda","cga","ega","vga","vdu"]);
let configFile = hardware.config || findConfig("/machines/pcx86/" + manufacturer + '/' + model + '/' + video + "/**/machine.xml");
if (configFile == "none") configFile = "";
if (configFile) {
let bootDisk = findOption(["", "DOS"]);
let demoDisk = diskette.name;
let sDiskettes = "";
let diskMatch = diskFile.match(/\/pcsig\/([0-9])[0-9]+-/);
if (diskMatch) {
sDiskettes = " diskettes: /machines/pcx86/diskettes.json,/disks/pcsigdisks/pcx86/diskettes.json\n";
}
if (diskette.bootable) {
bootDisk = demoDisk;
demoDisk = "";
} else {
if (sAutoType == undefined) sAutoType = "$date\\r$time\\rB:\\rDIR\\r";
}
let sMachineID = (model.length <= 4? manufacturer : "") + model;
let sMachine = " - id: " + sMachineID + "\n type: pcx86\n config: " + configFile + "\n";
for (let prop in hardware) {
if (prop == "autoType" || prop == "config" || prop == "machine" || prop == "options" || prop == "url" || prop[0] == '@') continue;
let chQuote = "";
if (prop == "drives" || prop == "floppyDrives") {
chQuote = "'";
if (prop == "drives") bootDisk = "None";
sAutoType = "";
}
sMachine += " " + prop + ": " + chQuote + hardware[prop] + chQuote + "\n";
}
if (bootDisk) bootDisk = " A: \"" + bootDisk + "\"\n";
if (demoDisk) demoDisk = " B: \"" + demoDisk + "\"\n";
let sAutoMount = " autoMount:\n" + bootDisk + demoDisk;
if (sAutoType) sAutoType = " autoType: " + sAutoType + "\n";
sFrontMatter += "machines:\n" + sMachine + sDiskettes + sAutoGen + sAutoMount + (sAutoType || "");
sIndexNew = sIndexNew.replace(matchFrontMatter[1], sFrontMatter);
sMachineEmbed = "\n{% include machine.html id=\"" + sMachineID + "\" %}\n";
}
}
}
/**
* Step 2: Making sure there's an up-to-date directory listing. The listing can include a picture of
* the diskette, if any, and a link to the source of the diskette, if any, so append those to the listing now.
*
* Picture example:
*
* ![MS C 1.03 Beta (Disk 1)]({{ site.software.miscdisks.server }}/pcx86/lang/microsoft/c/1.03/MSC103-BETA-DISK1.jpg)
*/
let sDiskPic = diskette.path.replace(".json", ".jpg");
if (!diskLib.existsFile(sDiskPic)) {
sDiskPic = diskette.path.replace(".json", ".png");
}
if (diskLib.existsFile(sDiskPic)) {
let sDiskServer = diskLib.getServerPrefix(sDiskPic);
if (sDiskServer) {
sListing += "\n![" + diskette.name + "]({{ site.software." + sDiskServer.replace("disks/", "") + ".server }}" + sDiskPic.slice(sDiskServer.length + 1) + ")\n";
}
/**
* Let's rematch the page header and see if the page also needs a preview image.
*/
matchFrontMatter = sIndexNew.match(/^(---\n[\s\S]*?\n---\n)/);
if (matchFrontMatter) {
let sFrontMatter = matchFrontMatter[1];
let match = sFrontMatter.match(/\npreview:.*\n/);
if (!match) {
match = sFrontMatter.match(/\npermalink:.*\n/);
if (match) {
let n = match.index + match[0].length;
sDiskPic = diskLib.getServerPath(sDiskPic, true);
sFrontMatter = sFrontMatter.slice(0, n) + "preview: " + sDiskPic + "\n" + sFrontMatter.slice(n);
sIndexNew = sFrontMatter + sIndexNew.slice(matchFrontMatter[0].length);
}
}
}
}
if (diskette.source && !diskette.source.indexOf("http")) {
sListing += "\n[[Source](" + diskette.source + ")]\n";
}
let sMatch = "\n(##+)\\s+Directory of " + diskette.name.replace("(","\\(").replace(")","\\)").replace("*","\\*").replace("+","\\+") + " *\n([\\s\\S]*?)(\n[^!{[\\s]|$)";
let matchDirectory = sIndexNew.match(new RegExp(sMatch));
if (matchDirectory) {
if (matchDirectory[1].length != 3) {
printf("warning: directory heading level '%s' should really be '###'\n", matchDirectory[1]);
}
let matchInclude = matchDirectory[2].match(/\n\{%.*?%}\n/);
/**
* Work around JavaScript's handling of '$' in the replacement string ('$' is interpreted as a back-reference
* indicator, with '$$' interpreted as '$', even when the search string is NOT a regular expression) by first
* replacing every '$' with '$$' in sListing (the only portion where we're likely encounter '$' characters).
*
* Note that the work-around itself is subject to the interpretation of '$$' as '$', therefore it must use '$$$$'.
*/
sIndexNew = sIndexNew.replace(matchDirectory[0], sHeading + (matchInclude? matchInclude[0] : "") + sListing.replace(/\$/g, "$$$$") + matchDirectory[3]);
} else {
/**
* Look for the last "Directory of ..." entry and insert this directory listing after it (and if there's none, append it).
*/
sListing = sHeading + sListing;
let matchLast, match, re = /\n(##+)\\s+Directory of [^\n]*\n([\\s\\S]*?)\n(\\S|$)/g;
while ((match = re.exec(sIndexNew))) {
matchLast = match;
}
let index = sIndexNew.length, length = 0;
if (matchLast) {
index = matchLast.index;
length = matchLast[0].length;
if (matchLast[3]) length--;
}
sIndexNew = sIndexNew.substr(0, index + length) + sListing + sIndex.substr(index + length);
}
/**
* Step 3: If a generated machine needs to be embedded, put it just ahead of the first directory listing (which
* is why we waited until now); if there are any diskette 'info' summary lines, we want it just ahead of those, too.
*/
let info = "";
if (diskette.info) {
let i;
info += "\n## Information about \"" + diskette.info.diskTitle + "\"\n\n";
for (i = 0; i < diskette.info.diskSummary.length; i++) {
info += " " + diskette.info.diskSummary[i] + "\n";
}
}
/**
* Along with any diskette info, see if there are any files in the decompressed archive folder that we might want
* to include in the index, too.
*
* For now, this inclusion is limited to the PC-SIG collection.
*/
let samples = "";
if (diskFile.indexOf("/pcsig/") >= 0) {
let sampleSpec = path.join(path.dirname(diskLib.getLocalPath(diskette.path)), "archive", "**", "*.{ASM,BAS,DOC,TXT}");
let sampleFiles = glob.sync(sampleSpec);
for (let sampleFile of sampleFiles) {
let sample = diskLib.readFileSync(sampleFile);
if (sample) {
if (CharSet.isText(sample)) {
let fileType = StrLib.getExtension(sampleFile) == "BAS"? "bas" : "";
if (sample[sample.length-1] != '\n') sample += '\n';
sample = "{% raw %}\n```" + fileType + "\n" + sample /* .replace(/([^\n]*\n)/g, ' $1\n') */ + "```\n{% endraw %}\n";
samples += "\n## " + path.basename(sampleFile) + "\n\n" + sample;
} else {
printf("warning: ignoring non-text file '%s'\n", sampleFile);
}
}
}
}
/**
* Clean out any old info and then add any new info. It should be bracketed by 'info_begin'/'info_end' comments.
*/
let sInsert = sMachineEmbed;
let match = sIndexNew.match(/\n\{% comment %\}info_begin\{% endcomment %\}[\S\s]*\{% comment %\}info_end\{% endcomment %\}\n\n/);
if (match) {
sIndexNew = sIndexNew.slice(0, match.index) + sIndexNew.slice(match.index + match[0].length);
} else {
let i = sIndexNew.indexOf(info); // look for (old) unbracketed info, too (probably don't need this anymore)
if (i >= 0) {
sIndexNew = sIndexNew.slice(0, i) + sIndexNew.slice(i + info.length);
}
}
if (info) {
sInsert += "\n{% comment %}info_begin{% endcomment %}\n" + info + "{% comment %}info_end{% endcomment %}\n\n";
}
/**
* Clean out any old samples and then add any new samples. They should be bracketed by 'samples_begin'/'samples_end' comments.
*/
match = sIndexNew.match(/\{% comment %\}samples_begin\{% endcomment %\}[\S\s]*\{% comment %\}samples_end\{% endcomment %\}\n/);
if (match) {
sIndexNew = sIndexNew.slice(0, match.index) + sIndexNew.slice(match.index + match[0].length);
}
if (samples) {
sInsert += "{% comment %}samples_begin{% endcomment %}\n" + samples + "\n{% comment %}samples_end{% endcomment %}\n";
}
if (sInsert) {
matchDirectory = sIndexNew.match(/\n(##+)\s+Directory of /);
if (matchDirectory) {
/**
* WARNING: This is another place where we need to work around JavaScript's handling of '$' in the replacement string.
*/
sIndexNew = sIndexNew.replace(matchDirectory[0], sInsert.replace(/\$/g, "$$$$") + matchDirectory[0]);
} else {
sIndexNew += sInsert;
}
}
/**
* Step 4: Add a document gallery section if there are any documents associated with this software.
*/
if (diskette.documents) {
let skip = true;
for (let document of diskette.documents) {
if (document['@link'] != path.dirname(sIndexFile) + '/') {
skip = false;
break;
}
}
if (!skip) {
let sHeader = "\n<!-- Documentation -->\n";
let sGallery = sHeader + "\n{% include gallery/documents.html width=\"200\" height=\"260\" %}\n";
if (sIndexNew && sIndexNew.indexOf(sHeader) < 0) {
sIndexNew += sGallery;
}
}
}
if (!sIndexNew) {
printf("\tmissing index for \"%s\": %s\n", diskette.title, sIndexFile);
}
else if (sIndexNew != sIndex) {
if (argv['rebuild']) {
if (diskLib.writeFileSync(diskLib.getLocalPath(sIndexFile), sIndexNew, true, true)) {
printf("\t%s index for \"%s\": %s\n", sAction, diskette.title, sIndexFile);
}
} else {
printf("\tindex for \"%s\" should be %s (%s); use --rebuild\n", diskette.title, sAction, sIndexFile);
}
}
}
/**
* NOTE: When recreating an IMG file from a JSON file, if the JSON file preserved the original BPB
* (which includes the original OEM signature), then you can use --legacy to tell writeDiskSync() to tell
* getData() to restore those BPB bytes as well. Otherwise, we leave the PCJS_OEM signature, if any, alone.
*/
if (!diskette) {
if (argv['boot']) {
di.updateBootSector(diskLib.readFileSync(argv['boot'], null));
}
let output = argv['output'];
if (!output || typeof output == "boolean") {
output = argv[1];
}
if (output) {
if (typeof output == "string") output = [output];
if (Array.isArray(output)) {
output.forEach((outputFile) => {
let file = outputFile.replace("%d", path.dirname(diskFile));
diskLib.writeDiskSync(file, di, argv['legacy'], argv['indent']? 2 : 0, argv['overwrite'], argv['quiet'], argv['writable'], argv['source']);
});
} else {
printf("missing output file(s)\n");
}
}
}
}
/**
* readCollection(argv)
*
* If "--collection=[string]" then the set of disks is limited to those where pathname contains [string].
*
* @param {Array} argv
*/
function readCollection(argv)
{
let family = "pcx86";
let asServers = ["diskettes", "gamedisks", "miscdisks", "pcsigdisks", "private"];
let cCollections = 0, cDisks = 0;
let asCollections = [];
asServers.forEach((server) => {
asCollections = asCollections.concat(glob.sync(path.join(rootDir, "disks" + path.sep + server + path.sep + family + path.sep + (server == "pcsigdisks"? "diskettes-annotated.json" : "diskettes.json"))));
});
let messages;
if (argv['quiet']) {
messages = device.setMessages(MESSAGE.WARNING + MESSAGE.ERROR, false);
}
let aDiskNames = {}; // we use this table of disk names to detect non-unique disk names
asCollections.forEach(function readAllCollections(collectionFile) {
collectionFile = collectionFile.substr(rootDir.length);
if (argv['verbose']) printf("reading collection %s...\n", collectionFile);
let library = diskLib.readJSONSync(collectionFile);
if (library) {
let aDiskettes = [];
JSONLib.parseDiskettes(aDiskettes, library, "/pcx86", "/diskettes");
aDiskettes.forEach(function readAllDiskettes(diskette) {
diskette.argc = 0;
diskette.argv = [];
if (!diskette.args) {
diskette.args = "";
} else {
[diskette.argc, diskette.argv] = PCJSLib.getArgs(diskette.args);
diskette.args = " " + diskette.args;
}
/**
* TODO: I don't think '@local' is being used anymore, so consider removing this support. The last
* place I saw it used was in the PCSIG08 diskettes.json files, but weblib.getResource() knows how to map
* local folder names (eg, /pcsig8a-disks) to the corresponding server names now, as does this module,
* so there's probably no longer any need for this.
*/
if (library['@local']) {
diskette.path = diskette.path.replace(library['@server'], library['@local']);
}
let diskFile = diskette.path;
if (typeof argv['collection'] == "string") {
if (argv['verbose']) printf("checking %s for '%s'...\n", diskFile, argv['collection']);
if (diskFile.indexOf(argv['collection']) < 0) return;
}
let sName = path.basename(diskFile);
if (aDiskNames[sName]) {
if (argv['verbose']) printf("warning: %s disk name is not unique (see %s)\n", diskFile, aDiskNames[sName]);
} else {
aDiskNames[sName] = diskFile;
}
let done = function(di, fWrite = true) {
if (di) {
if (fWrite) {
diskLib.writeDiskSync(diskFile, di, false, 0, undefined, undefined, undefined, diskette.source);
}
processDisk(di, diskFile, argv, diskette);
cDisks++;
}
};
let di = diskLib.readDiskSync(diskFile);
if (di) {
done(di, false);
} else {
createDisk(diskFile, diskette, argv, done);
}
});
}
cCollections++;
});
printf("%d config(s), %d disks(s) processed\n\n", cCollections, cDisks);
if (messages) device.setMessages(messages, true);
}
/**
* getArchiveOffset(sArchive, arcType, sOffset)
*
* There were some ARC archives embedded in EXE files (eg, old self-extracting archives) produced by PKware, before they
* started using ZIP archives. Examples include:
*
* PKX35A35.EXE
* PK361.EXE
* PKFIND11.EXE
*
* They can be detected by a 32-bit signature near the end of the file ("PK\xAA\x55" or 0x55aa4b50) followed by a 32-bit
* archive size. The beginning of the archive can be found by subtracting the archive size from the file size (ie, the file
* size up to and including the 32-bit archive size), and then subtracting another 40 (0x28) bytes from that value.
*
* TODO: Determine what that final 40-byte offset represents.
*
* However, since this is an expensive operation, we perform this search ONLY if 1) the caller doesn't provide an explicit
* offset, 2) the caller explicitly set the archive type to TYPE_ARC, and 3) the input file is an EXE file.
*
* There were self-extracting ARC archives produced by SEA (System Enhancement Associates) as well (eg, ARC602.EXE from 1989),
* but those used a different format; this function does not yet support those files.
*
* Self-extracting ZIP archives don't need any help locating the archive offset, because the ZIP file format specifically
* allows for prepended files (eg, EXE files).
*
* @param {string} sArchive
* @param {number} arcType
* @param {string} sOffset
* @returns {number} (the specified --offset value, if any, else the offset of the embedded ARC archive, if any; -1 if none)
*/
function getArchiveOffset(sArchive, arcType, sOffset)
{
let offset = 0;
if (sOffset) {
offset = +sOffset || 0;
} else {
if (arcType == StreamZip.TYPE_ARC && sArchive.toUpperCase().endsWith(".EXE")) {
offset = -1;
let data = diskLib.readFileSync(sArchive, null);
if (data) {
let sizeArc = -1, sizeFile;
let max = 512; // limit the search to the last 512 bytes of the file
for (let o = data.length - 8; o >= 0 && max--; o--) {
if (data.readUInt32LE(o) == 0x55aa4b50) {
sizeArc = data.readUInt32LE(o + 4);
sizeFile = o + 8;
break;
}
}