blob: 943fea4dfa2711ff26d8e2dffc385aa4ba151c8b [file] [log] [blame]
Thomas Heijligen75d1ff32023-12-04 13:40:11 +00001#include <stdio.h>
2#include <string.h>
3
4#include "vfs.h"
5#include "blockdev.h"
6
7struct fsys_entry {
8 char *name;
9 int (*mount_func) (void);
10 int (*read_func) (char *buf, int len);
11 int (*dir_func) (char *dirname);
12 int (*close_func) (void);
13} static const fsys_table[] = {
Nico Huberfcd5f0e2024-01-23 16:22:29 +010014 { "ext2", ext2fs_mount, ext2fs_read, ext2fs_dir, NULL },
Thomas Heijligen75d1ff32023-12-04 13:40:11 +000015};
16
17static const size_t fsys_table_length = sizeof(fsys_table) / sizeof(fsys_table[0]);
18
19const struct fsys_entry *fsys = NULL;
20int filemax;
21int filepos;
22
23int mount_fs(char *fs_name)
24{
25 for (size_t i = 0; i < fsys_table_length; i++) {
26 if ((!fs_name || !strcmp(fsys_table[i].name, fs_name)) && fsys_table[i].mount_func()) {
27 fsys = &fsys_table[i];
28 return 1;
29 }
30 }
31
32 if (fs_name)
33 fprintf(stderr, "mount_fs: Faild to mount filesystem: %s.\n", fs_name);
34 else
35 fprintf(stderr, "mount_fs: No matching filesystem found.\n");
36 return 0;
37}
38
39
40
41
42int file_open(const char *filename)
43{
Thomas Heijligen75d1ff32023-12-04 13:40:11 +000044 if (!fsys) {
45 fprintf(stderr, "file_open: no open filesystem.\n");
46 return 0;
47 }
48 char *path = strdup(filename);
49 if (!fsys->dir_func(path)) {
50 fprintf(stderr, "file_open: file not found '%s'.\n", filename);
51 return 0;
52 }
53 filepos = 0;
54 return 1;
55}
56
57int file_read(void *buf, unsigned long len)
58{
59 if (filepos < 0 || filepos > filemax)
60 filepos = filemax;
61
62 if (len < 0 || len > filemax - filepos)
63 len = filemax - filepos;
64 return fsys->read_func(buf, len);
65}
66
67unsigned long file_seek(unsigned long offset)
68{
69 if (offset <= filemax)
70 filepos = offset;
71 else
72 filepos = filemax;
73 return filepos;
74}
75
76unsigned long file_size(void)
77{
78 return filemax;
79}
80
81void file_close(void)
82{
83 // In contrast to filo the device is not closed here
84 // devclose();
85}
86