-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexfat.c
896 lines (868 loc) · 36 KB
/
exfat.c
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
//-----------------------------------------
// NAME: Xiaoran Xie
//
// REMARKS: file system
//
//-----------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <fcntl.h>
#include <unistd.h>
#include <assert.h>
#include <math.h>
#include <string.h>
#include <sys/stat.h>
#pragma pack(1)
#pragma pack(push)
typedef struct MAIN_BOOT_SECTOR
{
uint8_t jump_boot[3];
char fs_name[8];
uint8_t must_be_zero[53];
uint64_t partition_offset;
uint64_t volume_length;
uint32_t fat_offset;
uint32_t fat_length;
uint32_t cluster_heap_offset;
uint32_t cluster_count;
uint32_t first_cluster_of_root_directory;
uint32_t volume_serial_number;
uint16_t fs_revision;
uint16_t fs_flags;
uint8_t bytes_per_sector_shift;
uint8_t sectors_per_cluster_shift;
uint8_t number_of_fats;
uint8_t drive_select;
uint8_t percent_in_use;
uint8_t reserved[7];
uint8_t bootcode[390];
uint16_t boot_signature;
} main_boot_sector;
typedef struct VOLUME_LABEL
{
uint8_t EntryType;
uint8_t CharacterCount;
uint16_t VolumeLabel[22];
uint64_t Reserved;
} volume_label;
typedef struct ALLOCATION_BITMAP
{
uint8_t EntryType;
uint8_t BitmapFlags;
char Reserved[18];
uint32_t FirstCluster;
uint64_t DataLength;
} allocation_bitmap;
typedef struct FILE
{
uint8_t EntryType;
uint8_t SecondaryCount;
uint16_t SetChecksum;
uint16_t FileAttributes;
uint16_t Reserved1;
uint32_t CreateTimestamp;
uint32_t LastModifiedTimestamp;
uint32_t LastAccessedTimestamp;
uint8_t Create10msIncrement;
uint8_t LastModified10msIncrement;
uint8_t CreateUtcOffset;
uint8_t LastModifiedUtcOffset;
uint8_t LastAccessedUtcOffset;
char Reserved2[7];
} file;
typedef struct STREAM_EXTENSION
{
uint8_t EntryType;
char GeneralSecondaryFlags[8];
uint8_t Reserved1;
// The valid number of File Name directory entries in a File directory entry set is NameLength / 15,
// rounded up to the nearest integer.
uint8_t NameLength;
uint16_t NameHash;
uint16_t Reserved2;
uint64_t ValidDataLength;
uint32_t Reserved3;
uint32_t FirstCluster;
uint64_t DataLength;
} stream_extension;
typedef struct FILE_NAME
{
uint8_t EntryType;
uint8_t GeneralSecondaryFlags;
uint16_t FileName[30];
} file_name;
int fd = 0;
uint64_t sectorsPerCluster = 0;
uint64_t bytesPerSector = 0;
uint64_t bytesPerCluster = 0;
uint64_t clusterSize = 0;
#pragma pack(pop)
void read_volume(main_boot_sector *main_boot_sector, int handle);
void prepare_info_command(main_boot_sector *main_boot_sector, int handle, volume_label *volume_label, allocation_bitmap *allocation_bitmap);
void print_info_command(main_boot_sector *main_boot_sector, int handle, volume_label *volume_label, allocation_bitmap *allocation_bitmap);
void prepare_list_command(main_boot_sector *main_boot_sector, int handle, file *file, stream_extension *stream_extension, file_name *file_name);
void parse_list_command(uint64_t layerOfRecursion, uint32_t isFile, uint64_t root, main_boot_sector *main_boot_sector, int handle, file *file, stream_extension *stream_extension, file_name *file_name);
void prepare_get_command(char *wholePath, main_boot_sector *main_boot_sector, int handle, file *file, stream_extension *stream_extension, file_name *file_name);
void parse_get_command(uint32_t isFind, char *directoryPathway, char *pathway[], uint64_t layerOfRecursion, uint32_t isFile, uint64_t root, main_boot_sector *main_boot_sector, int handle, file *file, stream_extension *stream_extension, file_name *file_name);
static char *unicode2ascii(uint16_t *unicode_string, uint8_t length);
int main(int argc, char *argv[])
{
// exception
if (argc < 3)
{
perror("Invalid arguments\n");
exit(1);
}
// get parameter
char *volumeName = argv[1];
char *commandName = argv[2];
// read volume
fd = open(volumeName, O_RDONLY);
main_boot_sector main_boot_sector;
volume_label volume_label;
allocation_bitmap allocation_bitmap;
file file;
stream_extension stream_extension;
file_name file_name;
read_volume(&main_boot_sector, fd);
// check command
if (strcmp(commandName, "info") == 0)
{
printf("Command: %s\n", commandName);
prepare_info_command(&main_boot_sector, fd, &volume_label, &allocation_bitmap);
print_info_command(&main_boot_sector, fd, &volume_label, &allocation_bitmap);
}
else if (strcmp(commandName, "list") == 0)
{
printf("Command: %s\n", commandName);
prepare_list_command(&main_boot_sector, fd, &file, &stream_extension, &file_name);
}
else if (strcmp(commandName, "get") == 0)
{
printf("Command: %s\n", commandName);
char *wholePath = argv[3];
prepare_get_command(wholePath, &main_boot_sector, fd, &file, &stream_extension, &file_name);
}
return EXIT_SUCCESS;
}
//------------------------------------------------------
// myRoutine: read_volume
//
// PURPOSE: read and save main_boot_sector
// INPUT PARAMETERS:
// main_boot_sector *main_boot_sector
// int handle
//------------------------------------------------------
void read_volume(main_boot_sector *main_boot_sector, int handle)
{
assert(main_boot_sector != NULL);
assert(handle >= 0);
// JumpBoot 3
read(handle, &main_boot_sector->jump_boot, 3);
// FileSystemName 8
read(handle, &main_boot_sector->fs_name, 8);
// MustBeZero 53
read(handle, &main_boot_sector->must_be_zero, 53);
// PartitionOffset 8
read(handle, &main_boot_sector->partition_offset, 8);
// VolumeLength 8
read(handle, &main_boot_sector->volume_length, 8);
// FatOffset 4
read(handle, &main_boot_sector->fat_offset, 4);
// FatLength 4
read(handle, &main_boot_sector->fat_length, 4);
// cluster_heap_offset 4
read(handle, &main_boot_sector->cluster_heap_offset, 4);
// ClusterCount 4
read(handle, &main_boot_sector->cluster_count, 4);
// FirstClusterOfRootDirectory 4
read(handle, &main_boot_sector->first_cluster_of_root_directory, 4);
// VolumeSerialNumber 4
read(handle, &main_boot_sector->volume_serial_number, 4);
// FileSystemRevision 2
read(handle, &main_boot_sector->fs_revision, 2);
// VolumeFlags 2
read(handle, &main_boot_sector->fs_flags, 2);
// BytesPerSectorShift 1
read(handle, &main_boot_sector->bytes_per_sector_shift, 1);
// SectorsPerClusterShift 1
read(handle, &main_boot_sector->sectors_per_cluster_shift, 1);
// NumberOfFats 1
read(handle, &main_boot_sector->number_of_fats, 1);
// DriveSelect 1
read(handle, &main_boot_sector->drive_select, 1);
// PercentInUse 1
read(handle, &main_boot_sector->percent_in_use, 1);
// Reserved 7
read(handle, &main_boot_sector->reserved, 7);
// BootCode 390
read(handle, &main_boot_sector->bootcode, 390);
// BootSignature 2
read(handle, &main_boot_sector->boot_signature, 2);
}
//------------------------------------------------------
// myRoutine: prepare_info_command
//
// PURPOSE: read and save volume_label and allocation_bitmap
// INPUT PARAMETERS:
// main_boot_sector *main_boot_sector
// int handle
// volume_label *volume_label
// allocation_bitmap *allocation_bitmap
//------------------------------------------------------
void prepare_info_command(main_boot_sector *main_boot_sector, int handle, volume_label *volume_label, allocation_bitmap *allocation_bitmap)
{
assert(main_boot_sector != NULL);
assert(handle >= 0);
assert(volume_label != NULL);
sectorsPerCluster = 1 << main_boot_sector->sectors_per_cluster_shift;
bytesPerSector = 1 << main_boot_sector->bytes_per_sector_shift;
bytesPerCluster = (1 << main_boot_sector->sectors_per_cluster_shift) * (1 << main_boot_sector->bytes_per_sector_shift);
// Volume label
// jump to cluster heap, then jump to first cluster
lseek(handle, (main_boot_sector->cluster_heap_offset) * (bytesPerSector), SEEK_SET);
lseek(handle, (main_boot_sector->first_cluster_of_root_directory - 2) * (sectorsPerCluster) * (bytesPerSector), SEEK_CUR);
while (1)
{
uint8_t temp_entryType;
read(handle, &temp_entryType, 1);
if (temp_entryType == 0x83)
{
lseek(handle, -1, SEEK_CUR);
read(handle, &volume_label->EntryType, 1);
read(handle, &volume_label->CharacterCount, 1);
read(handle, &volume_label->VolumeLabel, 22);
read(handle, &volume_label->Reserved, 8);
break;
}
else
{
lseek(handle, 31, SEEK_CUR);
}
}
// Free space on the volume in KB
// jump to cluster heap, then jump to first cluster
lseek(handle, (main_boot_sector->cluster_heap_offset) * (bytesPerSector), SEEK_SET);
lseek(handle, (main_boot_sector->first_cluster_of_root_directory - 2) * (sectorsPerCluster) * (bytesPerSector), SEEK_CUR);
while (1)
{
uint8_t temp_entryType;
read(handle, &temp_entryType, 1);
if (temp_entryType == 0x81)
{
lseek(handle, -1, SEEK_CUR);
read(handle, &allocation_bitmap->EntryType, 1);
read(handle, &allocation_bitmap->BitmapFlags, 1);
read(handle, &allocation_bitmap->Reserved, 18);
read(handle, &allocation_bitmap->FirstCluster, 4);
read(handle, &allocation_bitmap->DataLength, 8);
break;
}
else
{
lseek(handle, 31, SEEK_CUR);
}
}
}
//------------------------------------------------------
// myRoutine: print_info_command
//
// PURPOSE: print info
// INPUT PARAMETERS:
// main_boot_sector *main_boot_sector
// int handle
// volume_label *volume_label
// allocation_bitmap *allocation_bitmap
//------------------------------------------------------
void print_info_command(main_boot_sector *main_boot_sector, int handle, volume_label *volume_label, allocation_bitmap *allocation_bitmap)
{
assert(main_boot_sector != NULL);
assert(handle >= 0);
assert(volume_label != NULL);
uint64_t i = 0;
int used_bitmap_cells_ap_amount = 0;
uint64_t bitmap_cells_population = 0;
int unused_bitmap_cells_amount = 0;
uint64_t freeSpace = 0;
// Volume label
char *temp = unicode2ascii(volume_label->VolumeLabel, 22);
printf("- Volume label: %s\n", temp);
// Volume serial number
printf("- Volume serial number: %u\n", main_boot_sector->volume_serial_number);
// Free space
lseek(handle, (main_boot_sector->cluster_heap_offset) * (bytesPerSector), SEEK_SET);
lseek(handle, (allocation_bitmap->FirstCluster - 2) * (sectorsPerCluster) * (bytesPerSector), SEEK_CUR);
for (i = 0; i < allocation_bitmap->DataLength; i++)
{
uint8_t data;
read(handle, &data, 1);
used_bitmap_cells_ap_amount += __builtin_popcount(data);
} // total have cluster_count bitmap cells
bitmap_cells_population = main_boot_sector->cluster_count;
unused_bitmap_cells_amount = bitmap_cells_population - used_bitmap_cells_ap_amount;
freeSpace = unused_bitmap_cells_amount * bytesPerCluster / 1024;
printf("- Free space on the volume: %lluKB\n", freeSpace);
// The cluster size, both in sectors and in bytes OR KB
printf("- The cluster size is %llu sectors\n", sectorsPerCluster);
printf("- The cluster size is %llu bytes\n", bytesPerCluster);
}
//------------------------------------------------------
// myRoutine: prepare_list_command
//
// PURPOSE: read and save file, stream_extension, file_name
// INPUT PARAMETERS:
// main_boot_sector *main_boot_sector
// int handle
// file *file
// stream_extension *stream_extension
// file_name *file_name
//------------------------------------------------------
void prepare_list_command(main_boot_sector *main_boot_sector, int handle, file *file, stream_extension *stream_extension, file_name *file_name)
{
assert(main_boot_sector != NULL);
assert(handle >= 0);
assert(file != NULL);
assert(stream_extension != NULL);
assert(file_name != NULL);
sectorsPerCluster = 1 << main_boot_sector->sectors_per_cluster_shift;
bytesPerSector = 1 << main_boot_sector->bytes_per_sector_shift;
bytesPerCluster = (1 << main_boot_sector->sectors_per_cluster_shift) * (1 << main_boot_sector->bytes_per_sector_shift);
clusterSize = bytesPerSector * sectorsPerCluster;
// printf("numOfFAT: %d\n", main_boot_sector->number_of_fats);
// printf("clusterSizeByBytes: %lu\n", clusterSize);
parse_list_command(0, 9999, main_boot_sector->first_cluster_of_root_directory, main_boot_sector, handle, file, stream_extension, file_name);
}
//------------------------------------------------------
// myRoutine: parse_list_command
//
// PURPOSE: parse entries: file, stream_extension, file_name
// INPUT PARAMETERS:
// uint64_t layerOfRecursion
// uint32_t isFile
// uint64_t root
// main_boot_sector *main_boot_sector
// int handle
// file *file
// stream_extension *stream_extension
// file_name *file_name
//------------------------------------------------------
void parse_list_command(uint64_t layerOfRecursion, uint32_t isFile, uint64_t root, main_boot_sector *main_boot_sector, int handle, file *file, stream_extension *stream_extension, file_name *file_name)
{
uint64_t fileEnrtyDone = 0;
uint64_t streamExtensionEntryDone = 0;
uint64_t fileNameEnrtyDone = 0;
uint64_t FATvalue = 0xffffffff;
uint16_t fileNameString[1024];
uint16_t currFileNameIndex = 0;
uint16_t fileNameLoopFlag = 0;
uint16_t i = 0;
do
{
if (FATvalue != 0 && FATvalue != 0xffffffff)
{
root = FATvalue;
}
// printf("root: %lu\n", root);
lseek(handle, main_boot_sector->fat_offset * bytesPerSector, SEEK_SET);
lseek(handle, root * 4, SEEK_CUR);
read(handle, &FATvalue, 4);
// printf("FAT[%lu]: %lu\n", root, FATvalue);
if (isFile == 1)
{
// printf(" file return recusion\n");
return;
}
lseek(handle, (main_boot_sector->cluster_heap_offset) * (bytesPerSector), SEEK_SET);
lseek(handle, (root - 2) * (sectorsPerCluster) * (bytesPerSector), SEEK_CUR);
uint64_t entryCounter = 0;
// char spaceInRecursion[1024];
while (entryCounter < clusterSize / 32)
{
uint8_t temp_entryType = 0;
read(handle, &temp_entryType, 1);
// printf("temp_entryType: %x\n", temp_entryType);
if (temp_entryType == 0x85 && fileNameLoopFlag == 0)
{
assert(temp_entryType == 0x85);
lseek(handle, -1, SEEK_CUR);
// file entry
read(handle, &file->EntryType, 1);
read(handle, &file->SecondaryCount, 1);
read(handle, &file->SetChecksum, 2);
read(handle, &file->FileAttributes, 2);
read(handle, &file->Reserved1, 2);
read(handle, &file->CreateTimestamp, 4);
read(handle, &file->LastModifiedTimestamp, 4);
read(handle, &file->LastAccessedTimestamp, 4);
read(handle, &file->Create10msIncrement, 1);
read(handle, &file->LastModified10msIncrement, 1);
read(handle, &file->CreateUtcOffset, 1);
read(handle, &file->LastModifiedUtcOffset, 1);
read(handle, &file->LastAccessedUtcOffset, 1);
read(handle, &file->Reserved2, 7);
entryCounter += 1;
fileEnrtyDone = 1;
// printf("| file entryCounter: %lu\n", entryCounter);
}
else if (temp_entryType == 0xc0 && fileEnrtyDone == 1 && fileNameLoopFlag == 0)
{
assert(temp_entryType == 0xc0);
lseek(handle, -1, SEEK_CUR);
// stream extension entry
read(handle, &stream_extension->EntryType, 1);
read(handle, &stream_extension->GeneralSecondaryFlags, 1);
read(handle, &stream_extension->Reserved1, 1);
read(handle, &stream_extension->NameLength, 1);
read(handle, &stream_extension->NameHash, 2);
read(handle, &stream_extension->Reserved2, 2);
read(handle, &stream_extension->ValidDataLength, 8);
read(handle, &stream_extension->Reserved3, 4);
read(handle, &stream_extension->FirstCluster, 4);
read(handle, &stream_extension->DataLength, 8);
entryCounter += 1;
streamExtensionEntryDone = 1;
// printf("| stream extension entryCounter: %lu\n", entryCounter);
}
else if (temp_entryType == 0xc1 && fileEnrtyDone == 1 && streamExtensionEntryDone == 1)
{
lseek(handle, -1, SEEK_CUR);
// printf("file->SecondaryCount: %d\n", file->SecondaryCount);
if (i < file->SecondaryCount - 1)
{
// file entry
read(handle, &file_name->EntryType, 1);
read(handle, &file_name->GeneralSecondaryFlags, 1);
read(handle, &file_name->FileName, 30);
// file entry
uint16_t j = 0;
for (j = 0; j < 15; j++)
{
fileNameString[currFileNameIndex] = file_name->FileName[j];
currFileNameIndex += 1;
}
entryCounter += 1;
// printf("| file name entryCounter: %lu\n", entryCounter);
i += 1;
fileNameEnrtyDone = 0;
fileNameLoopFlag = 1;
}
if (i == file->SecondaryCount - 1)
{
fileNameEnrtyDone = 1;
currFileNameIndex = 0;
i = 0;
fileNameLoopFlag = 0;
}
}
else
{
lseek(handle, 31, SEEK_CUR);
fileEnrtyDone = 0;
streamExtensionEntryDone = 0;
fileNameEnrtyDone = 0;
entryCounter += 1;
// printf("| none entryCounter: %lu\n", entryCounter);
}
if (fileEnrtyDone == 1 && streamExtensionEntryDone == 1 && fileNameEnrtyDone == 1)
{
uint64_t j = 0;
for (j = 0; j < layerOfRecursion; j++)
{
printf("-");
}
layerOfRecursion += 1;
// printf("first cluster: %u\n", stream_extension->FirstCluster);
printf("%s\n", unicode2ascii(fileNameString, stream_extension->NameLength));
if ((file->FileAttributes & (1 << 4)) >> 4 == 0)
{
assert((file->FileAttributes & (1 << 4)) >> 4 == 0);
// file is 0
isFile = 1;
// printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> into recusion\n");
assert(isFile == 1);
parse_list_command(layerOfRecursion, isFile, stream_extension->FirstCluster, main_boot_sector, handle, file, stream_extension, file_name);
// printf("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< quit recusion\n");
// printf("*** root: %lu\n", root);
// printf("*** FAT[%lu]: %lu\n", root, FATvalue);
}
else
{
assert((file->FileAttributes & (1 << 4)) >> 4 == 1);
// directory is 1
isFile = 0;
// printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> into recusion\n");
assert(isFile == 0);
parse_list_command(layerOfRecursion, isFile, stream_extension->FirstCluster, main_boot_sector, handle, file, stream_extension, file_name);
// printf("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< quit recusion\n");
// printf("*** root: %lu\n", root);
// printf("*** FAT[%lu]: %lu\n", root, FATvalue);
}
layerOfRecursion -= 1;
lseek(handle, (main_boot_sector->cluster_heap_offset) * (bytesPerSector), SEEK_SET);
lseek(handle, (root - 2) * (sectorsPerCluster) * (bytesPerSector), SEEK_CUR);
lseek(handle, entryCounter * 32, SEEK_CUR);
isFile = 0;
fileEnrtyDone = 0;
streamExtensionEntryDone = 0;
fileNameEnrtyDone = 0;
}
}
entryCounter = 0;
} while (FATvalue != 0 && FATvalue != 0xFFFFFFFF);
// printf(" directory return recusion\n");
return;
}
//------------------------------------------------------
// myRoutine: prepare_get_command
//
// PURPOSE: read and save file, stream_extension, file_name
// INPUT PARAMETERS:
// char *path
// main_boot_sector *main_boot_sector
// int handle
// file *file
// stream_extension *stream_extension
// file_name *file_name
//------------------------------------------------------
void prepare_get_command(char *wholePath, main_boot_sector *main_boot_sector, int handle, file *file, stream_extension *stream_extension, file_name *file_name)
{
assert(wholePath != NULL);
assert(main_boot_sector != NULL);
assert(handle >= 0);
assert(file != NULL);
assert(stream_extension != NULL);
assert(file_name != NULL);
sectorsPerCluster = 1 << main_boot_sector->sectors_per_cluster_shift;
bytesPerSector = 1 << main_boot_sector->bytes_per_sector_shift;
bytesPerCluster = (1 << main_boot_sector->sectors_per_cluster_shift) * (1 << main_boot_sector->bytes_per_sector_shift);
clusterSize = bytesPerSector * sectorsPerCluster;
// char wholePathway[1024];
// strcat(wholePathway, wholePath);
char *pathway[1024];
char *p;
int counter = 0;
p = strtok(wholePath, "/");
pathway[counter] = p;
counter += 1;
while ((p = strtok(NULL, "/")) != NULL)
{
pathway[counter] = p;
counter += 1;
}
int i = 0;
for (i = 0; i < counter; i++)
{
// printf("[%s]\n", pathway[i]);
}
parse_get_command(0, "", pathway, 0, 9999, main_boot_sector->first_cluster_of_root_directory, main_boot_sector, handle, file, stream_extension, file_name);
}
//------------------------------------------------------
// myRoutine: parse_get_command
//
// PURPOSE: parse entries: file, stream_extension, file_name
// INPUT PARAMETERS:
// uint32_t isFind
// char *directoryPathway
// char *pathway[]
// uint64_t layerOfRecursion
// uint32_t isFile
// uint64_t root
// main_boot_sector *main_boot_sector
// int handle
// file *file
// stream_extension *stream_extension
// file_name *file_name
//------------------------------------------------------
void parse_get_command(uint32_t isFind, char *directoryPathway, char *pathway[], uint64_t layerOfRecursion, uint32_t isFile, uint64_t root, main_boot_sector *main_boot_sector, int handle, file *file, stream_extension *stream_extension, file_name *file_name)
{
uint64_t fileEnrtyDone = 0;
uint64_t streamExtensionEntryDone = 0;
uint64_t fileNameEnrtyDone = 0;
uint64_t FATvalue = 0xffffffff;
uint16_t fileNameString[1024];
uint16_t currFileNameIndex = 0;
uint16_t fileNameLoopFlag = 0;
uint16_t i = 0;
do
{
if (FATvalue != 0 && FATvalue != 0xffffffff)
{
root = FATvalue;
}
// printf("root: %lu\n", root);
lseek(handle, main_boot_sector->fat_offset * bytesPerSector, SEEK_SET);
lseek(handle, root * 4, SEEK_CUR);
read(handle, &FATvalue, 4);
// printf("FAT[%lu]: %lu\n", root, FATvalue);
lseek(handle, (main_boot_sector->cluster_heap_offset) * (bytesPerSector), SEEK_SET);
lseek(handle, (root - 2) * (sectorsPerCluster) * (bytesPerSector), SEEK_CUR);
uint64_t entryCounter = 0;
if (isFile == 1)
{
if (isFind == 1)
{
uint64_t temp_root;
// create txt file
// printf("fileName: %s\n", pathway[layerOfRecursion - 1]);
// printf("directoryPathway: %s\n", directoryPathway);
//char *p;
//char *newFileName;
//p = strtok(pathway[layerOfRecursion - 1], ".");
//newFileName = p;
//strcat(newFileName, ".txt");
// check whether file in on the root directory
if (layerOfRecursion - 1 > 0)
{
strcat(directoryPathway, "/");
}
strcat(directoryPathway, pathway[layerOfRecursion - 1]);
printf("Get file successfully!\nYou can find it by path: %s\n", directoryPathway);
FILE *fp = fopen(directoryPathway, "w");
if (fp == NULL)
{
perror("Failed to create file!\n");
exit(0);
}
// check file content whether has chain
// write data into txt file
do
{
if (stream_extension->DataLength <= clusterSize)
{
// write data into txt file
uint64_t k = 0;
for (k = 0; k < stream_extension->DataLength; k++)
{
char *data;
read(handle, &data, 1);
fwrite(&data, 1, 1, fp);
}
temp_root = FATvalue;
}
else
{
// write data into txt file
uint64_t k = 0;
for (k = 0; k < clusterSize; k++)
{
char *data;
read(handle, &data, 1);
fwrite(&data, 1, 1, fp);
}
temp_root = FATvalue;
lseek(handle, main_boot_sector->fat_offset * bytesPerSector, SEEK_SET);
lseek(handle, temp_root * 4, SEEK_CUR);
read(handle, &FATvalue, 4);
// printf("FAT[%lu]: %lu\n", root, FATvalue);
lseek(handle, (main_boot_sector->cluster_heap_offset) * (bytesPerSector), SEEK_SET);
lseek(handle, (temp_root - 2) * (sectorsPerCluster) * (bytesPerSector), SEEK_CUR);
}
} while (temp_root != 0 && temp_root != 0xFFFFFFFF);
exit(1);
}
else
{
return;
}
}
else
{
while (entryCounter < clusterSize / 32)
{
uint8_t temp_entryType = 0;
read(handle, &temp_entryType, 1);
// printf("temp_entryType: %x\n", temp_entryType);
if (temp_entryType == 0x85 && fileNameLoopFlag == 0)
{
assert(temp_entryType == 0x85);
lseek(handle, -1, SEEK_CUR);
// file entry
read(handle, &file->EntryType, 1);
read(handle, &file->SecondaryCount, 1);
read(handle, &file->SetChecksum, 2);
read(handle, &file->FileAttributes, 2);
read(handle, &file->Reserved1, 2);
read(handle, &file->CreateTimestamp, 4);
read(handle, &file->LastModifiedTimestamp, 4);
read(handle, &file->LastAccessedTimestamp, 4);
read(handle, &file->Create10msIncrement, 1);
read(handle, &file->LastModified10msIncrement, 1);
read(handle, &file->CreateUtcOffset, 1);
read(handle, &file->LastModifiedUtcOffset, 1);
read(handle, &file->LastAccessedUtcOffset, 1);
read(handle, &file->Reserved2, 7);
entryCounter += 1;
fileEnrtyDone = 1;
// printf("| file entryCounter: %lu\n", entryCounter);
}
else if (temp_entryType == 0xc0 && fileEnrtyDone == 1 && fileNameLoopFlag == 0)
{
assert(temp_entryType == 0xc0);
lseek(handle, -1, SEEK_CUR);
// stream extension entry
read(handle, &stream_extension->EntryType, 1);
read(handle, &stream_extension->GeneralSecondaryFlags, 1);
read(handle, &stream_extension->Reserved1, 1);
read(handle, &stream_extension->NameLength, 1);
read(handle, &stream_extension->NameHash, 2);
read(handle, &stream_extension->Reserved2, 2);
read(handle, &stream_extension->ValidDataLength, 8);
read(handle, &stream_extension->Reserved3, 4);
read(handle, &stream_extension->FirstCluster, 4);
read(handle, &stream_extension->DataLength, 8);
entryCounter += 1;
streamExtensionEntryDone = 1;
// printf("| stream extension entryCounter: %lu\n", entryCounter);
}
else if (temp_entryType == 0xc1 && fileEnrtyDone == 1 && streamExtensionEntryDone == 1)
{
lseek(handle, -1, SEEK_CUR);
// printf("file->SecondaryCount: %d\n", file->SecondaryCount);
if (i < file->SecondaryCount - 1)
{
// file entry
read(handle, &file_name->EntryType, 1);
read(handle, &file_name->GeneralSecondaryFlags, 1);
read(handle, &file_name->FileName, 30);
// file entry
uint16_t j = 0;
for (j = 0; j < 15; j++)
{
fileNameString[currFileNameIndex] = file_name->FileName[j];
currFileNameIndex += 1;
}
entryCounter += 1;
// printf("| file name entryCounter: %lu\n", entryCounter);
i += 1;
fileNameEnrtyDone = 0;
fileNameLoopFlag = 1;
}
if (i == file->SecondaryCount - 1)
{
fileNameEnrtyDone = 1;
currFileNameIndex = 0;
i = 0;
fileNameLoopFlag = 0;
}
}
else
{
lseek(handle, 31, SEEK_CUR);
fileEnrtyDone = 0;
streamExtensionEntryDone = 0;
fileNameEnrtyDone = 0;
entryCounter += 1;
// printf("| none entryCounter: %lu\n", entryCounter);
}
if (fileEnrtyDone == 1 && streamExtensionEntryDone == 1 && fileNameEnrtyDone == 1)
{
char temp_directoryPathway[1024] = "";
if ((file->FileAttributes & (1 << 4)) >> 4 == 0)
{
// file is 0
isFile = 1;
}
else
{
// directory is 1
isFile = 0;
}
// printf("first cluster: %u\n", stream_extension->FirstCluster);
// printf("%s\n", unicode2ascii(fileNameString, stream_extension->NameLength));
// printf("pathWay[%lu]: %s\n", layerOfRecursion, pathway[layerOfRecursion]);
if (strcmp(unicode2ascii(fileNameString, stream_extension->NameLength), pathway[layerOfRecursion]) == 0)
{
if (isFile == 1)
{
// printf("Find it, don't create directory\n");
strcpy(temp_directoryPathway, directoryPathway);
isFind = 1;
}
else
{
uint64_t k = 0;
for (k = 0; k <= layerOfRecursion; k++)
{
if (k == 0)
{
strcat(temp_directoryPathway, pathway[k]);
}
else
{
strcat(temp_directoryPathway, "/");
strcat(temp_directoryPathway, pathway[k]);
}
}
mkdir(temp_directoryPathway, 0777);
isFind = 0;
}
layerOfRecursion += 1;
// printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> into recusion\n");
parse_get_command(isFind, temp_directoryPathway, pathway, layerOfRecursion, isFile, stream_extension->FirstCluster, main_boot_sector, handle, file, stream_extension, file_name);
// printf("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< quit recusion\n");
// printf("*** root: %lu\n", root);
// printf("*** FAT[%lu]: %lu\n", root, FATvalue);
layerOfRecursion -= 1;
}
else
{
// printf("This is not the file which we find\n");
strcpy(temp_directoryPathway, directoryPathway);
isFind = 0;
}
// printf("layerOfRecursion: %lu\n", layerOfRecursion);
lseek(handle, (main_boot_sector->cluster_heap_offset) * (bytesPerSector), SEEK_SET);
lseek(handle, (root - 2) * (sectorsPerCluster) * (bytesPerSector), SEEK_CUR);
lseek(handle, entryCounter * 32, SEEK_CUR);
isFile = 0;
fileEnrtyDone = 0;
streamExtensionEntryDone = 0;
fileNameEnrtyDone = 0;
}
}
}
entryCounter = 0;
} while (FATvalue != 0 && FATvalue != 0xFFFFFFFF);
// printf(" directory return recusion\n");
return;
}
/**
* Convert a Unicode-formatted string containing only ASCII characters
* into a regular ASCII-formatted string (16 bit chars to 8 bit
* chars).
*
* NOTE: this function does a heap allocation for the string it
* returns, caller is responsible for `free`-ing the allocation
* when necessary.
*
* uint16_t *unicode_string: the Unicode-formatted string to be
* converted.
* uint8_t length: the length of the Unicode-formatted string (in
* characters).
*
* returns: a heap allocated ASCII-formatted string.
*/
static char *unicode2ascii(uint16_t *unicode_string, uint8_t length)
{
assert(unicode_string != NULL);
assert(length > 0);
char *ascii_string = NULL;
if (unicode_string != NULL && length > 0)
{
// +1 for a NULL terminator
ascii_string = calloc(sizeof(char), length + 1);
if (ascii_string)
{
// strip the top 8 bits from every character in the
// unicode string
for (uint8_t i = 0; i < length; i++)
{
ascii_string[i] = (char)unicode_string[i];
}
// stick a null terminator at the end of the string.
ascii_string[length] = '\0';
}
}
return ascii_string;
}