-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoberon.c
400 lines (312 loc) · 8.51 KB
/
oberon.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/*
FUSE: Filesystem in Userspace
Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
This program can be distributed under the terms of the GNU GPLv2.
See the file COPYING.
*/
/** @file
*
* minimal oberon filesystem using high-level API
*
* Compile with:
*
* gcc -Wall oberon.c `pkg-config fuse3 --cflags --libs` -o oberon
*
* ## Source code ##
* \include oberon.c
*/
#define FUSE_USE_VERSION 31
#include <fuse.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <stddef.h>
#include <assert.h>
#include <fcntl.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include "Modules.h"
#include "FileDir.h"
#include "Kernel.h"
#include "Files.h"
/*
* Command line options
*
* We can't set default values for the char* fields here because
* fuse_opt_parse would attempt to free() them when the user specifies
* different values on the command line.
*/
static struct options {
int show_help;
const char *oberon_image;
int oberon_image_offset;
} options;
int oberon_fs_file_desc = -1;
#define OPTION(t, p) \
{ t, offsetof(struct options, p), 1 }
static const struct fuse_opt option_spec[] = {
OPTION("--image=%s", oberon_image),
OPTION("--offset=%d", oberon_image_offset),
OPTION("-h", show_help),
OPTION("--help", show_help),
FUSE_OPT_END
};
static void *oberon_init(struct fuse_conn_info *conn,
struct fuse_config *cfg)
{
conn->want &= ~(FUSE_CAP_ASYNC_READ|FUSE_CAP_POSIX_LOCKS|FUSE_CAP_ASYNC_DIO|FUSE_CAP_PARALLEL_DIROPS);
cfg->kernel_cache = 1;
Modules_Set_OberonFS_image(oberon_fs_file_desc, options.oberon_image_offset);
Modules_Init();
return NULL;
}
static time_t ClockToTime(int t)
{
/**
WritePair(W, " ", d DIV 20000H MOD 20H); (*day*)
WritePair(W, ".", d DIV 400000H MOD 10H); (*month*)
WritePair(W, ".", d DIV 4000000H MOD 40H); (*year*)
WritePair(W, " ", d DIV 1000H MOD 20H); (*hour*)
WritePair(W, ":", d DIV 40H MOD 40H); (*min*)
WritePair(W, ":", d MOD 40H) (*sec*)
ex. : 11.12.18 17:32:24
*/
struct tm date;
date.tm_mday = t / 0x20000 % 0x20; /* day */
date.tm_mon = t / 0x400000 % 0x10 - 1; /* month - 1 */
date.tm_year = t / 0x4000000 % 0x40 + 100; /* year - 1900 */
date.tm_hour = t / 0x1000 % 0x20; /* hour */
date.tm_min = t / 0x40 % 0x40; /* min */
date.tm_sec = t % 0x40; /* sec */
return mktime(&date);
}
static int oberon_getattr(const char *path, struct stat *stbuf,
struct fuse_file_info *fi)
{
(void) fi;
int res = 0;
int adr;
memset(stbuf, 0, sizeof(struct stat));
if (strcmp(path, "/") == 0) {
stbuf->st_mode = S_IFDIR | 0777;
stbuf->st_nlink = 2;
} else {
FileDir_Search(path+1, &adr);
if (adr) {
FileDir_FileHeader hp;
Kernel_GetSector(adr, (uint8_t *) &hp);
stbuf->st_mode = S_IFREG | 0666;
stbuf->st_nlink = 1;
stbuf->st_size = hp.aleng*FileDir_SectorSize + hp.bleng - FileDir_HeaderSize /* length */;
stbuf->st_mtim.tv_sec = ClockToTime(hp.date);
} else {
res = -ENOENT;
}
}
return res;
}
struct readdir_parameters {
fuse_fill_dir_t filler;
void *buf;
};
void List(FileDir_FileName name, int sec, bool *Continue, void *callerData)
{
struct readdir_parameters *par = callerData;
par->filler(par->buf, name, NULL, 0, 0);
}
static int oberon_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info *fi,
enum fuse_readdir_flags flags)
{
struct readdir_parameters par = {
.filler = filler,
.buf = buf,
};
(void) offset;
(void) fi;
(void) flags;
if (strcmp(path, "/") != 0)
return -ENOENT;
filler(buf, ".", NULL, 0, 0);
filler(buf, "..", NULL, 0, 0);
FileDir_Enumerate("", List, &par);
return 0;
}
static int oberon_open(const char *path, struct fuse_file_info *fi)
{
Files_File f;
Files_Rider *Rf;
f = Files_Old(path+1);
if (f == NULL) {
return -ENOENT;
}
Rf = malloc(sizeof *Rf);
assert(Rf);
Files_Set(Rf, f, 0);
fi->fh = (uint64_t) Rf;
return 0;
}
static int oberon_read(const char *path, char *buf, size_t size, off_t offset,
struct fuse_file_info *fi)
{
int i;
Files_Rider *Rf;
char ch;
assert(fi);
Rf = (Files_Rider *) fi->fh;
assert(Rf);
Files_Reset(Rf, offset);
Files_Read(Rf, &ch);
i = 0;
while (!Rf->eof && i<size) { buf[i++] = ch; Files_Read(Rf, &ch); }
return i;
}
static int oberon_write(const char *path, const char *buf, size_t size,
off_t offset, struct fuse_file_info *fi)
{
int i;
Files_Rider *Rf;
assert(fi);
Rf = (Files_Rider *) fi->fh;
assert(Rf);
Files_Reset(Rf, offset);
i = 0;
while (i<size) Files_Write(Rf, buf[i++]);
return i;
}
int oberon_release (const char *path, struct fuse_file_info *fi)
{
Files_Rider *Rf;
Files_File f;
assert(fi);
Rf = (Files_Rider *) fi->fh;
assert(Rf);
f = Files_Base(Rf);
assert(f);
Files_Close(f);
Files_Unset(Rf);
free(Rf);
return 0;
}
static int oberon_truncate(const char *path, off_t size,
struct fuse_file_info *fi)
{
int res;
Files_File f;
if (fi != NULL) {
Files_Rider *Rf = (Files_Rider *) fi->fh;
assert(Rf);
f = Files_Base(Rf);
} else {
f = Files_Old(path+1);
}
if (f == NULL) {
return -ENOENT;
}
Files_Truncate(f, size, &res);
if (res != -0)
return -EFBIG;
return 0;
}
static int oberon_create(const char *path, mode_t mode,
struct fuse_file_info *fi)
{
Files_File f;
Files_Rider *Rf;
f = Files_New(path+1);
if (f == NULL) {
return -ENOENT;
}
Files_Register(f);
Rf = malloc(sizeof *Rf);
assert(Rf);
Files_Set(Rf, f, 0);
fi->fh = (uint64_t) Rf;
return 0;
}
static int oberon_rename(const char *from, const char *to, unsigned int flags)
{
int res;
if (flags)
return -EINVAL;
Files_Rename(from, to, &res);
if (res > 1)
return -ENOENT;
return 0;
}
static int oberon_unlink(const char *path)
{
int res;
Files_Delete(path+1, &res);
if (res != 0)
return -ENOENT;
return 0;
}
static const struct fuse_operations oberon_oper = {
.init = oberon_init,
.getattr = oberon_getattr,
.readdir = oberon_readdir,
.open = oberon_open,
.read = oberon_read,
.write = oberon_write,
.release = oberon_release,
.create = oberon_create,
.rename = oberon_rename,
.unlink = oberon_unlink,
.truncate = oberon_truncate,
};
static void show_help(const char *progname)
{
printf("usage: %s [options] --image=<oberon image> <mountpoint>\n\n", progname);
printf("File-system specific options:\n"
" --image=<file name> Name of the oberon image file\n"
" --offset=<signed value> Number of bytes of offset to the\n"
" beginning of the file system.\n"
"\n"
" 0 means that the directory root\n"
" is located ad address 0x400.\n"
"\n"
" The RISCW32 emulator images have\n"
" their directory root located at 0x0\n"
" so offset equal to -1024 must be\n"
" specified\n"
"\n");
}
int main(int argc, char *argv[])
{
int ret;
struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
/* Parse options */
if (fuse_opt_parse(&args, &options, option_spec, NULL) == -1)
return 1;
/* Force single thread to avoid reentrance */
assert(fuse_opt_add_arg(&args, "-s") == 0);
/* When --help is specified, first print our own file-system
specific help text, then signal fuse_main to show
additional help (by adding `--help` to the options again)
without usage: line (by setting argv[0] to the empty
string) */
if (options.show_help) {
show_help(argv[0]);
assert(fuse_opt_add_arg(&args, "--help") == 0);
args.argv[0][0] = '\0';
} else {
if (!options.oberon_image) {
printf("Missing mandatory --image=<oberon image> parameter\n");
return 2;
}
oberon_fs_file_desc = open(options.oberon_image, O_RDWR);
if (oberon_fs_file_desc < 0) {
printf("Unable to open file %s for read\n", options.oberon_image);
return 3;
}
}
ret = fuse_main(args.argc, args.argv, &oberon_oper, NULL);
fuse_opt_free_args(&args);
if (oberon_fs_file_desc >= 0) {
close(oberon_fs_file_desc);
}
return ret;
}