-
Notifications
You must be signed in to change notification settings - Fork 4
/
directory.c
93 lines (82 loc) · 2.77 KB
/
directory.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
#include <ctype.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <stdbool.h>
#include "fat32disk.h"
bool isDirectory(DirEntry* dirEntry){
return dirEntry->DIR_Attr == 0x10;
}
char* getDisk(unsigned int fd){
struct stat fs;
if(fstat(fd, &fs) == -1)
{
perror("Error while reading file stat");
}
return mmap(NULL , fs.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
}
DirEntry* getclusterPtr(char* file_content, BootEntry* disk, unsigned int cluster){
unsigned int rootSector = (disk->BPB_RsvdSecCnt + disk->BPB_NumFATs*disk->BPB_FATSz32) + (cluster- 2)*disk->BPB_SecPerClus;
unsigned int rootClusterOffset = rootSector*disk->BPB_BytsPerSec;
DirEntry* dirEntry = (DirEntry*)(file_content + rootClusterOffset);
if (dirEntry==NULL){
perror("mmap failed");
exit(1);
}
return dirEntry;
}
void showRootDirectory(DirEntry* dirEntry){
if (isDirectory(dirEntry)){
int idx = 0;
while(dirEntry->DIR_Name[idx]!=' '){
printf("%c",dirEntry->DIR_Name[idx]);
idx++;
}
printf("/");
}
else{
for(int i=0;i<8;i++){
if(dirEntry->DIR_Name[i]==' ')
break;
printf("%c",dirEntry->DIR_Name[i]);
}
if(dirEntry->DIR_Name[8]!=' '){
printf(".");
for(int i=8;i<12;i++){
if(dirEntry->DIR_Name[i]==' ')
break;
printf("%c",dirEntry->DIR_Name[i]);
}
}
}
}
void getRootDirectoryEntries(int fd, BootEntry* disk){
unsigned int nEntries = 0;
unsigned int currCluster = disk->BPB_RootClus;
unsigned int totalPossibleEntry = (disk->BPB_SecPerClus * disk->BPB_BytsPerSec)/sizeof(DirEntry);
unsigned char *file_content = getDisk(fd);
do{
DirEntry* dirEntry = getclusterPtr(file_content,disk,currCluster);
for(int m=0;m<totalPossibleEntry;m++){
if (dirEntry->DIR_Attr == 0x00){ // no more dirEntry after this
break;
}
if(dirEntry->DIR_Name[0] != 0xe5){ // do not show deleted files
showRootDirectory(dirEntry);
int startCluster = dirEntry->DIR_FstClusHI << 16 | dirEntry->DIR_FstClusLO;
printf(" (size = %d, starting cluster = %d)\n",dirEntry->DIR_FileSize, startCluster);
nEntries++;
}
dirEntry++;
}
unsigned int *fat = (unsigned int*)(file_content + disk->BPB_RsvdSecCnt*disk->BPB_BytsPerSec + 4*currCluster);
if(*fat >= 0x0ffffff8 || *fat==0x00){
break;
}
currCluster=*fat;
} while(1);
printf("Total number of entries = %d\n",nEntries);
}