-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell.c
599 lines (497 loc) · 12.3 KB
/
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
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
#pragma warning(disable : 4995)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include "shell.h"
#include "disksim.h"
#define SECTOR DWORD
#define BLOCK_SIZE 1024
#define SECTOR_SIZE 1024
#define NUMBER_OF_SECTORS ( 4096 + 1 )
#define COND_MOUNT 0x01
#define COND_UMOUNT 0x02
typedef struct
{
char* name;
int(*handler)(int, char**);
char conditions;
} COMMAND;
extern void shell_register_filesystem(SHELL_FILESYSTEM*);
extern 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);
void do_shell(void);
void unknown_command(void);
int seperate_string(char* buf, char* ptrs[]);
int shell_cmd_format(int argc, char* argv[]);
int shell_cmd_mount(int argc, char* argv[]);
int shell_cmd_touch(int argc, char* argv[]);
int shell_cmd_cd(int argc, char* argv[]);
int shell_cmd_ls(int argc, char* argv[]);
int shell_cmd_mkdir(int argc, char* argv[]);
int shell_cmd_dumpsuperblock(int argc, char * argv[]);
int shell_cmd_dumpgd(int argc, char * argv[]);
int shell_cmd_dumpblockbitmap(int argc, char * argv[]);
int shell_cmd_dumpinodebitmap(int argc, char * argv[]);
int shell_cmd_dumpinodetable(int argc, char * argv[]);
int shell_cmd_dumpdatablockbyname(int argc, char * agrv[]);
int shell_cmd_dumpfileinode(int argc, char * argv[]);
int shell_cmd_fill(int argc, char* argv[]);
int shell_cmd_dumpdatablockbynum(int argc, char * argv[]);
int shell_cmd_cat(int argc, char * argv[]);
int shell_cmd_rmdir(int argc, char* argv[]);
int shell_cmd_rm(int argc, char* argv[]);
int shell_cmd_df(int argc, char* argv[]);
static COMMAND g_commands[] =
{
{ "cd", shell_cmd_cd, COND_MOUNT },
{ "cat", shell_cmd_cat, COND_MOUNT },
{ "mount", shell_cmd_mount, COND_UMOUNT },
{ "touch", shell_cmd_touch, COND_MOUNT },
{ "fill", shell_cmd_fill, COND_MOUNT },
{ "ls", shell_cmd_ls, COND_MOUNT },
{ "format", shell_cmd_format, COND_UMOUNT },
{ "mkdir", shell_cmd_mkdir, COND_MOUNT },
{ "dumpdatablockbynum", shell_cmd_dumpdatablockbynum, COND_MOUNT },
{ "dumpsuperblock" , shell_cmd_dumpsuperblock, COND_MOUNT },
{ "dumpgd" , shell_cmd_dumpgd , COND_MOUNT },
{ "dumpblockbitmap" , shell_cmd_dumpblockbitmap, COND_MOUNT },
{ "dumpinodebitmap" , shell_cmd_dumpinodebitmap, COND_MOUNT },
{ "dumpinodetable" , shell_cmd_dumpinodetable, COND_MOUNT },
{ "dumpdatablockbyname", shell_cmd_dumpdatablockbyname, COND_MOUNT },
{ "dumpfileinode", shell_cmd_dumpfileinode, COND_MOUNT },
{ "rmdir", shell_cmd_rmdir, COND_MOUNT },
{ "rm", shell_cmd_rm, COND_MOUNT},
{ "df", shell_cmd_df, COND_MOUNT}
};
static SHELL_FILESYSTEM g_fs;
static SHELL_FS_OPERATIONS g_fsOprs;
static SHELL_ENTRY g_rootDir;
static SHELL_ENTRY g_currentDir;
static DISK_OPERATIONS g_disk;
int g_commandsCount = sizeof(g_commands) / sizeof(COMMAND);
int g_isMounted;
int main(int argc, char* argv[])
{
if (disksim_init(NUMBER_OF_SECTORS, SECTOR_SIZE, &g_disk) < 0)
{
printf("disk simulator initialization has been failed\n");
return -1;
}
shell_register_filesystem(&g_fs);
do_shell();
return 0;
}
int check_conditions(int conditions)
{
if (conditions & COND_MOUNT && !g_isMounted)
{
printf("file system is not mounted\n");
return -1;
}
if (conditions & COND_UMOUNT && g_isMounted)
{
printf("file system is already mounted\n");
return -1;
}
return 0;
}
static int x = 0;
void do_shell(void)
{
char buf[1000];
char command[100];
char* argv[100];
int argc;
int i, j = 0;
printf("%s File system shell\n", g_fs.name);
while (-1)
{
printf("[%s]# ", g_currentDir.name);
fgets(buf, 1000, stdin);
argc = seperate_string(buf, argv);
if (argc == 0)
continue;
for (i = 0; i < g_commandsCount; i++)
{
if (strcmp(g_commands[i].name, argv[0]) == 0)
{
if (check_conditions(g_commands[i].conditions) == 0)
g_commands[i].handler(argc, argv);
break;
}
}
if (argc != 0 && i == g_commandsCount)
unknown_command();
}
}
void unknown_command(void)
{
int i;
printf(" * ");
for (i = 0; i < g_commandsCount; i++)
{
if (i < g_commandsCount - 1)
printf("%s, ", g_commands[i].name);
else
printf("%s", g_commands[i].name);
}
printf("\n");
}
int seperate_string(char* buf, char* ptrs[])
{
char prev = 0;
int count = 0;
while (*buf)
{
if (isspace(*buf))
*buf = 0;
else if (prev == 0)
ptrs[count++] = buf;
prev = *buf++;
}
return count;
}
int shell_cmd_dumpsuperblock(int argc, char * argv[])
{
SHELL_ENTRY entry;
printf_by_sel(&g_disk, &g_fsOprs, &g_currentDir, &entry, argv[1], 1, 0);
return 0;
}
int shell_cmd_dumpgd(int argc, char * argv[])
{
SHELL_ENTRY entry;
printf_by_sel(&g_disk, &g_fsOprs, &g_currentDir, &entry, argv[1], 2, 0);
return 0;
}
int shell_cmd_dumpblockbitmap(int argc, char * argv[])
{
SHELL_ENTRY entry;
printf_by_sel(&g_disk, &g_fsOprs, &g_currentDir, &entry, argv[1], 3, 0);
return 0;
}
int shell_cmd_dumpinodebitmap(int argc, char * argv[])
{
SHELL_ENTRY entry;
printf_by_sel(&g_disk, &g_fsOprs, &g_currentDir, &entry, argv[1], 4, 0);
return 0;
}
int shell_cmd_dumpinodetable(int argc, char * argv[])
{
SHELL_ENTRY entry;
printf_by_sel(&g_disk, &g_fsOprs, &g_currentDir, &entry, argv[1], 5, 0);
return 0;
}
int shell_cmd_dumpdatablockbyname(int argc, char * argv[])
{
SHELL_ENTRY entry;
printf_by_sel(&g_disk, &g_fsOprs, &g_currentDir, &entry, argv[1], 6, 0);
return 0;
}
int shell_cmd_dumpfileinode(int argc, char * argv[])
{
SHELL_ENTRY entry;
printf_by_sel(&g_disk, &g_fsOprs, &g_currentDir, &entry, argv[1], 7, 0);
return 0;
}
int shell_cmd_dumpdatablockbynum(int argc, char * argv[])
{
SHELL_ENTRY entry;
printf_by_sel(&g_disk, &g_fsOprs, &g_currentDir, &entry, argv[1], 8, atoi(argv[1]));
return 0;
}
int shell_cmd_cd(int argc, char* argv[])
{
SHELL_ENTRY newEntry;
int result, i;
static SHELL_ENTRY path[256];
static int pathTop = 0;
path[0] = g_rootDir;
if (argc > 2)
{
printf("usage : %s [directory]\n", argv[0]);
return 0;
}
if (argc == 1)
pathTop = 0;
else
{
if (strcmp(argv[1], ".") == 0)
return 0;
else if (strcmp(argv[1], "..") == 0 && pathTop > 0)
pathTop--;
else
{
result = g_fsOprs.lookup(&g_disk, &g_fsOprs, &g_currentDir, &newEntry, argv[1]);
if (result)
{
printf("directory not found\n");
return -1;
}
else if (!newEntry.isDirectory)
{
printf("%s is not a directory\n", argv[1]);
return -1;
}
path[++pathTop] = newEntry;
}
}
g_currentDir = path[pathTop];
return 0;
}
int shell_cmd_exit(int argc, char* argv[])
{
disksim_uninit(&g_disk);
_exit(0);
return 0;
}
int shell_cmd_mount(int argc, char* argv[])
{
int result;
x++;
if (g_fs.mount == NULL)
{
printf("The mount functions is NULL\n");
return 0;
}
result = g_fs.mount(&g_disk, &g_fsOprs, &g_rootDir);
g_currentDir = g_rootDir;
if (result < 0)
{
printf("%s file system mounting has been failed\n", g_fs.name);
return -1;
}
else
{ printf(" %s\n",g_currentDir.name);
printf("%s file system has been mounted successfully\n\n", g_fs.name);
g_isMounted = 1;
}
return 0;
}
int shell_cmd_umount(int argc, char* argv[])
{
g_isMounted = 0;
if (g_fs.umount == NULL)
return 0;
g_fs.umount(&g_disk, &g_fsOprs);
return 0;
}
int shell_cmd_touch(int argc, char* argv[])
{
SHELL_ENTRY entry;
int result;
if (argc < 2)
{
printf("usage : touch [files...]\n");
return 0;
}
result = g_fsOprs.fileOprs->create(&g_disk, &g_fsOprs, &g_currentDir, argv[1], &entry);
if (result)
{
printf("create failed\n");
return -1;
}
return 0;
}
int shell_cmd_fill(int argc, char* argv[])
{
SHELL_ENTRY entry;
char* buffer;
char* tmp;
int size;
int result;
char opt[3] = { 0, };
const char CREATE[3] = "-c";
const char APPEND[3] = "-a";
unsigned long offset;
if (argc != 4)
{
printf("usage : fill [file] [size] [option]\n");
return 0;
}
sscanf(argv[2], "%d", &size);
sscanf(argv[3], "%s", opt);
if (strcmp(opt, CREATE) == 0)
{
result = g_fsOprs.fileOprs->create(&g_disk, &g_fsOprs, &g_currentDir, argv[1], &entry);
if (result)
{
printf("create failed\n");
return -1;
}
offset = 0;
}
if (strcmp(opt, APPEND) == 0) {
g_fsOprs.lookup(&g_disk, &g_fsOprs, &g_currentDir, &entry, argv[1]);
offset = entry.size;
}
buffer = (char*)malloc(size + 13);
tmp = buffer;
while (tmp < buffer + size)
{
memcpy(tmp, "Can you see? ", 13);
tmp += 13;
}
g_fsOprs.fileOprs->write(&g_disk, &g_fsOprs, &g_currentDir, &entry, offset, size, buffer);
free(buffer);
return 0;
}
int shell_cmd_rm(int argc, char* argv[])
{
int i;
if (argc < 2)
{
printf("usage : rm [files...]\n");
return 0;
}
for (i = 1; i < argc; i++)
{
if (g_fsOprs.fileOprs->remove(&g_disk, &g_fsOprs, &g_currentDir, argv[i]))
printf("cannot remove file\n");
}
return 0;
}
typedef struct
{
char* address;
} DISK_MEMORY;
int shell_cmd_format(int argc, char* argv[])
{
int result;
unsigned int k = 0;
char* param = NULL;
x++;
if (argc >= 2)
param = argv[1];
result = g_fs.format(&g_disk, param);
if (result < 0)
{
printf("%s formatting is failed\n", g_fs.name);
return -1;
}
printf("disk has been formatted successfully\n\n");
return 0;
}
double get_percentage(unsigned int number, unsigned int total)
{
return ((double)number) / total * 100.;
}
int shell_cmd_df(int argc, char* argv[])
{
unsigned int used, total;
int result;
g_fsOprs.stat(&g_disk, &g_fsOprs, &total, &used);
printf("free sectors : %u(%.2lf%%)\tused sectors : %u(%.2lf%%)\ttotal : %u\n",
total - used, get_percentage(total - used, g_disk.numberOfSectors),
used, get_percentage(used, g_disk.numberOfSectors),
total);
return 0;
}
int shell_cmd_mkdir(int argc, char* argv[])
{
SHELL_ENTRY entry;
int result;
if (argc != 2)
{
printf("usage : %s [name]\n", argv[0]);
return 0;
}
result = g_fsOprs.mkdir(&g_disk, &g_fsOprs, &g_currentDir, argv[1], &entry);
if (result)
{
printf("cannot create directory\n");
return -1;
}
return 0;
}
int shell_cmd_rmdir(int argc, char* argv[])
{
int result;
if (argc != 2)
{
printf("usage : %s [name]\n", argv[0]);
return 0;
}
result = g_fsOprs.rmdir(&g_disk, &g_fsOprs, &g_currentDir, argv[1]);
if (result)
{
printf("cannot remove directory\n");
return -1;
}
return 0;
}
int shell_cmd_mkdirst(int argc, char* argv[])
{
SHELL_ENTRY entry;
int result, i, count;
char buf[10];
if (argc != 2)
{
printf("usage : %s [count]\n", argv[0]);
return 0;
}
sscanf(argv[1], "%d", &count);
for (i = 0; i < count; i++)
{
printf(buf, "%d", i);
result = g_fsOprs.mkdir(&g_disk, &g_fsOprs, &g_currentDir, buf, &entry);
if (result)
{
printf("cannot create directory\n");
return -1;
}
}
return 0;
}
int shell_cmd_cat(int argc, char* argv[])
{
SHELL_ENTRY entry;
char buf[1025] = { 0, };
int result;
unsigned long offset = 0;
if (argc != 2)
{
printf("usage : %s [file name]\n", argv[0]);
return 0;
}
result = g_fsOprs.lookup(&g_disk, &g_fsOprs, &g_currentDir, &entry, argv[1]);
if (result)
{
printf("%s lookup failed\n", argv[1]);
return -1;
}
printf("1\n");
while (g_fsOprs.fileOprs->read(&g_disk, &g_fsOprs, &g_currentDir, &entry, offset, 1024, buf) > 0)
{
printf("%s", buf);
offset += 1024;
memset(buf, 0, sizeof(buf));
}
printf("\n");
}
int shell_cmd_ls(int argc, char* argv[])
{
SHELL_ENTRY_LIST list = {0, };
SHELL_ENTRY_LIST_ITEM* current;
if (argc > 2)
{
printf("usage : %s [path]\n", argv[0]);
return 0;
}
init_entry_list(&list);
if (g_fsOprs.read_dir(&g_disk, &g_fsOprs, &g_currentDir, &list))
{
printf("Failed to read_dir\n");
return -1;
}
current = list.first;
printf("[File names] [D] [File sizes]\n");
while (current)
{
printf("%-12.11s %1d %12d\n",
current->entry.name, current->entry.isDirectory, current->entry.size);
current = current->next;
}
printf("\n");
release_entry_list(&list);
return 0;
}