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