-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile.c
More file actions
executable file
·263 lines (262 loc) · 6.94 KB
/
file.c
File metadata and controls
executable file
·263 lines (262 loc) · 6.94 KB
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
#include "file.h"
#include "struct.h"
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "debug.h"
#include <string.h>
BootEntry *bootEntry;
void getBootEntry(int fp) {
// return start of data area
if (bootEntry != NULL) {
free(bootEntry);
}
bootEntry = malloc(sizeof(BootEntry));
pread(fp, bootEntry, sizeof(BootEntry), 0);
}
long getClusterSize() {
return bootEntry->BPB_BytsPerSec * bootEntry->BPB_SecPerClus;
}
long getNextCluster(long cluster, int fp) {
uint32_t next;
pread(fp, &next, 4, bootEntry->BPB_RsvdSecCnt*bootEntry->BPB_BytsPerSec + cluster*4);
DEBUG(printf("Next cluster: %ld\n",next););
return next;
}
long getAddrByCluster(long cluster) {
return (bootEntry->BPB_RsvdSecCnt + bootEntry->BPB_NumFATs*bootEntry->BPB_FATSz32)*bootEntry->BPB_BytsPerSec + (cluster - bootEntry->BPB_RootClus) * getClusterSize();
}
long getSubDirEntry(int fp, char* dir, long cluster) {
int offset = 0;
int i;
DirEntry *dirEntry;
dirEntry = malloc(sizeof(DirEntry));
while(cluster > 0 && pread(fp, dirEntry, sizeof(DirEntry), getAddrByCluster(cluster)+offset) >= 0) {
if(dirEntry->DIR_Name[0] == 0) {
//end of directory : something wrong
return -1;
}
if(dirEntry->DIR_Attr == 0x0F) {
//LFN
offset += sizeof(DirEntry);
if (offset == getClusterSize()) {
cluster = getNextCluster(cluster,fp);
offset =0;
}
continue;
}
if ((dirEntry->DIR_Attr & 0b10000) != 0) {
// is a directory
for(i=0;i<8;i++) {
if (dirEntry->DIR_Name[i] == ' ')
dirEntry->DIR_Name[i] = 0;
}
dirEntry->DIR_Name[i] = 0;
if (strcmp(dirEntry->DIR_Name, dir) == 0) {
cluster = dirEntry->DIR_FstClusHI * 65535 + dirEntry->DIR_FstClusLO;
DEBUG(printf("start:%ld\n",cluster););
return cluster;
}
}
offset += sizeof(DirEntry);
if (offset == getClusterSize()) {
cluster = getNextCluster(cluster,fp);
offset =0;
}
}
return -1;
}
long getSubDirEntryByPath(int fp, char* lFile, long cluster) {
char *tok;
tok=strtok(lFile,"/");
while(tok) {
DEBUG(printf("dir: %s\n",tok););
cluster = getSubDirEntry(fp, tok, cluster);
tok=strtok(NULL,"/");
}
return cluster;
}
void list(char *device, char* lFile) {
int fp;
long cluster;
int offset = 0;
int num = 1;
int i;
long position;
DirEntry *dirEntry;
fp = open(device, O_RDONLY);
if (fp < 0) {
return;
}
getBootEntry(fp);
cluster = bootEntry->BPB_RootClus;
DEBUG(printf("%ld\n",cluster););
if (strcmp(lFile,"/") != 0) {
//not root, go into the deepest dir recursively
cluster = getSubDirEntryByPath(fp, lFile,cluster);
}
dirEntry = malloc(sizeof(DirEntry));
while(cluster < 0x0ffffff7 && pread(fp, dirEntry, sizeof(DirEntry), getAddrByCluster(cluster)+offset) >= 0) {
if(dirEntry->DIR_Name[0] == 0) {
//end of directory
break;
}
if(dirEntry->DIR_Attr == 0x0F) {
//LFN
offset += sizeof(DirEntry);
if (offset == getClusterSize()) {
//arrive at the cluster edge
cluster = getNextCluster(cluster,fp);
offset = 0;
}
continue;
}
printf("%d, ",num);
//print file name
for(i=0;i<11;i++) {
if (i==8 && dirEntry->DIR_Name[i] != ' ')
putchar('.');
if (dirEntry->DIR_Name[i] == 0xe5)
putchar('?');
else if(dirEntry->DIR_Name[i] == ' ')
continue;
else
putchar(dirEntry->DIR_Name[i]);
}
if ((dirEntry->DIR_Attr & 0b10000) != 0) {
// is a directory
putchar('/');
}
position = dirEntry->DIR_FstClusHI * 65535 + dirEntry->DIR_FstClusLO;
printf(", %d, %ld\n",dirEntry->DIR_FileSize,position);
offset += sizeof(DirEntry);
if (offset == getClusterSize()) {
//arrive at the cluster edge
cluster = getNextCluster(cluster,fp);
offset = 0;
}
num++;
}
free(dirEntry);
close(fp);
}
DirEntry *getFileEntry(int fp, char* filename, long cluster) {
int offset = 0;
int i;
DirEntry *dirEntry;
dirEntry = malloc(sizeof(DirEntry));
char name[11];
for(i=0;i<11;i++){
name[i] = ' ';
}
DEBUG(printf("finding file:%s in cluster %ld\n",filename,cluster););
if (strstr(filename,".") != NULL) {
filename = strtok(filename,".");
memcpy(name,filename, strlen(filename));
filename = strtok(NULL,".");
memcpy(name+8,filename, strlen(filename));
} else {
memcpy(name,filename, strlen(filename));
}
while(cluster < 0x0ffffff7 && pread(fp, dirEntry, sizeof(DirEntry), getAddrByCluster(cluster)+offset) >= 0) {
if(dirEntry->DIR_Name[0] == 0) {
//end of directory : something wrong
free(dirEntry);
return NULL;
}
if(dirEntry->DIR_Attr == 0x0F) {
//LFN
offset += sizeof(DirEntry);
if (offset == getClusterSize()) {
cluster = getNextCluster(cluster,fp);
offset =0;
}
continue;
}
if (dirEntry->DIR_Name[0] == 0xe5) {
// is a directory
for(i=1;i<11;i++) {
if(name[i] != dirEntry->DIR_Name[i]) {
break;
}
}
if (i ==11) {
//find a match
return dirEntry;
}
}
offset += sizeof(DirEntry);
if (offset == getClusterSize()) {
cluster = getNextCluster(cluster,fp);
offset =0;
}
}
free(dirEntry);
return NULL;
}
void recover(char* device, char *rFile, char *oFile) {
FILE *output;
int depth = 0;
int i;
char *tok;
long cluster;
int fp;
char* fileBuffer;
char filename[1024];
int fileSize;
DirEntry *dirEntry;
strcpy(filename,rFile);
output = fopen(oFile,"wb");
fp = open(device, O_RDONLY);
if (fp < 0) {
return;
}
getBootEntry(fp);
cluster = bootEntry->BPB_RootClus;
if (output <= 0) {
printf("[%s]: failed to open\n", oFile);
return;
}
for(i=0;i<strlen(rFile);i++) {
if(rFile[i]=='/')
depth++;
}
if (depth == 1) {
tok = strtok(rFile,"/");
} else {
tok = strtok(rFile,"/");
cluster = getSubDirEntry(fp, tok, cluster);
depth--;
while (depth > 1) {
//tok =
tok = strtok(NULL,"/");
cluster = getSubDirEntry(fp, tok, cluster);
depth --;
}
tok = strtok(NULL,"/");
}
dirEntry = getFileEntry(fp,tok,cluster);
if (dirEntry == NULL) {
printf("[%s]: error - file not found\n", filename);
return;
}
cluster = dirEntry->DIR_FstClusHI * 65535 + dirEntry->DIR_FstClusLO;
DEBUG(printf("file cluster:%ld\n",cluster););
fileSize = dirEntry->DIR_FileSize;
DEBUG(printf("file size:%ld\n",fileSize););
if (cluster != 0 && getNextCluster(cluster,fp) != 0) {
printf("[%s]: error - fail to recover\n", filename);
return;
}
fileBuffer = malloc(fileSize);
DEBUG(printf("file location: %ld\n", getAddrByCluster(cluster)););
pread(fp, fileBuffer, fileSize, getAddrByCluster(cluster));
close(fp);
fwrite(fileBuffer, 1, fileSize, output);
fclose(output);
free(fileBuffer);
free(dirEntry);
}