-
Notifications
You must be signed in to change notification settings - Fork 0
/
atarifilesystem.cpp
executable file
·1338 lines (1160 loc) · 39.1 KB
/
atarifilesystem.cpp
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
#include "atarifilesystem.h"
#include <QtGlobal>
#include <QDir>
#include <QMessageBox>
#include "diskeditdialog.h"
/* Compare functions */
bool atariDirEntryNoLessThan(const AtariDirEntry &e1, const AtariDirEntry &e2)
{
return e1.no < e2.no;
}
bool atariDirEntryNoGreaterThan(const AtariDirEntry &e1, const AtariDirEntry &e2)
{
return e1.no > e2.no;
}
bool atariDirEntryNameLessThan(const AtariDirEntry &e1, const AtariDirEntry &e2)
{
return e1.baseName() < e2.baseName();
}
bool atariDirEntryNameGreaterThan(const AtariDirEntry &e1, const AtariDirEntry &e2)
{
return e1.baseName() > e2.baseName();
}
bool atariDirEntryExtensionLessThan(const AtariDirEntry &e1, const AtariDirEntry &e2)
{
return e1.suffix() < e2.suffix();
}
bool atariDirEntryExtensionGreaterThan(const AtariDirEntry &e1, const AtariDirEntry &e2)
{
return e1.suffix() > e2.suffix();
}
bool atariDirEntrySizeLessThan(const AtariDirEntry &e1, const AtariDirEntry &e2)
{
return e1.size < e2.size;
}
bool atariDirEntrySizeGreaterThan(const AtariDirEntry &e1, const AtariDirEntry &e2)
{
return e1.size > e2.size;
}
bool atariDirEntryDateLessThan(const AtariDirEntry &e1, const AtariDirEntry &e2)
{
return e1.dateTime < e2.dateTime;
}
bool atariDirEntryDateGreaterThan(const AtariDirEntry &e1, const AtariDirEntry &e2)
{
return e1.dateTime > e2.dateTime;
}
bool atariDirEntryNotesLessThan(const AtariDirEntry &e1, const AtariDirEntry &e2)
{
return e1.attributeNames() < e2.attributeNames();
}
bool atariDirEntryNotesGreaterThan(const AtariDirEntry &e1, const AtariDirEntry &e2)
{
return e1.attributeNames() > e2.attributeNames();
}
/* AtariDirEntry */
QString AtariDirEntry::name() const
{
QString s = baseName();
QString e = suffix();
if (!e.isEmpty()) {
s.append(".");
s.append(e);
}
return s;
}
QString AtariDirEntry::niceName() const
{
QString n = name();
if (n == n.toUpper()) {
return n.toLower();
} else {
return n;
}
}
QString AtariDirEntry::baseName() const
{
return QString::fromLatin1(atariName.left(8).constData()).trimmed();
}
QString AtariDirEntry::suffix() const
{
return QString::fromLatin1(atariName.right(3).constData()).trimmed();
}
QString AtariDirEntry::attributeNames() const
{
QString result;
if (attributes & Locked) {
result += "Locked";
}
if (attributes & Hidden) {
if (!result.isEmpty()) {
result += ", ";
}
result += "Hidden";
}
if (attributes & Archived) {
if (!result.isEmpty()) {
result += ", ";
}
result += "Archived";
}
if (attributes & Directory) {
if (!result.isEmpty()) {
result += ", ";
}
result += "Directory";
}
if (attributes & Dos10) {
if (!result.isEmpty()) {
result += ", ";
}
result += "Dos 1.0 file";
}
if (attributes & Dos25) {
if (!result.isEmpty()) {
result += ", ";
}
result += "Dos 2.5 file";
}
if (attributes & MyDos) {
if (!result.isEmpty()) {
result += ", ";
}
result += "MyDos file";
}
return result;
}
void AtariDirEntry::makeFromAtariDosEntry(const QByteArray &entry, int aNo, int aDir, bool dd)
{
// Translate the attributes
attributes = 0;
internalData = entry;
quint8 f = (quint8) entry.at(0);
if (f & 0x10) {
attributes |= Directory;
}
if (f & 0x20) {
attributes |= Locked;
}
if ((f & 0x01) && !(f & 0x40)) {
attributes |= Dos25;
}
if (f & 0x04) {
attributes |= MyDos;
}
if (!(f & 0x02) && !(f & 0x10)) {
attributes |= Dos10;
}
// Translate the name
atariName = entry.mid(5, 11);
// Translate the size in blocks
if (attributes & Directory) {
size = -1;
} else {
if (dd) {
size = ((quint8) entry.at(1) + (quint8) entry.at(2) * 256) * 253;
} else {
size = ((quint8) entry.at(1) + (quint8) entry.at(2) * 256) * 125;
}
}
// Translate the first sector
firstSector = (quint8) entry.at(3) + (quint8) entry.at(4) * 256;
// Put an invalid date
dateTime = QDateTime();
dir = aDir;
no = aNo;
}
void AtariDirEntry::makeFromSpartaDosEntry(const QByteArray &entry, int aNo, int aDir)
{
// Translate the attributes
attributes = 0;
internalData = entry;
quint8 f = (quint8) entry.at(0);
if (f & 0x01) {
attributes |= Locked;
}
if (f & 0x02) {
attributes |= Hidden;
}
if (f & 0x04) {
attributes |= Archived;
}
if (f & 0x20) {
attributes |= Directory;
}
// Translate the name
atariName = entry.mid(6, 11);
// Translate the size
if (attributes & Directory) {
size = -1;
} else {
size = (quint8)entry.at(3) + (quint8)entry.at(4) * 256 + (quint8)entry.at(5) * 65536;
}
// Translate the first sector
firstSector = (quint8) entry.at(1) + (quint8) entry.at(2) * 256;
// Translate the date/time
int year = (quint8)entry.at(19) + 1900;
if (year < 1980) {
year += 100;
}
QDate date(year, (quint8)entry.at(18), (quint8)entry.at(17));
QTime time((quint8)entry.at(20), (quint8)entry.at(21), (quint8)entry.at(22));
dateTime = QDateTime(date, time);
dir = aDir;
no = aNo;
}
/* AtariFileSystem */
bool AtariFileSystem::extractRecursive(QList<AtariDirEntry> &entries, const QString &target)
{
foreach (AtariDirEntry e, entries) {
if (e.attributes & AtariDirEntry::Directory) {
QString newDir = target + "/" + e.niceName();
if (!QDir(newDir).mkdir(newDir)) {
QMessageBox::critical(m_image->editDialog(), tr("Atari file system error"), tr("Cannot create directory '%1'.").arg(e.niceName()));
return false;
}
QList <AtariDirEntry> subs = getEntries(e.firstSector);
bool res = extractRecursive(subs, newDir);
if (!res) {
return false;
}
continue;
}
if (!extract(e, target)) {
return false;
}
}
return true;
}
bool AtariFileSystem::deleteRecursive(QList<AtariDirEntry> &entries)
{
foreach (AtariDirEntry e, entries) {
if (e.attributes & AtariDirEntry::Directory) {
QList <AtariDirEntry> subs = getEntries(e.firstSector);
bool res = deleteRecursive(subs);
if (!res) {
return false;
}
removeDir(e);
continue;
}
if (!erase(e)) {
return false;
}
}
return true;
}
QList <AtariDirEntry> AtariFileSystem::insertRecursive(quint16 dir, QStringList files)
{
QList <AtariDirEntry> result;
foreach (QString name, files) {
AtariDirEntry entry;
QFileInfo info(name);
if (info.isDir()) {
entry = makeDir(dir, name);
if (!entry.isValid()) {
return result;
}
QDir subDir(name);
QStringList subList;
foreach (QFileInfo i, subDir.entryInfoList(QDir::NoDotAndDotDot | QDir::Dirs | QDir::Files)) {
subList.append(i.absoluteFilePath());
}
insertRecursive(entry.firstSector, subList);
} else {
entry = insert(dir, name);
if (!entry.isValid()) {
return result;
}
}
result.append(entry);
}
return result;
}
QByteArray AtariFileSystem::findName(quint16 dir, QString name)
{
QList <AtariDirEntry> entries = getEntries(dir);
QFileInfo info(name);
QString baseName = info.completeBaseName().toUpper();
QString extension = info.suffix().toUpper();
QString atariName, pfx;
QByteArray result;
baseName.remove(QRegExp("[^A-Z0-9]"));
baseName = baseName.left(8);
if (baseName.isEmpty()) {
baseName = "BADNAME";
}
extension.remove(QRegExp("[^A-Z0-9]"));
extension = extension.left(3);
while (extension.count() < 3) {
extension.append(" ");
}
pfx = baseName;
for (int i = 1; i < 99999999; i++) {
atariName = baseName;
while (atariName.count() < 8) {
atariName.append(" ");
}
atariName.append(extension);
result = atariName.toLatin1();
bool found = false;
foreach (AtariDirEntry e, entries) {
if (e.atariName == result) {
found = true;
break;
}
}
if (!found) {
return result;
}
QString sfx = QString::number(i + 1);
baseName = pfx;
if (baseName.count() + sfx.count() > 8) {
baseName.resize(8 - sfx.count());
}
baseName.append(sfx);
}
return QByteArray();
}
quint16 AtariFileSystem::findFreeSector(quint16 from)
{
quint8 masks[8] = {128, 64, 32, 16, 8, 4, 2, 1};
quint16 sector;
uint sectors = bitmap.count() * 8;
uint startFrom = from;
if (from < 4) {
startFrom = 4;
}
for (sector = startFrom; sector < sectors; sector++) {
if (bitmap.at(sector / 8) & masks[sector % 8]) {
return sector;
}
}
if (from == 0) {
return 0;
} else {
return findFreeSector(0);
}
}
void AtariFileSystem::allocateSector(quint16 sector)
{
quint8 masks[8] = {128, 64, 32, 16, 8, 4, 2, 1};
bitmap[sector / 8] = bitmap.at(sector / 8) & ~masks[sector % 8];
m_freeSectors--;
}
void AtariFileSystem::freeSector(quint16 sector)
{
quint8 masks[8] = {128, 64, 32, 16, 8, 4, 2, 1};
bitmap[sector / 8] = bitmap.at(sector / 8) | masks[sector % 8];
m_freeSectors++;
}
bool AtariFileSystem::sectorIsFree(quint16 sector)
{
uint sectors = bitmap.count() * 8;
if (sector < 4 || sector > sectors) {
return false;
}
quint8 masks[8] = {128, 64, 32, 16, 8, 4, 2, 1};
return (bitmap.at(sector / 8) & masks[sector % 8]) != 0;
}
/* Dos10FileSystem */
Dos10FileSystem::Dos10FileSystem(SimpleDiskImage *image)
: AtariFileSystem(image)
{
m_image->readSector(360, vtoc);
bitmap = vtoc.mid(10, 90);
m_freeSectors = (quint8)vtoc.at(3) + (quint8)vtoc.at(4) * 256;
}
QList <AtariDirEntry> Dos10FileSystem::getEntries(quint16 dir)
{
QList <AtariDirEntry> list;
bool dd = m_image->geometry().bytesPerSector() == 256;
int no = 0;
for (quint16 s = dir; s < dir + 8; s++) {
QByteArray data;
m_image->readSector(s, data);
for (uint e = 0; e < 8; e++, no++) {
QByteArray dosEntry = data.mid(e * 16, 16);
quint8 f = (quint8)dosEntry.at(0);
if (f == 0) {
goto bailout;
}
if (((f & 65) == 65) || (f & 128)) {
continue;
}
AtariDirEntry a;
a.makeFromAtariDosEntry(dosEntry, no, dir, dd);
list.append(a);
}
}
bailout:
return list;
}
uint Dos10FileSystem::totalCapacity()
{
return 707 * 125;
}
QString Dos10FileSystem::volumeLabel()
{
return QString();
}
bool Dos10FileSystem::setVolumeLabel(const QString &)
{
return false;
}
bool Dos10FileSystem::extract(const AtariDirEntry &entry, const QString &target)
{
QFile file(target + "/" + entry.niceName());
QFile::OpenMode mode;
mode = QFile::WriteOnly | QFile::Truncate;
if (!file.open(mode)) {
QMessageBox::critical(m_image->editDialog(), tr("Atari file system error"), tr("Cannot create file '%1'.").arg(entry.niceName()));
return false;
}
quint16 sector = entry.firstSector;
for (uint n = entry.size / (m_image->geometry().bytesPerSector() - 3); n > 0 && sector != 0; n--) {
QByteArray data;
if (!m_image->readSector(sector, data)) {
QMessageBox::critical(m_image->editDialog(), tr("Atari file system error"), tr("Cannot read '%1': %2").arg(entry.niceName()).arg(tr("Sector read failed.")));
return false;
}
if (!(entry.attributes & AtariDirEntry::MyDos)) {
int fileNo = (quint8)data.at(data.count() - 3) >> 2;
if (fileNo != entry.no) {
QMessageBox::critical(m_image->editDialog(), tr("Atari file system error"), tr("Cannot read '%1': %2").arg(entry.niceName()).arg(tr("File number mismatch.")));
return false;
}
sector = ((quint8)data.at(data.count() - 3) & 0x03) * 256 + (quint8)data.at(data.count() - 2);
} else {
sector = (quint8)data.at(data.count() - 3) * 256 + (quint8)data.at(data.count() - 2);
}
uint size = (quint8)data.at(data.count() - 1);
if (!(entry.attributes & AtariDirEntry::Dos10)) {
data.resize(size);
} else {
if (size & 128) {
data.resize(size - 128);
} else {
data.resize(125);
}
}
QByteArray nData;
nData.clear();
if (m_textConversion) {
int j = 0;
for (int i = 0; i < data.count(); i++) {
#ifdef Q_OS_WIN
if (data.at(i) == '\r') {
// ignore carriage return
} else if (data.at(i) == '\n') {
nData[j] = '\x9b';
j += 1;
} else if (data.at(i) == '\x9b') {
nData[j] = '\r';
j += 1;
nData[j] = '\n';
j += 1;
} else {
nData[j] = data.at(i);
j += 1;
}
#else
if (data.at(i) == '\n') {
nData[j] = '\x9b';
j += 1;
} else if (data.at(i) == '\x9b') {
nData[j] = '\n';
j += 1;
} else {
nData[j] = data.at(i);
j += 1;
}
#endif
}
} else {
nData = data;
}
if (file.write(nData) != nData.count()) {
QMessageBox::critical(m_image->editDialog(), tr("Atari file system error"), tr("Cannot write to '%1': %2").arg(file.fileName()).arg(file.errorString()));
return false;
}
}
return true;
}
AtariDirEntry Dos10FileSystem::insert(quint16 dir, const QString &name)
{
AtariDirEntry result;
QByteArray dosEntry = findName(dir, name);
if (dosEntry.isEmpty()) {
QMessageBox::critical(m_image->editDialog(), tr("Atari file system error"), tr("Cannot insert '%1': %2").arg(name).arg(tr("Cannot find a suitable file name.")));
return result;
}
int no = findFreeFileNo(dir);
if (no < 0) {
QMessageBox::critical(m_image->editDialog(), tr("Atari file system error"), tr("Cannot insert '%1': %2").arg(name).arg(tr("Directory is full.")));
return result;
}
dosEntry.prepend(QByteArray(5, 0));
QFile file(name);
QFile::OpenMode mode;
if (m_textConversion) {
mode = QFile::ReadOnly | QFile::Text;
} else {
mode = QFile::ReadOnly;
}
if (!file.open(mode)) {
QMessageBox::critical(m_image->editDialog(), tr("Atari file system error"), tr("Cannot open '%1': %2").arg(name).arg(file.errorString()));
return result;
}
if (file.size() > freeSpace()) {
QMessageBox::critical(m_image->editDialog(), tr("Atari file system error"), tr("Cannot insert '%1': %2").arg(name).arg(tr("Disk is full.")));
return result;
}
int dataSize = m_image->geometry().bytesPerSector() - 3;
int sector, newSector;
bool hiUsed = false;
bool myDos = fileSystemCode() == 3 && (vtoc.at(0) > 2);
int firstSector = findFreeSector(0);
sector = firstSector;
if (sector == 0) {
QMessageBox::critical(m_image->editDialog(), tr("Atari file system error"), tr("Cannot insert '%1': %2").arg(name).arg(tr("Disk is full.")));
return result;
}
allocateSector(sector);
int sectorCount = 0;
do {
sectorCount++;
if (sector > 719) {
hiUsed = true;
}
int size = dataSize;
if (file.bytesAvailable() < size) {
size = file.bytesAvailable();
}
QByteArray data = file.read(size);
// Only in binary mode data.count must be = size, so if the file opened in text mode don't display an error
// The message below should normally never display unless there is a system error.
if (data.count() < size) {
if(mode == (QFile::ReadOnly | QFile::Text)) {
size = data.count();
} else {
QMessageBox::critical(m_image->editDialog(), tr("File system error"), tr("Number of bytes (%1) read from '%2' is not equal to expected data size of (%3)").arg(data.count()).arg(name).arg(size));
return result;
}
}
if (!file.atEnd()) {
newSector = findFreeSector(sector + 1);
allocateSector(newSector);
} else {
newSector = 0;
}
if (m_textConversion) {
for (int i = 0; i < data.count(); i++) {
if (data.at(i) == '\n') {
data[i] = '\x9b';
} else if (data.at(i) == '\x9b') {
data[i] = '\n';
}
}
}
data.resize(dataSize + 3);
if (myDos) {
data[dataSize] = newSector / 256;
data[dataSize + 1] = newSector % 256;
data[dataSize + 2] = size;
} else {
data[dataSize] = (newSector / 256) | (no * 4);
data[dataSize + 1] = newSector % 256;
if (fileSystemCode() != 0) {
data[dataSize + 2] = size;
} else {
if (file.atEnd()) {
data[dataSize + 2] = size | 128;
} else {
data[dataSize + 2] = 0;
}
}
}
if (!m_image->writeSector(sector, data)) {
QMessageBox::critical(m_image->editDialog(), tr("Atari file system error"), tr("Cannot insert '%1': %2").arg(name).arg(tr("Sector write failed.")));
return result;
}
sector = newSector;
} while (!file.atEnd());
quint16 dirsec = dir + no / 8;
int start = (no % 8) * 16;
QByteArray data;
if (!image()->readSector(dirsec, data)) {
QMessageBox::critical(m_image->editDialog(), tr("Atari file system error"), tr("Cannot insert '%1': %2").arg(name).arg(tr("Sector read failed.")));
return result;
}
quint8 flag;
if (myDos) {
flag = 0x47;
} else if (fileSystemCode() == 2 && hiUsed) {
flag = 0x03;
} else {
flag = 0x42;
}
dosEntry[0] = flag;
dosEntry[1] = sectorCount % 256;
dosEntry[2] = sectorCount / 256;
dosEntry[3] = firstSector % 256;
dosEntry[4] = firstSector / 256;
data.replace(start, 16, dosEntry);
if (!image()->writeSector(dirsec, data)) {
QMessageBox::critical(m_image->editDialog(), tr("Atari file system error"), tr("Cannot insert '%1': %2").arg(name).arg(tr("Sector write failed.")));
return result;
}
writeBitmap();
result.makeFromAtariDosEntry(dosEntry, no, dir, m_image->geometry().bytesPerSector() == 256);
return result;
}
AtariDirEntry Dos10FileSystem::makeDir(quint16 dir, const QString &name)
{
AtariDirEntry result;
QByteArray dosEntry = findName(dir, name);
if (dosEntry.isEmpty()) {
QMessageBox::critical(m_image->editDialog(), tr("Atari file system error"), tr("Cannot insert '%1': %2").arg(name).arg(tr("Cannot find a suitable file name.")));
return result;
}
int no = findFreeFileNo(dir);
if (no < 0) {
QMessageBox::critical(m_image->editDialog(), tr("Atari file system error"), tr("Cannot insert '%1': %2").arg(name).arg(tr("Directory is full.")));
return result;
}
dosEntry.prepend(QByteArray(5, 0));
dosEntry[0] = 0x10;
dosEntry[1] = 8;
int first = findFreeSector(369);
int sector = first;
if (sector == 0) {
QMessageBox::critical(m_image->editDialog(), tr("Atari file system error"), tr("Cannot insert '%1': %2").arg(name).arg(tr("Disk is full.")));
return result;
}
bool found = false;
do {
if (sectorIsFree(sector + 1) &&
sectorIsFree(sector + 2) &&
sectorIsFree(sector + 3) &&
sectorIsFree(sector + 4) &&
sectorIsFree(sector + 5) &&
sectorIsFree(sector + 6) &&
sectorIsFree(sector + 7)) {
found = true;
break;
}
sector = findFreeSector(sector + 1);
} while (sector != first);
if (!found) {
QMessageBox::critical(m_image->editDialog(), tr("Atari file system error"), tr("Cannot insert '%1': %2").arg(name).arg(tr("Disk is full.")));
return result;
}
QByteArray empty(m_image->geometry().bytesPerSector(), 0);
for (int i = sector; i < sector + 8; i++) {
allocateSector(i);
if (!m_image->writeSector(i, empty)) {
QMessageBox::critical(m_image->editDialog(), tr("Atari file system error"), tr("Cannot insert '%1': %2").arg(name).arg(tr("Sector write failed.")));
return result;
}
}
dosEntry[3] = sector % 256;
dosEntry[4] = sector / 256;
quint16 dirsec = dir + no / 8;
int start = (no % 8) * 16;
QByteArray data;
if (!image()->readSector(dirsec, data)) {
QMessageBox::critical(m_image->editDialog(), tr("Atari file system error"), tr("Cannot insert '%1': %2").arg(name).arg(tr("Sector read failed.")));
return result;
}
data.replace(start, 16, dosEntry);
if (!image()->writeSector(dirsec, data)) {
QMessageBox::critical(m_image->editDialog(), tr("Atari file system error"), tr("Cannot insert '%1': %2").arg(name).arg(tr("Sector write failed.")));
return result;
}
writeBitmap();
result.makeFromAtariDosEntry(dosEntry, no, dir, m_image->geometry().bytesPerSector() == 256);
return result;
}
bool Dos10FileSystem::erase(const AtariDirEntry &entry)
{
quint16 dirsec = entry.dir + entry.no / 8;
int start = (entry.no % 8) * 16;
QByteArray data;
if (!image()->readSector(dirsec, data)) {
QMessageBox::critical(m_image->editDialog(), tr("Atari file system error"), tr("Cannot delete '%1': %2").arg(entry.niceName()).arg(tr("Sector read failed.")));
return false;
}
data[start] = 0x80;
if (!image()->writeSector(dirsec, data)) {
QMessageBox::critical(m_image->editDialog(), tr("Atari file system error"), tr("Cannot delete '%1': %2").arg(entry.niceName()).arg(tr("Sector write failed.")));
return false;
}
quint16 sector = entry.firstSector;
for (uint n = entry.size / (m_image->geometry().bytesPerSector() - 3); n > 0 && sector != 0; n--) {
freeSector(sector);
QByteArray data;
if (!m_image->readSector(sector, data)) {
QMessageBox::critical(m_image->editDialog(), tr("Atari file system error"), tr("Cannot delete '%1': %2").arg(entry.niceName()).arg(tr("Sector read failed.")));
return false;
}
if (!(entry.attributes & AtariDirEntry::MyDos)) {
int fileNo = (quint8)data.at(data.count() - 3) >> 2;
if (fileNo != entry.no) {
QMessageBox::critical(m_image->editDialog(), tr("Atari file system error"), tr("Cannot delete '%1': %2").arg(entry.niceName()).arg(tr("File number mismatch.")));
return false;
}
sector = ((quint8)data.at(data.count() - 3) & 0x03) * 256 + (quint8)data.at(data.count() - 2);
} else {
sector = (quint8)data.at(data.count() - 3) * 256 + (quint8)data.at(data.count() - 2);
}
}
if (!writeBitmap()) {
QMessageBox::critical(m_image->editDialog(), tr("Atari file system error"), tr("Cannot delete '%1': %2").arg(entry.niceName()).arg(tr("Bitmap write failed.")));
return false;
}
return true;
}
bool Dos10FileSystem::rename(const AtariDirEntry &entry, const QByteArray &name)
{
quint16 dirsec = entry.dir + entry.no / 8;
int start = (entry.no % 8) * 16;
QByteArray data;
if (!image()->readSector(dirsec, data)) {
return false;
}
data.replace(start + 5, 11, name);
if (!image()->writeSector(dirsec, data)) {
return false;
}
return true;
}
bool Dos10FileSystem::setTime(const AtariDirEntry &, const QDateTime &)
{
return false;
}
bool Dos10FileSystem::setReadOnly(const AtariDirEntry &)
{
return false;
}
bool Dos10FileSystem::setHidden(const AtariDirEntry &)
{
return false;
}
bool Dos10FileSystem:: setArchived(const AtariDirEntry &)
{
return false;
}
int Dos10FileSystem::findFreeFileNo(quint16 dir)
{
int no = 0;
for (int sector = dir; sector < dir + 8; sector++) {
QByteArray data;
if (!m_image->readSector(sector, data)) {
return -1;
}
for (int i = 0; i < 128; i += 16) {
int f = (quint8)data.at(i);
if (f == 0x80 || f == 0x00) {
return no;
}
no++;
}
}
return -1;
}
bool Dos10FileSystem::writeBitmap()
{
QByteArray data;
if (!m_image->readSector(360, data)) {
return false;
}
data.replace(10, 90, bitmap);
data[3] = m_freeSectors % 256;
data[4] = m_freeSectors / 256;
return m_image->writeSector(360, data);
}
bool Dos10FileSystem::removeDir(const AtariDirEntry &entry)
{
quint16 dirsec = entry.dir + entry.no / 8;
int start = (entry.no % 8) * 16;
QByteArray data;
if (!image()->readSector(dirsec, data)) {
QMessageBox::critical(m_image->editDialog(), tr("Atari file system error"), tr("Cannot delete '%1': %2").arg(entry.niceName()).arg(tr("Sector read failed.")));
return false;
}
data[start] = 0x80;
if (!image()->writeSector(dirsec, data)) {
QMessageBox::critical(m_image->editDialog(), tr("Atari file system error"), tr("Cannot delete '%1': %2").arg(entry.niceName()).arg(tr("Sector write failed.")));
return false;
}
for (int s = entry.firstSector; s < entry.firstSector + 8; s++) {
freeSector(s);
}
if (!writeBitmap()) {
QMessageBox::critical(m_image->editDialog(), tr("Atari file system error"), tr("Cannot delete '%1': %2").arg(entry.niceName()).arg(tr("Bitmap write failed.")));
return false;
}
return true;
}
/* Dos20FileSystem */
Dos20FileSystem::Dos20FileSystem(SimpleDiskImage *image)
: Dos10FileSystem(image)
{
}
uint Dos20FileSystem::totalCapacity()
{
return ((quint8)vtoc.at(1) + (quint8)vtoc.at(2) * 256) * (m_image->geometry().bytesPerSector() - 3);
}
/* Dos25FileSystem */
Dos25FileSystem::Dos25FileSystem(SimpleDiskImage *image)
: Dos20FileSystem(image)
{
m_image->readSector(1024, vtoc2);
bitmap.append(vtoc2.mid(84, 38));
m_freeSectors = (quint8)vtoc.at(3) + (quint8)vtoc.at(4) * 256 +
(quint8)vtoc2.at(122) + (quint8)vtoc2.at(123) * 256;
}
bool Dos25FileSystem::writeBitmap()
{
QByteArray data;
if (!m_image->readSector(360, data)) {
return false;
}
data.replace(10, 90, bitmap.left(90));
quint8 masks[8] = {128, 64, 32, 16, 8, 4, 2, 1};
quint16 sectors = 0;
for (quint16 sector = 0; sector < 720; sector++) {
if (bitmap.at(sector / 8) & masks[sector % 8]) {
sectors++;
}
}
data[3] = sectors % 256;
data[4] = sectors / 256;
if (!m_image->writeSector(360, data)) {
return false;
}
if (!m_image->readSector(1024, data)) {
return false;
}
data.replace(0, 122, bitmap.right(122));
data[122] = (m_freeSectors - sectors) % 256;
data[123] = (m_freeSectors - sectors) / 256;
return m_image->writeSector(1024, data);
}
uint Dos25FileSystem::totalCapacity()
{
return ((quint8)vtoc.at(1) + (quint8)vtoc.at(2) * 256) * (m_image->geometry().bytesPerSector() - 3);
}
/* MyDosFileSystem */
MyDosFileSystem::MyDosFileSystem(SimpleDiskImage *image)
: Dos20FileSystem(image)
{
int xvtocCount;
if (m_image->geometry().bytesPerSector() == 256) {
xvtocCount = (quint8)vtoc.at(0) - 2;
} else {
xvtocCount = ((quint8)vtoc.at(0) * 2) - 4;
}
if (xvtocCount < 0) {
xvtocCount = 0;
}
xvtoc = QByteArray();
for (quint16 s = 359; xvtocCount > 0; xvtocCount--, s--) {
QByteArray data;
m_image->readSector(s, data);
xvtoc.append(data);
}
bitmap.append(vtoc.right(m_image->geometry().bytesPerSector() - 100));
bitmap.append(xvtoc);
bitmap.resize((image->geometry().sectorCount() + 8) / 8);
}
bool MyDosFileSystem::writeBitmap()
{
QByteArray data;