-
Notifications
You must be signed in to change notification settings - Fork 0
/
ext2_shell.c
437 lines (339 loc) · 10.8 KB
/
ext2_shell.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
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include "ext2_shell.h"
typedef struct {
char * address;
}DISK_MEMORY;
void printFromP2P(char * start, char * end);
int fs_dumpDataSector(DISK_OPERATIONS* disk, int usedSector)
{
char * start;
char * end;
start = ((DISK_MEMORY *)disk->pdata)->address + usedSector * disk->bytesPerSector;
end = start + disk->bytesPerSector;
printFromP2P(start, end);
printf("\n\n");
return EXT2_SUCCESS;
}
void printFromP2P(char * start, char * end)
{
int start_int, end_int;
start_int = (int)start;
end_int = (int)end;
printf("start address : %#x , end address : %#x\n\n", start, end - 1);
start = (char *)(start_int &= ~(0xf));
end = (char *)(end_int |= 0xf);
while (start <= end)
{
if ((start_int & 0xf) == 0)
fprintf(stdout, "\n%#08x ", start);
fprintf(stdout, "%02X ", *(unsigned char *)start);
start++;
start_int++;
}
printf("\n\n");
}
void fs_dumpDataPart(DISK_OPERATIONS * disk, SHELL_FS_OPERATIONS * fsOprs, const SHELL_ENTRY * parent, SHELL_ENTRY * entry, const char * name)
{
EXT2_NODE EXT2Parent;
EXT2_NODE EXT2Entry;
INODE node;
int result;
char * start, *end;
shell_entry_to_ext2_entry(parent, &EXT2Parent);
if (result = ext2_lookup(&EXT2Parent, name, &EXT2Entry)) return result;
get_inode(EXT2Entry.fs, EXT2Entry.entry.inode, &node);
start = ((DISK_MEMORY *)disk->pdata)->address + (1 + node.block[0]) * disk->bytesPerSector;
end = start + disk->bytesPerSector;
printFromP2P(start, end);
}
void fs_dumpfileinode(DISK_OPERATIONS * disk, SHELL_FS_OPERATIONS * fsOprs, const SHELL_ENTRY * parent, SHELL_ENTRY * entry, const char * name)
{
EXT2_NODE EXT2Parent;
EXT2_NODE EXT2Entry;
INODE node;
int result, inode, i;
shell_entry_to_ext2_entry(parent, &EXT2Parent);
if (result = ext2_lookup(&EXT2Parent, name, &EXT2Entry)) return result;
get_inode(EXT2Entry.fs, EXT2Entry.entry.inode, &node);
inode = EXT2Entry.entry.inode;
printf("inode number : %d\n", inode);
for (i = 0; i < EXT2_N_BLOCKS; i++) {
printf("iblock[%.2d] : %u\n", i, node.block[i]);
}
}
void fs_dumpDataBlockByNum(DISK_OPERATIONS * disk, SHELL_FS_OPERATIONS * fsOprs, const SHELL_ENTRY * parent, SHELL_ENTRY * entry, int num)
{
char * start, *end;
start = ((DISK_MEMORY *)disk->pdata)->address + (1 + num) * disk->bytesPerSector;
end = start + disk->bytesPerSector;
printFromP2P(start, end);
}
void printf_by_sel(DISK_OPERATIONS* disk, SHELL_FS_OPERATIONS* fsOprs, const SHELL_ENTRY* parent, SHELL_ENTRY* entry, const char* name, int sel, int num)
{
switch (sel) {
case 1:
fs_dumpDataSector(disk, 1);
break;
case 2:
fs_dumpDataSector(disk, 2);
break;
case 3:
fs_dumpDataSector(disk, 3);
break;
case 4:
fs_dumpDataSector(disk, 4);
break;
case 5:
fs_dumpDataSector(disk, 5);
fs_dumpDataSector(disk, 6);
break;
case 6:
fs_dumpDataPart(disk, fsOprs, parent, entry, name);
break;
case 7:
fs_dumpfileinode(disk, fsOprs, parent, entry, name);
break;
case 8:
fs_dumpDataBlockByNum(disk, fsOprs, parent, entry, num);
break;
}
}
int fs_format(DISK_OPERATIONS* disk, void* param)
{
printf("formatting as a %s\n", (char *)param);
ext2_format(disk);
return 1;
}
static SHELL_FILE_OPERATIONS g_file =
{
fs_create,
fs_remove,
fs_read,
fs_write
};
static SHELL_FS_OPERATIONS g_fsOprs =
{
fs_read_dir,
fs_stat,
fs_mkdir,
fs_rmdir,
fs_lookup,
&g_file,
NULL
};
int fs_stat( DISK_OPERATIONS* disk, SHELL_FS_OPERATIONS* fsOprs, unsigned int* totalSectors, unsigned int* usedSectors )
{
return ext2_df( (EXT2_FILESYSTEM*)fsOprs->pdata, totalSectors, usedSectors );
}
int fs_remove (DISK_OPERATIONS* disk, SHELL_FS_OPERATIONS* fsOprs, const SHELL_ENTRY* current, const char* name)
{
EXT2_NODE EXT2_Current;
EXT2_NODE entry;
shell_entry_to_ext2_entry(current, &EXT2_Current);
// 해당 name이 있는지 찾고, entry에 찾은 entry채워줌
ext2_lookup(&EXT2_Current, name, &entry);
return ext2_remove(&entry);
}
//.rmdir(&g_disk, &g_fsOprs, &g_currentDir, argv[1]);
int fs_rmdir(DISK_OPERATIONS* disk, SHELL_FS_OPERATIONS* fsOprs, SHELL_ENTRY* current, const char* name)
{
EXT2_NODE EXT2_Current;
EXT2_NODE dir;
shell_entry_to_ext2_entry(current, &EXT2_Current);
// 해당 name이 있는지 찾고, dir에 찾은 dir_entry채워줌
ext2_lookup(&EXT2_Current, name, &dir);
return ext2_rmdir(&dir);
}
int fs_mount(DISK_OPERATIONS* disk, SHELL_FS_OPERATIONS* fsOprs, SHELL_ENTRY* root)
{
EXT2_FILESYSTEM* fs;
EXT2_NODE ext2_entry;
int result;
*fsOprs = g_fsOprs;
fsOprs->pdata = malloc(sizeof(EXT2_FILESYSTEM));
fs = FSOPRS_TO_EXT2FS(fsOprs);
ZeroMemory(fs, sizeof(EXT2_FILESYSTEM));
fs->disk = disk;
result = ext2_read_superblock(fs, &ext2_entry);
result = EXT2_SUCCESS;
if (result == EXT2_SUCCESS)
{
printf("number of groups : %d\n", NUMBER_OF_GROUPS);
printf("blocks per group : %d\n", fs->sb.block_per_group);
printf("bytes per block : %d\n", disk->bytesPerSector);
printf("free block count : %d\n", fs->sb.free_block_count);
printf("free inode count : %d\n", fs->sb.free_inode_count);
printf("first non reserved inode : %d\n", fs->sb.first_non_reserved_inode);
printf("inode structure size : %d\n", fs->sb.inode_structure_size);
printf("first data block number : %d\n", fs->sb.first_data_block_each_group);
printf("\n----------------------------------------------\n");
}
/*printf("%d\n", result);
printf("%s\n", ext2_entry.entry.name);*/
ext2_entry_to_shell_entry(fs, &ext2_entry, root);
return result;
}
void fs_umount(DISK_OPERATIONS* disk, SHELL_FS_OPERATIONS* fsOprs)
{
return;
}
static SHELL_FILESYSTEM g_ext2 =
{
"EXT2",
fs_mount,
fs_umount,
fs_format
};
int adder(EXT2_FILESYSTEM* fs, void* list, EXT2_NODE* entry)
{
SHELL_ENTRY_LIST* entryList = (SHELL_ENTRY_LIST*)list;
SHELL_ENTRY newEntry;
ext2_entry_to_shell_entry(fs, entry, &newEntry);
add_entry_list(entryList, &newEntry);
return EXT2_SUCCESS;
}
int fs_write(DISK_OPERATIONS* disk, SHELL_FS_OPERATIONS* fsOprs, const SHELL_ENTRY* parent, SHELL_ENTRY* entry, unsigned long offset, unsigned long length, const char* buffer)
{
EXT2_NODE EXT2Entry;
shell_entry_to_ext2_entry(entry, &EXT2Entry);
return ext2_write(&EXT2Entry, offset, length, buffer);
}
void shell_register_filesystem(SHELL_FILESYSTEM* fs)
{
*fs = g_ext2;
}
int fs_create(DISK_OPERATIONS* disk, SHELL_FS_OPERATIONS* fsOprs, const SHELL_ENTRY* parent, const char* name, SHELL_ENTRY* retEntry)
{
EXT2_NODE EXT2Parent;
EXT2_NODE EXT2Entry;
int result;
shell_entry_to_ext2_entry(parent, &EXT2Parent);
result = ext2_create(&EXT2Parent, name, &EXT2Entry);
ext2_entry_to_shell_entry(EXT2Parent.fs, &EXT2Entry, retEntry);
return result;
}
int shell_entry_to_ext2_entry(const SHELL_ENTRY* shell_entry, EXT2_NODE* ext2_entry)
{
EXT2_NODE* entry = (EXT2_NODE*)shell_entry->pdata;
*ext2_entry = *entry;
return EXT2_SUCCESS;
}
int ext2_entry_to_shell_entry(EXT2_FILESYSTEM* fs, const EXT2_NODE* ext2_entry, SHELL_ENTRY* shell_entry)
{
EXT2_NODE* entry = (EXT2_NODE*)shell_entry->pdata;
INODE inodeBuffer;
//BYTE* str = "/";
BYTE str [MAX_NAME_LENGTH];
//str[0]='/';
ZeroMemory(shell_entry, sizeof(SHELL_ENTRY));
int inode = ext2_entry->entry.inode;
int result = get_inode(fs, inode, &inodeBuffer);
if (ext2_entry->entry.name[0] != '.' && inode == 2)
{
memcpy(shell_entry->name, str, MAX_NAME_LENGTH);
}
else if(ext2_entry->entry.name[0] == '.')
{
memcpy(shell_entry->name, ext2_entry->entry.name, MAX_NAME_LENGTH);
}
else {
memcpy(&str[0], ext2_entry->entry.name, MAX_NAME_LENGTH-1);
memcpy(shell_entry->name, str, MAX_NAME_LENGTH);
//shell_entry->name = *str;
/*if (ext2_entry->entry.name[8] != 0x20)
{ printf("ddfsf \n");
str = my_strncpy(str, ".", 1);
printf("ddfsfrf \n");
str = my_strncpy(str, &ext2_entry->entry.name[8], 3);
printf("ddfsf \n");
} */
}
if (FILE_TYPE_DIR & inodeBuffer.mode)
{
shell_entry->isDirectory = 1;
}
else
{
shell_entry->isDirectory = 0;
}
shell_entry->permition = 0x01FF & inodeBuffer.mode;
shell_entry->size = inodeBuffer.size;
*entry = *ext2_entry;
return EXT2_SUCCESS;
}
int fs_lookup(DISK_OPERATIONS* disk, SHELL_FS_OPERATIONS* fsOprs, const SHELL_ENTRY* parent, SHELL_ENTRY* entry, const char* name)
{
EXT2_NODE EXT2Parent;
EXT2_NODE EXT2Entry;
int result;
shell_entry_to_ext2_entry(parent, &EXT2Parent);
if (result = ext2_lookup(&EXT2Parent, name, &EXT2Entry))return result;
ext2_entry_to_shell_entry(EXT2Parent.fs, &EXT2Entry, entry);
return result;
}
int fs_read_dir(DISK_OPERATIONS* disk, SHELL_FS_OPERATIONS* fsOprs, const SHELL_ENTRY* parent, SHELL_ENTRY_LIST* list)
{
EXT2_NODE entry;
if (list->count)
release_entry_list(list);
shell_entry_to_ext2_entry(parent, &entry);
ext2_read_dir(&entry, adder, list);
return EXT2_SUCCESS;
}
int fs_read( DISK_OPERATIONS* disk, SHELL_FS_OPERATIONS* fsOprs, const SHELL_ENTRY* parent, SHELL_ENTRY* entry, unsigned long offset, unsigned long length, char* buffer )
{
EXT2_NODE EXTEntry;
shell_entry_to_ext2_entry( entry, &EXTEntry );
return ext2_read( &EXTEntry, offset, length, buffer );
}
int my_strnicmp(const char* str1, const char* str2, int length)
{
char c1, c2;
while (((*str1 && *str1 != 0x20) || (*str2 && *str2 != 0x20)) && length-- > 0)
{
c1 = toupper(*str1);
c2 = toupper(*str2);
if (c1 > c2)
return -1;
else if (c1 < c2)
return 1;
str1++;
str2++;
}
return 0;
}
int is_exist(DISK_OPERATIONS* disk, SHELL_FS_OPERATIONS* fsOprs, const SHELL_ENTRY* parent, const char* name)
{
SHELL_ENTRY_LIST list;
SHELL_ENTRY_LIST_ITEM* current;
init_entry_list(&list);
fs_read_dir(disk, fsOprs, parent, &list);
current = list.first;
while (current)
{
if (my_strnicmp((char*)current->entry.name, name, 12) == 0)
{
release_entry_list(&list);
return EXT2_ERROR;
}
current = current->next;
}
release_entry_list(&list);
return EXT2_SUCCESS;
}
int fs_mkdir(DISK_OPERATIONS* disk, SHELL_FS_OPERATIONS* fsOprs, const SHELL_ENTRY* parent, const char* name, SHELL_ENTRY* retEntry)
{
EXT2_FILESYSTEM* ext2;
EXT2_NODE EXT2_Parent;
EXT2_NODE EXT2_Entry;
int result;
ext2 = (EXT2_FILESYSTEM*)fsOprs->pdata;
if (is_exist(disk, fsOprs, parent, name))
return EXT2_ERROR;
shell_entry_to_ext2_entry(parent, &EXT2_Parent);
result = ext2_mkdir(&EXT2_Parent, name, &EXT2_Entry);
ext2_entry_to_shell_entry(ext2, &EXT2_Entry, retEntry);
return result;
}