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 }, |
Thomas Heijligen | 75d1ff3 | 2023-12-04 13:40:11 +0000 | [diff] [blame] | 15 | }; |
| 16 | |
| 17 | static const size_t fsys_table_length = sizeof(fsys_table) / sizeof(fsys_table[0]); |
| 18 | |
| 19 | const struct fsys_entry *fsys = NULL; |
| 20 | int filemax; |
| 21 | int filepos; |
| 22 | |
| 23 | int 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 | |
| 42 | int file_open(const char *filename) |
| 43 | { |
| 44 | if (filename && filename[0] != '/') { |
| 45 | fprintf(stderr, "file_open: filename musst start with '/'," |
| 46 | "no support for device identifier"); |
| 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | if (!fsys) { |
| 51 | fprintf(stderr, "file_open: no open filesystem.\n"); |
| 52 | return 0; |
| 53 | } |
| 54 | char *path = strdup(filename); |
| 55 | if (!fsys->dir_func(path)) { |
| 56 | fprintf(stderr, "file_open: file not found '%s'.\n", filename); |
| 57 | return 0; |
| 58 | } |
| 59 | filepos = 0; |
| 60 | return 1; |
| 61 | } |
| 62 | |
| 63 | int file_read(void *buf, unsigned long len) |
| 64 | { |
| 65 | if (filepos < 0 || filepos > filemax) |
| 66 | filepos = filemax; |
| 67 | |
| 68 | if (len < 0 || len > filemax - filepos) |
| 69 | len = filemax - filepos; |
| 70 | return fsys->read_func(buf, len); |
| 71 | } |
| 72 | |
| 73 | unsigned long file_seek(unsigned long offset) |
| 74 | { |
| 75 | if (offset <= filemax) |
| 76 | filepos = offset; |
| 77 | else |
| 78 | filepos = filemax; |
| 79 | return filepos; |
| 80 | } |
| 81 | |
| 82 | unsigned long file_size(void) |
| 83 | { |
| 84 | return filemax; |
| 85 | } |
| 86 | |
| 87 | void file_close(void) |
| 88 | { |
| 89 | // In contrast to filo the device is not closed here |
| 90 | // devclose(); |
| 91 | } |
| 92 | |