-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-fs.c
319 lines (252 loc) · 5.66 KB
/
test-fs.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
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <uthread.h>
#include <fs.h>
#include <time.h>
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
#define test_fs_error(fmt, ...) \
fprintf(stderr, "%s: "fmt"\n", __func__, ##__VA_ARGS__)
#define die(...) \
do { \
test_fs_error(__VA_ARGS__); \
exit(1); \
} while (0)
#define die_perror(msg) \
do { \
perror(msg); \
exit(1); \
} while (0)
struct thread_arg {
int argc;
char **argv;
};
int thread_find_fs_fd(char* filename){
int fd=find_fs_fd(filename);
if(fd==-1){
die("can't find fs_fd of file %s", filename);
}
return fd;
}
void thread_fs_stat(void *arg)
{
struct thread_arg *t_arg = arg;
char *diskname, *filename;
int fs_fd;
size_t stat;
if (t_arg->argc < 2)
die("need <diskname> <filename>");
diskname = t_arg->argv[0];
filename = t_arg->argv[1];
if (fs_mount(diskname))
die("Cannot mount diskname");
fs_fd = fs_open(filename);
if (fs_fd < 0) {
fs_umount();
die("Cannot open file");
}
stat = fs_stat(fs_fd);
if (stat < 0) {
fs_umount();
die("Cannot stat file");
}
if (!stat) {
/* Nothing to read, file is empty */
printf("Empty file\n");
return;
}
if (fs_close(fs_fd)) {
fs_umount();
die("Cannot close file");
}
if (fs_umount())
die("cannot unmount diskname");
printf("Size of file '%s' is %zu bytes\n", filename, stat);
}
void thread_fs_cat(void *arg)
{
struct thread_arg *t_arg = arg;
char *diskname, *filename, *buf;
int fs_fd;
size_t stat, read;
if (t_arg->argc < 2)
die("need <diskname> <filename>");
diskname = t_arg->argv[0];
filename = t_arg->argv[1];
if (fs_mount(diskname))
die("Cannot mount diskname");
fs_fd = fs_open(filename);
if (fs_fd < 0) {
fs_umount();
die("Cannot open file");
}
stat = fs_stat(fs_fd);
if (stat < 0) {
fs_umount();
die("Cannot stat file");
}
if (!stat) {
/* Nothing to read, file is empty */
printf("Empty file\n");
return;
}
buf = malloc(stat);
if (!buf) {
perror("malloc");
fs_umount();
die("Cannot malloc");
}
read = fs_read(fs_fd, buf, stat);
if (fs_close(fs_fd)) {
fs_umount();
die("Cannot close file");
}
if (fs_umount())
die("cannot unmount diskname");
printf("Read file '%s' (%zu/%zu bytes)\n", filename, read, stat);
printf("Content of the file:\n%s", buf);
free(buf);
}
void thread_fs_rm(void *arg)
{
struct thread_arg *t_arg = arg;
char *diskname, *filename;
if (t_arg->argc < 2)
die("need <diskname> <filename>");
diskname = t_arg->argv[0];
filename = t_arg->argv[1];
if (fs_mount(diskname))
die("Cannot mount diskname");
if (fs_delete(filename)) {
fs_umount();
die("Cannot delete file");
}
if (fs_umount())
die("Cannot unmount diskname");
printf("Removed file '%s'\n", filename);
}
void thread_fs_add(void *arg)
{
struct thread_arg *t_arg = arg;
char *diskname, *filename, *buf;
int fd, fs_fd;
struct stat st;
size_t written;
if (t_arg->argc < 2)
die("Usage: <diskname> <host filename>");
diskname = t_arg->argv[0];
filename = t_arg->argv[1];
/* Open file on host computer */
fd = open(filename, O_RDONLY);
if (fd < 0)
die_perror("open");
if (fstat(fd, &st))
die_perror("fstat");
if (!S_ISREG(st.st_mode))
die("Not a regular file: %s\n", filename);
/* Map file into buffer */
buf = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (!buf)
die_perror("mmap");
/* Now, deal with our filesystem:
* - mount, create a new file, copy content of host file into this new
* file, close the new file, and unmount
*/
if (fs_mount(diskname))
die("Cannot mount diskname");
if (fs_create(filename, buf, st.st_size)) {
fs_umount();
die("Cannot create file");
}
fs_fd = thread_find_fs_fd(filename);
if (fs_close(fs_fd)) {
fs_umount();
die("Cannot close file");
}
if (fs_umount())
die("Cannot unmount diskname");
printf("Wrote file '%s' (%zu/%zu bytes)\n", filename, written,
st.st_size);
munmap(buf, st.st_size);
close(fd);
}
void thread_fs_ls(void *arg)
{
struct thread_arg *t_arg = arg;
char *diskname;
if (t_arg->argc < 1)
die("Usage: <diskname> <filename>");
diskname = t_arg->argv[0];
if (fs_mount(diskname))
die("Cannot mount diskname");
fs_ls();
if (fs_umount())
die("Cannot unmount diskname");
}
void thread_fs_info(void *arg)
{
struct thread_arg *t_arg = arg;
char *diskname;
if (t_arg->argc < 1)
die("Usage: <diskname>");
diskname = t_arg->argv[0];
if (fs_mount(diskname))
die("Cannot mount diskname");
fs_info();
if (fs_umount())
die("Cannot unmount diskname");
}
static struct {
const char *name;
uthread_func_t func;
} commands[] = {
{ "info", thread_fs_info },
{ "ls", thread_fs_ls },
{ "add", thread_fs_add },
{ "rm", thread_fs_rm },
{ "cat", thread_fs_cat },
{ "stat", thread_fs_stat },
};
void usage(void)
{
int i;
fprintf(stderr, "Usage: test-fs <command> [<arg>]\n");
fprintf(stderr, "Possible commands are:\n");
for (i = 0; i < ARRAY_SIZE(commands); i++)
fprintf(stderr, "\t%s\n", commands[i].name);
exit(1);
}
int main(int argc, char **argv)
{
// long start=clock();
int i;
char *cmd;
struct thread_arg arg;
/* Skip argv[0] */
argc--;
argv++;
if (!argc)
usage();
cmd = argv[0];
arg.argc = --argc;
arg.argv = &argv[1];
for (i = 0; i < ARRAY_SIZE(commands); i++) {
if (!strcmp(cmd, commands[i].name)) {
uthread_start(commands[i].func, &arg);
break;
}
}
if (i == ARRAY_SIZE(commands)) {
test_fs_error("invalid command '%s'", cmd);
usage();
}
// long end = clock();
// double execution_time = ((double)(end - start))/CLOCKS_PER_SEC;
// printf("%f\n", execution_time);
return 0;
}