-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththe3.cpp
1221 lines (1176 loc) · 47 KB
/
the3.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 <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include <ctype.h>
#include <math.h>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include "fat32.h"
using namespace std;
vector<string> MONTHS = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
enum nodeType {_FILE, _FOLDER, _DOT};
unsigned EOCVAL;
unsigned FAT_START;
unsigned FAT_SIZE;
unsigned DATA_START;
unsigned FS_INFO_START;
unsigned CLUSTER_SIZE;
unsigned NUM_FATS;
unsigned FREE_CLUSTERS;
FatFileEntry* ZERO_ENTRY;
char* imgFile;
vector<string> tokenizeString(string s, char delimeter) {
vector<string> tokens;
string current;
for (auto& character : s) {
if (character == delimeter) {
tokens.push_back(current);
current.erase();
} else {
current.push_back(character);
}
}
if (current.size()) {
tokens.push_back(current);
}
return tokens;
}
uint32_t toLittleEndian(uint32_t number) {
return (number & 0xFF000000) >> 24 + (number & 0x00FF0000) >> 16 + (number & 0x0000FF00) >> 8 + (number & 0x000000FF);
}
uint8_t getCurrentMs() {
timeval curTime;
gettimeofday(&curTime, NULL);
uint8_t ms = curTime.tv_usec / 1000;
return ms;
}
uint16_t getCurrentDate() {
time_t now = time(0);
tm *tm = localtime(&now);
uint16_t creationDate = ((tm->tm_year - 80) << 9) + (tm->tm_mon << 5) + tm->tm_mday;
return creationDate;
}
uint16_t getCurrentTime() {
time_t now = time(0);
tm *tm = localtime(&now);
uint16_t creationTime = (tm->tm_hour << 11) + (tm->tm_min << 5) + tm->tm_sec / 2;
return creationTime;
}
uint8_t lfn_checksum(char *pFCBName) {
int i;
uint8_t sum = 0;
for (i = 11; i; i--)
sum = ((sum & 1) << 7) + (sum >> 1) + *pFCBName++;
return sum;
}
vector<string> extractDirectories(string path) {
vector<string> directories;
string dirBuffer;
if (path[0] == '/') {
directories.push_back("/");
path = path.substr(1);
}
for (auto& dir : path) {
if (dir == '/') {
directories.push_back(dirBuffer);
dirBuffer.erase();
} else {
dirBuffer.push_back(dir);
}
}
if (dirBuffer.size() > 0) {
directories.push_back(dirBuffer);
}
return directories;
}
class FileNode {
public:
string name;
string realName;
FileNode* realNode;
enum nodeType type;
vector<unsigned>* clusterChain;
FileNode* parentRef;
unsigned firstClusterIndex;
vector<FileNode*> children;
FatFileEntry* entry;
int order;
uint8_t checksum;
uint16_t binaryModifiedDate;
unsigned short modifiedDay;
unsigned short modifiedYear;
string modifiedMonth;
uint16_t binaryModifiedTime;
unsigned short modifiedHour;
unsigned short modifiedMinute;
unsigned short modifiedSecond;
uint8_t creationMs;
unsigned fileSize;
FileNode() {
checksum = 0;
clusterChain = nullptr;
parentRef = nullptr;
firstClusterIndex = 0;
fileSize = 0;
entry = nullptr;
realNode = nullptr;
order = 0;
}
FileNode(string name, enum nodeType type, vector<unsigned>* clusterChain, FileNode* parentRef, unsigned firstClusterIndex, vector<FileNode*> children, FatFileEntry* entry) :
name(name),
type(type),
clusterChain(clusterChain),
parentRef(parentRef),
children(children),
entry(entry),
firstClusterIndex(firstClusterIndex)
{}
FileNode(const FileNode& base) {
order = 0;
fileSize = base.fileSize;
type = base.type;
clusterChain = base.clusterChain;
parentRef = base.parentRef;
firstClusterIndex = base.firstClusterIndex;
children = base.children;
entry = base.entry;
binaryModifiedDate = base.binaryModifiedDate;
modifiedDay = base.modifiedDay;
modifiedMonth = base.modifiedMonth;
modifiedYear = base.modifiedYear;
binaryModifiedTime = base.binaryModifiedTime;
modifiedHour = base.modifiedHour;
modifiedMinute = base.modifiedMinute;
modifiedSecond = base.modifiedSecond;
creationMs = base.creationMs;
}
bool isListable() {
return (this->name != "/" && this->children.size() > 2) || (this->name == "/" && this->children.size() > 0);
}
int getMaxOrder() {
int max = 0;
for (auto& child : this->children) {
max = child->order > max ? child->order : max;
}
return max;
}
void setModifiedDate(uint16_t date) {
binaryModifiedDate = date;
modifiedYear = 1980 + (date >> 9);
uint16_t mask = 480;
modifiedMonth = MONTHS[((date & mask) >> 5)];
uint16_t mask2 = 31;
modifiedDay = date & mask2;
}
void setModifiedTime(uint16_t time) {
binaryModifiedTime = time;
modifiedHour = time >> 11;
uint16_t mask = 2016; // 00000 111111 00000
modifiedMinute = (time & mask) >> 5;
uint16_t mask2 = 31; // 00000 000000 11111
modifiedSecond = (time & mask2) * 2;
}
void setModifiedTimeAndYear(int year, int month, int day, int hour, int minute, int second) {
modifiedYear = year + 1900;
modifiedMonth = MONTHS[month - 1];
modifiedDay = day;
modifiedHour = hour;
modifiedMinute = minute;
modifiedSecond = second;
}
};
FileNode** fileTree = new FileNode*;
bool isChild(FileNode* first, FileNode* second) {
bool cond = false;
for (auto& child : second->children) {
if (child->type != _DOT) {
cond = (child == first) || isChild(first, child);
}
}
return cond;
}
void updateFAT(FileNode* parentDirectory, deque<unsigned> newClusterIndices) {
FILE* fp = fopen(imgFile, "r+");
unsigned currentFATStart = FAT_START;
int i = 0;
vector<unsigned> parentChain = (*parentDirectory->clusterChain);
while (i < NUM_FATS) {
unsigned firstIndex = parentChain[parentChain.size() - 1];
for (auto& index : newClusterIndices) {
fseek(fp, currentFATStart + firstIndex * 4, SEEK_SET);
uint8_t bytes[4];
bytes[0] = index & 0xFF;
bytes[1] = (index & 0xFF00) >> 8;
bytes[2] = (index & 0xFF0000) >> 16;
bytes[3] = (index & 0xFF000000) >> 24;
for (int j = 0; j < 4; j++) {
fwrite(bytes + j, 1, 1, fp);
}
firstIndex = index;
}
fseek(fp, currentFATStart + firstIndex * 4, SEEK_SET);
fwrite(&EOCVAL, 4, 1, fp);
currentFATStart += FAT_SIZE;
i++;
}
fclose(fp);
}
void updateTimes(FileNode* parentDirectory, uint16_t date, uint16_t time) {
parentDirectory->setModifiedDate(date);
parentDirectory->setModifiedTime(time);
bool foundLfn = false;
bool completed = false;
FileNode* grandFather = parentDirectory->parentRef;
FILE* fpx = fopen(imgFile, "r+");
for (auto& cluster : *(grandFather->clusterChain)) {
for (int i = 0; i < CLUSTER_SIZE / sizeof(FatFileEntry) && !completed; i++) {
unsigned long offset = DATA_START + (cluster - 2) * CLUSTER_SIZE + i * sizeof(FatFileEntry);
fseek(fpx, offset, SEEK_SET);
FatFileEntry* entry = new FatFileEntry;
fread(entry, sizeof(FatFileEntry), 1, fpx);
if (entry->msdos.attributes == 0xF && entry->lfn.checksum == parentDirectory->checksum) {
foundLfn = true;
} else if (foundLfn && entry->msdos.attributes == 0x10) {
entry->msdos.modifiedDate = date;
entry->msdos.modifiedTime = time;
fseek(fpx, offset, SEEK_SET);
fwrite(entry, sizeof(FatFileEntry), 1, fpx);
completed = true;
}
delete entry;
}
if (completed) {
break;
}
}
}
/*
bool updateParent(FileNode* file, int emptyClusterIndex) {
FILE* fp = fopen(imgFile, "r+");
unsigned numEntries = ceil(file->name.size() / 13.0) + 1;
if (emptyClusterIndex == 0) {
for (auto& parentCluster : *(file->parentRef->clusterChain)) {
for (int i = 0; i < CLUSTER_SIZE / sizeof(FatFileEntry); i++) {
FatFileEntry* buffer;
unsigned offset = DATA_START + (parentCluster - 2) * CLUSTER_SIZE + i * sizeof(FatFileEntry);
fseek(fp, offset, SEEK_SET);
fread(buffer, sizeof(FatFileEntry), 1, fp);
if (buffer->lfn.checksum == file->checksum) {
numEntries--;
} else if (numEntries == 0 && emptyClusterIndex == 0) {
// NECESSARY? buffer->msdos.lastAccessTime
unsigned newStart = file->parentRef->clusterChain->at(1);
buffer->msdos.eaIndex = (newStart & 0xFFFF0000) >> 16;
buffer->msdos.firstCluster = (newStart & 0x0000FFFF);
fseek(fp, offset, SEEK_SET);
fwrite(buffer, sizeof(FatFileEntry), 1, fp);
}
}
}
}
if (emptyClusterIndex == -1) {
return true;
}
uint32_t zeroEntry = 0;
for (int i = 0; i < NUM_FATS; i++) {
fseek(fp, FAT_START + i * FAT_SIZE + file->clusterChain->at(emptyClusterIndex) * 4, SEEK_SET);
fwrite(&zeroEntry, 4, 1, fp);
}
if (emptyClusterIndex == 0) {
return true;
}
size_t parentCCSize = file->parentRef->clusterChain->size();
if (parentCCSize == 2) {
file->parentRef->clusterChain->erase(file->parentRef->clusterChain->begin() + emptyClusterIndex);
return true;
}
deque<unsigned> after;
for (int i = emptyClusterIndex + 1; i < parentCCSize; i++) {
after.push_back(file->parentRef->clusterChain->at(i));
}
for (int i = emptyClusterIndex; i < parentCCSize; i++) {
file->parentRef->clusterChain->erase(file->parentRef->clusterChain->begin() + i);
}
fclose(fp);
updateFAT(file->parentRef, after);
return true;
}
*/
bool reserveNewCluster(FileNode* parentDirectory, unsigned remainingEntries) {
unsigned neededClusters = remainingEntries / (CLUSTER_SIZE / sizeof(FatFileEntry)) + 1;
FILE* fp = fopen(imgFile, "r");
deque<unsigned> newClusterIndices;
for (unsigned i = 2; i < FAT_SIZE / 4, newClusterIndices.size() < neededClusters; i++) {
fseek(fp, FAT_START + i * 4, SEEK_SET);
uint32_t entry;
fread(&entry, 4, 1, fp);
if (entry != 0) {
continue;
}
fseek(fp, DATA_START + (i - 2) * CLUSTER_SIZE, SEEK_SET);
bool fullEmpty = true;
for (unsigned j = 0; j < CLUSTER_SIZE / sizeof(FatFileEntry); j++) {
FatFileEntry* entry = new FatFileEntry;
fread(entry, sizeof(FatFileEntry), 1, fp);
if (entry->msdos.attributes != 0) {
fullEmpty = false;
break;
}
delete entry;
}
if (fullEmpty) {
newClusterIndices.push_back(i);
}
}
fclose(fp);
if (newClusterIndices.size() == neededClusters) {
if (parentDirectory->clusterChain->size() == 0) { // First time creation, for the sake of consistency
parentDirectory->firstClusterIndex = newClusterIndices[0];
parentDirectory->clusterChain->push_back(newClusterIndices[0]);
newClusterIndices.pop_front();
}
updateFAT(parentDirectory, newClusterIndices);
for (auto& index : newClusterIndices) {
parentDirectory->clusterChain->push_back(index);
}
FILE* fp2 = fopen(imgFile, "r+");
FREE_CLUSTERS -= neededClusters;
fseek(fp2, FS_INFO_START + 488, SEEK_SET);
fwrite(&FREE_CLUSTERS, 4, 1, fp2);
fclose(fp2);
return true;
}
return false;
}
vector<unsigned> getAvailableAddresses(FileNode* parentDirectory, unsigned numEntries) {
bool addressesFound = false;
vector<unsigned> spaces;
FILE* fp = fopen(imgFile, "r");
FatFileEntry* dummyEntry = new FatFileEntry;
for (auto& cluster : *(parentDirectory->clusterChain)) {
fseek(fp, DATA_START + (cluster - 2) * CLUSTER_SIZE, SEEK_SET);
for (unsigned i = 0; i < CLUSTER_SIZE / sizeof(FatFileEntry); i++) {
fread(dummyEntry, sizeof(FatFileEntry), 1, fp);
if (dummyEntry->msdos.attributes == 0) { // found a space
spaces.push_back(DATA_START + (cluster - 2) * CLUSTER_SIZE + i * 32);
} else {
spaces.erase(spaces.begin(), spaces.end());
}
if (spaces.size() == numEntries) {
addressesFound = true;
break;
}
}
if (addressesFound) {
break;
}
}
delete dummyEntry;
fclose(fp);
if (!addressesFound) { // fallback
unsigned remaining = numEntries - spaces.size();
bool hasReserved = reserveNewCluster(parentDirectory, remaining);
// clusters in "spaces" are already available and can be reserved. find places for the remaining entries
if (hasReserved) {
return getAvailableAddresses(parentDirectory, numEntries);
} else { // address & new cluster not found, returns zero sized vector
spaces.erase(spaces.begin(), spaces.end());
return spaces;
}
/*
reserving needs to:
- find an available space in data area if exists
- update fat table (does it have to update all tables?):
- find new space in fat table (and store its address ex. 0x0a)
- write EOC in that address
- go to the last chain index of fat table and update it with stored address
- then inform the calling routine about reserved or not
-- sending arguments to reserveNewCluster will make it too specific.
it should be used in another context as well.
*/
}
return spaces;
}
vector<unsigned>* getClusterChain(uint32_t firstClusterIndex) {
vector<unsigned>* clusterChain = new vector<unsigned>;
unsigned currentCluster = firstClusterIndex;
if (currentCluster == 0) { // For empty files
return clusterChain;
}
clusterChain->push_back(currentCluster);
FILE* fp = fopen(imgFile, "r");
uint8_t* currentByte = new uint8_t;
while (1) {
fseek(fp, FAT_START + currentCluster * 4, SEEK_SET);
vector<unsigned> fatEntry;
unsigned entryValue;
for (int j = 0; j < 4; j++) {
fread(currentByte, sizeof(uint8_t), 1, fp);
fatEntry.push_back(unsigned(*currentByte));
}
entryValue = fatEntry[0] + (fatEntry[1] << 8) + (fatEntry[2] << 16) + (fatEntry[3] << 24);
if (entryValue == EOCVAL) {
break;
}
clusterChain->push_back(entryValue);
currentCluster = entryValue;
}
delete currentByte;
fclose(fp);
return clusterChain;
}
void getFileAndFolders(FileNode* root) {
FILE* fp = fopen(imgFile, "r+");
vector<unsigned>* clusterChain = root->clusterChain;
string concatLfn = "";
uint8_t checksum = 0;
for (auto& clusterIndex : *clusterChain) {
unsigned offset = DATA_START + (clusterIndex - 2) * CLUSTER_SIZE;
fseek(fp, offset, SEEK_SET);
for (int i = 0; i < CLUSTER_SIZE / sizeof(FatFileEntry); i++) {
FatFileEntry* fatFile = new FatFileEntry; // memleak
string name83;
fread(fatFile, sizeof(FatFileEntry), 1, fp);
unsigned attributes = fatFile->msdos.attributes;
if (fatFile->msdos.filename[0] == 0xE5) { // Deleted entry fix
fseek(fp, offset + i * sizeof(FatFileEntry), SEEK_SET);
fwrite(ZERO_ENTRY, sizeof(FatFileEntry), 1, fp);
} else if (attributes == 0xF) { // LFN entry
string lfnName;
for (int j = 0; j < 5; j++) {
if (!isascii(fatFile->lfn.name1[j]) || fatFile->lfn.name1[j] == 0 || fatFile->lfn.name1[j] == 32) {
break;
}
lfnName.push_back((char) fatFile->lfn.name1[j]);
}
for (int j = 0; j < 6; j++) {
if (!isascii(fatFile->lfn.name2[j]) || fatFile->lfn.name2[j] == 0 || fatFile->lfn.name2[j] == 32) {
break;
}
lfnName.push_back((char) fatFile->lfn.name2[j]);
}
for (int j = 0; j < 2; j++) {
if (!isascii(fatFile->lfn.name3[j]) || fatFile->lfn.name3[j] == 0 || fatFile->lfn.name3[j] == 32) {
break;
}
lfnName.push_back((char) fatFile->lfn.name3[j]);
}
concatLfn = lfnName + concatLfn;
checksum = fatFile->lfn.checksum;
} else if (attributes == 0x10 || attributes == 0x20) { // 8.3 entry
for (int j = 0; j < 8; j++) {
if (!isascii(fatFile->msdos.filename[j]) || fatFile->msdos.filename[j] == 0 || fatFile->msdos.filename[j] == 32) {
break;
}
name83.push_back(fatFile->msdos.filename[j]);
}
if (concatLfn.size()) { // end of LFN
uint32_t firstCluster = (fatFile->msdos.eaIndex << 16) + fatFile->msdos.firstCluster;
FileNode* newNode = new FileNode;
newNode->name = concatLfn;
newNode->parentRef = root;
newNode->type = attributes == 16 ? _FOLDER : _FILE;
newNode->firstClusterIndex = firstCluster;
newNode->clusterChain = getClusterChain(firstCluster);
newNode->entry = fatFile;
newNode->checksum = checksum;
string order;
for (int i = 1; i < 8; i++) {
if (name83[i] == ' ' || name83[i] == 0) {
break;
}
order.push_back(name83[i]);
}
newNode->order = stoi(order);
newNode->setModifiedDate(fatFile->msdos.modifiedDate);
newNode->setModifiedTime(fatFile->msdos.modifiedTime);
newNode->fileSize = fatFile->msdos.fileSize;
newNode->creationMs = fatFile->msdos.creationTimeMs;
root->children.push_back(newNode);
concatLfn.erase();
checksum = 0;
} else {
if (name83 == ".") {
FileNode* fptr = new FileNode(*root);
fptr->name = ".";
fptr->realName = root->name;
fptr->realNode = root;
fptr->type = _DOT;
root->children.push_back(fptr);
} else if (name83 == "..") {
FileNode* fptr = new FileNode(*(root->parentRef));
fptr->name = "..";
fptr->realName = root->parentRef->name;
fptr->realNode = root->parentRef;
fptr->type = _DOT;
root->children.push_back(fptr);
}
}
}
delete fatFile;
}
}
fclose(fp);
}
void createTree(FileNode* root) {
if (root->type == _FOLDER) {
getFileAndFolders(root);
for (auto& child : root->children) {
createTree(child);
}
}
}
FileNode* findFile(FileNode* currentDir, vector<string>& directories) {
bool found = false;
if (directories.size() == 0) {
return nullptr;
}
if (directories[0] == "/") {
currentDir = *fileTree;
directories.erase(directories.begin());
if (directories.size() == 0) {
return currentDir;
}
}
for (auto& child : currentDir->children) {
if (child->name == directories[0]) {
found = true;
if (child->type == _FOLDER) {
if (directories.size() == 1) {
return child;
}
vector<string> newVector(directories.begin() + 1, directories.end());
return findFile(child, newVector);
} else if (child->type == _DOT) {
if (directories.size() == 1) {
return child->realNode;
}
vector<string> newVector(directories.begin() + 1, directories.end());
return findFile(child->realNode, newVector);
} else if (child->type == _FILE && directories.size() == 1) {
return child;
}
} else if (directories[0] == "." && currentDir->name == "/") {
if (directories.size() == 1) {
return currentDir;
}
vector<string> newVector(directories.begin() + 1, directories.end());
return findFile(currentDir, newVector);
}
}
if (!found) {
return nullptr;
}
}
string findAbsolutePath(FileNode* file) { // USE FOR FOLDERS
if (file->name == "/") {
return "/";
}
string path = "";
deque<string> names;
while (file->parentRef != nullptr) {
names.push_front(file->name);
file = file->parentRef;
}
for (int i = 0; i < names.size() - 1; i++) {
path = path + names[i];
path.push_back('/');
}
path += names[names.size() - 1];
return "/" + path;
}
void printFatEntries(FileNode* node) {
FILE* fp = fopen(imgFile, "r");
for (auto& cluster : *node->clusterChain) {
cout << "cluster is " << cluster << endl;
fseek(fp, FAT_START + cluster * 4, SEEK_SET);
uint8_t* bytes = new uint8_t[4];
for (int j = 0; j < 4; j++) {
fread(bytes + j, sizeof(uint8_t), 1, fp);
printf("0x%X ", bytes[j]);
}
delete[] bytes;
cout << endl;
}
fclose(fp);
}
void printCluster(unsigned cluster) {
FatFileEntry* fatFile = new FatFileEntry;
FILE* fp = fopen(imgFile, "r");
fseek(fp, DATA_START + (cluster - 2) * CLUSTER_SIZE, SEEK_SET);
string concatName;
for (int i = 0; i < CLUSTER_SIZE / sizeof(FatFileEntry); i++) { // ROOT DIRECTORY - CLUSTER 2
char* name = new char[13];
char* extension = new char[3];
char* shortName = new char[7];
uint8_t sequenceNumber;
uint8_t firstByte;
fread(fatFile, sizeof(FatFileEntry), 1, fp);
unsigned attributes = fatFile->msdos.attributes;
if (attributes == 15) { // LFN entry
string current;
for (int j = 0; j < 5; j++) {
if (!isascii(fatFile->lfn.name1[j]) || fatFile->lfn.name1[j] == 0 || fatFile->lfn.name1[j] == 32) {
//break;
}
cout << "pushing val" << fatFile->lfn.name1[j] << "i = " << i << endl;
current.push_back((char) fatFile->lfn.name1[j]);
}
for (int j = 0; j < 6; j++) {
if (!isascii(fatFile->lfn.name2[j]) || fatFile->lfn.name2[j] == 0 || fatFile->lfn.name2[j] == 32) {
//break;
}
cout << "pushing val" << fatFile->lfn.name2[j] << "i = " << i << endl;
current.push_back((char) fatFile->lfn.name2[j]);
}
for (int j = 0; j < 2; j++) {
if (!isascii(fatFile->lfn.name3[j]) || fatFile->lfn.name3[j] == 0 || fatFile->lfn.name3[j] == 32) {
//break;
}
cout << "pushing val" << fatFile->lfn.name3[j] << "i = " << i << endl;
current.push_back((char) fatFile->lfn.name3[j]);
}
concatName = current + concatName;
cout << "name = " << concatName << "\t\t\t\t\t";
printf("sequence no = 0x%X ", fatFile->lfn.sequence_number);
cout << "checksum = " << fatFile->lfn.checksum << endl;
} else if (attributes == 16 || attributes == 32) { // 8.3 entry
concatName.erase();
for (int j = 0; j < 8; j++) {
if (!isascii(fatFile->msdos.filename[j]) || fatFile->msdos.filename[j] == 0 || fatFile->msdos.filename[j] == 32) {
break;
}
if (j == 0) {
printf("adding char[0] 0x%X", fatFile->msdos.filename[j]);
cout << " int value = " << (int) fatFile->msdos.filename[0] << endl;
} else {
cout << "adding a char -> " << fatFile->msdos.filename[j] << " int value = " << (int) fatFile->msdos.filename[j] << endl;
}
name[j] = fatFile->msdos.filename[j];
}
cout << "name = ..." << fatFile->msdos.filename << "...\n";
for (int j = 0; j < 3; j++) {
cout << "int val of extension + " << j << " = " << (int) fatFile->msdos.extension[j] << endl;
cout << "attributes = " << fatFile->msdos.attributes << endl;
}
}
printf("file size = %u ", fatFile->msdos.fileSize);
printf("attributes = 0x%X ", fatFile->msdos.attributes);
printf("first cluster 0-1 = 0x%X 0x%x ", fatFile->msdos.eaIndex, fatFile->msdos.firstCluster);
printf("modified date = %u ", fatFile->msdos.modifiedDate);
printf("modified time = %u ", fatFile->msdos.modifiedTime);
cout << "order = " << i << endl;
cout << endl;
delete[] name;
delete[] extension;
delete[] shortName;
}
fclose(fp);
cout << "END OF CLUSTER " << cluster << endl;
}
bool createDotEntries(FileNode* newDirNode) {
FileNode* dotEntry = new FileNode(*newDirNode);
dotEntry->name = ".";
dotEntry->realName = newDirNode->name;
dotEntry->realNode = newDirNode;
dotEntry->type = _DOT;
newDirNode->children.push_back(dotEntry);
FatFileEntry* dot83 = new FatFileEntry;
dot83->msdos.filename[0] = 0x2E;
for (int j = 1; j < 8; j++) {
dot83->msdos.filename[j] = 0x20;
}
for (int j = 0; j < 3; j++) {
dot83->msdos.extension[j] = 0x20;
}
dot83->msdos.fileSize = 0;
dot83->msdos.attributes = 0x10;
dot83->msdos.reserved = 0;
uint16_t creationTime = getCurrentTime();
uint16_t creationDate = getCurrentDate();
dot83->msdos.creationTimeMs = getCurrentMs();
dot83->msdos.creationDate = creationDate;
dot83->msdos.creationTime = creationTime;
dot83->msdos.modifiedDate = creationDate;
dot83->msdos.modifiedTime = creationTime;
dot83->msdos.eaIndex = (newDirNode->firstClusterIndex & 0xFFFF0000) >> 16;
dot83->msdos.firstCluster = (newDirNode->firstClusterIndex & 0x0000FFFF);
FileNode* parentDirectory = newDirNode->parentRef;
FileNode* twoDotEntry = new FileNode(*parentDirectory);
twoDotEntry->name = "..";
twoDotEntry->realName = parentDirectory->name;
twoDotEntry->realNode = parentDirectory;
twoDotEntry->type = _DOT;
newDirNode->children.push_back(twoDotEntry);
FatFileEntry* twoDot83 = new FatFileEntry;
twoDot83->msdos.filename[0] = 0x2E;
twoDot83->msdos.filename[1] = 0x2E;
for (int j = 2; j < 8; j++) {
twoDot83->msdos.filename[j] = 0x20;
}
for (int j = 0; j < 3; j++) {
twoDot83->msdos.extension[j] = 0x20;
}
twoDot83->msdos.fileSize = 0;
twoDot83->msdos.attributes = 0x10;
twoDot83->msdos.reserved = 0;
twoDot83->msdos.creationTimeMs = getCurrentMs();
twoDot83->msdos.creationDate = creationDate;
twoDot83->msdos.creationTime = creationTime;
twoDot83->msdos.modifiedDate = creationDate;
twoDot83->msdos.modifiedTime = creationTime;
twoDot83->msdos.eaIndex = parentDirectory->name == "/" ? 0 : (parentDirectory->firstClusterIndex & 0xFFFF0000) >> 16;
twoDot83->msdos.firstCluster = parentDirectory->name == "/" ? 0 : (parentDirectory->firstClusterIndex & 0x0000FFFF);
FILE* fpx = fopen(imgFile, "r+");
fseek(fpx, DATA_START + CLUSTER_SIZE * (newDirNode->firstClusterIndex - 2), SEEK_SET);
fwrite(dot83, sizeof(FatFileEntry), 1, fpx);
fseek(fpx, DATA_START + CLUSTER_SIZE * (newDirNode->firstClusterIndex - 2) + 32, SEEK_SET);
fwrite(twoDot83, sizeof(FatFileEntry), 1, fpx);
fclose(fpx);
}
FileNode* searchForParent(FileNode* currentDir, vector<string> directories) {
string folderName = directories[directories.size() - 1];
FileNode* parentDirectory;
bool parentContainsFolder = false;
if (directories.size() > 1) {
vector<string> v(directories.begin(), directories.end() - 1);
parentDirectory = findFile(currentDir, v);
} else {
parentDirectory = currentDir;
}
if (parentDirectory == nullptr) {
return nullptr;
}
for (auto& child : parentDirectory->children) {
if (child->name == folderName) {
parentContainsFolder = true;
break;
}
}
if (parentContainsFolder) {
return nullptr;
}
return parentDirectory;
}
FileNode* createChild(FileNode* parentDirectory, string name, enum nodeType type) {
int numLfnEntries = ceil(name.size() / 13.0);
FileNode* newDirNode = new FileNode;
newDirNode->name = name;
newDirNode->order = parentDirectory->getMaxOrder() + 1;
newDirNode->parentRef = parentDirectory;
newDirNode->type = type;
newDirNode->clusterChain = new vector<unsigned>;
vector<unsigned> availableAddresses = getAvailableAddresses(parentDirectory, numLfnEntries + 1);
if (availableAddresses.size() != numLfnEntries + 1) {
return nullptr;
}
if (type == _FOLDER && reserveNewCluster(newDirNode, 2) == false) {
return nullptr;
}
FatFileEntry** entries = new FatFileEntry*[numLfnEntries + 1]; // +1 for 8.3
FatFileEntry* msdos = new FatFileEntry;
char checkSumArg[11];
msdos->msdos.filename[0] = 0x7E;
checkSumArg[0] = 0x7E;
string order = to_string(parentDirectory->getMaxOrder() + 1);
int copiedChars = 0;
while (copiedChars < order.size()) {
msdos->msdos.filename[copiedChars + 1] = order[copiedChars];
checkSumArg[copiedChars + 1] = order[copiedChars];
copiedChars++;
}
for (int i = copiedChars + 1; i < 8; i++) {
msdos->msdos.filename[i] = 0x20;
checkSumArg[i] = 0x20;
}
for (int i = 0; i < 3; i++) {
msdos->msdos.extension[i] = 0x20;
checkSumArg[i + 8] = 0x20;
}
msdos->msdos.fileSize = 0;
msdos->msdos.eaIndex = (newDirNode->firstClusterIndex & 0xFFFF0000) >> 16;
msdos->msdos.firstCluster = newDirNode->firstClusterIndex & 0x0000FFFF;
msdos->msdos.attributes = type == _FOLDER ? 0x10 : 0x20;
msdos->msdos.reserved = 0;
uint16_t creationTime = getCurrentTime();
uint16_t creationDate = getCurrentDate();
msdos->msdos.creationTimeMs = getCurrentMs();
msdos->msdos.creationTime = creationTime;
msdos->msdos.modifiedTime = creationTime;
msdos->msdos.creationDate = creationDate;
msdos->msdos.modifiedDate = creationDate;
newDirNode->setModifiedDate(creationDate);
newDirNode->setModifiedTime(creationTime);
newDirNode->entry = msdos;
entries[numLfnEntries] = msdos;
uint8_t checksum = lfn_checksum(checkSumArg);
newDirNode->checksum = checksum;
vector<uint16_t> padding;
padding.reserve(13 * numLfnEntries);
padding[name.size()] = 0x00;
for (int i = name.size() + 1; i < 13 * numLfnEntries; i++) {
padding[i] = 0xFF;
}
for (int k = 0; k < numLfnEntries; k++) {
FatFileEntry* lfn = new FatFileEntry;
lfn->lfn.reserved = 0x00;
lfn->lfn.attributes = 0x0F;
lfn->lfn.checksum = checksum;
lfn->lfn.firstCluster = 0x0000;
if (k == numLfnEntries - 1) {
lfn->lfn.sequence_number = 0x40 + numLfnEntries;
} else {
lfn->lfn.sequence_number = k + 1;
}
for (int j = 0; j < 5; j++) {
lfn->lfn.name1[j] = name.size() > k * 13 + j ? name[k * 13 + j] : padding[k * 13 + j];
}
for (int j = 5; j < 11; j++) {
lfn->lfn.name2[j - 5] = name.size() > k * 13 + j ? name[k * 13 + j] : padding[k * 13 + j];
}
for (int j = 11; j < 13; j++) {
lfn->lfn.name3[j - 11] = name.size() > k * 13 + j ? name[k * 13 + j] : padding[k * 13 + j];
}
entries[numLfnEntries - 1 - k] = lfn;
}
FILE* fpx = fopen(imgFile, "r+");
for (int i = 0; i < availableAddresses.size(); i++) {
fseek(fpx, availableAddresses[i], SEEK_SET);
fwrite(entries[i], sizeof(FatFileEntry), 1, fpx);
}
fclose(fpx);
if (parentDirectory->name != "/") {
updateTimes(parentDirectory, creationDate, creationTime);
}
parentDirectory->children.push_back(newDirNode);
if (type == _FOLDER) {
bool created = createDotEntries(newDirNode);
if (!created) return nullptr;
}
return newDirNode;
}
int main(int argc, char** argv) {
// Bytes per sector = 512
// cluster size = 1024 bytes
// Sectors per FAT = 794
// #FATs = 2
// Sectors per cluster = 2
// # reserved sectors = 32
// start byte of fat = 16384
// FATs size = 2 * 794 * 512 = 813056
// data section start = 829440
ZERO_ENTRY = (FatFileEntry*) calloc(1, sizeof(FatFileEntry));
imgFile = argv[1];
BPB_struct* bpb = new BPB_struct;
BPB32_struct* bpb32 = new BPB32_struct;
void* vp = ((void*) bpb) + 36;
bpb32 = (BPB32_struct*) vp;
FILE* fp2 = fopen(argv[1], "r");
fread(bpb, sizeof(BPB_struct), 1, fp2);
CLUSTER_SIZE = bpb->BytesPerSector * bpb->SectorsPerCluster;
FAT_START = bpb->ReservedSectorCount * bpb->BytesPerSector;
FAT_SIZE = bpb32->FATSize * bpb->BytesPerSector;
NUM_FATS = bpb->NumFATs;
DATA_START = FAT_START + NUM_FATS * FAT_SIZE;
FS_INFO_START = bpb->BytesPerSector * bpb32->FSInfo;
fseek(fp2, FAT_START, SEEK_SET);
fread(&EOCVAL, 4, 1, fp2);
fseek(fp2, FS_INFO_START + 488, SEEK_SET);
fread(&FREE_CLUSTERS, 4, 1, fp2);
fclose(fp2);
string pwd = "/";
FileNode* root = new FileNode;
root->name = "/";
root->firstClusterIndex = bpb32->RootCluster;
root->clusterChain = getClusterChain(root->firstClusterIndex); // will have memleak;
root->type = _FOLDER;
const clock_t begin_time = clock();
createTree(root);
*fileTree = root;
string line;
FileNode* currentDir = root;
while (1) {
cout << pwd << "> ";
getline(cin, line);
vector<string> command = tokenizeString(line, ' ');
if (!command.size()) { continue; }
if (command[0] == "quit") {
break;
} else if (command[0] == "cd") {
string arg1 = string(command[1]);
vector<string> directories = extractDirectories(arg1);
FileNode* directory = findFile(currentDir, directories);
if (directory != nullptr && directory->type == _FOLDER) {
pwd = findAbsolutePath(directory);
currentDir = directory;
}
} else if (command[0] == "ls") {
FileNode* listedDirectory = currentDir;
if (command.size() > 1 && command[1] == "-l") {
if (command.size() > 2) { // ls -l <path>
vector<string> directories = extractDirectories(command[2]);
FileNode* directory = findFile(currentDir, directories);
listedDirectory = directory;
}
if (listedDirectory == nullptr || !listedDirectory->isListable()) {
continue;
}
for (auto& child : listedDirectory->children) {
if (child->type == _DOT) {
continue;
} else if (child->type == _FOLDER) {
cout << "drwx------ 1 root root 0 ";
} else if (child->type == _FILE) {
cout << "-rwx------ 1 root root " << child->fileSize << " ";
}
cout << child->modifiedYear << " " << child->modifiedMonth << " " << child->modifiedDay
<< " ";
if (child->modifiedHour < 10) {
cout << "0";
}
cout << child->modifiedHour << ":";
if (child->modifiedMinute < 10) {
cout << "0";
}
cout << child->modifiedMinute << " " << child->name << endl;
}
} else { // ls <path> or ls
if (command.size() > 1) {
vector<string> directories = extractDirectories(command[1]);
FileNode* directory = findFile(currentDir, directories);
listedDirectory = directory;
}
if (listedDirectory == nullptr || !listedDirectory->isListable()) {
continue;
}
for (auto& child : listedDirectory->children) {
if (child->type != _DOT) {
cout << child->name << " ";
}
}
cout << endl;
}
} else if (command[0] == "mkdir") {
vector<string> directories = extractDirectories(command[1]);
string folderName = directories[directories.size() - 1];
FileNode* parentDirectory = searchForParent(currentDir, directories);