forked from ish-app/ish
-
Notifications
You must be signed in to change notification settings - Fork 12
/
generic.c
194 lines (175 loc) · 5.7 KB
/
generic.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
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include "kernel/fs.h"
#include "fs/dev.h"
#include "kernel/process.h"
#include "kernel/errno.h"
struct fd *fd_create() {
struct fd *fd = malloc(sizeof(struct fd));
*fd = (struct fd) {};
fd->refcount = 1;
fd->flags = 0;
fd->mount = NULL;
list_init(&fd->poll_fds);
lock_init(&fd->lock);
return fd;
}
struct mount *find_mount(char *path) {
struct mount *mount;
for (mount = mounts; mount != NULL; mount = mount->next)
if (strncmp(path, mount->point, strlen(mount->point)) == 0)
break;
assert(mount != NULL); // this would mean there's no root FS mounted
return mount;
}
struct mount *find_mount_and_trim_path(char *path) {
struct mount *mount = find_mount(path);
char *dst = path;
const char *src = path + strlen(mount->point);
while (*src != '\0')
*dst++ = *src++;
*dst = '\0';
return mount;
}
struct fd *generic_openat(struct fd *at, const char *path_raw, int flags, int mode) {
// TODO really, really, seriously reconsider what I'm doing with the strings
char path[MAX_PATH];
int err = path_normalize(at, path_raw, path, true);
if (err < 0)
return ERR_PTR(err);
struct mount *mount = find_mount_and_trim_path(path);
struct fd *fd = mount->fs->open(mount, path, flags, mode);
if (IS_ERR(fd))
return fd;
fd->mount = mount;
struct statbuf stat;
err = fd->mount->fs->fstat(fd, &stat);
if (err >= 0) {
assert(!S_ISLNK(stat.mode));
if (S_ISBLK(stat.mode) || S_ISCHR(stat.mode)) {
int type;
if (S_ISBLK(stat.mode))
type = DEV_BLOCK;
else
type = DEV_CHAR;
int major = dev_major(stat.rdev);
int minor = dev_minor(stat.rdev);
err = dev_open(major, minor, type, fd);
if (err < 0) {
fd_close(fd);
return ERR_PTR(err);
}
}
}
return fd;
}
struct fd *generic_open(const char *path, int flags, int mode) {
return generic_openat(current->pwd, path, flags, mode);
}
int fd_close(struct fd *fd) {
if (--fd->refcount == 0) {
if (fd->ops->close) {
int err = fd->ops->close(fd);
if (err < 0)
return err;
}
free(fd);
}
return 0;
}
struct fd *generic_dup(struct fd *fd) {
fd->refcount++;
return fd;
}
int generic_access(const char *path_raw, int mode) {
char path[MAX_PATH];
int err = path_normalize(NULL, path_raw, path, true);
if (err < 0)
return err;
struct mount *mount = find_mount_and_trim_path(path);
struct statbuf stat;
err = mount->fs->stat(mount, path, &stat, true);
if (err < 0)
return err;
// TODO do an actual permissions check
return 0;
}
int generic_linkat(struct fd *src_at, const char *src_raw, struct fd *dst_at, const char *dst_raw) {
char src[MAX_PATH];
int err = path_normalize(src_at, src_raw, src, false);
if (err < 0)
return err;
char dst[MAX_PATH];
err = path_normalize(dst_at, dst_raw, dst, false);
if (err < 0)
return err;
struct mount *mount = find_mount_and_trim_path(src);
struct mount *dst_mount = find_mount_and_trim_path(dst);
if (mount != dst_mount)
return _EXDEV;
return mount->fs->link(mount, src, dst);
}
int generic_unlinkat(struct fd *at, const char *path_raw) {
char path[MAX_PATH];
int err = path_normalize(at, path_raw, path, false);
if (err < 0)
return err;
struct mount *mount = find_mount_and_trim_path(path);
return mount->fs->unlink(mount, path);
}
int generic_renameat(struct fd *src_at, const char *src_raw, struct fd *dst_at, const char *dst_raw) {
char src[MAX_PATH];
int err = path_normalize(src_at, src_raw, src, false);
if (err < 0)
return err;
char dst[MAX_PATH];
err = path_normalize(dst_at, dst_raw, dst, false);
if (err < 0)
return err;
struct mount *mount = find_mount_and_trim_path(src);
struct mount *dst_mount = find_mount_and_trim_path(dst);
if (mount != dst_mount)
return _EXDEV;
return mount->fs->rename(mount, src, dst);
}
int generic_symlinkat(const char *target, struct fd *at, const char *link_raw) {
char link[MAX_PATH];
int err = path_normalize(at, link_raw, link, false);
if (err < 0)
return err;
struct mount *mount = find_mount_and_trim_path(link);
return mount->fs->symlink(mount, target, link);
}
int generic_setattrat(struct fd *at, const char *path_raw, struct attr attr, bool follow_links) {
char path[MAX_PATH];
int err = path_normalize(at, path_raw, path, follow_links);
if (err < 0)
return err;
struct mount *mount = find_mount_and_trim_path(path);
return mount->fs->setattr(mount, path, attr);
}
ssize_t generic_readlink(const char *path_raw, char *buf, size_t bufsize) {
char path[MAX_PATH];
int err = path_normalize(NULL, path_raw, path, false);
if (err < 0)
return err;
struct mount *mount = find_mount_and_trim_path(path);
return mount->fs->readlink(mount, path, buf, bufsize);
}
int generic_mkdirat(struct fd *at, const char *path_raw, mode_t_ mode) {
char path[MAX_PATH];
int err = path_normalize(at, path_raw, path, true);
if (err < 0)
return err;
struct mount *mount = find_mount_and_trim_path(path);
return mount->fs->mkdir(mount, path, mode);
}
int generic_rmdirat(struct fd *at, const char *path_raw) {
char path[MAX_PATH];
int err = path_normalize(at, path_raw, path, true);
if (err < 0)
return err;
struct mount *mount = find_mount_and_trim_path(path);
return mount->fs->rmdir(mount, path);
}