-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvfs.c
261 lines (223 loc) · 5.57 KB
/
vfs.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
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <malloc.h>
#include <stdlib.h>
#include <errno.h>
#include <asm/lkl.h>
#include <asm/disk.h>
#include <asm/env.h>
#define assert(x) if (!(x)) { printf("assert failed: %s:%d: %s\n", __FILE__, __LINE__, #x); exit(0); }
static __kernel_dev_t dev;
void *f;
#ifdef CONFIG_LKL_ENV_APR
#include <apr.h>
#include <apr_file_io.h>
#include <assert.h>
apr_pool_t *root_pool;
void apr_init(void)
{
int rc;
apr_app_initialize(NULL, NULL, NULL);
rc = apr_pool_create(&root_pool, NULL);
assert(rc == APR_SUCCESS);
}
void* file_open(const char *name)
{
apr_file_t *file;
int rc;
rc=apr_file_open(&file, name,
APR_FOPEN_READ| APR_FOPEN_WRITE|
APR_FOPEN_BINARY, APR_OS_DEFAULT,
root_pool);
assert(rc == APR_SUCCESS);
return file;
}
void file_close(void *f)
{
apr_file_close(f);
}
#else /* CONFIG_LKL_ENV_APR */
void* file_open(const char *name)
{
return fopen(name, "r+b");
}
void file_close(void *f)
{
fclose(f);
}
#endif /* CONFIG_LKL_ENV_APR */
void mount_disk(const char *filename, char *mntpath, size_t mntpath_size)
{
int rc;
f = file_open("disk");
assert(f != NULL);
dev = lkl_disk_add_disk(f, 20480000);
assert(dev != 0);
rc = lkl_mount_dev(dev, NULL, 0, "data=ordered,barrier=1", mntpath, mntpath_size);
if (rc)
printf("mount_disk> lkl_mount_dev(dev=%d) rc=%d strerr=[%s]\n",
dev, rc, strerror(-rc));
}
void umount_disk(void)
{
int rc;
rc = lkl_umount_dev(dev, 0);
if (rc)
printf("umount_disk> lkl_umount_dev rc=%d\n", rc);
rc = lkl_disk_del_disk(dev);
if (rc)
printf("umount_disk> lkl_disk_del_disk rc=%d\n", rc);
file_close(f);
}
static char mode2kind(unsigned mode)
{
switch(mode & S_IFMT){
case S_IFSOCK: return 's';
case S_IFLNK: return 'l';
case S_IFREG: return '-';
case S_IFDIR: return 'd';
case S_IFBLK: return 'b';
case S_IFCHR: return 'c';
case S_IFIFO: return 'p';
default: return '?';
}
}
static void mode2str(unsigned mode, char *out)
{
*out++ = mode2kind(mode);
*out++ = (mode & 0400) ? 'r' : '-';
*out++ = (mode & 0200) ? 'w' : '-';
if(mode & 04000) {
*out++ = (mode & 0100) ? 's' : 'S';
} else {
*out++ = (mode & 0100) ? 'x' : '-';
}
*out++ = (mode & 040) ? 'r' : '-';
*out++ = (mode & 020) ? 'w' : '-';
if(mode & 02000) {
*out++ = (mode & 010) ? 's' : 'S';
} else {
*out++ = (mode & 010) ? 'x' : '-';
}
*out++ = (mode & 04) ? 'r' : '-';
*out++ = (mode & 02) ? 'w' : '-';
if(mode & 01000) {
*out++ = (mode & 01) ? 't' : 'T';
} else {
*out++ = (mode & 01) ? 'x' : '-';
}
*out = 0;
}
int read_stat64(char * path)
{
char mode[200];
struct __kernel_stat64 stat;
int rc = lkl_sys_stat64(path, &stat);
if (rc != 0) {
printf("sys_stat64(%s) error=%d strerr=[%s]\n", path, rc, strerror(-rc));
return rc;
}
mode2str(stat.st_mode, mode);
printf("%s %5lu %5d --- [%s]\n",
(char *) mode, (unsigned long) stat.st_size, (int)stat.st_uid, path);
return 0;
}
void list_files(const char * path)
{
int fd;
printf("-------- printing contents of [%s]\n", path);
fd = lkl_sys_open(path, O_RDONLY|O_LARGEFILE|O_DIRECTORY, 0);
if (fd >= 0) {
char x[4096];
int count, reclen;
struct __kernel_dirent *de;
count = lkl_sys_getdents(fd, (struct __kernel_dirent*) x, sizeof(x));
assert(count > 0);
de = (struct __kernel_dirent*) x;
while (count > 0) {
reclen = de->d_reclen;
char fullpath[4096];
fullpath[0] = '\0';
strncat(fullpath, path, sizeof(fullpath));
strncat(fullpath, "/",sizeof(fullpath));
strncat(fullpath, de->d_name,sizeof(fullpath));
read_stat64(fullpath);
de = (struct __kernel_dirent*) ((char*) de+reclen);
count-=reclen;
}
lkl_sys_close(fd);
}
printf("++++++++ done printing contents of [%s]\n", path);
}
void create_file(const char * mnt, const char * new_file, const char * contents)
{
int len = strlen(mnt) + strlen(new_file) + 2;
char * buf = malloc(len);
printf("create_file(%s/%s):: enter\n", mnt, new_file);
if (buf == NULL) {
printf("create_file(%s/%s) could not allocate %d bytes of mem\n",
mnt, new_file, len);
return;
}
snprintf(buf, len, "%s/%s", mnt, new_file);
printf("buf=[%s], mnt=[%s], new_file=[%s]\n", buf, mnt, new_file);
int fd = lkl_sys_open(buf, O_CREAT|O_WRONLY|O_TRUNC|O_EXCL, 0422);
if (fd < 0) {
printf("create_file: lkl_sys_open(%s) rc=%d str=[%s]\n", buf, -fd, strerror(-fd));
return;
}
free(buf);
int rc = lkl_sys_write(fd, contents, strlen(contents));
if (rc < 0) {
errno = -rc;
printf("create_file: lkl_sys_write rc=%d str=[%s]\n", -rc, strerror(-rc));
return;
}
rc = lkl_sys_close(fd);
if (rc < 0) {
errno = -rc;
printf("create_file: lkl_sys_close rc=%d str=[%s]", -rc, strerror(-rc));
return;
}
rc = lkl_sys_sync();
if (rc < 0) {
errno = -rc;
printf("create_file: lkl_sys_sync rc=%d str=[%s]", -rc, strerror(-rc));
return;
}
printf("create_file(%s/%s) success\n", mnt, new_file);
}
void test_timer(void)
{
struct __kernel_timespec ts = { .tv_sec = 1};
int i;
for(i = 3; i > 0; i--) {
printf("Shutdown in %d \r", i); fflush(stdout);
lkl_sys_nanosleep(&ts, NULL);
}
}
void test_file_system(const char * mntstr)
{
list_files("."); // "." should be equal to "/"
list_files("/dev/");
list_files(mntstr);
create_file(mntstr, "bibi02", "continut12");
}
int main(int argc, char **argv, char **env)
{
char mnt_str[]= { "/dev/xxxxxxxxxxxxxxxx" };
#ifdef CONFIG_LKL_ENV_APR
apr_init();
#endif
lkl_env_init(16*1024*1024);
mount_disk("disk", mnt_str, sizeof(mnt_str));
test_file_system(mnt_str);
umount_disk();
mount_disk("disk", mnt_str, sizeof(mnt_str));
test_file_system(mnt_str);
umount_disk();
test_timer();
lkl_env_fini();
return 0;
}