Thomas Heijligen | 75d1ff3 | 2023-12-04 13:40:11 +0000 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <string.h> |
| 3 | |
| 4 | #include "vfs.h" |
| 5 | #include "blockdev.h" |
| 6 | |
| 7 | struct 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 Huber | fcd5f0e | 2024-01-23 16:22:29 +0100 | [diff] [blame] | 14 | { "ext2", ext2fs_mount, ext2fs_read, ext2fs_dir, NULL }, |
Nico Huber | cc960f2 | 2024-01-29 01:13:45 +0100 | [diff] [blame] | 15 | { "iso9660", iso9660_mount, iso9660_read, iso9660_dir, NULL }, |
Nico Huber | dccd234 | 2024-01-29 14:52:20 +0100 | [diff] [blame] | 16 | { "nullfs", nullfs_mount, nullfs_read, nullfs_dir, NULL }, |
Thomas Heijligen | 75d1ff3 | 2023-12-04 13:40:11 +0000 | [diff] [blame] | 17 | }; |
| 18 | |
| 19 | static const size_t fsys_table_length = sizeof(fsys_table) / sizeof(fsys_table[0]); |
| 20 | |
| 21 | const struct fsys_entry *fsys = NULL; |
| 22 | int filemax; |
| 23 | int filepos; |
| 24 | |
| 25 | int 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 | |
| 44 | int file_open(const char *filename) |
| 45 | { |
Thomas Heijligen | 75d1ff3 | 2023-12-04 13:40:11 +0000 | [diff] [blame] | 46 | 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 | |
| 59 | int 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 | |
| 69 | unsigned long file_seek(unsigned long offset) |
| 70 | { |
| 71 | if (offset <= filemax) |
| 72 | filepos = offset; |
| 73 | else |
| 74 | filepos = filemax; |
| 75 | return filepos; |
| 76 | } |
| 77 | |
| 78 | unsigned long file_size(void) |
| 79 | { |
| 80 | return filemax; |
| 81 | } |
| 82 | |
| 83 | void file_close(void) |
| 84 | { |
| 85 | // In contrast to filo the device is not closed here |
| 86 | // devclose(); |
| 87 | } |
| 88 | |