-
Notifications
You must be signed in to change notification settings - Fork 3
/
testfs.c
executable file
·187 lines (166 loc) · 4.88 KB
/
testfs.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
#define _GNU_SOURCE
#include <stdbool.h>
#include <getopt.h>
#include "testfs.h"
#include "super.h"
#include "inode.h"
#include "dir.h"
#include "tx.h"
static int cmd_help(struct super_block *, struct context *c);
static int cmd_quit(struct super_block *, struct context *c);
static bool can_quit = false;
#define PROMPT printf("%s", "% ")
static struct {
const char *name;
int (*func)(struct super_block *sb, struct context *c);
int max_args;
} cmdtable[] = {
/* menus */
{ "?", cmd_help, 1, },
{ "cd", cmd_cd, 2, },
{ "pwd", cmd_pwd, 1, },
{ "ls", cmd_ls, 2, },
{ "lsr", cmd_lsr, 2, },
{ "touch", cmd_create, 2, },
{ "stat", cmd_stat, MAX_ARGS, },
{ "rm", cmd_rm, 2, },
{ "mkdir", cmd_mkdir, 2, },
{ "cat", cmd_cat, MAX_ARGS, },
{ "write", cmd_write, 2, },
{ "checkfs", cmd_checkfs, 1, },
{ "quit", cmd_quit, 1, },
{ NULL, NULL}
};
static int
cmd_help(struct super_block *sb, struct context *c)
{
int i = 0;
printf("Commands:\n");
for (; cmdtable[i].name; i++) {
printf("%s\n", cmdtable[i].name);
}
return 0;
}
static int
cmd_quit(struct super_block *sb, struct context *c)
{
printf("Bye!\n");
can_quit = true;
return 0;
}
static void
handle_command(struct super_block *sb, struct context *c, char * name,
char * args)
{
int i, j = 0;
if (name == NULL)
return;
for (i=0; cmdtable[i].name; i++) {
if (strcmp(name, cmdtable[i].name) == 0) {
char * token = args;
assert(cmdtable[i].func);
c->cmd[j++] = name;
while (j < cmdtable[i].max_args &&
(c->cmd[j] = strtok(token, " \t\n")) != NULL) {
j++;
token = NULL;
}
if ((c->cmd[j] = strtok(token, "\n")) != NULL) {
j++;
}
for ( c->nargs = j++; j <= MAX_ARGS; j++ ) {
c->cmd[j] = NULL;
}
errno = cmdtable[i].func(sb, c);
if (errno < 0) {
errno = -errno;
WARN(c->cmd[0]);
}
return;
}
}
printf("%s: command not found: type ?\n", c->cmd[0]);
}
static void
usage(const char * progname)
{
fprintf(stderr, "Usage: %s [-ch][--help] rawfile\n", progname);
exit(1);
}
struct args
{
const char * disk; // name of disk
int corrupt; // to corrupt or not
};
static struct args *
parse_arguments(int argc, char * const argv[])
{
static struct args args = { 0 };
static struct option long_options[] =
{
{"corrupt", no_argument, 0, 'c'},
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0},
};
int running = 1;
while (running)
{
int option_index = 0;
int c = getopt_long (argc, argv, "ch", long_options, &option_index);
switch (c)
{
case -1:
running = 0;
break;
case 0:
break;
case 'c':
args.corrupt = 1;
break;
case 'h':
usage(argv[0]);
break;
case '?':
usage(argv[0]);
break;
default:
abort();
}
}
if ( argc - optind != 1 )
usage(argv[0]);
args.disk = argv[optind];
return &args;
}
int
main(int argc, char * const argv[])
{
struct super_block *sb;
char *line = NULL;
size_t line_size = 0;
ssize_t nr;
int ret;
struct context c;
struct args * args = parse_arguments(argc, argv);
ret = testfs_init_super_block(args->disk,args-> corrupt, &sb);
if (ret) {
EXIT("testfs_init_super_block");
}
c.cur_dir = testfs_get_inode(sb, 0); /* root dir */
for (;
PROMPT,
(nr = getline(&line, &line_size, stdin)) != EOF; ) {
char * name;
char * args;
name = strtok(line, " \t\n");
args = strtok(NULL, "\n");
handle_command(sb, &c, name, args);
if ( can_quit ) {
break;
}
}
free(line);
testfs_put_inode(c.cur_dir);
testfs_close_super_block(sb);
return 0;
}