blob: 03e9374f682f9d49297218b2d103b5eb32ec555b [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 },
Nico Hubercc960f22024-01-29 01:13:45 +010015 { "iso9660", iso9660_mount, iso9660_read, iso9660_dir, NULL },
Thomas Heijligen75d1ff32023-12-04 13:40:11 +000016};
17
18static const size_t fsys_table_length = sizeof(fsys_table) / sizeof(fsys_table[0]);
19
20const struct fsys_entry *fsys = NULL;
21int filemax;
22int filepos;
23
24int mount_fs(char *fs_name)
25{
26 for (size_t i = 0; i < fsys_table_length; i++) {
27 if ((!fs_name || !strcmp(fsys_table[i].name, fs_name)) && fsys_table[i].mount_func()) {
28 fsys = &fsys_table[i];
29 return 1;
30 }
31 }
32
33 if (fs_name)
34 fprintf(stderr, "mount_fs: Faild to mount filesystem: %s.\n", fs_name);
35 else
36 fprintf(stderr, "mount_fs: No matching filesystem found.\n");
37 return 0;
38}
39
40
41
42
43int file_open(const char *filename)
44{
Thomas Heijligen75d1ff32023-12-04 13:40:11 +000045 if (!fsys) {
46 fprintf(stderr, "file_open: no open filesystem.\n");
47 return 0;
48 }
49 char *path = strdup(filename);
50 if (!fsys->dir_func(path)) {
51 fprintf(stderr, "file_open: file not found '%s'.\n", filename);
52 return 0;
53 }
54 filepos = 0;
55 return 1;
56}
57
58int file_read(void *buf, unsigned long len)
59{
60 if (filepos < 0 || filepos > filemax)
61 filepos = filemax;
62
63 if (len < 0 || len > filemax - filepos)
64 len = filemax - filepos;
65 return fsys->read_func(buf, len);
66}
67
68unsigned long file_seek(unsigned long offset)
69{
70 if (offset <= filemax)
71 filepos = offset;
72 else
73 filepos = filemax;
74 return filepos;
75}
76
77unsigned long file_size(void)
78{
79 return filemax;
80}
81
82void file_close(void)
83{
84 // In contrast to filo the device is not closed here
85 // devclose();
86}
87