-
Notifications
You must be signed in to change notification settings - Fork 0
/
sys_file.c
142 lines (129 loc) · 3.78 KB
/
sys_file.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
#include "syscall.h"
#include "types.h"
#include "memory.h"
#include "proc.h"
#include "fs.h"
#include "string.h"
#include "io.h"
#include "util.h"
#include "sys_file.h"
extern struct file console_file;
int sys_open(struct int_regs *int_frame) {
uint32_t user_esp = int_frame->esp;
char *path = NULL;
if (read_syscall_arg((void *)user_esp, 0, (uint32_t *)&path) < 0) {
return -1;
}
int flags;
if (read_syscall_arg((void *)user_esp, 1, (uint32_t *)&flags) < 0) {
return -1;
}
int fd = 0;
for (fd = 0; fd < MAX_OPEN_FILES; fd++) {
if (!(curr_task->open_files[fd])) {
// found empty slot
break;
}
}
struct file *fp = NULL;
if (strcmp("console", path) == 0) {
curr_task->open_files[fd] = &console_file;
} else if (strcmp("/", path) == 0 && !(fp = get_file(path))) {
// get_file("/") returned NULL.
// i.e., First time to list root directory.
// Allocate it.
if (!(fp = alloc_file(path))) {
return -1;
}
fp->type = FT_DIR;
if ((fp->size = list_rootdir(fp->data)) < 0) {
return -1;
}
} else if (flags == O_RDONLY) { // Read regular file
if (!(fp = get_file(path))) {
return -1;
}
} else if (flags == O_WRONLY || flags == O_RDWR) { // Write regular file
if ((fp = get_file(path))) {
// Delete data if file already exists
memset(fp->data, 0, sizeof(MAX_FILE_SIZE));
} else if (!(fp = alloc_file(path))) {
return -1;
}
fp->type = FT_REGULAR;
} else {
return -1;
}
if (fp) {
curr_task->open_files[fd] = fp;
fp->pos = 0;
}
return fd;
}
int sys_read(struct int_regs *int_frame) {
uint32_t user_esp = int_frame->esp;
int fd = 0;
char *buf = NULL;
size_t count = 0;
if (read_syscall_arg((void *)user_esp, 0, (uint32_t *)&fd) < 0) {
return -1;
}
if (read_syscall_arg((void *)user_esp, 1, (uint32_t *)&buf) < 0) {
return -1;
}
if (read_syscall_arg((void *)user_esp, 2, &count) < 0) {
return -1;
}
if (!curr_task->open_files[fd]) {
return -1;
}
if (curr_task->open_files[fd]->type == FT_DEV) {
return read_console(buf, count);
} else if (curr_task->open_files[fd]->type == FT_REGULAR) {
return read_file(curr_task->open_files[fd], buf, count);
} else if (curr_task->open_files[fd]->type == FT_DIR) {
return read_file(curr_task->open_files[fd], buf, count);
}
return -1;
}
int sys_write(struct int_regs *int_frame) {
uint32_t user_esp = int_frame->esp;
int fd;
char *buf;
size_t count;
if (read_syscall_arg((void *)user_esp, 0, (uint32_t *)&fd) < 0) {
return -1;
}
if (read_syscall_arg((void *)user_esp, 1, (uint32_t *)&buf) < 0) {
return -1;
}
if (read_syscall_arg((void *)user_esp, 2, &count) < 0) {
return -1;
}
if (!curr_task->open_files[fd]) {
return -1;
}
if (curr_task->open_files[fd]->type == FT_DEV) {
return printn(buf, count);
} else if (curr_task->open_files[fd]->type == FT_REGULAR) {
write_file(curr_task->open_files[fd], buf, count);
}
return -1;
}
int sys_close(struct int_regs *int_frame) {
// TODO: Think carefully about the implementation.
// I think there should be more things done in close
// but too lazy to consider about it.
uint32_t user_esp = int_frame->esp;
int fd;
if (read_syscall_arg((void *)user_esp, 0, (uint32_t *)&fd) < 0) {
return -1;
}
struct file *fp = curr_task->open_files[fd];
if (!fp)
return -1;
if (!fp->in_use)
return -1;
curr_task->open_files[fd] = NULL;
return 0;
}